ImGuiNET / ImGui.NET

An ImGui wrapper for .NET.
MIT License
1.89k stars 304 forks source link

Interest in ImGui Backends wrappers #311

Open Refragg opened 2 years ago

Refragg commented 2 years ago

Hello, I've been interested to use this library for a bit with SDL and more particularly the SDL renderer.
I came to realize that there are no wrappers for the backends so i tried porting the C++ one but that didn't go so well. However, i just realized that this is using cimgui and cimgui does have an api for the backends.

So my question is: would you all be interested in wrappers for the backends in this library? It seems to me that it could have it's place here but be sure to tell me what you think

benblo commented 2 years ago

If I understand correctly, ImGui.NET uses Veldrid as a backend, and you're proposing to add SDL support, correct? I am not an ImGui.NET dev, but as a long-time user of the C++ version, I think it makes total sense. That's the power of dear imgui, the ability to plug it into whatever platform you're currently working on.

Refragg commented 2 years ago

I think Veldrid is the only available implementation for ImGui.NET right now, i would ideally just want to be able to use the C++ backends directly by PInvoke or something, i did manage to have something working in the meantime but the problem is cimgui doesn't provide entry points for the backends as it does with all the ImGui stuff so i had to make modifications to cimgui's code in order for it to work and i also didn't do any code generation because i wanted to get something working and i haven't looked at how the code generation works

sonoro1234 commented 2 years ago

You shouldnt need to modify cimgui: https://github.com/cimgui/cimgui/issues/157

Refragg commented 2 years ago

Hello, i've tried looking at this way of doing it before but i don't know how this could be integrated for ImGui.NET since i don't think dotnet lets you directly just build cpp files and link against them but i might be wrong and it would be nice if you could prove me wrong

sonoro1234 commented 2 years ago

They are cpp files with a C interface. The same as cimgui.

GaryHuan9 commented 2 years ago

Hello, I am currently using ImGui.NET and SDL-CS in my project. It uses the SDL Platform and Renderer as a Dear ImGui backend. You can checkout the main integration implementation here: https://github.com/GaryHuan9/Echo/blob/c816c3ec4017a817ce23906b8029019cb5d8772e/Echo.UserInterface/src/Backend/ImGuiDevice.cs

clibequilibrium commented 1 year ago

@Refragg it is doable in couple steps you can see how it was done in those 2 forks. https://github.com/equilibrium-hub/ImGui.NET-nativebuild https://github.com/equilibrium-hub/ImGui.NET

1) You need to pass the right backend flags to cimgui generator, it will output the right json definitions from luajit in cimgui\generator\. 2) Alter CMakeLists.txt to include the source files for the backend of your choice, build the native library so the backend code is included in native cimgui library. Otherwise you will call Pinvoke C# methods but they would not exist in the native library. Make sure to link SDL libraries and include the SDL headers

#general settings
file(GLOB IMGUI_SOURCES
    cimgui.cpp
    imgui/imgui.cpp
    imgui/imgui_draw.cpp
    imgui/imgui_demo.cpp
    imgui/imgui_widgets.cpp
    imgui/backends/imgui_impl_sdl.cpp
    ${TABLES_SOURCE}
)

3) Copy the impl_definitions.json to CodeGenerator's definitions folder and tell it to use it here 4) Build ImGui.NET and observe the bindings being generated for your backend.

image

EDIT:

For SDL backend you might need to patch the generated json definitions as they will generate SDL_Window structs and have conflicting names as members, for example event. See this diff You won't have to do it frequently as headers for backend implementations change once every 4-5 years