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.
|
|
|
|
]]
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
local Menu = Object:extend()
|
|
|
|
|
2019-04-11 18:16:36 +02:00
|
|
|
local function updateWidgetByOrder(a, b)
|
|
|
|
if a.order ~= b.order then
|
|
|
|
return a.order < b.order
|
|
|
|
else
|
|
|
|
return a.creationID < b.creationID
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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)
|
|
|
|
self.menusystem = menusystem
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.w = w
|
|
|
|
self.h = h
|
|
|
|
|
|
|
|
self.widget = {}
|
|
|
|
self.widget.list = {}
|
|
|
|
self.widget.selected = 0
|
|
|
|
self.widget.selectedPrevious = 0
|
|
|
|
self.widget.cancel = 0
|
|
|
|
self:updateWidgetSize()
|
|
|
|
|
2019-04-07 12:38:33 +02:00
|
|
|
self.isDestroyed = false
|
|
|
|
self.isVisible = true
|
|
|
|
self.isActive = true
|
|
|
|
self.isLocked = false
|
|
|
|
self.isAlwaysVisible = false
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2019-03-22 18:02:33 +01:00
|
|
|
self.depth = 0
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
self.sound = {}
|
|
|
|
self.sound.asset = nil
|
|
|
|
self.sound.active = false
|
|
|
|
|
|
|
|
self:register()
|
|
|
|
end
|
|
|
|
|
2019-03-22 18:02:33 +01:00
|
|
|
function Menu:setDepth(depth)
|
|
|
|
self.depth = depth or 0
|
|
|
|
end
|
|
|
|
|
2019-03-22 17:22:45 +01:00
|
|
|
function Menu:setVisibility(visibility)
|
2019-04-07 12:38:33 +02:00
|
|
|
if self.isLocked == false and self.isAlwaysVisible == false then
|
|
|
|
-- if the menu is locked (thus is always active), it should also
|
|
|
|
-- be always visible.
|
2019-03-22 17:22:45 +01:00
|
|
|
self.isVisible = visibility
|
|
|
|
else
|
|
|
|
self.isVisible = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-07 12:38:33 +02:00
|
|
|
function Menu:setActivity(activity)
|
|
|
|
if self.isLocked == false then
|
|
|
|
self.isActive = activity
|
|
|
|
else
|
|
|
|
self.isActive = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-07 12:59:25 +02:00
|
|
|
function Menu:lock(lock)
|
|
|
|
self.isLocked = lock
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:lockVisibility(lock)
|
|
|
|
self.isAlwaysVisible = lock
|
|
|
|
end
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Menu:getFocus()
|
|
|
|
self.menusystem.focusedMenu = self.name
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:haveFocus()
|
|
|
|
return (self.menusystem.focusedMenu == self.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:register()
|
|
|
|
self.menusystem:addMenu(self.name, self)
|
|
|
|
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
|
|
|
|
|
2019-04-11 17:31:01 +02:00
|
|
|
function Menu:getWidgetNumber()
|
|
|
|
return #self.widget.list
|
|
|
|
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()
|
|
|
|
if (self.widget.cancel ~= 0) then
|
2019-04-07 12:41:19 +02:00
|
|
|
self.widget.list[self.widget.cancel]:action("key")
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:clear()
|
|
|
|
self.widget.list = {}
|
|
|
|
self.widget.cancel = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:resize(x,y,w,h)
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.w = w
|
|
|
|
self.h = h
|
|
|
|
|
|
|
|
self:updateWidgetSize()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:destroy()
|
|
|
|
self.destroyed = true
|
|
|
|
end
|
|
|
|
|
2019-04-11 18:16:36 +02:00
|
|
|
function Menu:updateWidgetsOrder()
|
|
|
|
table.sort(self.widget.list, updateWidgetByOrder)
|
|
|
|
end
|
|
|
|
|
2019-04-11 16:58:42 +02:00
|
|
|
-- UPDATE FUNCTIONS
|
|
|
|
-- Update the menu every game update
|
|
|
|
|
|
|
|
function Menu:update(dt)
|
|
|
|
-- Cette fonction ne contient rien par défaut
|
|
|
|
end
|
|
|
|
|
2019-04-10 18:01:46 +02:00
|
|
|
-- DRAW FUNCTIONS
|
|
|
|
-- Draw the menu and its content
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Menu:draw()
|
|
|
|
-- nothing here
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:drawCursor()
|
|
|
|
-- nothing here
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:drawCanvas()
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-04-10 18:01:46 +02:00
|
|
|
-- KEYBOARD FUNCTIONS
|
|
|
|
-- Handle key press
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Menu:keyreleased(key)
|
|
|
|
-- Cette fonction ne contient rien par défaut
|
|
|
|
end
|
|
|
|
|
2019-04-10 18:01:46 +02:00
|
|
|
-- MOUSE FUNCTIONS
|
|
|
|
-- Handle pointers (clic/touch)
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Menu:mousemoved(x, y)
|
|
|
|
-- Cette fonction ne contient rien par défaut
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:mousepressed( x, y, button, istouch )
|
|
|
|
-- Cette fonction ne contient rien par défaut
|
|
|
|
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)
|
|
|
|
if #self.widget.list == 0 then
|
|
|
|
self.widget.selected = 1
|
|
|
|
end
|
|
|
|
table.insert(self.widget.list, newwidget)
|
|
|
|
self:updateWidgetsID()
|
2019-04-11 18:16:36 +02:00
|
|
|
self:updateWidgetsOrder()
|
2019-03-16 12:27:38 +01:00
|
|
|
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: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
|
|
|
|
|
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)
|
|
|
|
self.widget.selected = cursorid --math.max(1, math.min(cursorid, #self.widget.list))
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:moveCursor(new_selected)
|
|
|
|
self:playSelectSound()
|
|
|
|
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
|
|
|
|
|
2019-04-10 18:01:46 +02:00
|
|
|
-- SOUND FUNCTION
|
|
|
|
-- Handle SFX
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Menu:setSound(soundasset)
|
|
|
|
self.sound.active = true
|
|
|
|
self.sound.asset = soundasset
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:playSelectSound()
|
|
|
|
if self.sound.active == true then
|
|
|
|
love.audio.stop( self.sound.asset )
|
|
|
|
self.sound.asset:setVolume(core.options.data.audio.sfx / 100)
|
|
|
|
love.audio.play( self.sound.asset )
|
|
|
|
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
|