Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PopupModal not appearing #8272

Open
zMidair opened this issue Dec 29, 2024 · 2 comments
Open

PopupModal not appearing #8272

zMidair opened this issue Dec 29, 2024 · 2 comments
Labels

Comments

@zMidair
Copy link

zMidair commented Dec 29, 2024

Version/Branch of Dear ImGui:

Version 1.91.5, Branch: master

Back-ends:

n/a

Compiler, OS:

Windows 11 + CMake

Full config/build information:

No response

Details:

Hello!
I am trying to make a custom popup modal. In order to keep things organized, I made a class function for making and drawing it.

alert() initializes a class variable (Popup* activePopup) that holds the popup info
drawAlerts() draws the popup that has been created

I do call drawAlerts in my draw function but it seems it is not appearing, did I do something wrong? How can I improve the system?

Screenshots/Video:

Popup functions:
{4BE9960F-2219-4B7F-89FA-DA7524CE1DB0}
Popup variable:
{67AA5BC3-640B-4DE1-9785-3CB9AF599485}

Minimal, Complete and Verifiable Example code:

class myclass {
      // I will leave my functions / variables here for better visibility
      static void drawAlerts()
      {
          if(activePopup == nullptr) return;
      
          ImGui::SetNextWindowSize(ImVec2(0, ImGui::CalcTextSize(activePopup->text).x));
          ImGui::SetNextWindowPos({
              ImGui::GetIO().DisplaySize.x / 2,
              ImGui::GetIO().DisplaySize.y / 2
          }, ImGuiCond_Always, ImVec2(0.5f, 0.5f));
      
          if(ImGui::BeginPopupModal(activePopup->title, NULL, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize))
          {
              AlignForWidth(ImGui::GetCurrentWindow()->SizeFull.x);
              ImGui::Text(activePopup->text);
              ImGui::Separator();
      
              ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
              ImGui::SetNextItemWidth(activePopup->btn2 != nullptr ? ImGui::GetCurrentWindow()->SizeFull.x / 2 : ImGui::GetCurrentWindow()->SizeFull.x);
              if(ImGui::Button(activePopup->btn1)) { ImGui::CloseCurrentPopup(); activePopup->text = activePopup->title = activePopup->btn1 = activePopup->btn2 = nullptr; }
              if(activePopup->btn2 != nullptr)
              {
                  ImGui::SameLine();
                  ImGui::SetNextItemWidth(activePopup->btn2 != nullptr ? ImGui::GetCurrentWindow()->SizeFull.x / 2 : ImGui::GetCurrentWindow()->SizeFull.x);
                  if(ImGui::Button(activePopup->btn2)) { ImGui::CloseCurrentPopup(); activePopup->text = activePopup->title = activePopup->btn1 = activePopup->btn2 = nullptr; }
              }
              ImGui::PopStyleColor();
      
              ImGui::EndPopup();
          }
      }
      
      static void alert(const char* title, const char* text, const char* btn1, const char* btn2 = nullptr, const std::function<void()>& callback = [](){})
      {
          activePopup = new Popup(title, text, btn1, btn2);
      
          ImGui::OpenPopup(activePopup->title);
      }
      // Variables
      struct Popup {
          const char* title;
          const char* text;
          const char* btn1;
          const char* btn2;
      };
      static Popup* activePopup = nullptr;

]; // End of class
@PathogenDavid
Copy link
Contributor

OpenPopup is sensitive to the current state of the ID stack.

Chances are you're opening the popup in a different ID context from where drawAlerts is called.

The way I've solved this sort of issue in the past is to add a bool wantsOpen field to your Popup struct and have OpenPopup get called from drawAlerts when it's true (and clear the field after the call so it only happens once.)

@zMidair
Copy link
Author

zMidair commented Dec 30, 2024

OpenPopup is sensitive to the current state of the ID stack.

Chances are you're opening the popup in a different ID context from where drawAlerts is called.

The way I've solved this sort of issue in the past is to add a bool wantsOpen field to your Popup struct and have OpenPopup get called from drawAlerts when it's true (and clear the field after the call so it only happens once.)

Hello, I've tried this but now it is not logging anything in the ImGui Debug Log either. Should I call drawAlerts() after I call Begin() to draw my main window or before?

Here are the changes:

class myclass {
struct Popup {
    const char* title;
    const char* text;
    const char* btn1;
    const char* btn2;
    bool shouldOpen;
};
static Popup* activePopup = nullptr;
static void drawAlerts()
{
    if(activePopup == nullptr) return;

    if(activePopup->shouldOpen)
    {
        ImGui::OpenPopup(activePopup->title);
        activePopup->shouldOpen = false;
    }

    ImGui::SetNextWindowSize(ImVec2(0, ImGui::CalcTextSize(activePopup->text).x));
    ImGui::SetNextWindowPos({
        ImGui::GetIO().DisplaySize.x / 2,
        ImGui::GetIO().DisplaySize.y / 2
    }, ImGuiCond_Always, ImVec2(0.5f, 0.5f));

    if(ImGui::BeginPopupModal(activePopup->title, NULL, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize))
    {
        AlignForWidth(ImGui::GetCurrentWindow()->SizeFull.x);
        ImGui::Text(activePopup->text);
        ImGui::Separator();

        ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
        ImGui::SetNextItemWidth(activePopup->btn2 != nullptr ? ImGui::GetCurrentWindow()->SizeFull.x / 2 : ImGui::GetCurrentWindow()->SizeFull.x);
        if(ImGui::Button(activePopup->btn1)) { ImGui::CloseCurrentPopup(); activePopup->text = activePopup->title = activePopup->btn1 = activePopup->btn2 = nullptr; }
        if(activePopup->btn2 != nullptr)
        {
            ImGui::SameLine();
            ImGui::SetNextItemWidth(activePopup->btn2 != nullptr ? ImGui::GetCurrentWindow()->SizeFull.x / 2 : ImGui::GetCurrentWindow()->SizeFull.x);
            if(ImGui::Button(activePopup->btn2)) { ImGui::CloseCurrentPopup(); activePopup->text = activePopup->title = activePopup->btn1 = activePopup->btn2 = nullptr; }
        }
        ImGui::PopStyleColor();

        ImGui::EndPopup();
    }
}

static void alert(const char* title, const char* text, const char* btn1, const char* btn2 = nullptr, const std::function<void()>& callback = [](){})
{
    activePopup = new Popup(title, text, btn1, btn2, true);
}
};

@ocornut ocornut added the popups label Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants