chore: refactor widget list in a menumodel class

This commit is contained in:
Kazhnuz 2020-11-28 18:32:49 +01:00
parent d9b8b71672
commit b1cd9a6683
2 changed files with 170 additions and 63 deletions

View file

@ -24,14 +24,7 @@
local GuiElement = require "birb.modules.menusystem.parent"
local Menu = GuiElement: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
local MenuModel = require "birb.modules.menusystem.menus.utils.menumodel"
-- INIT FUNCTIONS
-- Initialize and configure functions.
@ -40,11 +33,7 @@ function Menu:new(menusystem, name, x, y, w, h)
Menu.super.new(self, name, x, y, w, h)
self.menusystem = self:getGui()
self.widget = {}
self.widget.list = {}
self.widget.selected = 0
self.widget.selectedPrevious = 0
self.widget.cancel = 0
self.widget = MenuModel()
self:updateWidgetSize()
self:resetSound()
@ -54,14 +43,11 @@ end
-- Send actions to the widgets
function Menu:cancelAction()
if (self.widget.cancel ~= 0) then
self.widget.list[self.widget.cancel]:action("key")
end
self.widget:cancelAction()
end
function Menu:clear()
self.widget.list = {}
self.widget.cancel = 0
self.widget:clear()
end
function Menu:resize(x,y,w,h)
@ -74,7 +60,7 @@ end
function Menu:updateElement(dt)
self:update(dt)
self:updateWidgets(dt)
self.widget:update(dt)
end
-- DRAW FUNCTIONS
@ -99,42 +85,11 @@ end
-- Handle widgets of the functions
function Menu:addWidget(newwidget)
if #self.widget.list == 0 then
self.widget.selected = 1
end
table.insert(self.widget.list, newwidget)
self:updateWidgetsID()
self:updateWidgetsOrder()
end
function Menu:updateWidgets(dt)
self:removeDestroyedWidgets()
for i,v in ipairs(self.widget.list) do
v.id = i
v:update(dt)
end
end
function Menu:updateWidgetsOrder()
table.sort(self.widget.list, updateWidgetByOrder)
end
function Menu:updateWidgetsID()
for i,v in ipairs(self.widget.list) do
v.id = i
end
end
function Menu:removeDestroyedWidgets() -- On retire les widgets marquées comme supprimées
for i,v in ipairs(self.widget.list) do
if (v.destroyed == true) then
table.remove(self.widget.list, i)
end
end
self.widget:addWidget(newwidget)
end
function Menu:setCancelWidget(id)
self.widget.cancel = #self.widget.list
self.widget:setCancelWidget(id)
end
function Menu:updateWidgetSize()
@ -147,27 +102,19 @@ function Menu:getWidgetSize(id)
end
function Menu:getWidgetNumber()
return #self.widget.list
return self.widget:lenght()
end
-- CURSOR FUNCTIONS
-- Set or move the cursor of the menu
function Menu:setCursor(cursorid)
self.widget.selected = cursorid --math.max(1, math.min(cursorid, #self.widget.list))
self.widget:setCursor(cursorid)
end
function Menu:moveCursor(new_selected)
self:playNavigationSound()
if new_selected < 1 then
self.widget.selected = #self.widget.list + new_selected
else
if new_selected > #self.widget.list then
self.widget.selected = new_selected - #self.widget.list
else
self.widget.selected = new_selected
end
end
self.widget:moveCursorAbsolute(new_selected)
end
-- SOUND FUNCTION

View file

@ -0,0 +1,160 @@
local MenuModel = 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 basic functions.
function MenuModel:new()
self.list = {}
self.selected = 0
self.selectedPrevious = 0
self.cancel = 0
-- self:updateWidgetSize()
end
function MenuModel:clear()
self.list = {}
self.cancel = 0
end
-- UPDATE/DRAW FUNCTIONS
-- All the update functions
function MenuModel:update(dt)
self:removeDestroyedWidgets()
for id, widget in ipairs(self.list) do
widget.id = id
widget:update(dt)
end
end
function MenuModel:updateWidgetsOrder()
table.sort(self.list, updateWidgetByOrder)
end
function MenuModel:updateWidgetsID()
for id, widget in ipairs(self.list) do
widget.id = id
end
end
-- ACTION FUNCTIONS
-- All the actions callback used by the widgets
function MenuModel:cancelAction()
if (self.cancel ~= 0) then
self:action(self.cancel, "key")
end
end
function MenuModel:action(id, type)
if (self:widgetExist(id)) then
self.list[id]:action(type)
end
end
function MenuModel:selectedAction()
if (self.selected ~= 0) then
self:action(self.selected, "key")
end
end
-- WIDGET FUNCTIONS
-- All the functions to handle widgets
function MenuModel:addWidget(newWidget)
if #self.list == 0 then
self.selected = 1
end
table.insert(self.list, newWidget)
self:updateWidgetsID()
self:updateWidgetsOrder()
end
function MenuModel:getList(first, lenght)
local listWidget = {}
local first = first or 1
local lenght = lenght or #self.list
lenght = math.min(lenght, (#self.list + 1 - first))
for i = first, lenght do
table.insert(listWidget, self.list[i])
end
return listWidget
end
function MenuModel:removeDestroyedWidgets() -- On retire les widgets marquées comme supprimées
for i, v in ipairs(self.list) do
if (v.destroyed == true) then
table.remove(self.list, i)
end
end
end
function MenuModel:lenght()
return #self.list
end
function MenuModel:widgetExist(id)
local id = id or 0
return (id >= 1 and id <= #self.list)
end
-- CANCEL FUNCTIONS
-- Add a widget as a "cancel" function
function MenuModel:setCancelWidget(id)
self.cancel = #self.list
end
function MenuModel:getCancelWidget(id)
return self.cancel
end
-- CURSOR FUNCTIONS
-- Set or move the cursor of the menu
function MenuModel:haveCursor()
return self:widgetExist(self.selected)
end
function MenuModel:trySelectWidget(cursorid)
if (self:widgetExist(cursorid)) then
self:setCursor(cursorid)
return true
else
return false
end
end
function MenuModel:setCursor(cursorid)
self.selected = cursorid --math.max(1, math.min(cursorid, #self.list))
end
function MenuModel:moveCursorAbsolute(newSelected)
-- self:playNavigationSound()
if newSelected < 1 then
self.selected = #self.list + newSelected
else
if newSelected > #self.list then
self.selected = newSelected - #self.list
else
self.selected = newSelected
end
end
end
function MenuModel:moveCursor(relative)
self:moveCursorAbsolute(self.selected + relative)
end
return MenuModel