sonic-radiance/sonic-radiance.love/birb/modules/gui/elements/parent.lua

150 lines
3.1 KiB
Lua
Raw Normal View History

2021-08-22 13:02:52 +02:00
local Rect = require "birb.classes.2D.rect"
2021-08-21 12:45:49 +02:00
local GuiElement = Rect:extend()
local TweenManager = require "birb.classes.time"
2021-08-21 12:45:49 +02:00
function GuiElement:new(name, x, y, w, h)
GuiElement.super.new(self, x, y, w, h)
self.name = name
2021-08-22 13:02:52 +02:00
self.isVisible = true
2021-08-21 12:45:49 +02:00
self.screen = nil
2021-08-31 10:01:24 +02:00
self.depth = 10
2021-08-21 12:45:49 +02:00
self.tweens = TweenManager(self)
self.assets = self:getAssets()
2021-08-21 12:45:49 +02:00
self:register()
end
2021-08-31 10:56:56 +02:00
function GuiElement:setKeyPressAction(func)
self.func = func
end
2021-08-21 12:45:49 +02:00
function GuiElement:getGui()
2021-08-22 13:02:52 +02:00
local scene = core.scenemanager.nextScene or core.scenemanager.currentScene
2021-08-21 12:45:49 +02:00
return scene.gui
end
function GuiElement:getAssets()
local scene = core.scenemanager.nextScene or core.scenemanager.currentScene
return scene.assets
end
2021-08-21 12:45:49 +02:00
function GuiElement:register()
local gui = self:getGui()
2021-08-22 12:39:44 +02:00
self.creationId = gui:addElement(self.name, self)
2021-08-21 12:45:49 +02:00
end
function GuiElement:destroy()
local gui = self:getGui()
gui:deleteElement(self.name)
if (self.screen ~= nil) then
self.screen:deleteElement(self.name)
2021-08-21 12:45:49 +02:00
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()
2021-08-31 10:01:24 +02:00
gui:setFocus(self.name)
2021-08-21 12:45:49 +02:00
end
function GuiElement:haveFocus()
local gui = self:getGui()
return (gui.focusedElement == self.name)
2021-08-21 12:45:49 +02:00
end
function GuiElement:looseFocus()
if (self:haveFocus()) then
local gui = self:getGui()
2021-08-31 10:01:24 +02:00
gui:removeFocus()
2021-08-21 12:45:49 +02:00
end
end
-- UPDATE FUNCTIONS
-- Update the menu every game update
-- External update function
function GuiElement:updateElement(dt)
self:update(dt)
self.tweens:update(dt)
2021-08-21 12:45:49 +02:00
end
-- Internal update function
function GuiElement:update(dt)
-- Cette fonction ne contient rien par défaut
end
-- TWEEN FUNCTIONS
-- Handle tweening
function GuiElement:newTween(start, duration, target, easing)
self.tweens:newTween(start, duration, target, easing)
end
function GuiElement:newMovement(start, duration, x, y, easing)
self:newTween(start, duration, {x = x, y = y}, easing)
end
function GuiElement:newSwitch(start, bools)
self.tweens:newSwitch(start, bools)
end
function GuiElement:delayFocus(start)
self.tweens:newFunc(start, "focus", function ()
self:getFocus()
end)
end
2021-08-21 12:45:49 +02:00
-- 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:keypressed(key)
2021-08-31 10:56:56 +02:00
if (self.func ~= nil) then
self.func(key)
end
2021-08-21 12:45:49 +02:00
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