From 5920fc18ddd50889a136947416972e143170f2b1 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Mon, 11 Feb 2019 19:22:13 +0100 Subject: [PATCH] core/menusystem: use name index instead of numeral index for menus It makes more sense and avoid having to keep and update id of menus --- .../core/modules/menusystem/flowbox.lua | 4 ++-- .../core/modules/menusystem/grid.lua | 4 ++-- .../core/modules/menusystem/init.lua | 24 +++++++------------ .../core/modules/menusystem/listbox.lua | 4 ++-- .../core/modules/menusystem/parent.lua | 5 ++-- .../core/modules/menusystem/textmenu.lua | 4 ++-- 6 files changed, 19 insertions(+), 26 deletions(-) diff --git a/sonic-boost.love/core/modules/menusystem/flowbox.lua b/sonic-boost.love/core/modules/menusystem/flowbox.lua index 7b032a0..c144c8c 100644 --- a/sonic-boost.love/core/modules/menusystem/flowbox.lua +++ b/sonic-boost.love/core/modules/menusystem/flowbox.lua @@ -3,8 +3,8 @@ local Menu = require(cwd .. "parent") FlowBox = Menu:extend() -function FlowBox:new(controller, x,y,w,h,slots_hor,slots_vert) - ListBox.super.new(self, controller, x, y, w, h) +function FlowBox:new(controller, name, x, y, w, h, slots_hor, slots_vert) + ListBox.super.new(self, controller, name, x, y, w, h) self.slots = slots_hor * slots_vert self.slots_hor = slots_hor self.slots_vert = slots_vert diff --git a/sonic-boost.love/core/modules/menusystem/grid.lua b/sonic-boost.love/core/modules/menusystem/grid.lua index cf5501a..f401184 100644 --- a/sonic-boost.love/core/modules/menusystem/grid.lua +++ b/sonic-boost.love/core/modules/menusystem/grid.lua @@ -4,8 +4,8 @@ local Menu = require(cwd .. "parent") GridBox = Menu:extend() -function GridBox:new(controller, x,y,w,h,slots_hor,slots_vert) - ListBox.super.new(self, controller, x, y, w, h) +function GridBox:new(controller, name x, y, w, h, slots_hor, slots_vert) + ListBox.super.new(self, controller, name, x, y, w, h) self.slots = slots_hor * slots_vert self.slots_hor = slots_hor self.slots_vert = slots_vert diff --git a/sonic-boost.love/core/modules/menusystem/init.lua b/sonic-boost.love/core/modules/menusystem/init.lua index 68b0dec..435b0cd 100644 --- a/sonic-boost.love/core/modules/menusystem/init.lua +++ b/sonic-boost.love/core/modules/menusystem/init.lua @@ -21,14 +21,13 @@ function MenuSystem:reset() self.menus = {} end -function MenuSystem:addMenu(menu) - table.insert(self.menus, menu) +function MenuSystem:addMenu(name, menu) + self.menus[name] = menu end function MenuSystem:update(dt) self:removeDestroyedMenus() - for i,v in ipairs(self.menus) do - v.id = i + for l,v in pairs(self.menus) do v:update(dt) v:updateWidgets(dt) if v.haveFocus == true then @@ -43,25 +42,19 @@ end function MenuSystem:removeDestroyedMenus() -- On retire les entitées marquées comme supprimées - for i,v in ipairs(self.menus) do + for k,v in pairs(self.menus) do if (v.isDestroyed == true) then - table.remove(self.menus, i) + self.menus[k] = nil end end end -function MenuSystem:updateList() - for i,v in ipairs(self.menus) do - v.id = i - end -end - function MenuSystem:keyreleased(key, code) -- TODO:depreciated function end function MenuSystem:mousemoved(x, y, dx, dy) - for i,v in ipairs(self.menus) do + for k,v in pairs(self.menus) do if v.isActive then if (x > v.x) and (x < v.x + v.w) and (y > v.y) and (y < v.y + v.h) then v:mousemoved(x - v.x, y - v.y) @@ -75,7 +68,7 @@ function MenuSystem:mousemoved(x, y, dx, dy) end function MenuSystem:mousepressed( x, y, button, istouch ) - for i,v in ipairs(self.menus) do + for k,v in pairs(self.menus) do if (x > v.x) and (x < v.x + v.w) and (y > v.y) and (y < v.y + v.h) then v:mousepressed(x - v.x, y - v.y, button, istouch ) for j,u in ipairs(self.menus) do @@ -87,8 +80,7 @@ function MenuSystem:mousepressed( x, y, button, istouch ) end function MenuSystem:draw(dt) -- On dessine les entitées - for i,v in ipairs(self.menus) do - v.id = i + for k,v in pairs(self.menus) do if (v.isVisible) then v:draw(dt) end diff --git a/sonic-boost.love/core/modules/menusystem/listbox.lua b/sonic-boost.love/core/modules/menusystem/listbox.lua index 533941d..53b812f 100644 --- a/sonic-boost.love/core/modules/menusystem/listbox.lua +++ b/sonic-boost.love/core/modules/menusystem/listbox.lua @@ -3,8 +3,8 @@ local Menu = require(cwd .. "parent") ListBox = Menu:extend() -function ListBox:new(controller, x,y,w,h,slots) - ListBox.super.new(self, controller, x, y, w, h) +function ListBox:new(controller, name, x, y, w, h, slots) + ListBox.super.new(self, controller, name, x, y, w, h) self.slots = slots self.begin = 1 self.h = slots * self.widget.h -- On fait en sorte que la hauteur diff --git a/sonic-boost.love/core/modules/menusystem/parent.lua b/sonic-boost.love/core/modules/menusystem/parent.lua index 7476716..8ecac08 100644 --- a/sonic-boost.love/core/modules/menusystem/parent.lua +++ b/sonic-boost.love/core/modules/menusystem/parent.lua @@ -1,7 +1,8 @@ local Menu = Object:extend() -function Menu:new(controller, x,y,w,h) +function Menu:new(controller, name, x, y, w, h) self.controller = controller + self.name = name self.x = x self.y = y @@ -32,7 +33,7 @@ function Menu:new(controller, x,y,w,h) end function Menu:register() - self.controller:addMenu(self) + self.controller:addMenu(self.name, self) end function Menu:setCancelWidget(id) diff --git a/sonic-boost.love/core/modules/menusystem/textmenu.lua b/sonic-boost.love/core/modules/menusystem/textmenu.lua index e4ae7e8..f7a6843 100644 --- a/sonic-boost.love/core/modules/menusystem/textmenu.lua +++ b/sonic-boost.love/core/modules/menusystem/textmenu.lua @@ -3,8 +3,8 @@ local Menu = require(cwd .. "parent") local TextMenu = Menu:extend() -function TextMenu:new(controller, x, y, font, slots) - TextMenu.super.new(self, controller, x, y, 0, 0) +function TextMenu:new(controller, name, x, y, font, slots) + TextMenu.super.new(self, controller, name, x, y, 0, 0) self.ox = x self.oy = y self.font = font