Polyphase Game Engine
Loading...
Searching...
No Matches
Widget.h
Go to the documentation of this file.
1#pragma once
2
3#include "PolyphaseAPI.h"
4#include "Rect.h"
5#include "EngineTypes.h"
6#include "Factory.h"
7#include "AssetRef.h"
8
9#include "Nodes/Node.h"
10
11#include "Maths.h"
12#include <vector>
13#include <string>
14
16
17class Widget;
18
19enum class AnchorMode : uint8_t
20{
43
44enum MarginFlag : uint8_t
45{
46 MF_Left = 1 << 0,
47 MF_Top = 1 << 1,
48 MF_Right = 1 << 2,
49 MF_Bottom = 1 << 3
50};
51
53{
54public:
55
57
58 Widget();
59
60 virtual void Start() override;
61 virtual void GatherProperties(std::vector<Property>& outProps) override;
62 virtual void SetVisible(bool visible) override;
63
64 // Refresh any data used for rendering based on this widget's state. Use dirty flag.
65 // Recursively update children.
66 virtual void PrepareTick(std::vector<NodePtrWeak>& outTickNodes, bool game, bool recurse) override;
67 virtual void Render() override;
68 virtual VertexType GetVertexType() const override;
69
70 virtual bool IsWidget() const override;
71 Widget* GetParentWidget();
72 const Widget* GetParentWidget() const;
73 Widget* GetChildWidget(int32_t index);
74
75 virtual void PreRender();
76
77 Rect GetRect();
78
79 void SetX(float x);
80 void SetY(float y);
81 void SetWidth(float width);
82 void SetHeight(float height);
83
84 void SetXRatio(float x);
85 void SetYRatio(float y);
86 void SetWidthRatio(float width);
87 void SetHeightRatio(float height);
88
89 void SetLeftMargin(float left);
90 void SetTopMargin(float top);
91 void SetRightMargin(float right);
92 void SetBottomMargin(float bottom);
93
94 virtual void SetPosition(float x, float y);
95 virtual void SetDimensions(float width, float height);
96 void SetPosition(glm::vec2 position);
97 void SetDimensions(glm::vec2 dimensions);
98 void SetRect(float x, float y, float width, float height);
99 void SetRect(glm::vec2 position, glm::vec2 dimensions);
100 void SetRect(Rect rect);
101
102 void SetRatios(float x, float y, float width, float height);
103 void SetMargins(float left, float top, float right, float bottom);
104
105 // Offset may be stored in pixels or parent-percentage.
106 // Use SetPosition() to set offset from pixel space.
107 // Use GetPosition(), GetX(), GetY(), to get offset in pixel space.
108 void SetOffset(float x, float y);
109 glm::vec2 GetOffset() const;
110
111 // Size may be stored in pixels or parent-percentage.
112 // Use SetDimensions() to set size from pixel space.
113 // Use GetDimensions(), GetWidth(), GetHeight(), to get size in pixel space.
114 void SetSize(float x, float y);
115 glm::vec2 GetSize() const;
116
117 AnchorMode GetAnchorMode() const;
118 void SetAnchorMode(AnchorMode anchorMode);
119 bool AnchorStretchesX(AnchorMode mode) const;
120 bool AnchorStretchesY(AnchorMode mode) const;
121 inline bool StretchX() const { return AnchorStretchesX(mAnchorMode); }
122 inline bool StretchY() const { return AnchorStretchesY(mAnchorMode); }
123 glm::vec2 GetAnchorRatio() const;
124 void SetFullScreen();
125 void Centered();
126 void CenterPivot();
127
128 float GetX() const;
129 float GetY() const;
130 float GetWidth() const;
131 float GetHeight() const;
132
133 glm::vec2 GetPosition() const;
134 glm::vec2 GetDimensions() const;
135
136 void UpdateRect();
137 void UpdateColor();
138 void FitInsideParent();
139
140 float GetParentWidth() const;
141 float GetParentHeight() const;
142
143 virtual void SetColor(glm::vec4 color);
144 glm::vec4 GetColor() const;
145
146 void SetOpacity(uint8_t opacity);
147 uint8_t GetOpacity() const;
148
149 void SetOpacityFloat(float opacity);
150 float GetOpacityFloat() const;
151
152 virtual bool ShouldHandleInput();
153
154 virtual void MarkDirty();
155 void MarkClean();
156 bool IsDirty() const;
157
158 static float InterfaceToNormalized(float interfaceCoord, float interfaceSize);
159 static bool IsMouseInside(Rect rect);
160
161 bool ContainsMouse(bool testScissor = true) const;
162 virtual bool ContainsPoint(int32_t x, int32_t y);
163 void MoveToMousePosition();
164
165 const glm::mat3& GetTransform();
166 void SetRotation(float degrees);
167 float GetRotation() const;
168
169 void SetPivot(glm::vec2 pivot);
170 glm::vec2 GetPivot() const;
171
172 void SetScale(glm::vec2 scale);
173 glm::vec2 GetScale() const;
174 glm::vec2 GetAbsoluteScale() const { return mAbsoluteScale; }
175
176 bool IsScissorEnabled() const;
177 void EnableScissor(bool enable);
178 Rect GetScissorRect() const;
179
180 void SetUseGameResolution(bool use);
181 bool GetUseGameResolution() const;
182
183 // Tooltip
184 void SetTooltipName(const std::string& name);
185 const std::string& GetTooltipName() const;
186 void SetTooltipDescription(const std::string& description);
187 const std::string& GetTooltipDescription() const;
188 bool HasTooltip() const;
189
190protected:
191
193 static const char* sAnchorModeStrings[];
194
195 static bool HandlePropChange(Datum* datum, uint32_t index, const void* newValue);
196
197 virtual void SetParent(Node* parent) override;
198
199 float PixelsToRatioX(float x) const;
200 float PixelsToRatioY(float y) const;
201 float RatioToPixelsX(float x) const;
202 float RatioToPixelsY(float y) const;
203
206 void SetScissor(Rect& area);
207
208 Rect mRect; // The screen pos/dimensions that are computed on Update().
210 glm::mat3 mTransform;
211 glm::vec4 mColor;
212 glm::vec2 mOffset;
213 glm::vec2 mSize;
214 glm::vec2 mPivot;
215 glm::vec2 mScale;
216 glm::vec2 mAbsoluteScale;
221 bool mUseGameResolution = false;
222 uint8_t mOpacity;
223
224 // Tooltip
225 std::string mTooltipName;
227
228private:
229 bool mDirty[MAX_FRAMES] = {};
230};
Export macros for Polyphase Engine symbols.
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
VertexType
Definition Vertex.h:7
AnchorMode
Definition Widget.h:20
@ MidHorizontalStretch
@ MidVerticalStretch
MarginFlag
Definition Widget.h:45
@ MF_Bottom
Definition Widget.h:49
@ MF_Left
Definition Widget.h:46
@ MF_Right
Definition Widget.h:48
@ MF_Top
Definition Widget.h:47
Definition Datum.h:164
Definition Node.h:67
virtual void SetVisible(bool visible)
Definition Node.cpp:1456
virtual void SetParent(Node *parent)
Definition Node.cpp:2257
virtual void GatherProperties(std::vector< Property > &outProps) override
Definition Node.cpp:598
static bool HandlePropChange(Datum *datum, uint32_t index, const void *newValue)
Definition Node.cpp:74
virtual void Render()
Definition Node.cpp:583
virtual bool IsWidget() const
Definition Node.cpp:1531
virtual void Start()
Definition Node.cpp:459
virtual void PrepareTick(std::vector< NodePtrWeak > &outTickNodes, bool game, bool recurse)
Definition Node.cpp:525
virtual VertexType GetVertexType() const
Definition Node.cpp:593
Definition Rect.h:4
Definition SmartPointer.h:312
Definition Widget.h:53
glm::vec2 mOffset
Definition Widget.h:212
Rect mScissorRect
Definition Widget.h:209
void PopScissor()
glm::vec4 mColor
Definition Widget.h:211
bool StretchX() const
Definition Widget.h:121
Rect mRect
Definition Widget.h:208
glm::mat3 mTransform
Definition Widget.h:210
std::string mTooltipName
Definition Widget.h:225
glm::vec2 mSize
Definition Widget.h:213
void SetScissor(Rect &area)
uint8_t mActiveMargins
Definition Widget.h:219
glm::vec2 mAbsoluteScale
Definition Widget.h:216
std::string mTooltipDescription
Definition Widget.h:226
uint8_t mOpacity
Definition Widget.h:222
void PushScissor()
static WeakPtr< Widget > sWidgetToClean
Definition Widget.h:192
bool StretchY() const
Definition Widget.h:122
bool mUseScissor
Definition Widget.h:220
glm::vec2 mPivot
Definition Widget.h:214
DECLARE_NODE(Widget, Node)
glm::vec2 mScale
Definition Widget.h:215
float mRotation
Definition Widget.h:217
AnchorMode mAnchorMode
Definition Widget.h:218
glm::vec2 GetAbsoluteScale() const
Definition Widget.h:174