2022-08-12 10:50:42 +02:00
|
|
|
local Rect = require "framework.classes.2D.rect"
|
2021-12-04 13:18:54 +01:00
|
|
|
local GuiElement = Rect:extend()
|
|
|
|
|
2022-08-12 10:50:42 +02:00
|
|
|
local TweenManager = require "framework.classes.time"
|
2021-12-04 13:18:54 +01:00
|
|
|
|
|
|
|
function GuiElement:new(name, x, y, w, h)
|
|
|
|
GuiElement.super.new(self, x, y, w, h)
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
self.isVisible = true
|
|
|
|
self.screen = nil
|
|
|
|
|
|
|
|
self.depth = 10
|
|
|
|
|
|
|
|
self.tweens = TweenManager(self)
|
|
|
|
self:initWrapper()
|
|
|
|
|
|
|
|
self:register()
|
|
|
|
end
|
|
|
|
|
|
|
|
function GuiElement:initWrapper()
|
2022-07-27 21:57:34 +02:00
|
|
|
self.scene = core.scenemanager.nextScene or core.scenemanager.currentScene
|
2021-12-04 13:18:54 +01:00
|
|
|
self.gui = self.scene.gui
|
|
|
|
self.assets = self.scene.assets
|
|
|
|
end
|
|
|
|
|
|
|
|
function GuiElement:setKeyPressAction(func)
|
|
|
|
self.func = func
|
|
|
|
end
|
|
|
|
|
|
|
|
function GuiElement:register()
|
|
|
|
self.creationId = self.gui:addElement(self.name, self)
|
|
|
|
end
|
|
|
|
|
|
|
|
function GuiElement:destroy()
|
|
|
|
self.gui:deleteElement(self.name)
|
|
|
|
if (self.screen ~= nil) then
|
|
|
|
self.screen:deleteElement(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
|
|
|
|
|
2022-07-27 21:57:34 +02:00
|
|
|
function GuiElement:getFocus(widgetId, page)
|
|
|
|
self.gui:setFocus(self.name, widgetId, page)
|
2021-12-04 13:18:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function GuiElement:haveFocus()
|
|
|
|
return (self.gui.focusedElement == self.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
function GuiElement:looseFocus()
|
|
|
|
if (self:haveFocus()) then
|
|
|
|
self.gui:removeFocus()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 21:57:34 +02:00
|
|
|
function GuiElement:setSubFocus()
|
|
|
|
-- Useless for basic element
|
|
|
|
end
|
|
|
|
|
|
|
|
function GuiElement:isTransforming()
|
|
|
|
return self.tweens:haveTween()
|
|
|
|
end
|
|
|
|
|
2021-12-04 13:18:54 +01:00
|
|
|
-- UPDATE FUNCTIONS
|
|
|
|
-- Update the menu every game update
|
|
|
|
|
|
|
|
-- External update function
|
|
|
|
function GuiElement:updateElement(dt)
|
|
|
|
self:update(dt)
|
|
|
|
self.tweens:update(dt)
|
|
|
|
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:newFunc(start, name, func)
|
|
|
|
self.tweens:newFunc(start, name, func)
|
|
|
|
end
|
|
|
|
|
|
|
|
function GuiElement:delayFocus(start)
|
|
|
|
self.tweens:newFunc(start, "focus", function ()
|
|
|
|
self:getFocus()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- DRAW FUNCTIONS
|
|
|
|
-- Draw the menu and its content
|
|
|
|
|
|
|
|
function GuiElement:redraw()
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function GuiElement:drawElement()
|
|
|
|
self:draw()
|
|
|
|
end
|
|
|
|
|
|
|
|
function GuiElement:draw()
|
|
|
|
-- nothing here
|
|
|
|
end
|
|
|
|
|
|
|
|
-- KEYBOARD FUNCTIONS
|
|
|
|
-- Handle key press
|
|
|
|
|
|
|
|
function GuiElement:keypressed(key)
|
|
|
|
if (self.func ~= nil) then
|
|
|
|
self.func(key)
|
|
|
|
end
|
|
|
|
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
|