improve Prompt and use it to ask for level and file name when exporting

This commit is contained in:
binarycat 2022-03-05 14:29:01 -05:00
parent 8cfc6a9d18
commit edd064c2fd
4 changed files with 31 additions and 4 deletions

View File

@ -65,7 +65,21 @@ function stepEditor()
end
if Keybind:CheckPressed(Keybind.debug.reload) then
ExportLevel("test")
Prompt:new({
name = "level name",
input = "unnamed",
func = function(name_prompt)
if name_prompt.canceled then return end
Prompt:new({
name = "filename",
input = "level.lua",
func = function(file_prompt)
if file_prompt.canceled then return end
ExportLevel(name_prompt.input, file_prompt.input)
end,
}):activate()
end,
}):activate()
end
if Keybind:CheckPressed(Keybind.debug.editor) then

View File

@ -44,6 +44,7 @@ function Keybind:CheckDown(action)
end
end
-- relies on being called exactly once per frame to be accurate.
function Keybind:CheckPressed(action)
if Keybind:CheckDown(action) then
if not action.pressed then
@ -77,6 +78,7 @@ function Keybind:RemoveKeys(action)
action.keys = {}
end
-- this prolly should be used by Prompt:keypressed()
function Keybind:hasKey(action, key)
for _, v in pairs(action.keys) do
if v == key then

View File

@ -18,6 +18,8 @@ Prompt = {
pos = { x = 10, y = 10 },
input = "",
name = "input",
canceled = false,
closing = false,
active_prompt = nil,
}
@ -32,9 +34,12 @@ end
function Prompt:keypressed(key, scancode, isrepeat)
if key == "backspace" then
self.input = backspace(self.input)
elseif key == "return" or key == "kpenter" then
elseif key == "return" or key == "kpenter" or key == "escape" then
if key == "escape" then
self.canceled = true
end
self.closing = true
self:func()
Prompt.active_prompt = nil
end
end

View File

@ -98,7 +98,13 @@ function love.update(dt)
end
if Prompt.active_prompt then
Prompt.active_prompt:update()
-- try to stop the keypress that closed the menu from spilling into the rest of the game
Keybind:CheckPressed(Keybind.menu.pause)
if Prompt.active_prompt.closing then
Prompt.active_prompt = nil
else
Prompt.active_prompt:update()
end
return
end