ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
59.74k stars 10.17k forks source link

Is there a way to include callbacks in between lines ? if not, how can someone gracefully implement it ? #8017

Closed aymen157 closed 2 days ago

aymen157 commented 2 days ago

Version/Branch of Dear ImGui:

1.9

Back-ends:

imgui_impl_opengl

Compiler, OS:

Windows 11

Full config/build information:

No response

Details:

for example (c#)

ImGui.Text("Hello");
ImGui.CallbackCall("textStyler");
ImGui.Text("Hello with Injected Style");
ImGui.CallbackCall("afterStyler");

later in any part of the project we can do

ImGui.PushCallback("textStyler", pointerToSomeCoolStyleMethod);
ImGui.PushCallback("afterStyler", pointerToSomeDoubleClickHandler);

like this anyone can extend style without having to modify the source code of the app. pushing styles, or even handling input events/introducing new ones etc.. i'm wondering if there's something like this already i missed ?

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

No response

ocornut commented 2 days ago

What prevents from implementing exactly what you are suggesting? You shouldn't need any support from imgui to do it.

like this anyone can extend style without having to modify the source code of the app.

You are modifying the source code of the app: What's the ImGui.CallbackCall("textStyler"); call? You are simply adding an extra indirection layer (which IMHO is not very valuable).

Why not doing:

ImGui.Text("Hello");
ImGuiEx.PushSomeCoolStyleMethod(); // your function
ImGui.Text("Hello with Injected Style");
ImGuiEx.PopSomeCoolStyleMethod(); // your function

dear imgui is functional you can implement those things with your functions. It's also understood that you are using dear imgui because you want to control everything in code.

aymen157 commented 2 days ago

Why not doing: ....

im currently doing that. and at high scale, it becomes terrible. questions start to popup so frequently like "where did i put this?" or "should i define this callback in this or in this" etc... these feel ok at lower scale but as things start to grow it becomes a headache. now imagine that with not your software only, but others libs too.

you could think of it of openpop and if(popup) ... why not define a global static bool for that popup if its open etc.. ? but this is much worse if you want much extensibility (as it becomes much more frequent) (you can see in that example, a text call took 2 callbacks)

that aside i think Imgui.Callback() is prettier :) than ImGuiExtensionNum1Part1Ep2.Callback()

ocornut commented 2 days ago

I don't really understand what you are aiming at to be honest. You seem to be pushing against dear imgui's design.

Can you share actual code you are using?

Again, if you like the sample code you suggested you are feel to implement it exactly as you wrote it.. you don't need to modify imgui for that. But I am thinking your design is fishy here. It seems like a wide-scale XY Problem where your code is going against how imgui code is typically written.

aymen157 commented 2 days ago

this is an example. the global search in my engine should be modifiable by other scripts. aka not hardcoded style. (eg. the green lines are where other code/plugins can insert stuff) currently thee way im doing it is creating class for that and adding events callbacks. but im fighting against imgui and would love the have sth like DrawGlobalSearch(name);

that func would contain many calls like "ImGui.Callback(name + "green_line");" which would make it sooo much easier to subscribe events from anywhere in the code base and from any plugin.

currently this is very difficult to do without classes (also using a func + func params would be overkill since there are too many varsiables, alternatively, using a class as parameter itself is against the imgui design etc..)

Again, if you like the sample code you suggested you are feel to implement it exactly as you wrote it

Yeah, for now i think i will make it, i was asking if there was sth like i missed. but i dont know it seems like a useful feature to have it in the main lib. idk.

image

ocornut commented 2 days ago

I'm afraid that we have a misunderstanding here. Nothing here actually has anything to do with Dear ImGui and its design.

If you want your code to be pluggable by third-party code, it's up to you to come up with a design that works for whatever you want to make. And there are many solutions and perhaps people here or elsewhere will help you. But I personally cannot. Dear ImGui display the things you are asking it to display.

When you ask about this in programming forums you shouldn't be thinking about "callbacks" or "classes" or any predetermined solutions that will hinder possible answers. Instead, try to explain your problem with concrete use cases. As you dig into this you will understand that your problem has nothing to do with Dear ImGui itself.

GamingMinds-DanielC commented 1 day ago

If you want callback support in your windows (or in custom widgets), there is nothing preventing you from adding it in. But as Omar said, that has nothing to do with ImGui directly.

If you are struggling with the right approach, you can use or get inspiration from other libraries that are designed to provide exactly that, like f.e. this one: https://github.com/libsigcplusplus/libsigcplusplus

aymen157 commented 1 day ago

No. having a global state machine for callbacks that will be used to draw is not a bad idea at all. I'm pretty sure it will be scattered across so many projects since its reduces so much friction at higher scale. even if you can't see it. i do. it's whatever though 👍.