epervier-old/framework/modules/gui/menus/model/init.lua

228 lines
4.7 KiB
Lua
Raw Normal View History

local MenuModel = Object:extend()
local Page = require "framework.modules.gui.menus.model.page"
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()
2020-12-05 09:01:41 +01:00
self:clear()
self.list = {}
self.selected = 0
self.selectedPrevious = 0
self.cancel = 0
2020-11-29 12:50:07 +01:00
self.limit = -1
-- self:updateWidgetSize()
2022-07-27 21:57:34 +02:00
self.hoverFunc = nil
end
function MenuModel:clear()
2020-12-05 09:01:41 +01:00
self.pages = {}
self:addPage("main")
end
2020-12-05 09:01:41 +01:00
-- PAGE FUNCTIONS
-- Handle pages
function MenuModel:addPage(pageName)
local page = Page()
2022-07-27 21:57:34 +02:00
page.name = pageName
2020-12-05 09:01:41 +01:00
self.pages[pageName] = page
self.currentPage = pageName
return page
end
function MenuModel:addSubmenu(pageName, parent)
local page = self:addPage(pageName)
page:setParent(parent)
end
function MenuModel:removePage(pageName)
self.pages[pageName] = nil
end
function MenuModel:pageExists(pageName)
return (self.pages[pageName] ~= nil)
end
function MenuModel:getPage(pageName)
return self.pages[pageName]
end
function MenuModel:switch(pageName)
if (self:pageExists(pageName)) then
self.currentPage = pageName
2022-07-27 21:57:34 +02:00
self:hoverAction()
2020-12-05 09:01:41 +01:00
end
end
function MenuModel:back()
local page = self:getCurrentPage()
self:switch(page:getParent())
end
function MenuModel:getCurrentPage()
return self:getPage(self.currentPage)
2020-11-29 12:50:07 +01:00
end
2022-07-27 21:57:34 +02:00
function MenuModel:getCurrentPageName()
return self.currentPage
end
-- UPDATE/DRAW FUNCTIONS
-- All the update functions
function MenuModel:update(dt)
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
page:update(dt)
end
function MenuModel:updateWidgetsOrder()
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
page:updateWidgetByOrder()
end
function MenuModel:updateWidgetsID()
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
page:updateWidgetsID()
end
-- ACTION FUNCTIONS
-- All the actions callback used by the widgets
2022-07-27 21:57:34 +02:00
function MenuModel:addHoverAction(func)
self.hoverFunc = func
end
function MenuModel:cancelAction()
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
page:cancelAction()
end
function MenuModel:action(id, type)
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
page:action(id, type)
end
function MenuModel:selectedAction()
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
page:selectedAction()
end
2022-07-27 21:57:34 +02:00
function MenuModel:hoverAction()
local page = self:getCurrentPage()
if (self.hoverFunc ~= nil) then
page:hoverAction(self.hoverFunc)
end
end
function MenuModel:lateralAction(func, key)
local page = self:getCurrentPage()
page:lateralAction(func, key)
end
-- WIDGET FUNCTIONS
-- All the functions to handle widgets
function MenuModel:addWidget(newWidget)
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
page:addWidget(newWidget)
end
function MenuModel:getList(first, lenght)
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
return page:getList(first, lenght)
end
2020-12-05 09:01:41 +01:00
function MenuModel:removeDestroyedWidgets()
local page = self:getCurrentPage()
page:removeDestroyedWidgets()
end
function MenuModel:lenght()
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
return page:lenght()
end
function MenuModel:widgetExist(id)
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
return page:widgetExist(id)
end
function MenuModel:setLimit(limit)
local page = self:getCurrentPage()
page:setLimit(limit)
end
-- CANCEL FUNCTIONS
-- Add a widget as a "cancel" function
2021-12-04 13:18:54 +01:00
function MenuModel:setCancelWidget(id)
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
2021-12-04 13:18:54 +01:00
page:setCancelWidget(id)
end
2020-12-05 09:01:41 +01:00
function MenuModel:getCancelWidget()
local page = self:getCurrentPage()
return page:getCancelWidget()
end
-- CURSOR FUNCTIONS
-- Set or move the cursor of the menu
2020-12-04 12:00:28 +01:00
function MenuModel:getSelected()
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
return page:getSelected()
2020-12-04 12:00:28 +01:00
end
function MenuModel:haveCursor()
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
return page:haveCursor()
end
function MenuModel:trySelectWidget(cursorid)
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
2022-07-27 21:57:34 +02:00
local isSuccess = page:trySelectWidget(cursorid)
if (isSuccess) then
self:hoverAction()
end
return isSuccess
end
function MenuModel:setCursor(cursorid)
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
page:setCursor(cursorid)
2022-07-27 21:57:34 +02:00
self:hoverAction()
end
function MenuModel:moveCursorAbsolute(newSelected)
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
page:moveCursorAbsolute(newSelected)
2022-07-27 21:57:34 +02:00
self:hoverAction()
end
function MenuModel:moveCursor(relative)
2020-12-05 09:01:41 +01:00
local page = self:getCurrentPage()
page:moveCursor(relative)
2022-07-27 21:57:34 +02:00
self:hoverAction()
end
2021-12-04 13:18:54 +01:00
-- DRAW
-- Draw widget
function MenuModel:redraw()
local page = self:getCurrentPage()
page:redraw()
end
return MenuModel