chore: create a generic guiElement

This commit is contained in:
Kazhnuz 2020-11-28 14:17:48 +01:00
parent dca462803f
commit 7e7cfe3763
3 changed files with 154 additions and 116 deletions

View file

@ -199,9 +199,7 @@ function MenuSystem:update(dt)
if (self.isActive) then if (self.isActive) then
self:removeDestroyedMenus() self:removeDestroyedMenus()
for _, guiElement in pairs(self.menus) do for _, guiElement in pairs(self.menus) do
guiElement:update(dt) guiElement:updateElement(dt)
--TODO: move this inside the menu
guiElement:updateWidgets(dt)
end end
if self.menus[self.focusedMenu] ~= nil then if self.menus[self.focusedMenu] ~= nil then
@ -274,13 +272,7 @@ function MenuSystem:draw(dt)
for _, drawObject in ipairs(self.drawList) do for _, drawObject in ipairs(self.drawList) do
local guiElement = self.menus[drawObject.name] local guiElement = self.menus[drawObject.name]
if (guiElement.isVisible) then if (guiElement.isVisible) then
guiElement:draw(dt) guiElement:drawElement(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 end
end end

View file

@ -21,9 +21,9 @@
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]] ]]
local Rect = require "birb.objects.2D.rect" local GuiElement = require "birb.modules.menusystem.parent"
local Menu = Rect:extend() local Menu = GuiElement:extend()
local function updateWidgetByOrder(a, b) local function updateWidgetByOrder(a, b)
if a.order ~= b.order then if a.order ~= b.order then
@ -37,10 +37,8 @@ end
-- Initialize and configure functions. -- Initialize and configure functions.
function Menu:new(menusystem, name, x, y, w, h) function Menu:new(menusystem, name, x, y, w, h)
self.menusystem = menusystem Menu.super.new(self, name, x, y, w, h)
self.name = name self.menusystem = self:getGui()
Menu.super.new(self, x, y, w, h)
self.widget = {} self.widget = {}
self.widget.list = {} self.widget.list = {}
@ -49,76 +47,7 @@ function Menu:new(menusystem, name, x, y, w, h)
self.widget.cancel = 0 self.widget.cancel = 0
self:updateWidgetSize() self:updateWidgetSize()
self.isDestroyed = false
self.isVisible = true
self.isActive = true
self.isLocked = false
self.isAlwaysVisible = false
self.depth = 0
self:resetSound() self:resetSound()
self:register()
end
function Menu:setDepth(depth)
self.depth = depth or 0
end
function Menu:setVisibility(visibility)
if self.isLocked == false and self.isAlwaysVisible == false then
-- if the menu is locked (thus is always active), it should also
-- be always visible.
self.isVisible = visibility
else
self.isVisible = true
end
end
function Menu:setActivity(activity)
if self.isLocked == false then
self.isActive = activity
else
self.isActive = true
end
end
function Menu:lock(lock)
self.isLocked = lock
end
function Menu:lockVisibility(lock)
self.isAlwaysVisible = lock
end
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
function Menu:getWidgetNumber()
return #self.widget.list
end end
-- ACTION FUNCTIONS -- ACTION FUNCTIONS
@ -141,26 +70,21 @@ function Menu:resize(x,y,w,h)
self:updateWidgetSize() self:updateWidgetSize()
end end
function Menu:destroy()
self.destroyed = true
end
function Menu:updateWidgetsOrder()
table.sort(self.widget.list, updateWidgetByOrder)
end
-- UPDATE FUNCTIONS -- UPDATE FUNCTIONS
-- Update the menu every game update
function Menu:update(dt) function Menu:updateElement(dt)
-- Cette fonction ne contient rien par défaut self:update(dt)
self:updateWidgets(dt)
end end
-- DRAW FUNCTIONS -- DRAW FUNCTIONS
-- Draw the menu and its content -- Draw the menu and its content
function Menu:draw() function Menu:drawElement()
-- nothing here self:draw()
if (self:haveFocus()) then
self:drawCursor()
end
end end
function Menu:drawCursor() function Menu:drawCursor()
@ -171,24 +95,6 @@ function Menu:drawCanvas()
end end
-- KEYBOARD FUNCTIONS
-- Handle key press
function Menu:keyreleased(key)
-- Cette fonction ne contient rien par défaut
end
-- MOUSE FUNCTIONS
-- Handle pointers (clic/touch)
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
-- WIDGET FUNCTIONS -- WIDGET FUNCTIONS
-- Handle widgets of the functions -- Handle widgets of the functions
@ -209,6 +115,10 @@ function Menu:updateWidgets(dt)
end end
end end
function Menu:updateWidgetsOrder()
table.sort(self.widget.list, updateWidgetByOrder)
end
function Menu:updateWidgetsID() function Menu:updateWidgetsID()
for i,v in ipairs(self.widget.list) do for i,v in ipairs(self.widget.list) do
v.id = i v.id = i
@ -223,6 +133,23 @@ function Menu:removeDestroyedWidgets() -- On retire les widgets marquées comme
end 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 -- CURSOR FUNCTIONS
-- Set or move the cursor of the menu -- Set or move the cursor of the menu

View file

@ -0,0 +1,119 @@
local Rect = require "birb.objects.2D.rect"
local GuiElement = Rect:extend()
function GuiElement:new(name, x, y, w, h)
GuiElement.super.new(self, x, y, w, h)
self.name = name
self.isDestroyed = false
self.isVisible = true
self.isActive = true
self.isLocked = false
self.isAlwaysVisible = false
self.depth = 0
self:register()
end
function GuiElement:getGui()
local scene = core.scenemanager.currentScene
return scene.menusystem
end
function GuiElement:register()
local gui = self:getGui()
gui:addMenu(self.name, self)
end
function GuiElement:destroy()
self.destroyed = true
end
-- VISIBILITY/ACTIVITY
-- Handle drawing and how we interact with
function GuiElement:setDepth(depth)
self.depth = depth or 0
end
function GuiElement:setVisibility(visibility)
if self.isLocked == false and self.isAlwaysVisible == false then
self.isVisible = visibility
else
-- if the element is locked (thus is always active), it should also
-- be always visible.
self.isVisible = true
end
end
function GuiElement:setActivity(activity)
if self.isLocked == false then
self.isActive = activity
else
self.isActive = true
end
end
function GuiElement:lock(lock)
self.isLocked = lock
end
function GuiElement:lockVisibility(lock)
self.isAlwaysVisible = lock
end
function GuiElement:getFocus()
local gui = self:getGui()
gui.focusedMenu = self.name
end
function GuiElement:haveFocus()
local gui = self:getGui()
return (gui.focusedMenu == self.name)
end
-- UPDATE FUNCTIONS
-- Update the menu every game update
-- External update function
function GuiElement:updateElement(dt)
self:update(dt)
end
-- Internal update function
function GuiElement:update(dt)
-- Cette fonction ne contient rien par défaut
end
-- DRAW FUNCTIONS
-- Draw the menu and its content
function GuiElement:drawElement()
self:draw()
end
function GuiElement:draw()
-- nothing here
end
-- KEYBOARD FUNCTIONS
-- Handle key press
function GuiElement:keyreleased(key)
-- Cette fonction ne contient rien par défaut
end
-- MOUSE FUNCTIONS
-- Handle pointers (clic/touch)
function GuiElement:mousemoved(x, y)
-- Cette fonction ne contient rien par défaut
end
function GuiElement:mousepressed(x, y, button, istouch)
-- Cette fonction ne contient rien par défaut
end
return GuiElement