ImGuiNET / ImGui.NET

An ImGui wrapper for .NET.
MIT License
1.91k stars 306 forks source link

Pushing some style vars with float crashes #316

Open Shadowblitz16 opened 2 years ago

Shadowblitz16 commented 2 years ago

Idk whats going on but ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding , 0.0f); crashes even if I pop the same amount of times I push.

        public static void BeginDockSpace  ()
        {
            rlImGui.Begin();

            var windowX     = 0;
            var windowY     = 0;
            var windowW     = Raylib.GetScreenWidth();
            var windowH     = Raylib.GetScreenHeight();
            var windowFlags = ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.NoDocking;
            ImGui.SetNextWindowPos (new(windowX,windowY), ImGuiCond.Always);
            ImGui.SetNextWindowSize(new(windowW,windowH), ImGuiCond.Always);

            ImGui.PushStyleVar(ImGuiStyleVar.WindowRounding  , 0.0f);
            ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding   , 0.0f);
            windowFlags |=  ImGuiWindowFlags.NoBringToFrontOnFocus |
                            ImGuiWindowFlags.NoNavFocus            |
                            ImGuiWindowFlags.NoTitleBar            | 
                            ImGuiWindowFlags.NoCollapse            | 
                            ImGuiWindowFlags.NoResize              | 
                            ImGuiWindowFlags.NoMove;

            ImGui.Begin("DockSpace Dummy", windowFlags);
            ImGui.PopStyleVar();
            ImGui.PopStyleVar();

            // dock space
            ImGui.DockSpace(ImGui.GetID("DockSpace"));

        }
        public static void EndDockSpace()
        {
            ImGui.End();
            rlImGui.End();
        }
        public static void Main(string[] args)
        {
            Raylib.InitWindow(0, 0, "Pure Editor");
            Raylib.SetTargetFPS(60);

            rlImGui.Setup();
            Canvas = Raylib.LoadRenderTexture(256, 240);
            while (!Raylib.WindowShouldClose())
            {
                Raylib.BeginDrawing();
                Raylib.ClearBackground(Raylib.BLACK);

                Raylib.BeginTextureMode(Canvas);
                Raylib.DrawLine(0, 0, 256, 240, Raylib.WHITE);
                Raylib.EndTextureMode();

                BeginDockSpace();
                Viewport();
                EndDockSpace();

                Raylib.EndDrawing();
            }
            rlImGui.Shutdown();
            Raylib.CloseWindow();
        }
        public static void Viewport()
        {
            if (ImGui.Begin("Viewport"))
            {
                var size = ImGui.GetContentRegionAvail();

                Rectangle viewRect = new Rectangle();
                viewRect.x         = Canvas.texture.width  / 2 - size.X / 2;
                viewRect.y         = Canvas.texture.height / 2 - size.Y / 2;
                viewRect.width     =  size.X;
                viewRect.height    = -size.Y;

                rlImGui.ImageRect(Canvas.texture, (int)size.X, (int)size.Y, viewRect);
            }
            ImGui.End();
        }
Thraka commented 2 years ago

I've run into this too

Thraka commented 2 years ago

Looking through ImGui code, it looks like the logic that pushes the variable would fail:

https://github.com/ocornut/imgui/blob/v1.88/imgui.cpp#L2888-L2900

void ImGui::PushStyleVar(ImGuiStyleVar idx, float val)
{
    const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx);
    if (var_info->Type == ImGuiDataType_Float && var_info->Count == 1)
    {
        ImGuiContext& g = *GImGui;
        float* pvar = (float*)var_info->GetVarPtr(&g.Style);
        g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar));
        *pvar = val;
        return;
    }
    IM_ASSERT(0 && "Called PushStyleVar() float variant but variable is not a float!");
}

It's checking if count == 1 which on one of those variables, in the table above the code, it's 2. I must be missing something because I see the C++ folks in the ImGui issues doing this all the time.

https://github.com/ocornut/imgui/blob/v1.88/imgui.cpp#L2856

    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowPadding) },       // ImGuiStyleVar_WindowPadding
Thraka commented 2 years ago

Ok.. I've been working through this and I noticed there is a Vector2 overload, that's the one you need to use when the count is 2. Your window padding should be

ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, new Vector2(0.0f));