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
This commit is contained in:
Kazhnuz 2019-02-11 19:22:13 +01:00
parent 464a1309c2
commit 5920fc18dd
6 changed files with 19 additions and 26 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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