rework menus

This commit is contained in:
airstruck
2015-11-10 17:01:17 -05:00
parent 623b5ad776
commit bdc130aab9
5 changed files with 95 additions and 86 deletions

View File

@@ -34,8 +34,8 @@ local style = {
local mainForm = { id = 'mainWindow', type = 'panel', local mainForm = { id = 'mainWindow', type = 'panel',
{ type = 'menu', id = 'menubar', flow = 'x', { type = 'menu', id = 'menubar', flow = 'x',
{ text = 'File', id = 'menuFile', { text = 'File',
{ text = 'Save', id = 'menuFileSave', key = 'ctrl-s' }, { text = 'Save', id = 'menuSave', key = 'ctrl-s' },
{ text = 'Quit', id = 'menuQuit', key = 'escape' }, { text = 'Quit', id = 'menuQuit', key = 'escape' },
}, },
{ text = 'Edit', { text = 'Edit',
@@ -54,9 +54,9 @@ local mainForm = { id = 'mainWindow', type = 'panel',
}, },
}, },
{ text = 'Help', { text = 'Help',
{ text = 'About Luigi', icon = 'icon/16px/Book.png', key = 'backspace', }, { text = 'About Luigi', icon = 'icon/16px/Book.png', key = 'f1', },
{ text = 'About Luigi Demo', icon = 'icon/16px/Book Red.png' }, { text = 'About Luigi Demo', icon = 'icon/16px/Book Red.png', key = 'f2' },
{ text = 'Licenses' }, { text = 'Licenses', key = 'f3' },
}, },
}, },
{ type = 'panel', id = 'toolbar', flow = 'x', { type = 'panel', id = 'toolbar', flow = 'x',
@@ -161,12 +161,8 @@ erge rg eg erg er ergs erg er ge rh erh rth]]
layout.mainCanvas.align = 'top' layout.mainCanvas.align = 'top'
print(layout.menuQuit) layout.menuQuit:onPress(function (event) love.event.quit() end)
--[[
Layout.menuQuit:onPress(function (event)
love.event.quit()
end)
]]
layout:show() layout:show()

View File

@@ -87,14 +87,13 @@ function Layout:show ()
self:unhook() -- return self:unhook() -- return
self.isShown = nil self.isShown = nil
end end
local root = self.root
if not self.input then if not self.input then
self.input = Input.default -- Input(self) self.input = Input.default -- Input(self)
end end
self:manageInput() self:manageInput()
root:reshape() self.root:reshape()
end end
--[[-- --[[--
@@ -162,16 +161,18 @@ Widget to search within, defaults to layout root.
--]]-- --]]--
function Layout:getWidgetAt (x, y, root) function Layout:getWidgetAt (x, y, root)
local widget = root or self.root local widget = root or self.root
local childCount = #widget
-- Loop through in reverse, because siblings defined later in the tree -- Loop through in reverse, because siblings defined later in the tree
-- will overdraw earlier siblings. -- will overdraw earlier siblings.
local childCount = #widget
for i = childCount, 1, -1 do for i = childCount, 1, -1 do
local child = widget[i] local child = widget[i]
local inner = self:getWidgetAt(x, y, child) local inner = self:getWidgetAt(x, y, child)
if inner then return inner end if inner then return inner end
end end
if widget:isAt(x, y) then return widget end if widget:isAt(x, y) then return widget end
-- if widget == self.root then return widget end
end end
-- Internal, called from Widget:new -- Internal, called from Widget:new

View File

@@ -248,7 +248,9 @@ function Renderer:renderIconAndText (widget)
end end
function Renderer:renderChildren (widget) function Renderer:renderChildren (widget)
for i, child in ipairs(widget) do self:render(child) end for i, child in ipairs(widget) do
self:render(child)
end
end end
function Renderer:render (widget) function Renderer:render (widget)

View File

@@ -291,9 +291,9 @@ The newly added child widget.
--]]-- --]]--
function Widget:addChild (data) function Widget:addChild (data)
local layout = self.layout local layout = self.layout
local child = Widget(layout, data or {}) local child = data and data.isWidget and data or Widget(layout, data or {})
table.insert(self, child) self[#self + 1] = child
child.parent = self child.parent = self
child.layout = self.layout child.layout = self.layout

View File

@@ -1,6 +1,6 @@
local ROOT = (...):gsub('[^.]*.[^.]*.[^.]*$', '') local ROOT = (...):gsub('[^.]*.[^.]*.[^.]*$', '')
local Layout local Layout, Event
local show local show
@@ -53,54 +53,8 @@ local function activate (event, ignoreIfNoneOpen)
end end
end end
show = function (self) local function registerLayoutEvents (self)
if not self.items or #self.items < 1 then local menuLayout = self.menuLayout
return
end
if self.menuLayout then
self.menuLayout:show()
return
end
local Layout = Layout or require(ROOT .. 'layout')
local isSubmenu = self.parentMenu and self.parentMenu.parentMenu
local x = isSubmenu and self:getWidth() or 0
local y = isSubmenu and 0 or self:getHeight()
local menuLayout = Layout {
type = 'submenu',
left = self:getX() + x,
top = self:getY() + y,
width = 0,
height = 0,
}
local root = menuLayout.root
local rootPad = root.padding or 0
local textWidth = 0
local keyWidth = 0
for index, child in ipairs(self.items) do
child.type = child.type or 'menu.item'
root:addChild(child)
local h = child:getHeight()
root.height = root:getHeight() + h
if child.type == 'menu.item' then
local pad = child.padding or 0
local tw = child.fontData:getAdvance(child[2].text)
+ pad * 2 + h
local kw = child.fontData:getAdvance(child[3].text)
+ pad * 4
textWidth = math.max(textWidth, tw)
keyWidth = math.max(keyWidth, kw)
end
end
root.width = textWidth + keyWidth + rootPad
menuLayout:onReshape(function (event) menuLayout:onReshape(function (event)
menuLayout:hide() menuLayout:hide()
@@ -119,34 +73,65 @@ show = function (self)
end) end)
menuLayout:onEnter(activate) menuLayout:onEnter(activate)
menuLayout:onPressEnter(activate) menuLayout:onPressEnter(activate)
menuLayout:show()
self.menuLayout = menuLayout
end end
local function extractChild (self, index, child) local function addLayoutChildren (self)
self[index] = nil local root = self.menuLayout.root
self.items[#self.items + 1] = child local textWidth = 0
child.parentMenu = self local keyWidth = 0
child.rootMenu = self.rootMenu local height = 0
child.type = child.type or 'menu.item'
while #root > 0 do rawset(root, #root, nil) end
root.height = 0
root.width = 0
for index, child in ipairs(self.items) do
child.type = child.type or 'menu.item'
root:addChild(child)
local childHeight = child:getHeight()
height = height + childHeight
if child.type == 'menu.item' then
local pad = child.padding or 0
local tw = child.fontData:getAdvance(child[2].text)
+ pad * 2 + childHeight
local kw = child.fontData:getAdvance(child[3].text)
+ pad * 2 + childHeight
textWidth = math.max(textWidth, tw)
keyWidth = math.max(keyWidth, kw)
end
end
local isSubmenu = self.parentMenu and self.parentMenu.parentMenu
local x = isSubmenu and self:getWidth() or 0
local y = isSubmenu and 0 or self:getHeight()
root.left = self:getX() + x
root.top = self:getY() + y
root.height = height
root.width = textWidth + keyWidth + (root.padding or 0)
end end
return function (self) local function createLayout (self)
Layout = Layout or require(ROOT .. 'layout')
self.menuLayout = Layout { type = 'submenu' }
end
show = function (self)
if not self.items or #self.items < 1 then return end
addLayoutChildren(self)
self.menuLayout:show()
end
local function initialize (self)
local pad = self.padding or 0 local pad = self.padding or 0
local isSubmenu = self.parentMenu and self.parentMenu.parentMenu local isSubmenu = self.parentMenu and self.parentMenu.parentMenu
local text, key, icon = self.text or '', self.key or '', self.icon local text, key, icon = self.text or '', self.key or '', self.icon
local textWidth = self.fontData:getAdvance(text) + pad * 2 local textWidth = self.fontData:getAdvance(text) + pad * 2
self.items = self.items or {}
for index, child in ipairs(self) do
extractChild(self, index, child)
end
if isSubmenu then if isSubmenu then
local tc = self.textColor or { 0, 0, 0 } local tc = self.textColor or { 0, 0, 0 }
local keyColor = { tc[1], tc[2], tc[3], 0x90 } local keyColor = { tc[1], tc[2], tc[3], 0x90 }
@@ -170,7 +155,20 @@ return function (self)
else else
self.width = textWidth self.width = textWidth
end end
end
local function extractChildren (self)
self.items = {}
for index, child in ipairs(self) do
self[index] = nil
self.items[#self.items + 1] = child
child.parentMenu = self
child.rootMenu = self.rootMenu
child.type = child.type or 'menu.item'
end
end
local function registerEvents (self)
self:onPressStart(activate) self:onPressStart(activate)
self:onEnter(function (event) self:onEnter(function (event)
@@ -180,5 +178,17 @@ return function (self)
self:onPressEnter(function (event) self:onPressEnter(function (event)
activate(event, true) activate(event, true)
end) end)
end
return function (self)
extractChildren(self)
initialize(self)
registerEvents(self)
self.rootMenu.layout:addWidget(self)
if not self.items or #self.items < 1 then return end
createLayout(self)
registerLayoutEvents(self)
addLayoutChildren(self)
end end