Compare commits

...

3 Commits

Author SHA1 Message Date
binarycat
edd064c2fd improve Prompt and use it to ask for level and file name when exporting 2022-03-05 14:29:01 -05:00
binarycat
8cfc6a9d18 cleaned up file stuff in ExportLevel 2022-03-05 14:00:27 -05:00
binarycat
70958c5762 add Prompt 2022-03-05 13:46:33 -05:00
7 changed files with 141 additions and 5 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

@ -1,7 +1,9 @@
function ExportLevel(levelname, filename)
os.execute( "mkdir \"./export\"" )
love.filesystem.createDirectory("export")
filename = filename or "output.lua"
filename = "export/"..filename
if string.sub(filename, 1, 1) ~= "/" then
filename = "export/"..filename
end
exportFile = io.open(filename, "w+")
if exportFile 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,17 @@ 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
return true
end
end
return false
end
function Keybind:Default()
--Menu
Keybind.menu.pause.keys = {"escape"}

View File

@ -1,3 +1,5 @@
-- data
require "data/animations"
require "data/shaders"

View File

@ -7,3 +7,4 @@ end
require "code/ui/button"
require "code/ui/dialog"
require "code/ui/prompt"

68
code/ui/prompt.lua Normal file
View File

@ -0,0 +1,68 @@
local utf8 = require("utf8")
local function backspace(text)
local byteoffset = utf8.offset(text, -1)
if byteoffset then
-- remove the last UTF-8 character.
-- string.sub operates on bytes rather than UTF-8 characters,
-- so we couldn't do string.sub(text, 1, -2).
return string.sub(text, 1, byteoffset - 1)
end
return ""
end
Prompt = {
-- defaults for instance variables
pos = { x = 10, y = 10 },
input = "",
name = "input",
canceled = false,
closing = false,
active_prompt = nil,
}
function Prompt:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function Prompt:keypressed(key, scancode, isrepeat)
if key == "backspace" then
self.input = backspace(self.input)
elseif key == "return" or key == "kpenter" or key == "escape" then
if key == "escape" then
self.canceled = true
end
self.closing = true
self:func()
end
end
function Prompt:update()
end
function Prompt:textinput(text)
self.input = self.input .. text
end
function Prompt:draw()
love.graphics.print(self.name .. ": " .. self.input, self.pos.x, self.pos.y)
end
function Prompt:activate()
Prompt.active_prompt = self
love.keyboard.setTextInput(true)
end
-- demonstration of how default values work
local test_prompt = Prompt:new()
assert(test_prompt.name == "input")
test_prompt.name = "foobar"
assert(Prompt.name == "input")

View File

@ -62,6 +62,18 @@ function love.load()
--love.audio.play(music.placeholder)
end
function love.textinput(text)
if Prompt.active_prompt then
Prompt.active_prompt:textinput(text)
end
end
function love.keypressed(...)
if Prompt.active_prompt then
Prompt.active_prompt:keypressed(...)
end
end
function love.update(dt)
-- audio update
love.audio.update()
@ -85,15 +97,36 @@ function love.update(dt)
logWrite("Second "..secs..": "..memoryUsage.." kB")
end
if Prompt.active_prompt then
-- 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
if love.keyboard.isDown("f7") then
local test_prompt = Prompt:new({
name = "test prompt",
func = function(prompt)
print("test prompt got input: "..prompt.input)
end,
})
test_prompt:activate()
end
--keypressed
if Keybind:CheckPressed(Keybind.menu.pause) then
if do_pause then
if do_pause then
do_pause = false
else
menu_type = "pause"
MenuInit(menu_type)
end
end
end
--MenuStep
if menu_type ~= nil then MenuStep(menu_type) end
@ -134,5 +167,9 @@ function love.draw()
if DemoRecording or DemoPlayback then Demo:draw() end
if Prompt.active_prompt then
Prompt.active_prompt:draw()
end
frameDebugFlush()
end