212 lines
4.8 KiB
Lua
212 lines
4.8 KiB
Lua
-- parent.lua : The parent of the functions.
|
|
|
|
--[[
|
|
Copyright © 2019 Kazhnuz
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
the Software without restriction, including without limitation the rights to
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
]]
|
|
|
|
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
|
|
|
|
-- INIT FUNCTIONS
|
|
-- Initialize and configure functions.
|
|
|
|
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:updateWidgetSize()
|
|
|
|
self:resetSound()
|
|
end
|
|
|
|
-- ACTION FUNCTIONS
|
|
-- Send actions to the widgets
|
|
|
|
function Menu:cancelAction()
|
|
if (self.widget.cancel ~= 0) then
|
|
self.widget.list[self.widget.cancel]:action("key")
|
|
end
|
|
end
|
|
|
|
function Menu:clear()
|
|
self.widget.list = {}
|
|
self.widget.cancel = 0
|
|
end
|
|
|
|
function Menu:resize(x,y,w,h)
|
|
self:set(x,y,w,h)
|
|
|
|
self:updateWidgetSize()
|
|
end
|
|
|
|
-- UPDATE FUNCTIONS
|
|
|
|
function Menu:updateElement(dt)
|
|
self:update(dt)
|
|
self:updateWidgets(dt)
|
|
end
|
|
|
|
-- DRAW FUNCTIONS
|
|
-- Draw the menu and its content
|
|
|
|
function Menu:drawElement()
|
|
self:draw()
|
|
if (self:haveFocus()) then
|
|
self:drawCursor()
|
|
end
|
|
end
|
|
|
|
function Menu:drawCursor()
|
|
-- nothing here
|
|
end
|
|
|
|
function Menu:drawCanvas()
|
|
|
|
end
|
|
|
|
-- WIDGET FUNCTIONS
|
|
-- 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
|
|
end
|
|
|
|
function Menu:setCancelWidget(id)
|
|
self.widget.cancel = #self.widget.list
|
|
end
|
|
|
|
function Menu:updateWidgetSize()
|
|
self.widget.h = 0
|
|
self.widget.w = 0
|
|
end
|
|
|
|
function Menu:getWidgetSize(id)
|
|
return self.widget.w, self.widget.h
|
|
end
|
|
|
|
function Menu:getWidgetNumber()
|
|
return #self.widget.list
|
|
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))
|
|
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
|
|
end
|
|
|
|
-- SOUND FUNCTION
|
|
-- Handle SFX
|
|
|
|
function Menu:resetSound()
|
|
self.sound = {}
|
|
self.sound.active = false
|
|
self.sound.asset = nil
|
|
end
|
|
|
|
function Menu:setSoundFromSceneAssets(name)
|
|
self:setSound(self.menusystem.scene.assets:getWithType(name, "sfx"))
|
|
end
|
|
|
|
function Menu:setSound(soundasset)
|
|
self.sound.active = true
|
|
self.sound.asset = soundasset
|
|
end
|
|
|
|
function Menu:playNavigationSound()
|
|
if self.sound.active == true then
|
|
self.sound.asset:play()
|
|
end
|
|
end
|
|
|
|
-- VIEW FUNCTIONS
|
|
-- Handle the view of the menu
|
|
|
|
function Menu:resetView()
|
|
-- ne sert à rien ici, c'est juste pour éviter des crash
|
|
end
|
|
|
|
function Menu:updateView()
|
|
-- ne sert à rien ici, c'est juste pour éviter des crash
|
|
end
|
|
|
|
function Menu:moveView()
|
|
-- ne sert à rien ici, c'est juste pour éviter des crash
|
|
end
|
|
|
|
return Menu
|