2019-04-10 18:01:46 +02:00
|
|
|
-- 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.
|
|
|
|
]]
|
|
|
|
|
2020-11-28 14:17:48 +01:00
|
|
|
local GuiElement = require "birb.modules.menusystem.parent"
|
2020-11-27 16:02:54 +01:00
|
|
|
|
2020-11-28 14:17:48 +01:00
|
|
|
local Menu = GuiElement:extend()
|
2020-12-04 13:16:32 +01:00
|
|
|
local MenuModel = require "birb.modules.menusystem.menus.model"
|
2019-04-11 18:16:36 +02:00
|
|
|
|
2020-12-04 13:16:32 +01:00
|
|
|
local menuUtils = require "birb.modules.menusystem.utils"
|
2019-04-10 18:01:46 +02:00
|
|
|
-- INIT FUNCTIONS
|
|
|
|
-- Initialize and configure functions.
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Menu:new(menusystem, name, x, y, w, h)
|
2020-11-28 14:17:48 +01:00
|
|
|
Menu.super.new(self, name, x, y, w, h)
|
|
|
|
self.menusystem = self:getGui()
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2020-11-28 18:32:49 +01:00
|
|
|
self.widget = MenuModel()
|
2020-12-04 12:57:24 +01:00
|
|
|
self.widgetSize = {}
|
2019-03-16 12:27:38 +01:00
|
|
|
self:updateWidgetSize()
|
|
|
|
|
2019-04-20 17:45:58 +02:00
|
|
|
self:resetSound()
|
2019-04-11 17:31:01 +02:00
|
|
|
end
|
|
|
|
|
2020-12-04 12:57:24 +01:00
|
|
|
-- INTERACTION FUNCTIONS
|
|
|
|
-- Keyboard and mouse
|
|
|
|
|
|
|
|
function Menu:keyreleased(key)
|
|
|
|
self:moveByKeys(key)
|
|
|
|
self:actionAndCancel(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:mousemoved(x, y)
|
|
|
|
local widgetID = self:getWidgetAtPoint(x, y)
|
|
|
|
|
|
|
|
if (widgetID ~= nil) then
|
|
|
|
if self.widget:trySelectWidget(widgetID) then
|
|
|
|
self:getFocus()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:mousepressed(x, y, button, istouch)
|
|
|
|
local widgetID = self:getWidgetAtPoint(x, y)
|
|
|
|
|
|
|
|
if (widgetID ~= nil) then
|
|
|
|
if self.widget:trySelectWidget(widgetID) then
|
|
|
|
self:getFocus()
|
|
|
|
self.widget:action(widgetID, "pointer")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:moveByKeys(key)
|
|
|
|
-- Cette fonction ne contient rien par défaut
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:actionAndCancel(key)
|
|
|
|
if key == "A" then
|
|
|
|
self.widget:selectedAction()
|
|
|
|
end
|
|
|
|
|
|
|
|
if key == "B" then
|
|
|
|
self.widget:cancelAction()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-10 18:01:46 +02:00
|
|
|
-- ACTION FUNCTIONS
|
|
|
|
-- Send actions to the widgets
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Menu:cancelAction()
|
2020-11-28 18:32:49 +01:00
|
|
|
self.widget:cancelAction()
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:clear()
|
2020-11-28 18:32:49 +01:00
|
|
|
self.widget:clear()
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:resize(x,y,w,h)
|
2020-11-27 16:02:54 +01:00
|
|
|
self:set(x,y,w,h)
|
2019-03-16 12:27:38 +01:00
|
|
|
|
|
|
|
self:updateWidgetSize()
|
|
|
|
end
|
|
|
|
|
2019-04-11 16:58:42 +02:00
|
|
|
-- UPDATE FUNCTIONS
|
|
|
|
|
2020-11-28 14:17:48 +01:00
|
|
|
function Menu:updateElement(dt)
|
|
|
|
self:update(dt)
|
2020-11-28 18:32:49 +01:00
|
|
|
self.widget:update(dt)
|
2019-04-11 16:58:42 +02:00
|
|
|
end
|
|
|
|
|
2019-04-10 18:01:46 +02:00
|
|
|
-- DRAW FUNCTIONS
|
|
|
|
-- Draw the menu and its content
|
|
|
|
|
2020-11-28 14:17:48 +01:00
|
|
|
function Menu:drawElement()
|
|
|
|
self:draw()
|
|
|
|
if (self:haveFocus()) then
|
2020-12-04 12:57:24 +01:00
|
|
|
local x, y, w, h = self:getGraphicalCursorPosition()
|
|
|
|
self:drawGraphicalCursor(x, y, w, h)
|
2020-11-28 14:17:48 +01:00
|
|
|
end
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-12-04 12:57:24 +01:00
|
|
|
function Menu:drawGraphicalCursor(x, y, w, h)
|
|
|
|
menuUtils.drawCursor(x, y, w, h)
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:drawCanvas()
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-04-10 18:01:46 +02:00
|
|
|
-- WIDGET FUNCTIONS
|
|
|
|
-- Handle widgets of the functions
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Menu:addWidget(newwidget)
|
2020-11-28 18:32:49 +01:00
|
|
|
self.widget:addWidget(newwidget)
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-28 14:17:48 +01:00
|
|
|
function Menu:setCancelWidget(id)
|
2020-11-28 18:32:49 +01:00
|
|
|
self.widget:setCancelWidget(id)
|
2020-11-28 14:17:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:updateWidgetSize()
|
2020-12-04 12:57:24 +01:00
|
|
|
self.widgetSize.h = 0
|
|
|
|
self.widgetSize.w = 0
|
2020-11-28 14:17:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:getWidgetSize(id)
|
2020-12-04 12:57:24 +01:00
|
|
|
return self.widgetSize.w, self.widgetSize.h
|
2020-11-28 14:17:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:getWidgetNumber()
|
2020-11-28 18:32:49 +01:00
|
|
|
return self.widget:lenght()
|
2020-11-28 14:17:48 +01:00
|
|
|
end
|
|
|
|
|
2020-12-04 12:57:24 +01:00
|
|
|
function Menu:getWidgetAtPoint(x, y)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-04-10 18:01:46 +02:00
|
|
|
-- CURSOR FUNCTIONS
|
|
|
|
-- Set or move the cursor of the menu
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Menu:setCursor(cursorid)
|
2020-11-28 18:32:49 +01:00
|
|
|
self.widget:setCursor(cursorid)
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:moveCursor(new_selected)
|
2019-04-20 17:42:04 +02:00
|
|
|
self:playNavigationSound()
|
2020-11-28 18:32:49 +01:00
|
|
|
self.widget:moveCursorAbsolute(new_selected)
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2019-04-10 18:01:46 +02:00
|
|
|
-- SOUND FUNCTION
|
|
|
|
-- Handle SFX
|
|
|
|
|
2019-04-20 17:45:58 +02:00
|
|
|
function Menu:resetSound()
|
|
|
|
self.sound = {}
|
2019-04-20 17:47:57 +02:00
|
|
|
self.sound.active = false
|
2019-04-20 17:45:58 +02:00
|
|
|
self.sound.asset = nil
|
|
|
|
end
|
|
|
|
|
2019-04-20 17:54:22 +02:00
|
|
|
function Menu:setSoundFromSceneAssets(name)
|
2020-11-13 19:11:09 +01:00
|
|
|
self:setSound(self.menusystem.scene.assets:getWithType(name, "sfx"))
|
2019-04-20 17:54:22 +02:00
|
|
|
end
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Menu:setSound(soundasset)
|
|
|
|
self.sound.active = true
|
|
|
|
self.sound.asset = soundasset
|
|
|
|
end
|
|
|
|
|
2019-04-20 17:42:04 +02:00
|
|
|
function Menu:playNavigationSound()
|
2019-03-16 12:27:38 +01:00
|
|
|
if self.sound.active == true then
|
2020-11-13 17:43:28 +01:00
|
|
|
self.sound.asset:play()
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-10 18:01:46 +02:00
|
|
|
-- VIEW FUNCTIONS
|
|
|
|
-- Handle the view of the menu
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
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
|