mirror of
https://github.com/airstruck/luigi.git
synced 2025-12-19 02:16:43 +00:00
merge window into layout
This commit is contained in:
@@ -2,10 +2,10 @@ local ROOT = (...):gsub('[^.]*$', '')
|
||||
|
||||
local Base = require(ROOT .. 'base')
|
||||
local Event = require(ROOT .. 'event')
|
||||
local Window = require(ROOT .. 'window')
|
||||
local Widget = require(ROOT .. 'widget')
|
||||
local Input = require(ROOT .. 'input')
|
||||
local Style = require(ROOT .. 'style')
|
||||
local Hooker = require(ROOT .. 'hooker')
|
||||
|
||||
local Layout = Base:extend()
|
||||
|
||||
@@ -16,6 +16,10 @@ function Layout:constructor (data)
|
||||
self.root = Widget.create(self, data or {})
|
||||
self:setStyle()
|
||||
self:setTheme()
|
||||
|
||||
self.isMousePressed = false
|
||||
self.isManagingInput = false
|
||||
self.hooks = {}
|
||||
end
|
||||
|
||||
function Layout:setStyle (rules)
|
||||
@@ -40,20 +44,29 @@ function Layout:show ()
|
||||
if not self.input then
|
||||
self.input = Input(self)
|
||||
end
|
||||
if not self.window then
|
||||
self.window = Window(self.input)
|
||||
|
||||
local currentWidth, currentHeight, flags = love.window.getMode()
|
||||
love.window.setMode(width or currentWidth, height or currentHeight, flags)
|
||||
if title then
|
||||
love.window.setTitle(title)
|
||||
end
|
||||
self.window:show(width, height, title)
|
||||
self:manageInput(self.input)
|
||||
end
|
||||
|
||||
function Layout:hide ()
|
||||
self.window:hide()
|
||||
if not self.isManagingInput then
|
||||
return
|
||||
end
|
||||
self.isManagingInput = false
|
||||
self:unhook()
|
||||
end
|
||||
|
||||
-- Update the display. Call this after you change widget properties
|
||||
-- that affect display.
|
||||
function Layout:update (reshape)
|
||||
self.window:update(reshape)
|
||||
-- Reflow the layout. Call this after you change widget positions/dimensions.
|
||||
function Layout:reflow (reshape)
|
||||
for i, widget in ipairs(self.widgets) do
|
||||
widget.position = {}
|
||||
widget.dimensions = {}
|
||||
end
|
||||
end
|
||||
|
||||
-- Get the innermost widget at a position, within a root widget.
|
||||
@@ -82,6 +95,51 @@ function Layout:addWidget (widget)
|
||||
table.insert(self.widgets, widget)
|
||||
end
|
||||
|
||||
-- event stuff
|
||||
|
||||
function Layout:hook (key, method)
|
||||
self.hooks[#self.hooks + 1] = Hooker.hook(key, method)
|
||||
end
|
||||
|
||||
function Layout:unhook ()
|
||||
for _, item in ipairs(self.hooks) do
|
||||
Hooker.unhook(item)
|
||||
end
|
||||
self.hooks = {}
|
||||
end
|
||||
|
||||
function Layout:manageInput (input)
|
||||
if self.isManagingInput then
|
||||
return
|
||||
end
|
||||
self.isManagingInput = true
|
||||
|
||||
self:hook('draw', function ()
|
||||
input:handleDisplay()
|
||||
end)
|
||||
self:hook('resize', function (width, height)
|
||||
return input:handleReshape(width, height)
|
||||
end)
|
||||
self:hook('mousepressed', function (x, y, button)
|
||||
self.isMousePressed = true
|
||||
return input:handlePressStart(button, x, y)
|
||||
end)
|
||||
self:hook('mousereleased', function (x, y, button)
|
||||
self.isMousePressed = false
|
||||
return input:handlePressEnd(button, x, y)
|
||||
end)
|
||||
self:hook('mousemoved', function (x, y, dx, dy)
|
||||
if self.isMousePressed then
|
||||
return input:handlePressedMotion(x, y)
|
||||
else
|
||||
return input:handleMotion(x, y)
|
||||
end
|
||||
end)
|
||||
self:hook('keypressed', function (key, isRepeat)
|
||||
return input:handleKeyboard(key, love.mouse.getX(), love.mouse.getY())
|
||||
end)
|
||||
end
|
||||
|
||||
-- event binders
|
||||
|
||||
Event.injectBinders(Layout)
|
||||
|
||||
Reference in New Issue
Block a user