17#ifndef _FGE_C_GUIELEMENT_HPP_INCLUDED
18#define _FGE_C_GUIELEMENT_HPP_INCLUDED
20#include "FastEngine/fge_extern.hpp"
22#include "FastEngine/C_event.hpp"
23#include "FastEngine/C_tunnel.hpp"
24#include "FastEngine/graphic/C_renderTarget.hpp"
27#define FGE_GUI_ELEMENT_PRIORITY_LAST 0
28#define FGE_GUI_ELEMENT_PRIORITY_DEFAULT 50
29#define FGE_GUI_ELEMENT_PRIORITY_FIRST std::numeric_limits<fge::GuiElement::Priority>::max()
30#define FGE_SCENE_BAD_SID std::numeric_limits<fge::ObjectSid>::max()
35using ObjectSid = uint32_t;
38using ObjectDataShared = std::shared_ptr<fge::ObjectData>;
39using ObjectDataWeak = std::weak_ptr<fge::ObjectData>;
42class GuiElementHandler;
47 bool _recursive{
false};
48 std::size_t _index{0};
49 fge::Vector2f _mouseGuiPosition;
50 fge::Vector2i _mousePosition;
52 std::vector<fge::ObjectDataShared>* _keepAliveObject{
nullptr};
61 SIZE_DEFAULT = SIZE_AUTO
64 fge::Vector2f _fixedSize{0.0f, 0.0f};
65 fge::Vector2<fge::DynamicSize::SizeModes> _sizeMode{fge::DynamicSize::SizeModes::SIZE_DEFAULT,
66 fge::DynamicSize::SizeModes::SIZE_DEFAULT};
67 fge::Vector2f _offset{0.0f, 0.0f};
69 [[nodiscard]]
inline fge::Vector2f getSize(fge::Vector2f
const& position, fge::Vector2f
const& targetSize)
const
73 switch (this->_sizeMode.x)
75 case SizeModes::SIZE_FIXED:
76 size.x = this->_fixedSize.x;
78 case SizeModes::SIZE_AUTO:
79 size.x = (targetSize.x - position.x) + this->_offset.x;
85 switch (this->_sizeMode.y)
87 case SizeModes::SIZE_FIXED:
88 size.y = this->_fixedSize.y;
90 case SizeModes::SIZE_AUTO:
91 size.y = (targetSize.y - position.y) + this->_offset.y;
122 using Priority = uint8_t;
125 explicit GuiElement(fge::GuiElement::Priority priority) :
126 _g_priority(priority)
143 void setGuiScale(fge::Vector2f
const& scale) { this->_g_scale = scale; }
149 [[nodiscard]] fge::Vector2f
const&
getGuiScale()
const {
return this->_g_scale; }
157 void setPriority(fge::GuiElement::Priority priority)
const { this->_g_priority = priority; }
163 [[nodiscard]] fge::GuiElement::Priority
getPriority()
const {
return this->_g_priority; }
174 if (element ==
nullptr)
208 inline static void setGlobalGuiScale(fge::Vector2f
const& scale)
210 fge::GuiElement::_GlobalGuiScale = scale;
211 fge::GuiElement::_onGlobalGuiScaleChange.call(scale);
213 inline static fge::Vector2f
const& getGlobalGuiScale() {
return fge::GuiElement::_GlobalGuiScale; }
216 mutable fge::GuiElement::Priority _g_priority{FGE_GUI_ELEMENT_PRIORITY_DEFAULT};
217 fge::Vector2f _g_scale{1.0f, 1.0f};
220 static fge::Vector2f _GlobalGuiScale;
244 inline void setEvent(
fge::Event& event) { this->g_event = &event; }
245 [[nodiscard]]
inline fge::Event& getEvent() {
return *this->g_event; }
246 [[nodiscard]]
inline fge::Event const& getEvent()
const {
return *this->g_event; }
247 inline void setRenderTarget(
fge::RenderTarget const& target) { this->g_target = ⌖ }
248 [[nodiscard]]
inline fge::RenderTarget const& getRenderTarget()
const {
return *this->g_target; }
250 void setEventCallback();
252 void onMouseWheelScrolled(
fge::Event const& evt, SDL_MouseWheelEvent
const& arg);
253 void onMouseButtonPressed(
fge::Event const& evt, SDL_MouseButtonEvent
const& arg);
254 void onMouseButtonReleased(
fge::Event const& evt, SDL_MouseButtonEvent
const& arg);
255 void onMouseMoved(
fge::Event const& evt, SDL_MouseMotionEvent
const& arg);
257 void checkViewSize();
261 fge::Vector2f _lastSize{0.0f, 0.0f};
287 [[maybe_unused]] SDL_EventType evtType,
293 {this->g_rect._width * this->_g_scale.x, this->g_rect._height * this->_g_scale.y}};
294 if (rect.contains(context._mouseGuiPosition))
296 context._prioritizedElement =
this;
301 void setRectangle(
fge::RectFloat const& rect) { this->g_rect = rect; }
302 fge::RectFloat const& getRectangle()
const {
return this->g_rect; }
323 [[maybe_unused]] SDL_EventType evtType,
328 context._prioritizedElement =
this;
349 if (context._recursive)
351 this->verifyRecursively(evt, evtType, context);
357 context._prioritizedElement =
this;
368 context2._mouseGuiPosition = context._mouseGuiPosition;
369 context2._mousePosition = context._mousePosition;
370 context2._handler = context._handler;
371 context2._keepAliveObject = context._keepAliveObject;
373 for (std::size_t i = 0; i < this->_elements.getGatesSize(); ++i)
376 this->_elements[i]->onGuiVerify(evt, evtType, context2);
379 if (context2._prioritizedElement !=
nullptr)
381 if (context2._prioritizedElement->isRecursive())
383 context2._recursive =
true;
384 context2._prioritizedElement->onGuiVerify(evt, evtType, context2);
388 context._prioritizedElement = context2._prioritizedElement;
389 context._index = context2._index;
This class is used to handle callbacks in a safe way.
Definition C_callback.hpp:189
This class is a wrapper for SDL events.
Definition C_event.hpp:59
A GUI element that verify a list of GUI elements.
Definition C_guiElement.hpp:339
void onGuiVerify(fge::Event const &evt, SDL_EventType evtType, fge::GuiElementContext &context) override
Function called to verify if the element is hovered by the mouse.
Definition C_guiElement.hpp:347
A GUI element that does not any verification bounds of the mouse the mouse.
Definition C_guiElement.hpp:314
void onGuiVerify(fge::Event const &evt, SDL_EventType evtType, fge::GuiElementContext &context) override
Function called to verify if the element is hovered by the mouse.
Definition C_guiElement.hpp:322
A class to handle highest priority selection of GUI elements.
Definition C_guiElement.hpp:235
A GUI element that verify if the mouse is inside a rectangle.
Definition C_guiElement.hpp:274
void onGuiVerify(fge::Event const &evt, SDL_EventType evtType, fge::GuiElementContext &context) override
Function called to verify if the element is hovered by the mouse.
Definition C_guiElement.hpp:286
Definition C_guiElement.hpp:224
bool isRecursive() const final
Check if this GuiElement is recursive.
Definition C_guiElement.hpp:226
A base class for all GUI elements.
Definition C_guiElement.hpp:120
fge::Vector2f const & getGuiScale() const
Get the scale of the element.
Definition C_guiElement.hpp:149
fge::CallbackHandler< fge::Event const &, SDL_MouseMotionEvent const &, fge::GuiElementContext & > _onGuiMouseMoved
Callback called when the element is verified and the mouse is moved.
Definition C_guiElement.hpp:205
fge::CallbackHandler< fge::Event const &, SDL_MouseButtonEvent const &, fge::GuiElementContext & > _onGuiMouseButtonReleased
Callback called when the element is verified and a mouse button is released.
Definition C_guiElement.hpp:203
fge::CallbackHandler< fge::Event const &, SDL_MouseButtonEvent const &, fge::GuiElementContext & > _onGuiMouseButtonPressed
Callback called when the element is verified and the mouse is pressed.
Definition C_guiElement.hpp:201
virtual bool isRecursive() const
Check if this GuiElement is recursive.
Definition C_guiElement.hpp:137
void setPriority(fge::GuiElement::Priority priority) const
Set the priority of the element.
Definition C_guiElement.hpp:157
bool verifyPriority(fge::GuiElement *element) const
Verify if the priority of the element is higher than the given element.
Definition C_guiElement.hpp:172
fge::CallbackHandler< fge::Event const &, SDL_MouseWheelEvent const &, fge::GuiElementContext & > _onGuiMouseWheelScrolled
Callback called when the element is verified and the mouse wheel is scrolled.
Definition C_guiElement.hpp:199
virtual void onGuiVerify(fge::Event const &evt, SDL_EventType evtType, fge::GuiElementContext &context)=0
Function called to verify if the element is hovered by the mouse.
void setGuiScale(fge::Vector2f const &scale)
Set the scale of the element.
Definition C_guiElement.hpp:143
fge::GuiElement::Priority getPriority() const
Get the priority of the element.
Definition C_guiElement.hpp:163
Definition C_renderTarget.hpp:56
This class is a useful utility to "link" multiple objects around.
Definition C_subscription.hpp:150
Definition C_tunnel.hpp:30
Definition C_guiElement.hpp:56
Definition C_guiElement.hpp:45