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

View file

@ -23,6 +23,14 @@
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
-- Initialize and configure functions.
@ -143,6 +151,10 @@ function Menu:destroy()
self.destroyed = true
end
function Menu:updateWidgetsOrder()
table.sort(self.widget.list, updateWidgetByOrder)
end
-- UPDATE FUNCTIONS
-- Update the menu every game update
@ -192,6 +204,7 @@ function Menu:addWidget(newwidget)
end
table.insert(self.widget.list, newwidget)
self:updateWidgetsID()
self:updateWidgetsOrder()
end
function Menu:updateWidgets(dt)

View file

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