173 lines
3.6 KiB
Lua
173 lines
3.6 KiB
Lua
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.limit = -1
|
|
-- self:updateWidgetSize()
|
|
end
|
|
|
|
function MenuModel:clear()
|
|
self.list = {}
|
|
self.cancel = 0
|
|
end
|
|
|
|
function MenuModel:setLimit(limit)
|
|
self.limit = limit
|
|
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.limit ~= -1 and #self.list >= self.limit) then
|
|
return
|
|
end
|
|
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))
|
|
local last = (first + lenght - 1)
|
|
|
|
for i = first, (last) 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:getSelected()
|
|
return self.selected
|
|
end
|
|
|
|
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
|