Skip to content

Commit

Permalink
add input ui element
Browse files Browse the repository at this point in the history
  • Loading branch information
Bauumm committed Nov 8, 2023
1 parent c701751 commit 1934716
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 2 deletions.
4 changes: 2 additions & 2 deletions assets/font/promptfont.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@
"Vertical Dots": "e28f91",
"Hamburger Menu": "e28f92",
"Arrow Left": "e28fb4",
"Arrow Up": "e28fb5",
"Arrow Right": "e28fb6",
"Arrow Up": "e28fb6",
"Arrow Right": "e28fb5",
"Arrow Down": "e28fb7",
"WASD": "e290a3",
"Arrow Keys": "e290a4",
Expand Down
225 changes: 225 additions & 0 deletions ui/elements/input.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
local icon_mappings = {
keyboard = {
a = "Key A",
b = "Key B",
c = "Key C",
d = "Key D",
e = "Key E",
f = "Key F",
g = "Key G",
h = "Key H",
i = "Key I",
j = "Key J",
k = "Key K",
l = "Key L",
m = "Key M",
n = "Key N",
o = "Key O",
p = "Key P",
q = "Key Q",
r = "Key R",
s = "Key S",
t = "Key T",
u = "Key U",
v = "Key V",
w = "Key W",
x = "Key X",
y = "Key Y",
z = "Key Z",
[0] = "Key 0",
[1] = "Key 1",
[2] = "Key 2",
[3] = "Key 3",
[4] = "Key 4",
[5] = "Key 5",
[6] = "Key 6",
[7] = "Key 7",
[8] = "Key 8",
[9] = "Key 9",
space = "Space",
["!"] = "ASCII !",
['"'] = 'ASCII "',
["#"] = "ASCII #",
["$"] = "ASCII $",
["&"] = "ASCII &",
["'"] = "ASCII '",
["("] = "ASCII (",
[")"] = "ASCII )",
["*"] = "ASCII *",
["+"] = "ASCII +",
[","] = "ASCII ,",
["-"] = "ASCII -",
["."] = "ASCII .",
["/"] = "ASCII /",
[":"] = "ASCII :",
[";"] = "ASCII ;",
["<"] = "ASCII <",
["="] = "ASCII =",
[">"] = "ASCII >",
["?"] = "ASCII ?",
["@"] = "ASCII @",
["["] = "ASCII [",
["\\"] = "ASCII \\",
["]"] = "ASCII ]",
["^"] = "ASCII ^",
["_"] = "ASCII _",
["`"] = "ASCII `",
kp0 = "",
kp1 = "",
kp2 = "",
kp3 = "",
kp4 = "",
kp5 = "",
kp6 = "",
kp7 = "",
kp8 = "",
kp9 = "",
["kp."] = "",
["kp,"] = "",
["kp/"] = "",
["kp*"] = "",
["kp-"] = "",
["kp+"] = "",
kpenter = "",
["kp="] = "",
up = "Arrow Up",
down = "Arrow Down",
right = "Arrow Right",
left = "Arrow Left",
home = "Home",
["end"] = "End",
pageup = "Page Up",
pagedown = "Page Down",
insert = "Insert",
backspace = "Backspace",
tab = "Tab",
clear = "",
["return"] = "Enter",
delete = "Delete",
f1 = "F1",
f2 = "F2",
f3 = "F3",
f4 = "F4",
f5 = "F5",
f6 = "F6",
f7 = "F7",
f8 = "F8",
f9 = "F9",
f10 = "F10",
f11 = "F11",
f12 = "F12",
f13 = "",
f14 = "",
f15 = "",
f16 = "",
f17 = "",
f18 = "",
numlock = "NumLock",
capslock = "Caps",
scrolllock = "ScrLk",
rshift = "",
lshift = "Shift",
rctrl = "",
lctrl = "Ctrl",
ralt = "",
lalt = "Alt",
rgui = "",
lgui = "Super",
mode = "",
www = "",
mail = "",
calculator = "",
computer = "",
appsearch = "",
apphome = "",
appback = "",
appforward = "",
apprefresh = "",
appbookmarks = "",
pause = "Pause",
escape = "Esc",
help = "",
printscreen = "PrtSc",
sysreq = "",
menu = "",
application = "",
power = "",
currencyunit = "",
undo = "",
},
mouse = {
l = "Mouse Button 1",
m = "Mouse Button 3",
r = "Mouse Button 2",
wd = "Scroll Down",
wu = "Scroll Up",
x1 = "Mouse Button 4",
x2 = "Mouse Button 5",
},
touch = {
left = "square-half",
right = "square-half:mirror",
},
}
local icon = require("ui.elements.icon")
local input_schemes = require("input_schemes")
local async = require("async")

local input = {}
input.__index = setmetatable(input, icon)

function input:new(scheme, id, options)
local icon_id = icon_mappings[scheme][id]
if icon_id == "" then
-- set it to something known for now to change later
icon_id = "circle" -- no input has this icon
end
local obj = icon:new(icon_id, options)
setmetatable(obj, input)
if icon_id == "circle" then
-- no icon found for input, display text instead
obj.raw_text = id
end
obj.scheme = scheme
obj.waiting = false
return obj
end

function input:set_icon(id)
local icon_id = icon_mappings[self.scheme][id]
if icon_id ~= "" then
self:set(icon_id)
else
self.raw_text = id
self.changed = true
end
end

function input:process_event(name, ...)
icon.process_event(self, name, ...)
if self.waiting and self.resolve then
for id in pairs(icon_mappings[self.scheme]) do
if input_schemes[self.scheme].is_down(id) then
self.resolve(id)
self.resolve = nil
break
end
end
else
require("ui").only_interactable_element = nil
end
end

input.wait_for_input = async(function(self)
self.waiting = true
self.text:set("")
local ui = require("ui")
ui.only_interactable_element = self
local id = async.await(async.promise:new(function(resolve)
self.resolve = resolve
end))
self.waiting = false
self:set_icon(id)
end)

return input
4 changes: 4 additions & 0 deletions ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ end
---@param name string
---@param ... unknown
function ui.process_event(name, ...)
if ui.only_interactable_element then
ui.only_interactable_element:process_event(name, ...)
return
end
-- reset scrolled_already value (determines if a container can still scroll, ensures child priority over parent with scrolling (children are processed before parents))
scroll.scrolled_already = false
love.graphics.origin()
Expand Down

0 comments on commit 1934716

Please sign in to comment.