GigaPasjans
Loading...
Searching...
No Matches
Component.h
1// Plik utworzony przez Piotra ChudziƄskiego w dniu 11.04.2025
2#pragma once
3
4#include <foundation/Common.h>
5#include <foundation/Math/Box.h>
6#include <foundation/Math/Math.h>
7#include "IActionProvider.h"
8
9#include "DrawContext.h"
10
11#define COMPONENT_CENTER_X INT32_MIN
12
13namespace Andromenda
14{
15 using ComponentCallback = std::function<void(const String &)>;
16 class Surface;
17
23 {
24 protected:
25 friend class Surface;
26 friend class ComponentRegistry;
27
32 bool m_IsActive = false;
33
38 std::atomic<bool> m_Dirty = false;
39
45
50 ComponentCallback m_Callback;
51
56 Surface *m_OwnerSurface = nullptr;
57
62
68 int32 getCenteredX();
69
75 virtual Int2 getSize() = 0;
76
81 void markAsDirty();
82
83 public:
84 ~Component() override;
85
91 bool UpdateState();
92
98 void SetActive(bool active);
99
105 Box GetBounds();
106
112 void SetPosition(const Int2 &position);
113
115 void ProvideActions(ActionRegistry &registry) override = 0;
116
121 [[nodiscard]] virtual bool IsSelectable() const = 0;
122
127 virtual void Draw(DrawContext &context) = 0;
128 };
129
134 class ComponentRegistry : protected std::vector<std::pair<String, Component *>>
135 {
136 protected:
141 std::vector<std::pair<String, Component *>> &getComponents();
142
147 ComponentCallback m_Callback;
148
149 public:
150 virtual ~ComponentRegistry() = default;
155 virtual void RegisterComponent(const String &name, Component *component);
156
162 virtual void UnregisterComponent(const String &name);
163
168 Component *GetComponent(const String &name);
169
174 virtual void Clear();
175
180 void PutAtTop(const String &id);
181 };
182
183} // namespace Andromenda
Represent registry of Action objects.
Definition ActionRegistry.h:74
Represents a renderable identified object.
Definition Component.h:23
void ProvideActions(ActionRegistry &registry) override=0
Provides own actions to the ActionRegistry.
virtual void Draw(DrawContext &context)=0
Draws self.
std::atomic< bool > m_Dirty
A value indicating, whether object is dirty.
Definition Component.h:38
void SetActive(bool active)
Sets m_IsActive to given state.
Definition Components.cpp:9
Surface * m_OwnerSurface
Owning surface.
Definition Component.h:56
int32 getCenteredX()
Returns X aligned to the center.
Definition Components.cpp:25
virtual Int2 getSize()=0
Returns size of a component.
ComponentCallback m_Callback
Action component callback.
Definition Component.h:50
bool m_IsActive
A value indicating, whether object is being pointed by pointer.
Definition Component.h:32
void SetPosition(const Int2 &position)
Sets position to given.
Definition Components.cpp:47
Int2 m_Position
Component position.
Definition Component.h:61
Box GetBounds()
Returns Component bounds. Position is already aligned.
Definition Components.cpp:27
bool UpdateState()
Checks whether component is dirty, if yes then returns true and sets dirty state to false,...
Definition Components.cpp:36
virtual bool IsSelectable() const =0
Returns value indicating, whether component is selectable.
void markAsDirty()
Sets m_Dirty to true.
Definition Components.cpp:32
String m_Id
Object identifier, that is unique per ComponentRegistry.
Definition Component.h:44
ComponentRegistry doesn't take over ownership of components.
Definition Component.h:135
virtual void Clear()
Deletes all components from list. It does not destroy objects.
Definition Components.cpp:49
void PutAtTop(const String &id)
Move component mapped to given unique name to the top of a surface.
Definition Components.cpp:62
virtual void RegisterComponent(const String &name, Component *component)
Registers given component to given unique name.
Definition Components.cpp:11
ComponentCallback m_Callback
Action component callback.
Definition Component.h:147
std::vector< std::pair< String, Component * > > & getComponents()
Returns reference to a list of components.
Definition Components.cpp:23
virtual void UnregisterComponent(const String &name)
Unregister component mapped to given unique name.
Definition Components.cpp:18
Component * GetComponent(const String &name)
Finds component with given unique name.
Definition Components.cpp:51
Represents an object that can provide actions.
Definition IActionProvider.h:12
The renderer of component registry.
Definition Surface.h:28
Represents a mutable sequence of characters, providing various member functions for string manipulati...
Definition String.h:16
A utility which contains all essential class instances and functions for rendering objects.
Definition DrawContext.h:14
Represents a rectangular area defined by a position and size, using signed 32-bit integers.
Definition Box.h:11
Represents a two component vector. Components are 32-bit and they are signed.
Definition Int2.h:11