mirror of
https://github.com/airstruck/luigi.git
synced 2026-01-10 08:18:22 +00:00
allow events bound to widget classes
This commit is contained in:
@@ -60,7 +60,7 @@ local mainForm = { title = "Test window", id = 'mainWindow', type = 'panel',
|
|||||||
{ type = 'sash', width = 4, },
|
{ type = 'sash', width = 4, },
|
||||||
{ type = 'panel', id = 'rightSideBox', width = 200,
|
{ type = 'panel', id = 'rightSideBox', width = 200,
|
||||||
{ type = 'panel', text = 'A slider', align = 'bottom', height = 24, padding = 4 },
|
{ type = 'panel', text = 'A slider', align = 'bottom', height = 24, padding = 4 },
|
||||||
{ type = 'slider', height = 32, padding = 4 },
|
{ type = 'slider', height = 32, padding = 4, id = 'slidey', },
|
||||||
{ type = 'panel', text = 'A stepper', align = 'bottom', height = 24, padding = 4 },
|
{ type = 'panel', text = 'A stepper', align = 'bottom', height = 24, padding = 4 },
|
||||||
{ type = 'stepper', height = 32, padding = 4, options = {
|
{ type = 'stepper', height = 32, padding = 4, options = {
|
||||||
{ value = 1, text = 'Thing One' },
|
{ value = 1, text = 'Thing One' },
|
||||||
@@ -89,6 +89,11 @@ layout.leftSideBox:addChild {
|
|||||||
align = 'middle right'
|
align = 'middle right'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
layout.slidey:onMotion(function (event)
|
||||||
|
layout.statusbar.text = event.x
|
||||||
|
return false
|
||||||
|
end)
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
local KEY_ESCAPE = 27
|
local KEY_ESCAPE = 27
|
||||||
|
|
||||||
|
|||||||
@@ -3,22 +3,26 @@ local ROOT = (...):gsub('[^.]*$', '')
|
|||||||
local Base = require(ROOT .. 'base')
|
local Base = require(ROOT .. 'base')
|
||||||
local Hooker = require(ROOT .. 'hooker')
|
local Hooker = require(ROOT .. 'hooker')
|
||||||
|
|
||||||
local Event = Base:extend({ name = 'Event' })
|
local Event = Base:extend { name = 'Event' }
|
||||||
|
|
||||||
function Event:emit (observer, data, defaultAction)
|
function Event:emit (target, data, defaultAction)
|
||||||
local callbacks = self.registry[observer]
|
while target do
|
||||||
if not callbacks then
|
local handlers = rawget(target, 'eventHandlers')
|
||||||
if defaultAction then defaultAction() end
|
local callbacks = handlers and handlers[self.name]
|
||||||
return
|
if callbacks then
|
||||||
|
local result = callbacks(data or {})
|
||||||
|
if result ~= nil then return result end
|
||||||
|
end
|
||||||
|
target = target.widgetClass
|
||||||
end
|
end
|
||||||
local result = callbacks(data or {})
|
|
||||||
if result ~= nil then return result end
|
|
||||||
if defaultAction then defaultAction() end
|
if defaultAction then defaultAction() end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Event:bind (observer, callback)
|
function Event:bind (target, callback)
|
||||||
local registry = self.registry
|
if not rawget(target, 'eventHandlers') then
|
||||||
return Hooker.hook(registry, observer, callback)
|
target.eventHandlers = {}
|
||||||
|
end
|
||||||
|
return Hooker.hook(target.eventHandlers, self.name, callback)
|
||||||
end
|
end
|
||||||
|
|
||||||
local eventNames = {
|
local eventNames = {
|
||||||
@@ -30,10 +34,7 @@ local eventNames = {
|
|||||||
local weakKeyMeta = { __mode = 'k' }
|
local weakKeyMeta = { __mode = 'k' }
|
||||||
|
|
||||||
for i, name in ipairs(eventNames) do
|
for i, name in ipairs(eventNames) do
|
||||||
Event[name] = Event:extend({
|
Event[name] = Event:extend { name = name }
|
||||||
name = name,
|
|
||||||
registry = setmetatable({}, weakKeyMeta),
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Event.injectBinders (t)
|
function Event.injectBinders (t)
|
||||||
|
|||||||
@@ -252,7 +252,7 @@ function Renderer:renderChildren (widget)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Renderer:render (widget)
|
function Renderer:render (widget)
|
||||||
Event.Display:emit(widget, {}, function()
|
Event.Display:emit(widget, { target = widget }, function()
|
||||||
self:renderBackground(widget)
|
self:renderBackground(widget)
|
||||||
self:renderOutline(widget)
|
self:renderOutline(widget)
|
||||||
self:renderSlices(widget)
|
self:renderSlices(widget)
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ function Widget.create (layout, data)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Widget:constructor (layout, data)
|
function Widget:constructor (layout, data)
|
||||||
|
local meta = getmetatable(self)
|
||||||
|
local metaIndex = meta.__index
|
||||||
|
self.widgetClass = metaIndex
|
||||||
self.type = 'generic'
|
self.type = 'generic'
|
||||||
self.layout = layout
|
self.layout = layout
|
||||||
self.children = {}
|
self.children = {}
|
||||||
@@ -33,15 +36,13 @@ function Widget:constructor (layout, data)
|
|||||||
self:extract(data)
|
self:extract(data)
|
||||||
layout:addWidget(self)
|
layout:addWidget(self)
|
||||||
local widget = self
|
local widget = self
|
||||||
local meta = getmetatable(self)
|
|
||||||
local metaIndex = meta.__index
|
|
||||||
function meta:__index(property)
|
function meta:__index(property)
|
||||||
local value = metaIndex[property]
|
local value = metaIndex[property]
|
||||||
local style = widget.layout.style
|
|
||||||
local theme = widget.layout.theme
|
|
||||||
if value ~= nil then return value end
|
if value ~= nil then return value end
|
||||||
|
local style = widget.layout.style
|
||||||
value = style and style:getProperty(self, property)
|
value = style and style:getProperty(self, property)
|
||||||
if value ~= nil then return value end
|
if value ~= nil then return value end
|
||||||
|
local theme = widget.layout.theme
|
||||||
return theme and theme:getProperty(self, property)
|
return theme and theme:getProperty(self, property)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,36 +2,32 @@ local Widget = require((...):gsub('%.[^.]*$', ''))
|
|||||||
|
|
||||||
local Sash = Widget:extend()
|
local Sash = Widget:extend()
|
||||||
|
|
||||||
function Sash:constructor (layout, data)
|
Sash:onPressDrag(function (event)
|
||||||
Widget.constructor(self, layout, data)
|
local self = event.target
|
||||||
|
local axis = self.parent.flow
|
||||||
|
if axis == 'x' then
|
||||||
|
dimension = 'width'
|
||||||
|
else
|
||||||
|
axis = 'y'
|
||||||
|
dimension = 'height'
|
||||||
|
end
|
||||||
|
local prevSibling = self:getPrevious()
|
||||||
|
local nextSibling = self:getNext()
|
||||||
|
local prevSize = prevSibling and prevSibling[dimension]
|
||||||
|
local nextSize = nextSibling and nextSibling[dimension]
|
||||||
|
if prevSize then
|
||||||
|
prevSibling:setDimension(dimension,
|
||||||
|
event[axis] - prevSibling:calculatePosition(axis))
|
||||||
|
end
|
||||||
|
if nextSize then
|
||||||
|
nextSibling:setDimension(dimension,
|
||||||
|
nextSibling:calculatePosition(axis) +
|
||||||
|
nextSibling[dimension] - event[axis])
|
||||||
|
end
|
||||||
|
|
||||||
self:onPressDrag(function (event)
|
prevSibling:reflow()
|
||||||
local axis = self.parent.flow
|
nextSibling:reflow()
|
||||||
if axis == 'x' then
|
self:reflow()
|
||||||
dimension = 'width'
|
end)
|
||||||
else
|
|
||||||
axis = 'y'
|
|
||||||
dimension = 'height'
|
|
||||||
end
|
|
||||||
local prevSibling = self:getPrevious()
|
|
||||||
local nextSibling = self:getNext()
|
|
||||||
local prevSize = prevSibling and prevSibling[dimension]
|
|
||||||
local nextSize = nextSibling and nextSibling[dimension]
|
|
||||||
if prevSize then
|
|
||||||
prevSibling:setDimension(dimension,
|
|
||||||
event[axis] - prevSibling:calculatePosition(axis))
|
|
||||||
end
|
|
||||||
if nextSize then
|
|
||||||
nextSibling:setDimension(dimension,
|
|
||||||
nextSibling:calculatePosition(axis) +
|
|
||||||
nextSibling[dimension] - event[axis])
|
|
||||||
end
|
|
||||||
|
|
||||||
prevSibling:reflow()
|
|
||||||
nextSibling:reflow()
|
|
||||||
self:reflow()
|
|
||||||
end)
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
return Sash
|
return Sash
|
||||||
|
|||||||
@@ -6,47 +6,48 @@ function Slider:constructor (layout, data)
|
|||||||
Widget.constructor(self, layout, data)
|
Widget.constructor(self, layout, data)
|
||||||
|
|
||||||
self.value = 0.5
|
self.value = 0.5
|
||||||
|
|
||||||
self:onPressDrag(function (event)
|
|
||||||
local x1, y1, x2, y2 = self:getRectangle(true, true)
|
|
||||||
self.value = (event.x - x1) / (x2 - x1)
|
|
||||||
if self.value < 0 then self.value = 0 end
|
|
||||||
if self.value > 1 then self.value = 1 end
|
|
||||||
end)
|
|
||||||
|
|
||||||
self:onDisplay(function (event)
|
|
||||||
local x1, y1, x2, y2 = self:getRectangle(true, true)
|
|
||||||
local padding = self.padding or 0
|
|
||||||
|
|
||||||
local sx1 = math.floor(x1 + self.value * (x2 - x1) - padding) + 0.5
|
|
||||||
local sy1 = math.floor(y1 + padding) + 0.5
|
|
||||||
local sx2 = padding * 2
|
|
||||||
local sy2 = y2 - y1 - padding
|
|
||||||
|
|
||||||
love.graphics.push('all')
|
|
||||||
|
|
||||||
love.graphics.setColor(self.outline)
|
|
||||||
|
|
||||||
love.graphics.rectangle('fill',
|
|
||||||
x1,
|
|
||||||
y1 + ((y2 - y1) / 2),
|
|
||||||
x2 - x1,
|
|
||||||
padding
|
|
||||||
)
|
|
||||||
|
|
||||||
love.graphics.setColor(self.background)
|
|
||||||
|
|
||||||
love.graphics.rectangle('fill', sx1, sy1, sx2, sy2)
|
|
||||||
|
|
||||||
love.graphics.setColor(self.outline)
|
|
||||||
|
|
||||||
love.graphics.rectangle('line', sx1, sy1, sx2, sy2)
|
|
||||||
|
|
||||||
love.graphics.pop()
|
|
||||||
|
|
||||||
return false
|
|
||||||
end)
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Slider:onPressDrag(function (event)
|
||||||
|
local self = event.target
|
||||||
|
local x1, y1, x2, y2 = self:getRectangle(true, true)
|
||||||
|
self.value = (event.x - x1) / (x2 - x1)
|
||||||
|
if self.value < 0 then self.value = 0 end
|
||||||
|
if self.value > 1 then self.value = 1 end
|
||||||
|
end)
|
||||||
|
|
||||||
|
Slider:onDisplay(function (event)
|
||||||
|
local self = event.target
|
||||||
|
local x1, y1, x2, y2 = self:getRectangle(true, true)
|
||||||
|
local padding = self.padding or 0
|
||||||
|
|
||||||
|
local sx1 = math.floor(x1 + self.value * (x2 - x1) - padding) + 0.5
|
||||||
|
local sy1 = math.floor(y1 + padding) + 0.5
|
||||||
|
local sx2 = padding * 2
|
||||||
|
local sy2 = y2 - y1 - padding
|
||||||
|
|
||||||
|
love.graphics.push('all')
|
||||||
|
|
||||||
|
love.graphics.setColor(self.outline)
|
||||||
|
|
||||||
|
love.graphics.rectangle('fill',
|
||||||
|
x1,
|
||||||
|
y1 + ((y2 - y1) / 2),
|
||||||
|
x2 - x1,
|
||||||
|
padding
|
||||||
|
)
|
||||||
|
|
||||||
|
love.graphics.setColor(self.background)
|
||||||
|
|
||||||
|
love.graphics.rectangle('fill', sx1, sy1, sx2, sy2)
|
||||||
|
|
||||||
|
love.graphics.setColor(self.outline)
|
||||||
|
|
||||||
|
love.graphics.rectangle('line', sx1, sy1, sx2, sy2)
|
||||||
|
|
||||||
|
love.graphics.pop()
|
||||||
|
|
||||||
|
return false
|
||||||
|
end)
|
||||||
|
|
||||||
return Slider
|
return Slider
|
||||||
|
|||||||
Reference in New Issue
Block a user