sonic-radiance/sonic-radiance.love/core/modules/menusystem/init.lua
2019-03-23 12:02:01 +01:00

140 lines
3.1 KiB
Lua

local MenuSystem = Object:extend()
local cwd = (...):gsub('%.init$', '') .. "."
MenuSystem.Parent = require(cwd .. "parent")
MenuSystem.ListBox = require(cwd .. "listbox")
MenuSystem.FlowBox = require(cwd .. "flowbox")
MenuSystem.Grid = require(cwd .. "grid")
MenuSystem.TextMenu = require(cwd .. "textmenu")
MenuSystem.Widget = require(cwd .. "widgets")
--local VirtualPad = require "modules.virtualpad"
function MenuSystem:new()
self.menus = {}
self.focusedMenu = ""
end
function MenuSystem:reset()
self.menus = {}
end
function MenuSystem:addMenu(name, menu)
self.menus[name] = menu
end
function MenuSystem:update(dt)
self:removeDestroyedMenus()
for k,v in pairs(self.menus) do
v:update(dt)
v:updateWidgets(dt)
end
if self.menus[self.focusedMenu] ~= nil then
-- Only check buttons if the current focused menu is actually active
if self.menus[self.focusedMenu].isActive then
for k,v in pairs(self.keys) do
if self.keys[k].isPressed then
self.menus[self.focusedMenu]:keyreleased(k)
end
end
end
end
end
function MenuSystem:switchMenu(menu)
for k,v in pairs(self.menus) do
if k == menu then
v:getFocus()
v:setVisibility(true)
v.isActive = true
else
v:setVisibility(false)
v.isActive = false
end
end
end
function MenuSystem:setAllMenuVisibility(visibility)
for k,v in pairs(self.menus) do
v:setVisibility(visibility)
end
end
function MenuSystem:setAllMenuActivity(activity)
for k,v in pairs(self.menus) do
v.isActive = activity
end
end
function MenuSystem:removeDestroyedMenus()
-- On retire les entitées marquées comme supprimées
for k,v in pairs(self.menus) do
if (v.isDestroyed == true) then
self.menus[k] = nil
end
end
end
function MenuSystem:keyreleased(key, code)
-- TODO:depreciated function
end
function MenuSystem:mousemoved(x, y, dx, dy)
for k,v in pairs(self.menus) do
if v.isActive then
if (x > v.x) and (x < v.x + v.w) and (y > v.y) and (y < v.y + v.h) then
v:mousemoved(x - v.x, y - v.y)
break;
end
end
end
end
function MenuSystem:mousepressed( x, y, button, istouch )
for k,v in pairs(self.menus) do
if v.isActive then
if (x > v.x) and (x < v.x + v.w) and (y > v.y) and (y < v.y + v.h) then
v:mousepressed(x - v.x, y - v.y, button, istouch )
break;
end
end
end
end
function MenuSystem:getDrawList()
local drawList = {}
for k,v in pairs(self.menus) do
local drawObject = {}
drawObject.name = k
drawObject.depth = v.depth
table.insert(drawList, drawObject)
end
table.sort(drawList, function(a,b) return a.depth > b.depth end)
return drawList
end
function MenuSystem:draw(dt) -- On dessine les entitées
self.drawList = self:getDrawList()
for i,v in ipairs(self.drawList) do
local v2 = self.menus[v.name]
if (v2.isVisible) then
v2:draw(dt)
end
end
if self.menus[self.focusedMenu] ~= nil then
if (self.menus[self.focusedMenu].isVisible) then
self.menus[self.focusedMenu]:drawCursor()
end
end
end
return MenuSystem