Search code examples
c++user-interfaceimgui

How to remove button in IMGui and prevent settings window from closing?


I was using docking tree code of IMGui and I created a Application.cpp and Application.h in example_win32_directx9. I am using release and x64 to compile. I have set subsystem in system in Linker to Windows. Code of Application.cpp is:

#include "Application.h"

#include "imgui.h"

namespace MyApp
{

    void RenderUI()
    {
        bool Decimal_places = false;
        bool settings = false;
        ImGui::Begin("Main");
        if (ImGui::Button("Settings")) {
            settings = true;
        }
        else {
            settings = false;
        }
        ImGui::End();
        if (settings==true) {
            ImGui::Begin("Settings");
            if (Decimal_places == false) {
                if (ImGui::Button("Off")) {
                    Decimal_places = true;
                }
            }
            ImGui::End();
        }
        //static float value = 0.0f;
        //ImGui::DragFloat("Value", &value);
    }

}

Code of Application.h:

#pragma once

namespace MyApp
{

    void RenderUI();

}

It works somewhat fine but I do not how to remove button "off" or change it from "off" to "on" and why do settings window closes. I have already included header file in main.cpp and also using the function of header file in main.cpp.


Solution

  • To fix it, bools need to be declared out of function. After editing it, the fixed Application.cpp with more functions was:

    #include "Application.h"
    
    #include "imgui.h"
    
    namespace MyApp
    {
        bool settings = false;
        bool p_open = true;
        bool Decimal_places = false;
    
        void RenderUI()
        {
            ImGui::Begin("Main");
            if (ImGui::Button("Settings")) {
                settings = true;
            }
            ImGui::End();
            if (settings == true) {
                if (p_open == true) {
                    if (!ImGui::Begin("Settings", &p_open)) {
                        ImGui::End();
                    }
                    else {
                        if (Decimal_places == false) {
                            if (ImGui::Button("Off")) {
                                Decimal_places = true;
                            }
                        }
                        else if (Decimal_places == true) {
                            if (ImGui::Button("On")) {
                                Decimal_places = false;
                            }
                        }
                        ImGui::End();
                    }
                }
                else {
                    p_open = true;
                    settings = false;
                }
            }
        }
    
    }