From 70958c5762081816ad431477737ae7cb385c4132 Mon Sep 17 00:00:00 2001 From: binarycat Date: Sat, 5 Mar 2022 13:46:33 -0500 Subject: [PATCH] add Prompt --- code/keybind.lua | 10 ++++++++ code/require.lua | 2 ++ code/ui.lua | 1 + code/ui/prompt.lua | 63 ++++++++++++++++++++++++++++++++++++++++++++++ main.lua | 35 ++++++++++++++++++++++++-- 5 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 code/ui/prompt.lua diff --git a/code/keybind.lua b/code/keybind.lua index 7458089..083b5d4 100644 --- a/code/keybind.lua +++ b/code/keybind.lua @@ -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"} diff --git a/code/require.lua b/code/require.lua index aeece38..310f5d9 100644 --- a/code/require.lua +++ b/code/require.lua @@ -1,3 +1,5 @@ + + -- data require "data/animations" require "data/shaders" diff --git a/code/ui.lua b/code/ui.lua index 274798f..7cff837 100644 --- a/code/ui.lua +++ b/code/ui.lua @@ -7,3 +7,4 @@ end require "code/ui/button" require "code/ui/dialog" +require "code/ui/prompt" diff --git a/code/ui/prompt.lua b/code/ui/prompt.lua new file mode 100644 index 0000000..18eb60d --- /dev/null +++ b/code/ui/prompt.lua @@ -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") + diff --git a/main.lua b/main.lua index bac5bc5..be8b2c0 100644 --- a/main.lua +++ b/main.lua @@ -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