mirror of
https://github.com/airstruck/luigi.git
synced 2026-01-10 08:18:22 +00:00
keyboard accelerators
This commit is contained in:
@@ -40,7 +40,7 @@ local style = {
|
|||||||
|
|
||||||
local mainForm = { title = "Test window", id = 'mainWindow', type = 'panel',
|
local mainForm = { title = "Test window", id = 'mainWindow', type = 'panel',
|
||||||
{ type = 'panel', id = 'toolbar', flow = 'x',
|
{ type = 'panel', id = 'toolbar', flow = 'x',
|
||||||
{ type = 'button', id = 'newButton', style = 'toolButton',
|
{ type = 'button', id = 'newButton', style = 'toolButton', key = 'z',
|
||||||
icon = 'icon/emblem-default.png' },
|
icon = 'icon/emblem-default.png' },
|
||||||
{ type = 'button', id = 'loadButton', style = 'toolButton',
|
{ type = 'button', id = 'loadButton', style = 'toolButton',
|
||||||
icon = 'icon/emblem-documents.png' },
|
icon = 'icon/emblem-documents.png' },
|
||||||
@@ -77,7 +77,7 @@ local mainForm = { title = "Test window", id = 'mainWindow', type = 'panel',
|
|||||||
{ type = 'panel', flow = 'x', height = 48, padding = 2,
|
{ type = 'panel', flow = 'x', height = 48, padding = 2,
|
||||||
{ type = 'text', id = 'aTextField', text = 'a text field',
|
{ type = 'text', id = 'aTextField', text = 'a text field',
|
||||||
font = 'font/liberation/LiberationMono-Regular.ttf' },
|
font = 'font/liberation/LiberationMono-Regular.ttf' },
|
||||||
{ type = 'button', width = 80, id = 'aButton', text = 'Styling!' },
|
{ type = 'button', key='return', width = 80, id = 'aButton', text = 'Styling!' },
|
||||||
},
|
},
|
||||||
{ type = 'panel', id = 'statusbar', height = 24, padding = 4, textColor = { 255, 0, 0 } },
|
{ type = 'panel', id = 'statusbar', height = 24, padding = 4, textColor = { 255, 0, 0 } },
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,7 @@ local Event = Base:extend({ name = 'Event' })
|
|||||||
|
|
||||||
function Event:emit (target, data, defaultAction)
|
function Event:emit (target, data, defaultAction)
|
||||||
local callbacks = self.registry[target]
|
local callbacks = self.registry[target]
|
||||||
if not callbacks then
|
local result = callbacks and callbacks(data or {})
|
||||||
if defaultAction then defaultAction() end
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local result = callbacks(data or {})
|
|
||||||
if result ~= nil then return result end
|
if result ~= nil then return result end
|
||||||
if defaultAction then defaultAction() end
|
if defaultAction then defaultAction() end
|
||||||
end
|
end
|
||||||
@@ -22,7 +18,7 @@ function Event:bind (target, callback)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local eventNames = {
|
local eventNames = {
|
||||||
'Reshape', 'Display', 'Keyboard', 'TextInput', 'Motion',
|
'Reshape', 'Display', 'KeyPress', 'KeyRelease', 'TextInput', 'Motion',
|
||||||
'Enter', 'Leave', 'PressEnter', 'PressLeave',
|
'Enter', 'Leave', 'PressEnter', 'PressLeave',
|
||||||
'PressStart', 'PressEnd', 'PressDrag', 'PressMove', 'Press',
|
'PressStart', 'PressEnd', 'PressDrag', 'PressMove', 'Press',
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,12 +29,36 @@ function Input:handleDisplay ()
|
|||||||
Event.Display:emit(self.layout)
|
Event.Display:emit(self.layout)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Input:handleKeyboard (key, x, y)
|
function Input:handleKeyPress (key, x, y)
|
||||||
local widget = self.layout.focusedWidget or self.layout:getWidgetAt(x, y)
|
local widget = self.layout.focusedWidget or self.layout:getWidgetAt(x, y)
|
||||||
self:bubbleEvent('Keyboard', widget, {
|
local result = self:bubbleEvent('KeyPress', widget, {
|
||||||
target = widget,
|
target = widget,
|
||||||
key = key, x = x, y = y
|
key = key, x = x, y = y
|
||||||
})
|
})
|
||||||
|
if result ~= nil then return result end
|
||||||
|
|
||||||
|
local acceleratedWidget = self.layout.accelerators[key]
|
||||||
|
|
||||||
|
if acceleratedWidget then
|
||||||
|
acceleratedWidget.hovered = true
|
||||||
|
self:handlePressStart(key, x, y, acceleratedWidget, key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Input:handleKeyRelease (key, x, y)
|
||||||
|
local widget = self.layout.focusedWidget or self.layout:getWidgetAt(x, y)
|
||||||
|
local result = self:bubbleEvent('KeyRelease', widget, {
|
||||||
|
target = widget,
|
||||||
|
key = key, x = x, y = y
|
||||||
|
})
|
||||||
|
if result ~= nil then return result end
|
||||||
|
|
||||||
|
local acceleratedWidget = self.layout.accelerators[key]
|
||||||
|
|
||||||
|
if acceleratedWidget then
|
||||||
|
acceleratedWidget.hovered = false
|
||||||
|
self:handlePressEnd(key, x, y, acceleratedWidget, key)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Input:handleTextInput (text, x, y)
|
function Input:handleTextInput (text, x, y)
|
||||||
@@ -119,31 +143,35 @@ function Input:handlePressedMotion (x, y)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Input:handlePressStart (button, x, y)
|
function Input:handlePressStart (button, x, y, widget, accelerator)
|
||||||
local widget = self.layout:getWidgetAt(x, y)
|
local widget = widget or self.layout:getWidgetAt(x, y)
|
||||||
widget.pressed = true
|
widget.pressed = true
|
||||||
self.pressedWidgets[button] = widget
|
self.pressedWidgets[button] = widget
|
||||||
self.passedWidgets[button] = widget
|
self.passedWidgets[button] = widget
|
||||||
self:bubbleEvent('PressStart', widget, {
|
self:bubbleEvent('PressStart', widget, {
|
||||||
target = widget,
|
target = widget,
|
||||||
button = button,
|
button = button,
|
||||||
|
accelerator = accelerator,
|
||||||
x = x, y = y
|
x = x, y = y
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
function Input:handlePressEnd (button, x, y)
|
function Input:handlePressEnd (button, x, y, widget, accelerator)
|
||||||
local widget = self.layout:getWidgetAt(x, y)
|
local widget = widget or self.layout:getWidgetAt(x, y)
|
||||||
local originWidget = self.pressedWidgets[button]
|
local originWidget = self.pressedWidgets[button]
|
||||||
originWidget.pressed = nil
|
originWidget.pressed = nil
|
||||||
self:bubbleEvent('PressEnd', widget, {
|
self:bubbleEvent('PressEnd', widget, {
|
||||||
target = widget,
|
target = widget,
|
||||||
origin = originWidget,
|
origin = originWidget,
|
||||||
|
accelerator = accelerator,
|
||||||
button = button, x = x, y = y
|
button = button, x = x, y = y
|
||||||
})
|
})
|
||||||
if (widget == originWidget) then
|
if (widget == originWidget) then
|
||||||
self:bubbleEvent('Press', widget, {
|
self:bubbleEvent('Press', widget, {
|
||||||
target = widget,
|
target = widget,
|
||||||
button = button, x = x, y = y
|
button = button,
|
||||||
|
accelerator = accelerator,
|
||||||
|
x = x, y = y
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
self.pressedWidgets[button] = nil
|
self.pressedWidgets[button] = nil
|
||||||
@@ -151,11 +179,11 @@ function Input:handlePressEnd (button, x, y)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Input:handleReshape (width, height)
|
function Input:handleReshape (width, height)
|
||||||
local layout = self.layout
|
local root = self.layout.root
|
||||||
local root = layout.root
|
|
||||||
layout.root:reflow()
|
|
||||||
root.width = width
|
root.width = width
|
||||||
root.height = height
|
root.height = height
|
||||||
|
root:reflow()
|
||||||
end
|
end
|
||||||
|
|
||||||
return Input
|
return Input
|
||||||
|
|||||||
@@ -13,13 +13,14 @@ local weakValueMeta = { __mode = 'v' }
|
|||||||
|
|
||||||
function Layout:constructor (data)
|
function Layout:constructor (data)
|
||||||
self.widgets = setmetatable({}, weakValueMeta)
|
self.widgets = setmetatable({}, weakValueMeta)
|
||||||
self.root = Widget(self, data or {})
|
self.accelerators = {}
|
||||||
self:setStyle()
|
self:setStyle()
|
||||||
self:setTheme()
|
self:setTheme()
|
||||||
|
|
||||||
self.isMousePressed = false
|
self.isMousePressed = false
|
||||||
self.isManagingInput = false
|
self.isManagingInput = false
|
||||||
self.hooks = {}
|
self.hooks = {}
|
||||||
|
self.root = Widget(self, data or {})
|
||||||
end
|
end
|
||||||
|
|
||||||
function Layout:setStyle (rules)
|
function Layout:setStyle (rules)
|
||||||
@@ -85,6 +86,9 @@ function Layout:addWidget (widget)
|
|||||||
if widget.id then
|
if widget.id then
|
||||||
self[widget.id] = widget
|
self[widget.id] = widget
|
||||||
end
|
end
|
||||||
|
if widget.key then
|
||||||
|
self.accelerators[widget.key] = widget
|
||||||
|
end
|
||||||
table.insert(self.widgets, widget)
|
table.insert(self.widgets, widget)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -143,7 +147,10 @@ function Layout:manageInput (input)
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
self:hook('keypressed', function (key, isRepeat)
|
self:hook('keypressed', function (key, isRepeat)
|
||||||
return input:handleKeyboard(key, love.mouse.getX(), love.mouse.getY())
|
return input:handleKeyPress(key, love.mouse.getX(), love.mouse.getY())
|
||||||
|
end)
|
||||||
|
self:hook('keyreleased', function (key)
|
||||||
|
return input:handleKeyRelease(key, love.mouse.getX(), love.mouse.getY())
|
||||||
end)
|
end)
|
||||||
self:hook('textinput', function (text)
|
self:hook('textinput', function (text)
|
||||||
return input:handleTextInput(text, love.mouse.getX(), love.mouse.getY())
|
return input:handleTextInput(text, love.mouse.getX(), love.mouse.getY())
|
||||||
|
|||||||
Reference in New Issue
Block a user