ImGuiNET / ImGui.NET

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

Main menu bar not working? #258

Closed Stay1444 closed 2 years ago

Stay1444 commented 3 years ago

Im new to Dear ImGUI so i searched how to make a Menu bar (File, save etc) I found and example in C++, and when using it in C# it doenst work.

ImGui.Begin("testWindow" + "###" + id, ref Show);
                if (ImGui.BeginMainMenuBar())
                {
                    if (ImGui.BeginMenu("File"))
                    {
                        if (ImGui.MenuItem("New"))
                        {
                        }
                        ImGui.EndMenu();
                    }

                    ImGui.EndMainMenuBar();
                }
                ImGui.End();

What am i doing wrong?

mellinoe commented 3 years ago

Take a look at the demo code over in the main repo to get an example of how to use it. The misunderstanding here is that BeginMainMenuBar should not be inside of a Begin/End call -- it makes it's own top-level block.

"Main menu" actually refers to the menu at the top of the application window. If you wanted the "testWindow" that you are creating to have a menu bar at the top of it, you would just use BeginMenuBar+EndMenuBar instead.

Hope that helps.

Shadowblitz16 commented 3 years ago

I don't understand this either I am doing this and it crashes the application...

    public class Program : Game
    {
        static void Main(string[] args)
        {
            using (var program = new Program())
            {
                program.Run(100,100,800,600, "Test");
            }
        }

        public override void OnDraw()
        {
            if (ImGui.BeginMainMenuBar())
            {
                if (ImGui.BeginMenu("File"))
                {
                    if (ImGui.MenuItem("New"))
                    {

                    }
                }
                ImGui.EndMenu(); 
            }
            ImGui.EndMainMenuBar();
        }
    }
using System;
using System.Numerics;
using ImGuiNET;
using SharpDX.Text;
using Veldrid;
using Veldrid.Sdl2;
using Veldrid.SPIRV;
using Veldrid.StartupUtilities;

namespace GameSharp
{
    public abstract class Game : IDisposable
    {
        public void Run(int x, int y, uint w, uint h, string title)
        {
            VeldridStartup.CreateWindowAndGraphicsDevice
            (
                new WindowCreateInfo(x, y, (int)w, (int)h, Veldrid.WindowState.Normal, title),
                out var window,
                out var gd
            );
            ImGuiRenderer imGuiRenderer = new ImGuiRenderer
            (
                gd, 
                gd.MainSwapchain.Framebuffer.OutputDescription,
                (int)gd.MainSwapchain.Framebuffer.Width, 
                (int)gd.MainSwapchain.Framebuffer.Height
            );
            var cl = gd.ResourceFactory.CreateCommandList();

            OnLoad();

            while (window.Exists)
            {
                var input = window.PumpEvents();
                if (!window.Exists) { break; }
                imGuiRenderer.Update(1f / 60f, input); // Compute actual value for deltaSeconds.

                // Draw stuff
                OnStep();
                OnDraw();

                cl.Begin();
                cl.SetFramebuffer(gd.MainSwapchain.Framebuffer);
                cl.ClearColorTarget(0, RgbaFloat.Black);
                imGuiRenderer.Render(gd, cl);
                cl.End();
                gd.SubmitCommands(cl);
                gd.SwapBuffers(gd.MainSwapchain);
            }

            OnQuit();
        }

        public virtual void OnLoad()
        {
        }
        public virtual void OnStep()
        {
        }
        public virtual void OnDraw()
        {
        }
        public virtual void OnQuit()
        {
        }

        public void Dispose()
        {
            OnQuit();
        }
    }
}
dctucker commented 2 years ago

I got around crashing in a nim application by making it only run EndMenu if the BeginMenu call returns true (basically moving EndMenu under the conditional instead of after).

Apparently the behavior is different from ImGui.Begin/ImGui.End.