mirror of
https://github.com/airstruck/luigi.git
synced 2025-12-19 02:16:43 +00:00
add menus
This commit is contained in:
127
luigi/input.lua
127
luigi/input.lua
@@ -8,62 +8,95 @@ local Input = Base:extend()
|
||||
|
||||
local weakValueMeta = { __mode = 'v' }
|
||||
|
||||
function Input:constructor (layout)
|
||||
self.layout = layout
|
||||
function Input:constructor () --(layout)
|
||||
-- layout = layout
|
||||
self.pressedWidgets = setmetatable({}, weakValueMeta)
|
||||
self.passedWidgets = setmetatable({}, weakValueMeta)
|
||||
end
|
||||
|
||||
function Input:handleDisplay ()
|
||||
local root = self.layout.root
|
||||
function Input:handleDisplay (layout)
|
||||
local root = layout.root
|
||||
if root then Renderer:render(root) end
|
||||
Event.Display:emit(self.layout)
|
||||
Event.Display:emit(layout)
|
||||
end
|
||||
|
||||
function Input:handleKeyPress (key, x, y)
|
||||
local widget = self.layout.focusedWidget or self.layout:getWidgetAt(x, y)
|
||||
function Input:handleKeyPress (layout, key, x, y)
|
||||
local widget = layout.focusedWidget or layout:getWidgetAt(x, y)
|
||||
local hit = true
|
||||
if not widget then
|
||||
hit = nil
|
||||
widget = layout.root
|
||||
end
|
||||
local result = widget:bubbleEvent('KeyPress', {
|
||||
hit = hit,
|
||||
key = key, x = x, y = y
|
||||
})
|
||||
if result ~= nil then return result end
|
||||
return hit
|
||||
end
|
||||
|
||||
function Input:handleKeyRelease (key, x, y)
|
||||
local widget = self.layout.focusedWidget or self.layout:getWidgetAt(x, y)
|
||||
function Input:handleKeyRelease (layout, key, x, y)
|
||||
local widget = layout.focusedWidget or layout:getWidgetAt(x, y)
|
||||
local hit = true
|
||||
if not widget then
|
||||
hit = nil
|
||||
widget = layout.root
|
||||
end
|
||||
local result = widget:bubbleEvent('KeyRelease', {
|
||||
hit = hit,
|
||||
key = key, x = x, y = y
|
||||
})
|
||||
if result ~= nil then return result end
|
||||
return hit
|
||||
end
|
||||
|
||||
function Input:handleTextInput (text, x, y)
|
||||
local widget = self.layout.focusedWidget or self.layout:getWidgetAt(x, y)
|
||||
function Input:handleTextInput (layout, text, x, y)
|
||||
local widget = layout.focusedWidget or layout:getWidgetAt(x, y)
|
||||
local hit = true
|
||||
if not widget then
|
||||
hit = nil
|
||||
widget = layout.root
|
||||
end
|
||||
widget:bubbleEvent('TextInput', {
|
||||
hit = hit,
|
||||
text = text, x = x, y = y
|
||||
})
|
||||
return hit
|
||||
end
|
||||
|
||||
function Input:handleMove (x, y)
|
||||
local widget = self.layout:getWidgetAt(x, y)
|
||||
function Input:handleMove (layout, x, y)
|
||||
local widget = layout:getWidgetAt(x, y)
|
||||
local hit = true
|
||||
if not widget then
|
||||
hit = nil
|
||||
widget = layout.root
|
||||
end
|
||||
local previousWidget = self.previousMoveWidget
|
||||
if not widget.hovered then
|
||||
if widget ~= previousWidget then
|
||||
if previousWidget then
|
||||
previousWidget.hovered = nil
|
||||
for ancestor in previousWidget:eachAncestor(true) do
|
||||
ancestor.hovered = nil
|
||||
end
|
||||
end
|
||||
for ancestor in widget:eachAncestor(true) do
|
||||
ancestor.hovered = true
|
||||
end
|
||||
widget.hovered = true
|
||||
end
|
||||
widget:bubbleEvent('Move', {
|
||||
hit = hit,
|
||||
oldTarget = previousWidget,
|
||||
x = x, y = y
|
||||
})
|
||||
if widget ~= previousWidget then
|
||||
if previousWidget then
|
||||
previousWidget:bubbleEvent('Leave', {
|
||||
hit = hit,
|
||||
newTarget = widget,
|
||||
x = x, y = y
|
||||
})
|
||||
end
|
||||
widget:bubbleEvent('Enter', {
|
||||
hit = hit,
|
||||
oldTarget = previousWidget,
|
||||
x = x, y = y
|
||||
})
|
||||
@@ -74,21 +107,29 @@ function Input:handleMove (x, y)
|
||||
end
|
||||
self.previousMoveWidget = widget
|
||||
end
|
||||
return hit
|
||||
end
|
||||
|
||||
function Input:handlePressedMove (x, y)
|
||||
local widget = self.layout:getWidgetAt(x, y)
|
||||
function Input:handlePressedMove (layout, x, y)
|
||||
local widget = layout:getWidgetAt(x, y)
|
||||
local hit = true
|
||||
if not widget then
|
||||
hit = nil
|
||||
widget = layout.root
|
||||
end
|
||||
for button = 1, 3 do
|
||||
local originWidget = self.pressedWidgets[button]
|
||||
local passedWidget = self.passedWidgets[button]
|
||||
if originWidget then
|
||||
originWidget:bubbleEvent('PressDrag', {
|
||||
hit = hit,
|
||||
newTarget = widget,
|
||||
button = button,
|
||||
x = x, y = y
|
||||
})
|
||||
if (widget == passedWidget) then
|
||||
widget:bubbleEvent('PressMove', {
|
||||
hit = hit,
|
||||
origin = originWidget,
|
||||
button = button,
|
||||
x = x, y = y
|
||||
@@ -97,6 +138,7 @@ function Input:handlePressedMove (x, y)
|
||||
originWidget.pressed = (widget == originWidget) or nil
|
||||
if passedWidget then
|
||||
passedWidget:bubbleEvent('PressLeave', {
|
||||
hit = hit,
|
||||
newTarget = widget,
|
||||
origin = originWidget,
|
||||
button = button,
|
||||
@@ -104,42 +146,58 @@ function Input:handlePressedMove (x, y)
|
||||
})
|
||||
end
|
||||
widget:bubbleEvent('PressEnter', {
|
||||
oldTarget = passedWidget,
|
||||
origin = originWidget,
|
||||
button = button,
|
||||
x = x, y = y
|
||||
})
|
||||
hit = hit,
|
||||
oldTarget = passedWidget,
|
||||
origin = originWidget,
|
||||
button = button,
|
||||
x = x, y = y
|
||||
})
|
||||
self.passedWidgets[button] = widget
|
||||
end
|
||||
end
|
||||
end
|
||||
return hit
|
||||
end
|
||||
|
||||
function Input:handlePressStart (button, x, y, widget, accelerator)
|
||||
local widget = widget or self.layout:getWidgetAt(x, y)
|
||||
function Input:handlePressStart (layout, button, x, y, widget, accelerator)
|
||||
local widget = widget or layout:getWidgetAt(x, y)
|
||||
local hit = true
|
||||
if not widget then
|
||||
hit = nil
|
||||
widget = layout.root
|
||||
end
|
||||
widget.pressed = true
|
||||
self.pressedWidgets[button] = widget
|
||||
self.passedWidgets[button] = widget
|
||||
widget:focus()
|
||||
widget:bubbleEvent('PressStart', {
|
||||
hit = hit,
|
||||
button = button,
|
||||
accelerator = accelerator,
|
||||
x = x, y = y
|
||||
})
|
||||
return hit
|
||||
end
|
||||
|
||||
function Input:handlePressEnd (button, x, y, widget, accelerator)
|
||||
local widget = widget or self.layout:getWidgetAt(x, y)
|
||||
function Input:handlePressEnd (layout, button, x, y, widget, accelerator)
|
||||
local widget = widget or layout:getWidgetAt(x, y)
|
||||
local hit = true
|
||||
if not widget then
|
||||
hit = nil
|
||||
widget = layout.root
|
||||
end
|
||||
local originWidget = self.pressedWidgets[button]
|
||||
if not originWidget then return end
|
||||
originWidget.pressed = nil
|
||||
widget:bubbleEvent('PressEnd', {
|
||||
hit = hit,
|
||||
origin = originWidget,
|
||||
accelerator = accelerator,
|
||||
button = button, x = x, y = y
|
||||
})
|
||||
if (widget == originWidget) then
|
||||
widget:bubbleEvent('Press', {
|
||||
hit = hit,
|
||||
button = button,
|
||||
accelerator = accelerator,
|
||||
x = x, y = y
|
||||
@@ -147,13 +205,24 @@ function Input:handlePressEnd (button, x, y, widget, accelerator)
|
||||
end
|
||||
self.pressedWidgets[button] = nil
|
||||
self.passedWidgets[button] = nil
|
||||
return hit
|
||||
end
|
||||
|
||||
function Input:handleReshape (width, height)
|
||||
local root = self.layout.root
|
||||
function Input:handleReshape (layout, width, height)
|
||||
local root = layout.root
|
||||
|
||||
Event.Reshape:emit(layout, {
|
||||
target = layout
|
||||
})
|
||||
|
||||
if root.float then
|
||||
return
|
||||
end
|
||||
|
||||
root.width = width
|
||||
root.height = height
|
||||
end
|
||||
|
||||
Input.default = Input()
|
||||
|
||||
return Input
|
||||
|
||||
Reference in New Issue
Block a user