keyboard accelerators

This commit is contained in:
airstruck
2015-10-29 13:52:34 -04:00
parent 0d8a01b405
commit 1c491335f5
4 changed files with 51 additions and 20 deletions

View File

@@ -40,7 +40,7 @@ local style = {
local mainForm = { title = "Test window", id = 'mainWindow', type = 'panel',
{ 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' },
{ type = 'button', id = 'loadButton', style = 'toolButton',
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 = 'text', id = 'aTextField', text = 'a text field',
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 } },
}

View File

@@ -7,11 +7,7 @@ local Event = Base:extend({ name = 'Event' })
function Event:emit (target, data, defaultAction)
local callbacks = self.registry[target]
if not callbacks then
if defaultAction then defaultAction() end
return
end
local result = callbacks(data or {})
local result = callbacks and callbacks(data or {})
if result ~= nil then return result end
if defaultAction then defaultAction() end
end
@@ -22,7 +18,7 @@ function Event:bind (target, callback)
end
local eventNames = {
'Reshape', 'Display', 'Keyboard', 'TextInput', 'Motion',
'Reshape', 'Display', 'KeyPress', 'KeyRelease', 'TextInput', 'Motion',
'Enter', 'Leave', 'PressEnter', 'PressLeave',
'PressStart', 'PressEnd', 'PressDrag', 'PressMove', 'Press',
}

View File

@@ -29,12 +29,36 @@ function Input:handleDisplay ()
Event.Display:emit(self.layout)
end
function Input:handleKeyboard (key, x, y)
function Input:handleKeyPress (key, 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,
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
function Input:handleTextInput (text, x, y)
@@ -119,31 +143,35 @@ function Input:handlePressedMotion (x, y)
end
end
function Input:handlePressStart (button, x, y)
local widget = self.layout:getWidgetAt(x, y)
function Input:handlePressStart (button, x, y, widget, accelerator)
local widget = widget or self.layout:getWidgetAt(x, y)
widget.pressed = true
self.pressedWidgets[button] = widget
self.passedWidgets[button] = widget
self:bubbleEvent('PressStart', widget, {
target = widget,
button = button,
accelerator = accelerator,
x = x, y = y
})
end
function Input:handlePressEnd (button, x, y)
local widget = self.layout:getWidgetAt(x, y)
function Input:handlePressEnd (button, x, y, widget, accelerator)
local widget = widget or self.layout:getWidgetAt(x, y)
local originWidget = self.pressedWidgets[button]
originWidget.pressed = nil
self:bubbleEvent('PressEnd', widget, {
target = widget,
origin = originWidget,
accelerator = accelerator,
button = button, x = x, y = y
})
if (widget == originWidget) then
self:bubbleEvent('Press', widget, {
target = widget,
button = button, x = x, y = y
button = button,
accelerator = accelerator,
x = x, y = y
})
end
self.pressedWidgets[button] = nil
@@ -151,11 +179,11 @@ function Input:handlePressEnd (button, x, y)
end
function Input:handleReshape (width, height)
local layout = self.layout
local root = layout.root
layout.root:reflow()
local root = self.layout.root
root.width = width
root.height = height
root:reflow()
end
return Input

View File

@@ -13,13 +13,14 @@ local weakValueMeta = { __mode = 'v' }
function Layout:constructor (data)
self.widgets = setmetatable({}, weakValueMeta)
self.root = Widget(self, data or {})
self.accelerators = {}
self:setStyle()
self:setTheme()
self.isMousePressed = false
self.isManagingInput = false
self.hooks = {}
self.root = Widget(self, data or {})
end
function Layout:setStyle (rules)
@@ -85,6 +86,9 @@ function Layout:addWidget (widget)
if widget.id then
self[widget.id] = widget
end
if widget.key then
self.accelerators[widget.key] = widget
end
table.insert(self.widgets, widget)
end
@@ -143,7 +147,10 @@ function Layout:manageInput (input)
end
end)
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)
self:hook('textinput', function (text)
return input:handleTextInput(text, love.mouse.getX(), love.mouse.getY())