Flix01 / imgui

Dear ImGui Addons Branch = plain unmodified dear imgui plus some extra addon.
https://github.com/Flix01/imgui/wiki/ImGui-Addons-Branch-Home
MIT License
396 stars 34 forks source link

Cancel option for imguifilesystem? #70

Closed thecosmicslug closed 8 months ago

thecosmicslug commented 8 months ago

I have been using imgui to provide an interface for an embeded device, and imguifilesystem for a file dialog. It is working with a gamepad but the only issue is i am unable to cancel the dialog without a selection being made. I have settled for toggling the UI with a button but on return to UI the dialog is still showing. Is it possible to code in a cancel key? Or is there someway to reset the state next time UI is toggled back on?

Flix01 commented 8 months ago

AFAIR, the open file dialog is the only one without a cancel button, but you can close the dialog with its (window) close button. In any case, to check for valid entries, the first demo (main.cpp) uses this code:

        //------------------------------------------------------------------------------------------
        // 1 - ChooseFileDialogButton setup:
        //------------------------------------------------------------------------------------------
        ImGui::Text("Please choose a file: ");ImGui::SameLine();
        const bool browseButtonPressed = ImGui::Button("...");
        static ImGuiFs::Dialog fsInstance;
        const char* chosenPath = fsInstance.chooseFileDialog(browseButtonPressed,startingFolder,optionalFileExtensionFilterString);
        if (strlen(chosenPath)>0) {
            // A path (chosenPath) has been chosen right now. However we can retrieve it later using: fsInstance.getChosenPath()
        }
        if (strlen(fsInstance.getChosenPath())>0) ImGui::Text("Chosen path: \"%s\"",fsInstance.getChosenPath());

Hope it helps.

P.S. The part of the demo related to file system dialogs can be found here: code

thecosmicslug commented 8 months ago

Thankyou for such a quick response! Sorry if I wasn't clear before but my issue is that on this embedded platform I have no cursor, only gamepad navigation... Which is working great for everything, except I have no way to reach that close 'X' on the window and cancel the dialog! I just wondered is there another way but i guess not, thanks anyway.

Flix01 commented 8 months ago

Sorry (I didn't know that close buttons are not reachable without cursor support). However on a quick test, if you can modify the source code a bit, you can try adding here imguifilesystem.cpp: line 2084 something like:

// End selection field----------------------------------------------------------------
    // start new code
    if (rv[0]==0 && (ImGui::IsKeyPressed(ImGuiKey_Delete) || ImGui::IsKeyPressed(ImGuiKey_GamepadBack) )) {
        ImGui::CloseCurrentPopup();
#       ifdef IMGUI_USE_MINIZIP
        I.unz.close();
#       endif // IMGUI_USE_MINIZIP
        I.freeMemory();

        I.userHasJustCancelledDialog = true;
    }
    // end new code
    if (rv[0]!=0 || !I.open) { [...]

This way when Canc key or gamepad_back are pressed, the dialog should cancel (change the keys as you like). My preliminary tests show that it works for all the dialogs and there are no memory leaks... but it's a modification that I'd rather not add to the repository (well, mainly because it is so outdated that I cannot update it without losing a lot of 'addons'... but this is another problem...).

Hope this can be enough for your case (but I guess you probably need a proper physical close button instead...).

thecosmicslug commented 8 months ago

Thankyou, addding that to line 2804 works!