sonic-bluestreak/sonic-bluestreak.love/birb/modules/gui/menus/model/init.lua

197 lines
4 KiB
Lua
Raw Permalink Normal View History

2021-11-25 10:46:15 +01:00
local MenuModel = Object:extend()
local Page = require "birb.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()
self:clear()
self.list = {}
self.selected = 0
self.selectedPrevious = 0
self.cancel = 0
self.limit = -1
-- self:updateWidgetSize()
end
function MenuModel:clear()
self.pages = {}
self:addPage("main")
end
-- PAGE FUNCTIONS
-- Handle pages
function MenuModel:addPage(pageName)
local page = Page()
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
end
end
function MenuModel:back()
local page = self:getCurrentPage()
self:switch(page:getParent())
end
function MenuModel:getCurrentPage()
return self:getPage(self.currentPage)
end
-- UPDATE/DRAW FUNCTIONS
-- All the update functions
function MenuModel:update(dt)
local page = self:getCurrentPage()
page:update(dt)
end
function MenuModel:updateWidgetsOrder()
local page = self:getCurrentPage()
page:updateWidgetByOrder()
end
function MenuModel:updateWidgetsID()
local page = self:getCurrentPage()
page:updateWidgetsID()
end
-- ACTION FUNCTIONS
-- All the actions callback used by the widgets
function MenuModel:cancelAction()
local page = self:getCurrentPage()
page:cancelAction()
end
function MenuModel:action(id, type)
local page = self:getCurrentPage()
page:action(id, type)
end
function MenuModel:selectedAction()
local page = self:getCurrentPage()
page:selectedAction()
end
-- WIDGET FUNCTIONS
-- All the functions to handle widgets
function MenuModel:addWidget(newWidget)
local page = self:getCurrentPage()
page:addWidget(newWidget)
end
function MenuModel:getList(first, lenght)
local page = self:getCurrentPage()
return page:getList(first, lenght)
end
function MenuModel:removeDestroyedWidgets()
local page = self:getCurrentPage()
page:removeDestroyedWidgets()
end
function MenuModel:lenght()
local page = self:getCurrentPage()
return page:lenght()
end
function MenuModel:widgetExist(id)
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
function MenuModel:setCancelWidget(id)
local page = self:getCurrentPage()
page:setCancelWidget(id)
end
function MenuModel:getCancelWidget()
local page = self:getCurrentPage()
return page:getCancelWidget()
end
-- CURSOR FUNCTIONS
-- Set or move the cursor of the menu
function MenuModel:getSelected()
local page = self:getCurrentPage()
return page:getSelected()
end
function MenuModel:haveCursor()
local page = self:getCurrentPage()
return page:haveCursor()
end
function MenuModel:trySelectWidget(cursorid)
local page = self:getCurrentPage()
return page:trySelectWidget(cursorid)
end
function MenuModel:setCursor(cursorid)
local page = self:getCurrentPage()
page:setCursor(cursorid)
end
function MenuModel:moveCursorAbsolute(newSelected)
local page = self:getCurrentPage()
page:moveCursorAbsolute(newSelected)
end
function MenuModel:moveCursor(relative)
local page = self:getCurrentPage()
page:moveCursor(relative)
end
-- DRAW
-- Draw widget
function MenuModel:redraw()
local page = self:getCurrentPage()
page:redraw()
end
return MenuModel