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
47
48enum MarginFlag : uint8_t
49{
50 MF_Left = 1 << 0,
51 MF_Top = 1 << 1,
52 MF_Right = 1 << 2,
53 MF_Bottom = 1 << 3
54};
55
57{
58public:
59
61
62 Widget();
63
64 virtual void Start() override;
65 virtual void GatherProperties(std::vector<Property>& outProps) override;
66 virtual void SetVisible(bool visible) override;
67
68 // Refresh any data used for rendering based on this widget's state. Use dirty flag.
69 // Recursively update children.
70 virtual void PrepareTick(std::vector<NodePtrWeak>& outTickNodes, bool game, bool recurse) override;
71 virtual void Render() override;
72 virtual VertexType GetVertexType() const override;
73
74 virtual bool IsWidget() const override;
75 Widget* GetParentWidget();
76 const Widget* GetParentWidget() const;
77 Widget* GetChildWidget(int32_t index);
78
79 virtual void PreRender();
80
81 Rect GetRect();
82
83 void SetX(float x);
84 void SetY(float y);
85 void SetWidth(float width);
86 void SetHeight(float height);
87
88 void SetXRatio(float x);
89 void SetYRatio(float y);
90 void SetWidthRatio(float width);
91 void SetHeightRatio(float height);
92
93 void SetLeftMargin(float left);
94 void SetTopMargin(float top);
95 void SetRightMargin(float right);
96 void SetBottomMargin(float bottom);
97
98 virtual void SetPosition(float x, float y);
99 virtual void SetDimensions(float width, float height);
100 void SetPosition(glm::vec2 position);
101 void SetDimensions(glm::vec2 dimensions);
102 void SetRect(float x, float y, float width, float height);
103 void SetRect(glm::vec2 position, glm::vec2 dimensions);
104 void SetRect(Rect rect);
105
106 void SetRatios(float x, float y, float width, float height);
107 void SetMargins(float left, float top, float right, float bottom);
108
109 // Offset may be stored in pixels or parent-percentage.
110 // Use SetPosition() to set offset from pixel space.
111 // Use GetPosition(), GetX(), GetY(), to get offset in pixel space.
112 void SetOffset(float x, float y);
113 glm::vec2 GetOffset() const;
114
115 // Size may be stored in pixels or parent-percentage.
116 // Use SetDimensions() to set size from pixel space.
117 // Use GetDimensions(), GetWidth(), GetHeight(), to get size in pixel space.
118 void SetSize(float x, float y);
119 glm::vec2 GetSize() const;
120
121 AnchorMode GetAnchorMode() const;
122 void SetAnchorMode(AnchorMode anchorMode);
123 bool AnchorStretchesX(AnchorMode mode) const;
124 bool AnchorStretchesY(AnchorMode mode) const;
125 bool AnchorFillsX(AnchorMode mode) const;
126 bool AnchorFillsY(AnchorMode mode) const;
127 inline bool StretchX() const { return AnchorStretchesX(mAnchorMode); }
128 inline bool StretchY() const { return AnchorStretchesY(mAnchorMode); }
129 inline bool FillsX() const { return AnchorFillsX(mAnchorMode); }
130 inline bool FillsY() const { return AnchorFillsY(mAnchorMode); }
131 glm::vec2 GetAnchorRatio() const;
132 void SetFullScreen();
133 void Centered();
134 void CenterPivot();
135
136 float GetX() const;
137 float GetY() const;
138 float GetWidth() const;
139 float GetHeight() const;
140
141 glm::vec2 GetPosition() const;
142 glm::vec2 GetDimensions() const;
143
144 virtual void UpdateRect();
145 void UpdateColor();
146 void FitInsideParent();
147
148#if EDITOR
149 // Editor helper: computes a letterboxed sub-region of the raw editor viewport
150 // that preserves the currently-selected GamePreview target aspect ratio.
151 // outVp is viewport-LOCAL (0-based within the raw viewport). Returns false
152 // when the letterbox doesn't apply (non-main screen, PIE-in-game-window,
153 // or no valid GamePreview preset).
154 static bool ComputeGamePreviewLetterbox(glm::uvec4& outVp, glm::vec2& outExtraScale);
155#endif
156
157 // Container-driven slot layout. When set, UpdateRect uses this rect
158 // (in parent-local coord space: mX/mY are absolute like parent->mRect)
159 // instead of the actual parent rect. Consumed on read — the container
160 // must re-set every tick, otherwise it clears itself.
161 void SetParentRectOverride(const Rect& rect);
162 void ClearParentRectOverride();
163 bool HasParentRectOverride() const { return mHasParentRectOverride; }
164
165 float GetParentWidth() const;
166 float GetParentHeight() const;
167
168 virtual void SetColor(glm::vec4 color);
169 glm::vec4 GetColor() const;
170
171 void SetOpacity(uint8_t opacity);
172 uint8_t GetOpacity() const;
173
174 void SetOpacityFloat(float opacity);
175 float GetOpacityFloat() const;
176
177 virtual bool ShouldHandleInput();
178
179 virtual void MarkDirty();
180 void MarkClean();
181 bool IsDirty() const;
182
183 static float InterfaceToNormalized(float interfaceCoord, float interfaceSize);
184 static bool IsMouseInside(Rect rect);
185
186 bool ContainsMouse(bool testScissor = true) const;
187 virtual bool ContainsPoint(int32_t x, int32_t y);
188 void MoveToMousePosition();
189
190 const glm::mat3& GetTransform();
191 void SetRotation(float degrees);
192 float GetRotation() const;
193
194 void SetPivot(glm::vec2 pivot);
195 glm::vec2 GetPivot() const;
196
197 void SetScale(glm::vec2 scale);
198 glm::vec2 GetScale() const;
199 glm::vec2 GetAbsoluteScale() const { return mAbsoluteScale; }
200
201 bool IsScissorEnabled() const;
202 void EnableScissor(bool enable);
203 Rect GetScissorRect() const;
204
205 void SetUseGameResolution(bool use);
206 bool GetUseGameResolution() const;
207
208 // Tooltip
209 void SetTooltipName(const std::string& name);
210 const std::string& GetTooltipName() const;
211 void SetTooltipDescription(const std::string& description);
212 const std::string& GetTooltipDescription() const;
213 bool HasTooltip() const;
214
215protected:
216
218 static const char* sAnchorModeStrings[];
219
220 static bool HandlePropChange(Datum* datum, uint32_t index, const void* newValue);
221
222 virtual void SetParent(Node* parent) override;
223
224 float PixelsToRatioX(float x) const;
225 float PixelsToRatioY(float y) const;
226 float RatioToPixelsX(float x) const;
227 float RatioToPixelsY(float y) const;
228
231 void SetScissor(Rect& area);
232
233 Rect mRect; // The screen pos/dimensions that are computed on Update().
236 glm::mat3 mTransform;
237 glm::vec4 mColor;
238 glm::vec2 mOffset;
239 glm::vec2 mSize;
240 glm::vec2 mPivot;
241 glm::vec2 mScale;
242 glm::vec2 mAbsoluteScale;
247 bool mUseGameResolution = false;
248 bool mHasParentRectOverride = false;
249 uint8_t mOpacity;
250
251 // Tooltip
252 std::string mTooltipName;
254
255private:
256 bool mDirty[MAX_FRAMES] = {};
257};
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:49
@ MF_Bottom
Definition Widget.h:53
@ MF_Left
Definition Widget.h:50
@ MF_Right
Definition Widget.h:52
@ MF_Top
Definition Widget.h:51
Definition Datum.h:169
Definition Node.h:67
virtual void SetVisible(bool visible)
Definition Node.cpp:1508
virtual void SetParent(Node *parent)
Definition Node.cpp:2338
virtual void GatherProperties(std::vector< Property > &outProps) override
Definition Node.cpp:618
static bool HandlePropChange(Datum *datum, uint32_t index, const void *newValue)
Definition Node.cpp:89
virtual void Render()
Definition Node.cpp:598
virtual bool IsWidget() const
Definition Node.cpp:1583
virtual void Start()
Definition Node.cpp:474
virtual void PrepareTick(std::vector< NodePtrWeak > &outTickNodes, bool game, bool recurse)
Definition Node.cpp:540
virtual VertexType GetVertexType() const
Definition Node.cpp:613
Definition Rect.h:4
Definition SmartPointer.h:312
Definition Widget.h:57
glm::vec2 mOffset
Definition Widget.h:238
Rect mScissorRect
Definition Widget.h:234
void PopScissor()
glm::vec4 mColor
Definition Widget.h:237
bool StretchX() const
Definition Widget.h:127
Rect mParentRectOverride
Definition Widget.h:235
Rect mRect
Definition Widget.h:233
glm::mat3 mTransform
Definition Widget.h:236
std::string mTooltipName
Definition Widget.h:252
glm::vec2 mSize
Definition Widget.h:239
void SetScissor(Rect &area)
uint8_t mActiveMargins
Definition Widget.h:245
glm::vec2 mAbsoluteScale
Definition Widget.h:242
std::string mTooltipDescription
Definition Widget.h:253
uint8_t mOpacity
Definition Widget.h:249
void PushScissor()
bool FillsY() const
Definition Widget.h:130
bool HasParentRectOverride() const
Definition Widget.h:163
static WeakPtr< Widget > sWidgetToClean
Definition Widget.h:217
bool StretchY() const
Definition Widget.h:128
bool mUseScissor
Definition Widget.h:246
glm::vec2 mPivot
Definition Widget.h:240
DECLARE_NODE(Widget, Node)
bool FillsX() const
Definition Widget.h:129
glm::vec2 mScale
Definition Widget.h:241
float mRotation
Definition Widget.h:243
AnchorMode mAnchorMode
Definition Widget.h:244
glm::vec2 GetAbsoluteScale() const
Definition Widget.h:199