helium/shell/input.lua
2021-05-04 05:45:26 +03:00

75 lines
1.5 KiB
Lua

local path = string.sub(..., 1, string.len(...) - string.len(".shell.input"))
local state = require(path.. ".hooks.state")
local input = require(path.. ".core.input")
local utf8 = require("utf8")
return function(onChange, onFinish, startStr, onEnter, onExit, x, y, w, h)
local textState = state {
focused = false,
text = startStr or '',
over = false
}
local keyInput, textInput
keyInput = input('keypressed', function(key)
if key == 'backspace' then
local byteoffset = utf8.offset(textState.text, -1)
if byteoffset then
textState.text = string.sub(textState.text, 1, byteoffset - 1)
end
if onChange then
onChange(textState.text)
end
end
if key == 'return' then
textState.focused = false
keyInput:off()
textInput:off()
if onFinish then
onFinish(textState.text)
end
end
end)
textInput = input('textinput', function(text)
textState.text = textState.text .. text
if onChange then
onChange(textState.text)
end
end)
input('mousepressed', function()
textState.focused = true
keyInput:on()
textInput:on()
end)
input('mousepressed_outside', function()
textState.focused = false
keyInput:off()
textInput:off()
if onFinish then
onFinish(textState.text)
end
end)
input('hover', function(x, y, w, h)
if onEnter then
onEnter(x, y, w, h)
end
textState.over = true
return function(x, y, w, h)
if onExit then
onExit(x, y, w, h)
end
textState.over = false
end
end)
return textState
end