jvcleave / ofxImGui

Use ImGui in openFrameworks
300 stars 123 forks source link

ImGui in a separate app window #63

Closed rjx-ray closed 7 years ago

rjx-ray commented 7 years ago

I'm trying to build a separate ImGui settings window for my app. I've followed the multiWindowOneAppExample which does this using ofxGui not ofxImGui

I can create a separate window with a menubar and a drop down menu like this: image

The problem is that whenever I click in the window off the menubar or the dropdown item I lose the mouse focus, the menu drop down is greyed out and I can never get it back.

I made a simple test program to illustrate the issue in app.h I have

    void drawGui(ofEventArgs & args);
    ofxImGui::Gui gui;
    bool flag;

in main.cpp I have

int main( ){
    // Create main window
    ofGLFWWindowSettings mainWinSettings;
    mainWinSettings.width = 1920;
    mainWinSettings.height = 240;
    mainWinSettings.resizable = true;
    shared_ptr<ofAppBaseWindow> mainWindow = ofCreateWindow(mainWinSettings);

    // create a separate window for imGui
    ofGLFWWindowSettings guiWinSettings;
    guiWinSettings.width = 600;
    guiWinSettings.height = 600;
    guiWinSettings.resizable = true;
    shared_ptr<ofAppBaseWindow> guiWindow = ofCreateWindow(guiWinSettings);

    shared_ptr<ofApp> mainApp(new ofApp);
    mainApp->setupGui();
    ofAddListener(guiWindow->events().draw, mainApp.get(), &ofApp::drawGui);

    ofRunApp(mainWindow, mainApp);
    ofRunMainLoop();
}

and in app.cpp I have

//--------------------------------------------------------------
void ofApp::setupGui() {
    gui.setup();
}
//--------------------------------------------------------------
void ofApp::drawGui(ofEventArgs & args) {
    gui.begin();
    ImGui::SetNextWindowSize(ofVec2f(ofGetWidth(), ofGetHeight()));
    ImGui::SetNextWindowPos(ofVec2f(0, 0));
    ImGuiWindowFlags window_flags = 0;
    window_flags |= ImGuiWindowFlags_NoTitleBar;
    window_flags |= ImGuiWindowFlags_NoResize;
    window_flags |= ImGuiWindowFlags_NoMove;
    window_flags |= ImGuiWindowFlags_NoSavedSettings;

    bool p_open = true;
    ImGui::Begin("gui", &p_open, window_flags);  // Menubar Window
    if (ImGui::BeginMainMenuBar()) {
        // View menu
        if (ImGui::BeginMenu("View")) {
            ImGui::Checkbox("Flag", &flag);
            ImGui::EndMenu();
        }
        ImGui::EndMainMenuBar();
    }
    ImGui::End();
    gui.end();
}

I must be missing something somewhere but I can't figure it out. Thanks for any help

jvcleave commented 7 years ago

Can you try:

static bool p_open = true;

rjx-ray commented 7 years ago

Thanks for the quick response, but that makes no difference

jvcleave commented 7 years ago

I've only used the Menubar once - here's how I did it. It's a bit different than yours so maybe it helps

ofxImGui gui;
bool showControlsWindow;

void drawMenuBar()
{
    if (ImGui::BeginMainMenuBar())
    {
        if (ImGui::BeginMenu("File"))
        {
            if (ImGui::MenuItem("Quit"))
            {
                ofExit();
            }
            ImGui::EndMenu();
        }

        if (ImGui::BeginMenu("Windows"))
        {
            ImGui::MenuItem("Show Controls", NULL, &showControlsWindow);

            bool doCloseAll = false;
            if(ImGui::MenuItem("Close All", NULL, &doCloseAll))
            {
                showControlsWindow = false;
            }

            ImGui::EndMenu();
        }
        ImGui::EndMainMenuBar();
    }

}

void draw()
{
    gui.begin();
    drawMenuBar();
    if(showControlsWindow)
    {
        if(!ImGui::Begin("Controls Window", &showControlsWindow))
        {
            ImGui::End();
        }else
        {
            ImGui::End();
        }
    }
    gui.end();

}
rjx-ray commented 7 years ago

The behaviour is still the same, click anywhere within the window but outside the menu and you lose focus. It must be something to do with how the event is passed

rjx-ray commented 7 years ago

I discovered my error. I need to set ImGuiWindowFlags_MenuBar and then use BeginMenuBar instead of BeginMainMenuBar. Everything then works as expected