flamendless / Slab

An immediate mode GUI for the Love2D framework.
MIT License
286 stars 25 forks source link

OpenFileDialog has a weird behavior #143

Closed FriskTheFallenHuman closed 1 year ago

FriskTheFallenHuman commented 1 year ago

I'm having an issue with Slab.FileDialog the dialog displays but it instantly closes itself I followed both wiki and SlabTest.lua but both ways trigger this issue here's a video click of the problem in action:

https://user-images.githubusercontent.com/12750944/198930711-a07c00b2-914c-4225-acb4-edd0786e15d1.mp4

here's what the code looks like:

local Class = require("lib.middleclass")
local Slab = require("lib.slab")
local SlabGUI = Class("SlabGUI")

-- New: this is our constructor
function SlabGUI:Initialize()
end

-- Load : This is our post constructor
function SlabGUI:Load()
    Slab.Initialize()
end

-- Update : Update this object every frame
function SlabGUI:Update(dt)
    Slab.Update(dt)

    if GAMEINFO.BUILD_TYPE == "Debug" then
        if Slab.BeginContextMenuWindow() then
            if Slab.BeginMenu("Demo") then
                if Slab.MenuItem("Load Starbound Files") then
                    SlabGUI:OpenDialogEx(FILETYPES, 'openfile')
                end
                Slab.MenuItem("Save Starbound Files")
                Slab.EndMenu()
            end

            if Slab.MenuItem("Quit") then
                love.event.quit()
            end

            Slab.EndContextMenu()
        end

    end
end

-- Draw : Draws anything on screen
function SlabGUI:Draw()
    Slab.Draw()

    if GAMEINFO.BUILD_TYPE == "Debug" then
        love.graphics.printf(GAMEINFO.GAME_NAME .. " " .. GAMEINFO.GAME_VERSION .. "\n" .. "Build " .. GAMEINFO.BUILD_TYPE .. "\n" .. "This is a early preview build",
                            (love.graphics.getWidth() / 2) + 192,
                            (love.graphics.getHeight() / 2) + 250,
                            200,
                            "right")
    end
end

-- OpenFileDialogEx : Setups a File dialog for us, this iteractes on tables
function SlabGUI:OpenDialogEx(table, type)
    if type ~= '' then
        type = ''
        local stubtable = {}
        setmetatable(stubtable, {_index = table})
        local Result = Slab.FileDialog({Type = type, Filters = stubtable})
        if Result.Button ~= "" then
            type = ''
            if Result.Button == "Ok" then
                -- Return a value in here
            end
        end
    end
end

return SlabGUI
flamendless commented 1 year ago

Hi, you're supposed to run/call your file dialog function outside of the if statements of your menu. A good way to do this is to set a flag to true when the MenuItem is clicked, then use that flag whether to draw the file dialog or not after the BeginContextMenuWindow scope.

Think of Immediate Mode UIs as happening per frame, if you click the menu item, the file dialog will be drawn, but on the next frame, since you are not clicking on the menu item, the line (inside the conditionals) will not be called and thus no file dialog draw.