From 9ff9a53d2ad934d290cb277a2187812d9b29b7e2 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 21 Aug 2021 12:45:49 +0200 Subject: [PATCH] wip: initial version of the GUI --- .../birb/modules/gui/elements/linked.lua | 21 ++++ .../birb/modules/gui/elements/parent.lua | 112 ++++++++++++++++++ sonic-radiance.love/birb/modules/gui/init.lua | 65 ++++++++++ .../birb/modules/gui/mixins/elements.lua | 28 +++++ .../birb/modules/gui/mixins/screens.lua | 16 +++ .../birb/modules/gui/screen.lua | 42 +++++++ sonic-radiance.love/birb/modules/scenes.lua | 5 + 7 files changed, 289 insertions(+) create mode 100644 sonic-radiance.love/birb/modules/gui/elements/linked.lua create mode 100644 sonic-radiance.love/birb/modules/gui/elements/parent.lua create mode 100644 sonic-radiance.love/birb/modules/gui/init.lua create mode 100644 sonic-radiance.love/birb/modules/gui/mixins/elements.lua create mode 100644 sonic-radiance.love/birb/modules/gui/mixins/screens.lua create mode 100644 sonic-radiance.love/birb/modules/gui/screen.lua diff --git a/sonic-radiance.love/birb/modules/gui/elements/linked.lua b/sonic-radiance.love/birb/modules/gui/elements/linked.lua new file mode 100644 index 0000000..3e44fea --- /dev/null +++ b/sonic-radiance.love/birb/modules/gui/elements/linked.lua @@ -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 \ No newline at end of file diff --git a/sonic-radiance.love/birb/modules/gui/elements/parent.lua b/sonic-radiance.love/birb/modules/gui/elements/parent.lua new file mode 100644 index 0000000..e672886 --- /dev/null +++ b/sonic-radiance.love/birb/modules/gui/elements/parent.lua @@ -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 \ No newline at end of file diff --git a/sonic-radiance.love/birb/modules/gui/init.lua b/sonic-radiance.love/birb/modules/gui/init.lua new file mode 100644 index 0000000..06319f0 --- /dev/null +++ b/sonic-radiance.love/birb/modules/gui/init.lua @@ -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 \ No newline at end of file diff --git a/sonic-radiance.love/birb/modules/gui/mixins/elements.lua b/sonic-radiance.love/birb/modules/gui/mixins/elements.lua new file mode 100644 index 0000000..a5270a8 --- /dev/null +++ b/sonic-radiance.love/birb/modules/gui/mixins/elements.lua @@ -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 \ No newline at end of file diff --git a/sonic-radiance.love/birb/modules/gui/mixins/screens.lua b/sonic-radiance.love/birb/modules/gui/mixins/screens.lua new file mode 100644 index 0000000..4e594eb --- /dev/null +++ b/sonic-radiance.love/birb/modules/gui/mixins/screens.lua @@ -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 \ No newline at end of file diff --git a/sonic-radiance.love/birb/modules/gui/screen.lua b/sonic-radiance.love/birb/modules/gui/screen.lua new file mode 100644 index 0000000..6fe8196 --- /dev/null +++ b/sonic-radiance.love/birb/modules/gui/screen.lua @@ -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 \ No newline at end of file diff --git a/sonic-radiance.love/birb/modules/scenes.lua b/sonic-radiance.love/birb/modules/scenes.lua index 6a9750e..646b8a6 100644 --- a/sonic-radiance.love/birb/modules/scenes.lua +++ b/sonic-radiance.love/birb/modules/scenes.lua @@ -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