0

I am using ImGui in my application for UI. My question is when I press the button the code inside 'if condition' will execute. But once I pressed the button, I can't press the other button including the pressed button. Could any one let me know whats the problem?

Example Code:

while(window)

{
    // Poll and handle events (inputs, window resize, etc.)
    // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
    // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
    // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
    // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
    glfwPollEvents();

    // Start the Dear ImGui frame
    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplGlfw_NewFrame();
    ImGui::NewFrame();

    // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
    if (show_demo_window)
        ImGui::ShowDemoWindow(&show_demo_window);

    // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
    {
        static float f = 0.0f;
        static int counter = 0;

        ImGui::Begin("Hello, world!");                          // Create a window called "Hello, world!" and append into it.

        ImGui::Text("This is some useful text.");               // Display some text (you can use a format strings too)
        ImGui::Checkbox("Demo Window", &show_demo_window);      // Edit bools storing our window open/close state
        ImGui::Checkbox("Another Window", &show_another_window);

        ImGui::SliderFloat("float", &f, 0.0f, 1.0f);            // Edit 1 float using a slider from 0.0f to 1.0f
        ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color

        if (ImGui::Button("Button"))                            // Buttons return true when clicked (most widgets return true when edited/activated)

        {

            for (int i = 0; i < 1000000; i++)

            {
                cout << i;
            }
           
        }
h_uat
  • 33
  • 4
chethana
  • 19
  • 5
  • 1
    Your million prints will take quite a long time to execute, the gui presumably freezes until they're complete – Alan Birtles Jun 29 '20 at 06:45
  • May I know How to over come this freez, I tried using thread but still have the same problem, – chethana Jun 29 '20 at 06:53
  • Remove the million prints? – Alan Birtles Jun 29 '20 at 07:16
  • Hi, thankyou for you reply , the above code i just an example I am calling other functions inside the button , like its freezing when i press the button – chethana Jun 29 '20 at 08:18
  • 1
    doing work on GUI threads will in every GUI framework I know of freeze the GUI until that work is complete – Alan Birtles Jun 29 '20 at 10:18
  • Hi Alan Birtles , Sorry I didn't get your point – chethana Jun 30 '20 at 01:14
  • 1
    Please, show [mcve](stackoverflow.com/help/mcve). You speak of two buttons, yet the code contains only one. – Quimby Jun 30 '20 at 20:03
  • Hi Quimby in place of single button if i use the following code, if i call any function or for example the for loop that print one million numbers at that time the other button and also pressed button will freeze I tried to use thread when button pressed but no use could you give me some suggestion how to over come this problem if (ImGui::Button("Save")) { cout << "Button Pressed"; //or your IDE equivalent output log } if (ImGui::Button("Cancel")) { cout << "Button Pressed"; //or your IDE equivalent output log } ImGui::End(); – chethana Jul 02 '20 at 02:35
  • if (ImGui::Button("Save")) { cout << "Button Pressed"; //or your IDE equivalent output log } if (ImGui::Button("Cancel")) { cout << "Button Pressed"; //or your IDE equivalent output log } ImGui::End(); – chethana Jul 02 '20 at 02:38

2 Answers2

1

In the moment you press the button, everything in the corresponding if statement will be executed. This means that there will be no update of your user interface until all the is are successfully printed. Once this is done, your program will return to executing the GUI routine and the button can be pressed again.

If you want to run the printing of the is in the background you could consider using threads. Through this, you could continue using your GUI for other things which do not depend on the execution of the for loop. Most likely you also want to disable the button once you launched your thread. In each update of the frame, you can then check if the thread printing the is finished executing. If it did so, join the thread and enable the button again.

h_uat
  • 33
  • 4
-1

Immediate mode GUI doesnt mean all work can be done in the gui thread.

You could trigger actions to be done when a button is pressed. I would suggest only UI updation and simple logic inside gui render loops and all processing shall be moved to runner kind of threads. Lets say the execution time of a render loop cycle keep a quarter of 1/renderFPS

Naveen
  • 459
  • 4
  • 10