[*] General code improvement.

* Rework navigation allowing direct management and triggering for grabkeyboard widgets.
* Navigation code now behaves better with deeply nested layouts.
* Allow widget hierarchies deeper than 2 (__call() is implemented for every Widget metatable).
* Make BASE locals more secure (match '<filename>$' in regexp)
This commit is contained in:
2022-08-24 11:18:48 +02:00
parent 3b2b460012
commit 1d42498ee7
13 changed files with 283 additions and 187 deletions

View File

@ -1,12 +1,15 @@
local BASE = (...):gsub('input', '')
local BASE = (...):gsub('input$', '')
local Widget = require(BASE..'widget')
local core = require(BASE..'core')
local utf8 = require 'utf8'
-- Grabs keyboard on focus
local Input = setmetatable({ grabkeyboard = true }, Widget)
-- NOTE: Input manages keyboard directly.
local Input = setmetatable({
grabkeyboard = true,
__call = function(cls, args) return cls.new(args) end
}, Widget)
Input.__index = Input
@ -28,6 +31,18 @@ function Input.new(args)
return self
end
-- NOTE: Input steals keyboard input on focus.
function Input:gainFocus()
love.keyboard.setTextInput(true, self.x,self.y,self.w,self.h)
love.keyboard.setKeyRepeat(true)
end
function Input:loseFocus()
if love.system.getOS() == 'Android' or love.system.getOS() == 'iOS' then
love.keyboard.setTextInput(false)
end
love.keyboard.setKeyRepeat(false)
end
function Input:onPointerInput(px,py, clicked)
if clicked then
self:grabFocus()
@ -92,9 +107,12 @@ function Input:keyreleased(key)
self.cursor = 1
elseif key == 'end' then
self.cursor = utf8.len(self.text)+1
elseif key == 'return' then
self:onChange(self.text)
self.cursor = 1
elseif key == 'up' or key == 'down' then
self.ui:navigate(key)
elseif key == 'tab' or key == 'return' then
self.ui:navigate('right')
elseif key == 'escape' then
self.ui:navigate('cancel')
end
end
end