Refonte pour utiliser le systeme de GUI #112

Merged
kazhnuz merged 102 commits from feat/gui into master 2022-01-06 19:15:16 +01:00
7 changed files with 289 additions and 0 deletions
Showing only changes of commit 9ff9a53d2a - Show all commits

View file

@ -0,0 +1,21 @@
local Parent = require "birb.modules.gui.elements.parent"
local LinkedElement = Parent:extend()
function LinkedElement:new(name, x, y, w, h, object, varName)
LinkedElement.super.new(self, name, x, y, w, h)
self.object = object
self.variables = {}
self:addVariable(varName, "main")
end
function LinkedElement:addVariable(varName, internalName)
internalName = internalName or varName
self.variables[internalName] = varName
end
function LinkedElement:getVariableContent(varName)
varName = varName or "main"
return self.object[self.variables[varName]]
end
return LinkedElement

View file

@ -0,0 +1,112 @@
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.isVisible = false
self.screen = nil
self.depth = 0
self:register()
end
function GuiElement:getGui()
local scene = core.scenemanager.currentScene
return scene.gui
end
function GuiElement:register()
local gui = self:getGui()
gui:addElement(self.name, self)
end
function GuiElement:destroy()
local gui = self:getGui()
gui:removeElement(self.name)
if (self.screen == nil) then
self.screen:removeElement(self.name)
end
end
-- VISIBILITY/ACTIVITY
-- Handle drawing and how we interact with
function GuiElement:setDepth(depth)
self.depth = depth or 0
end
function GuiElement:getVisibility()
if (self.screen ~= nil) then
return (self.isVisible and self.screen.isVisible)
else
return self.isVisible
end
end
function GuiElement:setVisibility(visibility)
self.isVisible = visibility
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
function GuiElement:looseFocus()
if (self:haveFocus()) then
local gui = self:getGui()
gui.focusedMenu = nil
end
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

View file

@ -0,0 +1,65 @@
-- modules/gui :: a simple gui system, that manage all overhead elements of the game.
--[[
Copyright © 2021 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 Gui = Object:extend()
local ElementList = require "birb.modules.gui.mixins.elements"
local ScreenList = require "birb.modules.gui.mixins.screens"
Gui:implement(ScreenList)
Gui:implement(ElementList)
function Gui:new(scene)
self.scene = scene
self:reset()
end
function Gui:reset()
self:initElements()
self:initScreens()
end
function Gui:update(dt)
for _, element in ipairs(self.elements) do
if (element ~= nil) then
element:updateElement(dt)
end
end
end
-- DRAW FUNCTIONS
-- Draw the menu and its content
function Gui:drawTop()
for _, element in ipairs(self:getVisibleElement(true)) do
element:drawElement()
end
end
function Gui:drawBottom()
for _, element in ipairs(self:getVisibleElement(false)) do
element:drawElement()
end
end
return Gui

View file

@ -0,0 +1,28 @@
local ElementList = Object:extend()
function ElementList:initElements()
self.elements = {}
self.focusedElem = nil
end
function ElementList:addElement(name, element)
self.elements[name] = element
end
function ElementList:deleteElement(name)
self.elements[name] = nil
end
function ElementList:getVisibleElement(topLayer)
local visibleList = {}
for _, element in ipairs(self.elements) do
if (element ~= nil) then
if (element:getVisibility() and ((element.depth) < 0 == topLayer)) then
table.insert(visibleList, element)
end
end
end
return visibleList
end
return ElementList

View file

@ -0,0 +1,16 @@
local ScreenList = Object:extend()
function ScreenList:initScreens()
self.screens = {}
end
function ScreenList:addScreen(name, screen)
self.screens[name] = screen
end
function ScreenList:deleteScreen(name)
self.screens[name]:purgeElements()
self.screens[name] = nil
end
return ScreenList

View file

@ -0,0 +1,42 @@
local GuiScreen = Object:extend()
local ElementList = require "birb.modules.gui.mixins.elements"
local ScreenList = require "birb.modules.gui.mixins.screens"
GuiScreen:implement(ScreenList)
GuiScreen:implement(ElementList)
function GuiScreen:new(controller, name)
self.controller = controller
self.name = name
self.isVisible = false
self:reset()
end
function GuiScreen:getGui()
local scene = core.scenemanager.currentScene
return scene.gui
end
function GuiScreen:reset()
self:initElements()
self:initScreens()
end
function GuiScreen:registerElements()
local elementList = self:createElements()
for _, element in ipairs(elementList) do
if (element.is ~= nil) then
self:addElement(element.name, element)
else
self:addElement(element[1].name, element[1])
if (element[2] == false) then
element[1].isVisible = false
end
end
end
end
function GuiScreen:createElements()
-- Empty function
end
return GuiScreen

View file

@ -28,6 +28,7 @@ local Scene = Object:extend()
local Assets = require(cwd .. "assets")
local MenuSystem = require(cwd .. "menusystem")
local Gui = require (cwd .. "gui")
-- INIT FUNCTIONS
-- Initialize and configure the scene
@ -39,6 +40,7 @@ function Scene:new()
self.assets = Assets()
self.menusystem = MenuSystem(self)
self.sources = core.input:getSources()
self.gui = Gui(self)
self.inputLocked = true
self.inputLockedTimer = 2
@ -71,6 +73,7 @@ function Scene:updateScene(dt)
self.assets:update(dt)
self:updateMenus(dt)
self:updateDialog(dt)
self.gui:update(dt)
self:updateWorld(dt)
self:update(dt)
self:updateEnd(dt)
@ -150,9 +153,11 @@ function Scene:drawScene()
self:drawWorld()
self:draw()
self:drawMenus()
self.gui:drawBottom()
self:drawDialog()
self:drawEnd()
core.screen:drawTransition()
self.gui:drawTop()
self:drawOverTransition()
end