2022-09-12 09:34:46 +02:00
|
|
|
--- Layout container widget
|
|
|
|
--
|
|
|
|
-- Implements a layout widget
|
|
|
|
--
|
|
|
|
-- @classmod yui.Layout
|
|
|
|
-- @copyright 2022, The DoubleFourteen Code Forge
|
|
|
|
-- @author Lorenzo Cogotti, Andrea Pasquini
|
|
|
|
--
|
2022-10-25 13:26:30 +02:00
|
|
|
-- @{Layout} is an internal class, serving as a container of widgets and
|
|
|
|
-- defining their organization and display order.
|
2022-09-12 09:34:46 +02:00
|
|
|
-- It is useful for arrangement customization.
|
|
|
|
|
|
|
|
|
2022-08-24 11:18:48 +02:00
|
|
|
local BASE = (...):gsub('layout$', '')
|
2022-08-15 23:41:17 +02:00
|
|
|
|
2022-08-16 00:23:52 +02:00
|
|
|
local Widget = require(BASE..'widget')
|
2022-08-15 23:41:17 +02:00
|
|
|
|
2022-08-16 00:24:33 +02:00
|
|
|
local gear = require 'lib.gear'
|
2022-08-15 23:41:17 +02:00
|
|
|
|
|
|
|
local isinstance = gear.meta.isinstance
|
|
|
|
local rectunion = gear.rect.union
|
|
|
|
local pointinrect = gear.rect.pointinside
|
|
|
|
|
2022-08-24 11:18:48 +02:00
|
|
|
local Layout = setmetatable({
|
2022-10-25 13:26:30 +02:00
|
|
|
__call = function(cls, args) return cls:new(args) end
|
2022-08-24 11:18:48 +02:00
|
|
|
}, Widget)
|
2022-08-15 23:41:17 +02:00
|
|
|
Layout.__index = Layout
|
|
|
|
|
|
|
|
|
|
|
|
-- Calculate initial widget size.
|
|
|
|
local function calcsize(sizes, widget)
|
|
|
|
local w, h = widget.w, widget.h
|
|
|
|
if w == nil then
|
|
|
|
assert(#sizes > 0, "Default width is undefined!")
|
|
|
|
w = sizes[#sizes].w
|
|
|
|
end
|
|
|
|
if h == nil then
|
|
|
|
assert(#sizes > 0, "Default height is undefined!")
|
|
|
|
h = sizes[#sizes].h
|
|
|
|
end
|
|
|
|
|
|
|
|
if w == 'max' then
|
|
|
|
w = 0
|
|
|
|
for _,v in ipairs(sizes) do
|
|
|
|
if v.w > w then
|
|
|
|
w = v.w
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif w == 'median' then
|
|
|
|
w = 0
|
|
|
|
for _,v in ipairs(sizes) do
|
|
|
|
w = w + v.w
|
|
|
|
end
|
|
|
|
w = math.ceil(w / #sizes)
|
|
|
|
elseif w == 'min' then
|
|
|
|
w = math.huge
|
|
|
|
for _,v in ipairs(sizes) do
|
|
|
|
if v.w < w then
|
|
|
|
w = v.w
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
assert(type(w) == 'number')
|
|
|
|
end
|
|
|
|
|
|
|
|
if h == 'max' then
|
|
|
|
h = 0
|
|
|
|
for _,v in ipairs(sizes) do
|
|
|
|
if v.h > h then
|
|
|
|
h = v.h
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif h == 'median' then
|
|
|
|
h = 0
|
|
|
|
for _,v in ipairs(sizes) do
|
|
|
|
h = h + v.h
|
|
|
|
end
|
|
|
|
h = math.ceil(h / #sizes)
|
|
|
|
elseif h == 'min' then
|
|
|
|
h = math.huge
|
|
|
|
for _,v in ipairs(sizes) do
|
|
|
|
if v.h < h then
|
|
|
|
h = v.h
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
assert(type(h) == 'number')
|
|
|
|
end
|
|
|
|
|
|
|
|
sizes[#sizes+1] = { w = w, h = h }
|
|
|
|
|
|
|
|
widget.w, widget.h = w, h
|
|
|
|
return w, h
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Lay down container widgets according to Layout type.
|
|
|
|
function Layout:layoutWidgets()
|
|
|
|
local nx,ny = self.x,self.y
|
|
|
|
local sizes = {}
|
|
|
|
local stack = self.stack
|
|
|
|
local pad = self.padding
|
|
|
|
|
|
|
|
-- Container bounds, empty
|
|
|
|
local rx,ry,rw,rh = nx,ny,-1,-1
|
|
|
|
|
|
|
|
-- Layout container children
|
|
|
|
for _,widget in ipairs(self) do
|
|
|
|
widget.x, widget.y = nx, ny
|
|
|
|
widget.ui = self.ui
|
|
|
|
widget.parent = self
|
|
|
|
|
|
|
|
if isinstance(widget, Layout) then
|
|
|
|
widget:layoutWidgets()
|
|
|
|
end
|
|
|
|
|
|
|
|
local w,h = calcsize(sizes, widget)
|
|
|
|
rx,ry,rw,rh = rectunion(rx,ry,rw,rh, nx,ny,w,h)
|
|
|
|
|
|
|
|
nx,ny = self.advance(nx,ny, w,h, pad)
|
|
|
|
|
|
|
|
stack[#stack+1] = widget
|
|
|
|
end
|
|
|
|
|
|
|
|
self.x = rx
|
|
|
|
self.y = ry
|
|
|
|
self.w = math.max(rw, 0)
|
|
|
|
self.h = math.max(rh, 0)
|
2023-09-19 18:17:22 +02:00
|
|
|
|
|
|
|
-- A Layout ignores focus if empty or containing only nofocus widgets
|
|
|
|
self.nofocus = true
|
|
|
|
for _,w in ipairs(self) do
|
|
|
|
if not w.nofocus then
|
|
|
|
self.nofocus = false
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2022-08-15 23:41:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Find layout's child containing the provided widget.
|
|
|
|
local function childof(layout, widget)
|
|
|
|
local parent = widget.parent
|
2023-09-19 18:17:22 +02:00
|
|
|
while not rawequal(parent, layout) do
|
2022-08-15 23:41:17 +02:00
|
|
|
widget = parent
|
|
|
|
parent = widget.parent
|
|
|
|
end
|
|
|
|
return widget
|
|
|
|
end
|
2022-08-24 11:18:48 +02:00
|
|
|
local function scanforward(layout, from)
|
|
|
|
from = from or 1
|
|
|
|
|
|
|
|
for i = from,#layout do
|
|
|
|
local w = layout[i]
|
|
|
|
|
|
|
|
if not w.nofocus then
|
|
|
|
return isinstance(w, Layout) and scanforward(w) or w
|
2022-08-15 23:41:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-08-24 11:18:48 +02:00
|
|
|
local function scanbackwards(layout, from)
|
|
|
|
from = from or #layout
|
|
|
|
|
|
|
|
for i = from,1,-1 do
|
|
|
|
local w = layout[i]
|
|
|
|
|
|
|
|
if not w.nofocus then
|
|
|
|
return isinstance(w, Layout) and scanforward(w) or w
|
2022-08-15 23:41:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-08-24 11:18:48 +02:00
|
|
|
|
2022-09-12 09:34:46 +02:00
|
|
|
--- Attributes accepted by the Layout widget
|
|
|
|
--
|
|
|
|
-- @field padding (number) number of pixels between two elements
|
|
|
|
-- @table LayoutAttributes
|
|
|
|
|
|
|
|
|
|
|
|
--- Layout constructor
|
|
|
|
-- @param args (@{LayoutAttributes}) widget attributes
|
2022-10-25 13:26:30 +02:00
|
|
|
function Layout:new(args)
|
|
|
|
self = setmetatable(args, self)
|
2022-08-24 11:18:48 +02:00
|
|
|
|
|
|
|
self.padding = self.padding or 0
|
|
|
|
self.stack = {}
|
|
|
|
return self
|
2022-08-15 23:41:17 +02:00
|
|
|
end
|
2022-08-24 11:18:48 +02:00
|
|
|
|
2022-09-12 09:34:46 +02:00
|
|
|
--- Find the first widget in the layout accepting focus.
|
2022-08-24 11:18:48 +02:00
|
|
|
function Layout:first()
|
|
|
|
return scanforward(self)
|
2022-08-15 23:41:17 +02:00
|
|
|
end
|
2022-08-24 11:18:48 +02:00
|
|
|
|
2022-09-12 09:34:46 +02:00
|
|
|
--- Find the last widget in the layout accepting focus.
|
2022-08-24 11:18:48 +02:00
|
|
|
function Layout:last()
|
|
|
|
return scanbackwards(self)
|
2022-08-15 23:41:17 +02:00
|
|
|
end
|
|
|
|
|
2022-09-12 09:34:46 +02:00
|
|
|
--- Find the next focusable widget after the provided one.
|
2022-08-24 11:18:48 +02:00
|
|
|
function Layout:after(widget)
|
|
|
|
widget = childof(self, widget)
|
2022-08-15 23:41:17 +02:00
|
|
|
|
2022-08-24 11:18:48 +02:00
|
|
|
for i = 1,#self do
|
2023-09-19 18:17:22 +02:00
|
|
|
if rawequal(self[i], widget) then
|
2022-08-24 11:18:48 +02:00
|
|
|
-- Search to the right/down
|
|
|
|
return scanforward(self, i+1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-08-15 23:41:17 +02:00
|
|
|
|
2022-09-12 09:34:46 +02:00
|
|
|
--- Find the previous focusable widget before the provided one.
|
2022-08-24 11:18:48 +02:00
|
|
|
function Layout:before(widget)
|
|
|
|
widget = childof(self, widget)
|
|
|
|
|
|
|
|
for i = 1,#self do
|
2023-09-19 18:17:22 +02:00
|
|
|
if rawequal(self[i], widget) then
|
2022-08-24 11:18:48 +02:00
|
|
|
-- Search to the left/up
|
|
|
|
return scanbackwards(self, i-1)
|
|
|
|
end
|
2022-08-15 23:41:17 +02:00
|
|
|
end
|
2022-08-24 11:18:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Layout:onPointerInput(px,py, clicked, down)
|
|
|
|
local stack = self.stack
|
|
|
|
|
|
|
|
-- Propagate pointer event from topmost widget to bottom
|
|
|
|
for i = #stack,1,-1 do
|
|
|
|
local widget = stack[i]
|
|
|
|
local x,y,w,h = widget.x,widget.y,widget.w,widget.h
|
2022-08-15 23:41:17 +02:00
|
|
|
|
2022-08-24 11:18:48 +02:00
|
|
|
if pointinrect(px,py, x,y,w,h) then
|
|
|
|
widget:onPointerInput(px,py, clicked, down)
|
|
|
|
break
|
|
|
|
end
|
2022-08-15 23:41:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Layout:update(dt)
|
|
|
|
for _,widget in ipairs(self.stack) do
|
|
|
|
widget:update(dt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Layout:draw()
|
|
|
|
-- Draw all children according to their order (topmost last)
|
|
|
|
for _,widget in ipairs(self.stack) do
|
|
|
|
widget:draw()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Layout
|