ocornut / imgui

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

Stacking 2+ Popup Windows does not work. #5737

Closed PsychoCoffee closed 2 years ago

PsychoCoffee commented 2 years ago

Hello.

After calling PopupModalWindow that lets you select what save you want to play, I want to call another PopupModalWindow that shows you the progress of loading the level.

But, the LoadingGameDialog(); PopupModal never appears on the screen, no matter what I tried to change around. No errors come up when I call the function.

This code is extremely un-clean and I agree, since I am a beginner at side of game development. I believe the error is either that I can't stack 2 PopupModal windows, or that it's a syntax error I can't find anywhere.

So the order is: Main Menu Buttons (Basic Window) > Start New Game Screen (PopupModal) > Loading Game Dialog (PopupModal)

What I tried:

  1. I tried to call ImGui::CloseCurrentPopup(); before calling LoadingGameDialog(); (Doesn't still work)
  2. I tried to use a combination of PopupModal for the first popup and PopupWindow for the second popup (Didn't fix either)
  3. I tried changing orders of where I called LoadingGameDialog(); but it didn't fix it.

 void Graphics::newGame() {
    ImVec2 center = ImGui::GetMainViewport()->GetCenter();
    ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
    if (ImGui::BeginPopupModal("##Select Dialog NEWGAME01"))
    {
        ImGui::Text("NEW GAME");
        ImGui::Separator();
        ImGui::Text("Pick a world save you want to continue off from...");
        if (ImGui::Button("Start new game", ImVec2(130, 0))) {
            ImGui::CloseCurrentPopup();
            ImGui::OpenPopup("##LoadGame");
            LoadingGameDialog();
        } ImGui::SameLine();
        if (ImGui::Button("Cancel", ImVec2(50, 0))) { ImGui::CloseCurrentPopup(); }
        ImGui::EndPopup();
    }
}

//-----------The PopupModal Loading Dialog Area AKA. LoadingGameDialog(); -----------
 void Graphics::LoadingGameDialog() {
    ImVec2 center = ImGui::GetMainViewport()->GetCenter();
    ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
    if (ImGui::BeginPopupModal("##LoadGame"))
    {
        ImGui::Text("LOAD GAME");
        ImGui::Separator();
        ImGui::Text("Loading Game...");
        ImGui::EndPopup();
    }
}

//----------The Main Menu Screen --------------------
void Graphics::drawMainMenu() {
    lockMouse = false;
    if (allowDrawMainMenu) {
        ImGui_ImplDX11_NewFrame();
        ImGui_ImplWin32_NewFrame();
        ImGui::NewFrame();

        if (ImGui::Begin("##MainMenuButtons")) {
            ImGui::NewLine();
            if (ImGui::Button("New Game")) {
                if (doSFX)
                    sound.buttonClickSound();
                ImGui::OpenPopup("##Select Dialog NEWGAME01");
            }
            if (isInGame) {
                ImGui::NewLine();
                if (ImGui::Button("Save Game")) {
                    if (doSFX)
                        sound.buttonClickSound();
                    ImGui::OpenPopup("##Select Dialog SAVEGAME01");
                }

            }
            ImGui::NewLine();
            if (ImGui::Button("Load Game")) {
                if (doSFX)
                    sound.buttonClickSound();
                ImGui::OpenPopup("##Select Dialog LOADGAME01");
            }

            ImGui::NewLine();
            if (ImGui::Button("Options"))
            {
                if (doSFX)
                    sound.buttonClickSound();
                ImGui::OpenPopup("##Select Dialog OPTIONS01");
            }
            ImGui::NewLine();
            if (ImGui::Button("Quit Game")) {
                if (doSFX)
                    sound.buttonClickSound();
                ImGui::OpenPopup("##Confirm Dialog EXIT01");
            }
            newGame();
            loadGame();
            saveGame();
            SettingsMenuMain(&show_settings_menu);
            exitGame();
        }
    }
    endDrawMainMenu(); //This is End(); and Render(); , I made it so I could call from anywhere and not just rewrite it every where.
}

//Sorry if the issue is unclear, I am trying my best!
ocornut commented 2 years ago

I believe the error is either that I can't stack 2 PopupModal windows, or that it's a syntax error I can't find anywhere.

It is neither. There's a bug with your logic:

        if (ImGui::Button("Start new game", ImVec2(130, 0))) {
            ImGui::CloseCurrentPopup();
            ImGui::OpenPopup("##LoadGame");
            LoadingGameDialog();
        }

You are only calling the loading popup logic for a single frame when the button is clicked. You should probably move the LoadingGameDialog() call in the same location as the newGame() call.

See #5684 for the same issue with a more thorough explanation.

PsychoCoffee commented 2 years ago

Wow! Thank you and the OP very much. I got it to work.

I did a slight modification:

if (ImGui::Button("Start new game", ImVec2(130, 0))) {
            isLoadingDialog = true;
        }
//At the end of the method for drawing main menu I did:
if (isLoadingDialog) {
        ImGui::OpenPopup("##LoadGame");
    }
    LoadingGameDialog();