add Prompt

This commit is contained in:
binarycat 2022-03-05 13:46:33 -05:00
parent 3c4f763ae7
commit 70958c5762
5 changed files with 109 additions and 2 deletions

View File

@ -77,6 +77,16 @@ function Keybind:RemoveKeys(action)
action.keys = {}
end
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"

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

@ -0,0 +1,63 @@
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",
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" then
self:func()
Prompt.active_prompt = nil
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()
@ -84,16 +96,31 @@ function love.update(dt)
end
logWrite("Second "..secs..": "..memoryUsage.." kB")
end
if Prompt.active_prompt then
Prompt.active_prompt:update()
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
@ -133,6 +160,10 @@ function love.draw()
love.graphics.print(game.scale,10,40)
if DemoRecording or DemoPlayback then Demo:draw() end
if Prompt.active_prompt then
Prompt.active_prompt:draw()
end
frameDebugFlush()
end