modules/menusystem: add an arbitrary order system to widgets & menus

This commit is contained in:
Kazhnuz 2019-04-11 18:16:36 +02:00
parent 9367bee577
commit a81ac5b819
3 changed files with 18 additions and 5 deletions

View file

@ -62,7 +62,7 @@ end
function MainMenu:addSubMenu(name, fullname) function MainMenu:addSubMenu(name, fullname)
self:addMenu(name) self:addMenu(name)
SubMenuWidget(self, "main", name, fullname) SubMenuWidget(self, "main", name, fullname)
SubMenuWidget(self, name, "main", "< Back") SubMenuWidget(self, name, "main", "< Back", 1)
end end
function MainMenu:addScene(submenu, scene, fullname) function MainMenu:addScene(submenu, scene, fullname)
@ -79,12 +79,13 @@ end
-- Submenu widget :: go to a submenu -- Submenu widget :: go to a submenu
function SubMenuWidget:new(scene, menu, newmenu, fullname) function SubMenuWidget:new(scene, menu, newmenu, fullname, order)
self.scene = scene self.scene = scene
local widgetmenu = self.scene.menusystem.menus[menu] local widgetmenu = self.scene.menusystem.menus[menu]
local font = self.scene.assets.fonts["medium"] local font = self.scene.assets.fonts["medium"]
self.newmenu = newmenu self.newmenu = newmenu
SubMenuWidget.super.new(self, widgetmenu, font, fullname) SubMenuWidget.super.new(self, widgetmenu, font, fullname)
self.order = order or 0
end end
function SubMenuWidget:action() function SubMenuWidget:action()

View file

@ -23,6 +23,14 @@
local Menu = Object:extend() local Menu = Object:extend()
local function updateWidgetByOrder(a, b)
if a.order ~= b.order then
return a.order < b.order
else
return a.creationID < b.creationID
end
end
-- INIT FUNCTIONS -- INIT FUNCTIONS
-- Initialize and configure functions. -- Initialize and configure functions.
@ -143,6 +151,10 @@ function Menu:destroy()
self.destroyed = true self.destroyed = true
end end
function Menu:updateWidgetsOrder()
table.sort(self.widget.list, updateWidgetByOrder)
end
-- UPDATE FUNCTIONS -- UPDATE FUNCTIONS
-- Update the menu every game update -- Update the menu every game update
@ -192,6 +204,7 @@ function Menu:addWidget(newwidget)
end end
table.insert(self.widget.list, newwidget) table.insert(self.widget.list, newwidget)
self:updateWidgetsID() self:updateWidgetsID()
self:updateWidgetsOrder()
end end
function Menu:updateWidgets(dt) function Menu:updateWidgets(dt)

View file

@ -37,18 +37,17 @@ function BaseWidget:new(menu)
self.selection_margin = 0 self.selection_margin = 0
self.margin = 2 self.margin = 2
self:register()
self.canvas = {} self.canvas = {}
self.canvas.texture = nil self.canvas.texture = nil
self.canvas.needRedraw = true self.canvas.needRedraw = true
self.order = 0 self.order = 0
self:register()
end end
function BaseWidget:register() function BaseWidget:register()
self.menu:addWidget(self)
self.creationID = self.menu:getWidgetNumber() self.creationID = self.menu:getWidgetNumber()
self.menu:addWidget(self)
end end
function BaseWidget:redrawCanvas() function BaseWidget:redrawCanvas()