From a81ac5b8197bbbbda0b64046f6cbf8adeab54152 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 11 Apr 2019 18:16:36 +0200 Subject: [PATCH] modules/menusystem: add an arbitrary order system to widgets & menus --- examples/mainmenu/init.lua | 5 +++-- gamecore/modules/menusystem/parent.lua | 13 +++++++++++++ gamecore/modules/menusystem/widgets/init.lua | 5 ++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/examples/mainmenu/init.lua b/examples/mainmenu/init.lua index 3210127..568c546 100644 --- a/examples/mainmenu/init.lua +++ b/examples/mainmenu/init.lua @@ -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() diff --git a/gamecore/modules/menusystem/parent.lua b/gamecore/modules/menusystem/parent.lua index 5cb33be..b3ae4bb 100644 --- a/gamecore/modules/menusystem/parent.lua +++ b/gamecore/modules/menusystem/parent.lua @@ -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) diff --git a/gamecore/modules/menusystem/widgets/init.lua b/gamecore/modules/menusystem/widgets/init.lua index 1aeb141..c5182a8 100644 --- a/gamecore/modules/menusystem/widgets/init.lua +++ b/gamecore/modules/menusystem/widgets/init.lua @@ -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()