Touch Interface Designer 2025.2
Create touch interface with a visual editor and manage all mobile inputs like gestures
Loading...
Searching...
No Matches
SLayoutComponent.h
Go to the documentation of this file.
1// Copyright 2024 Lost in Game Studio. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "Widgets/SCompoundWidget.h"
7#include "Widgets/Layout/SConstraintCanvas.h"
8
10enum class EInteractionType : uint8;
12class SConstraintCanvas;
13class SOverlay;
14class SRetainerWidget;
15
17{
18 TSharedPtr<SWidget> DecoratorWidget;
19 TEnumAsByte<EHorizontalAlignment> HorizontalAlignment;
20 TEnumAsByte<EVerticalAlignment> VerticalAlignment;
21 int32 ZOrder;
24
25 FDecorator(TSharedPtr<SWidget> InDecorator, const int32 DesiredZOrder, const bool bVisibleOnPress, const bool bVisibleOnRelease)
26 : DecoratorWidget(InDecorator)
27 , HorizontalAlignment(HAlign_Fill)
28 , VerticalAlignment(VAlign_Fill)
29 , ZOrder(DesiredZOrder)
30 , bShowOnPress(bVisibleOnPress)
31 , bShowOnRelease(bVisibleOnRelease)
32 {
33
34 }
35
36 FDecorator(TSharedPtr<SWidget> InDecorator, TEnumAsByte<EHorizontalAlignment> InHorizontalAlignment, TEnumAsByte<EVerticalAlignment> InVerticalAlignment, const int32 DesiredZOrder, const bool bVisibleOnPress, const bool bVisibleOnRelease)
37 : DecoratorWidget(InDecorator)
38 , HorizontalAlignment(InHorizontalAlignment)
39 , VerticalAlignment(InVerticalAlignment)
40 , ZOrder(DesiredZOrder)
41 , bShowOnPress(bVisibleOnPress)
42 , bShowOnRelease(bVisibleOnRelease)
43 {
44
45 }
46};
47
48DECLARE_MULTICAST_DELEGATE_ThreeParams(FOnTouchDelegate, const FGeometry&, const FGeometry&, const FPointerEvent&)
49DECLARE_MULTICAST_DELEGATE_ThreeParams(FOnTickDelegate, const FGeometry&, double, float)
50
54class TOUCHINTERFACE_API SLayoutComponent : public SCompoundWidget
55{
56public:
58 : _WidgetSlot(nullptr)
59 , _LayoutWidget(nullptr)
60 , _Parent(nullptr)
61 , _bSupportChildren(false)
62 , _InputPriority(0)
63 , _RecenterOffset(ForceInitToZero)
64 , _bConsumeInput(false)
65 , _bRecenterOnTouch(false)
66 , _TopLeftPosition(ForceInitToZero)
67 , _InteractionSize(100.0f)
68 , _bRecenterHitBox(false)
69 , _bIsEditor(false)
70 {
71 }
72
73 SLATE_ARGUMENT(SConstraintCanvas::FSlot*, WidgetSlot)
74 SLATE_ARGUMENT(TSharedPtr<STouchInterface>, LayoutWidget)
75 SLATE_ARGUMENT(TSharedPtr<SLayoutComponent>, Parent)
76 SLATE_ARGUMENT(TArray<FDecorator>, Decorators)
77 SLATE_ARGUMENT(bool, bSupportChildren)
78 SLATE_ARGUMENT(int32, InputPriority)
79 SLATE_ARGUMENT(FVector2D, RecenterOffset)
80 SLATE_ARGUMENT(EInteractionType, InteractionShape)
81 SLATE_ARGUMENT(bool, bConsumeInput)
82 SLATE_ARGUMENT(bool, bRecenterOnTouch)
83 SLATE_ARGUMENT(FVector4f, AnchorOffset)
84 SLATE_ARGUMENT(FVector2D, TopLeftPosition)
85 SLATE_ARGUMENT(FVector2D, Size)
86 SLATE_ARGUMENT(float, InteractionSize)
87 SLATE_ARGUMENT(bool, bRecenterHitBox)
88 SLATE_ARGUMENT(FName, ComponentName)
89 SLATE_ARGUMENT(bool, bIsEditor)
90
91 SLATE_END_ARGS()
92
94
95 virtual ~SLayoutComponent();
96
97 //Constructs this widget with InArgs
98 void Construct(const FArguments& InArgs);
99
100 void Destruct();
101
102 EActiveTimerReturnType PostGeometryInitialization(double InCurrentTime, float InDeltaTime);
103
104 //Constructs UUserWidget (Widget Decorator)
105 void ConstructWidget() const;
106
107 //Get position of widget in layout space
108 FVector2D GetTopLeftPosition() const { return LayoutTopLeftPosition; }
109
110 FVector2D GetSize() const { return Size; }
111
112 TSharedPtr<STouchInterfaceRuntime> GetRuntimeTouchInterfaceWidget() const;
113
114 int32 GetInputPriority() const { return InputPriority; }
115
116
117 void SetDecorators(TArray<FDecorator> NewDecorators);
118 void AddDecorator(TSharedPtr<SWidget> Decorator);
119 void InsertDecorator(TSharedPtr<SWidget> Decorator, int32 Index);
120 void RemoveDecorator(TSharedPtr<SWidget> DecoratorToRemove);
121
122 void ShowDecorator(TSharedPtr<SWidget> DecoratorWidget);
123 void HideDecorator(TSharedPtr<SWidget> DecoratorWidget);
124
125 //TODO: Switch decorator position. Decorator at index 4 moves to index 2
126
127
128 //Returns true if consume input
129 virtual bool ProcessPressEvent(const FGeometry& LayoutGeometry, const FPointerEvent& PointerEvent);
130 virtual void ProcessMoveEvent(const FGeometry& LayoutGeometry, const FPointerEvent& PointerEvent);
131 virtual void ProcessReleaseEvent(const FGeometry& LayoutGeometry, const FPointerEvent& PointerEvent);
132
133 //Begin SWidget
134 virtual FVector2D ComputeDesiredSize(float LayoutScaleMultiplier) const override { return Size; }
135 virtual void Tick(const FGeometry& AllottedGeometry, const double InCurrentTime, const float InDeltaTime) override;
136 virtual int32 OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const override;
137 //End SWidget
138
139 //TODO: Remove double click
140
141 virtual void PaintDebug(const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle) const;
142
143
144 //Called by STouchInterfaceLayout when geometry change
145 virtual void UpdateScaleFactor(const FGeometry& LayoutGeometry, const float& ScaleFactor);
146
147 //Mainly called by parent (when parent layout top left position change)
148 //virtual void UpdateLayoutTopLeftLocation(FVector2D ParentTopLeftLocation);
149
150 virtual FVector2D Recenter(const FVector2D AbsoluteLocation);
151
152 virtual void ResetPosition();
153
154 //TODO: Smooth refresh for position like geometry or orientation change
155
156 //TODO: Use FRuntimeVectorCurve
157 void Animate();
158
159 // Remove all UUserWidget from container and rebuild
160 void FullRebuildWidgetDecorator();
161
162 virtual void SetAnchor(const FAnchors NewAnchors);
163
164 virtual void SetSize(const FVector2D NewSize);
165
166 virtual void SetAnchorOffset(const FVector4f Offset);
167
168 virtual void SetAlignment(const FVector2D NewAlignment);
169
170 virtual void SetInteractionType(const EInteractionType NewType);
171
172 virtual void SetInteractionSize(const float Value) { InteractionSize = Value; }
173
174 virtual void SetInputPriority(const int32 Value) { InputPriority = Value; }
175
176
179 virtual void SetPositionFromTopLeft(const FVector2D NewPosition);
180
181 void SetDrawDebugEnabled(const bool bEnable) { bDrawDebug = bEnable; }
182
183 void SetDebugType(const int32 InDebugType) { DebugType = InDebugType; }
184
185 virtual bool IsUnderLocation(const FVector2D AbsoluteLocation);
186
187 virtual bool CanInteract() const;
188
189 virtual bool IsPressed() const { return bIsPressed; }
190
191 virtual void BlockInput(const bool bBlock) { bBlockInput = bBlock; }
192
193
194 FVector2D ScreenSpaceToLayoutSpace(const FVector2D Location);
195 FVector2D LocalSpaceToNormalized(const FVector2D Location) const;
196
197 SConstraintCanvas::FSlot* CreateSlot(const FAnchors Anchor, const FVector2D Alignment);
198 void AddChild(TSharedPtr<SLayoutComponent> Component);
199 void AddChild(TSharedPtr<SLayoutComponent> Component, const FAnchors Anchor, const FVector2D Alignment);
200 void RemoveChild(TSharedPtr<SLayoutComponent> Component);
201
202#if WITH_EDITOR
203 virtual void PaintOutline(const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle) const;
204
205 virtual void Select() { bDrawSelectOutline = true; }
206 virtual void Unselect() { bDrawSelectOutline = false; }
207
208 virtual void Hover() { bDrawHoverOutline = true; }
209 virtual void UnHover() { bDrawHoverOutline = false; }
210#endif
211
212protected:
213 void SetSlotPosition(const FVector2D& SlotPosition) const;
214 void SetSlotSize(const FVector2D SlotSize) const;
215
216private:
217 FVector2D ComponentLocationToTopLeftLocation(const FVector2D InComponentLocation) const;
218
219 FVector2D TopLeftLocationToComponentLocation(const FVector2D TopLeftLocation) const;
220
221public:
222 FOnTouchDelegate OnPress;
223 FOnTouchDelegate OnMove;
224 FOnTouchDelegate OnRelease;
225 FOnTickDelegate OnTick;
226
227protected:
231
232 //Position of component in layout space
234
235 //Offset from pivot to center of geometry
236 FVector2D PivotOffset;
237
238 //Offset from origin to recenter location
239 FVector2D RecenterOffset;
240
241 FVector2D Size;
242
243 FVector2D ScaledSize;
244
246
247private:
248 TSharedPtr<SConstraintCanvas> CanvasWidget;
249
250 TSharedPtr<SRetainerWidget> RetainerWidget;
251
252 TSharedPtr<SOverlay> OverlayWidget;
253
254 TSharedPtr<SLayoutComponent> Parent;
255
256 TArray<TSharedPtr<SLayoutComponent>> Children;
257
258 TArray<FDecorator> Decorators;
259
260 TSharedPtr<STouchInterface> LayoutWidget;
261
262 SConstraintCanvas::FSlot* WidgetSlot;
263
264 TArray<int32> AffectedFingerIndex;
265
266 uint8 bForceRefresh:1;
267
268 int32 DebugType;
269
270 //FBox2D Box2DRepresentation;
271
272 //TODO: Implement Slate Attribute (See SColorBlock for example)
273 //TSlateAttribute<UInputDecorator*> Decorator;
274 //TSlateAttribute<FVector2D> RecenterOffset;
275
276 EInteractionType InteractionShape;
277
278 bool bConsumeInput;
279 bool bRecenterOnTouch;
280 bool bRecenterHitBox;
281 bool bDrawDebug;
282 bool bBlockInput;
283 bool bIsPressed;
284 bool bIsEditorWidget;
285
286 FVector4f AnchorOffset;
287
288 FVector2D TopLeftPosition;
289
290 float InteractionSize;
291
292 FName ComponentName;
293
294#if WITH_EDITORONLY_DATA
295 uint8 bDrawSelectOutline:1;
296 uint8 bDrawHoverOutline:1;
297#endif
298};
EInteractionType
Definition LayoutComponent.h:32
DECLARE_MULTICAST_DELEGATE_ThreeParams(FOnTouchDelegate, const FGeometry &, const FGeometry &, const FPointerEvent &) DECLARE_MULTICAST_DELEGATE_ThreeParams(FOnTickDelegate
Definition SLayoutComponent.h:55
void SetDebugType(const int32 InDebugType)
Definition SLayoutComponent.h:183
SLATE_BEGIN_ARGS(SLayoutComponent)
Definition SLayoutComponent.h:57
FOnTickDelegate OnTick
Definition SLayoutComponent.h:225
FVector2D LayoutTopLeftPosition
Definition SLayoutComponent.h:233
FVector2D Size
Definition SLayoutComponent.h:241
virtual bool IsPressed() const
Definition SLayoutComponent.h:189
int32 InputPriority
Definition SLayoutComponent.h:245
FOnTouchDelegate OnPress
Definition SLayoutComponent.h:222
virtual void SetInputPriority(const int32 Value)
Definition SLayoutComponent.h:174
FVector2D GetSize() const
Definition SLayoutComponent.h:110
void SetDrawDebugEnabled(const bool bEnable)
Definition SLayoutComponent.h:181
FVector2D GetTopLeftPosition() const
Definition SLayoutComponent.h:108
FVector2D RecenterOffset
Definition SLayoutComponent.h:239
virtual void BlockInput(const bool bBlock)
Definition SLayoutComponent.h:191
FOnTouchDelegate OnMove
Definition SLayoutComponent.h:223
virtual void SetInteractionSize(const float Value)
Definition SLayoutComponent.h:172
FOnTouchDelegate OnRelease
Definition SLayoutComponent.h:224
FVector2D ComponentLocation
Definition SLayoutComponent.h:230
int32 GetInputPriority() const
Definition SLayoutComponent.h:114
SLayoutComponent()
Definition SLayoutComponent.cpp:17
FVector2D ScaledSize
Definition SLayoutComponent.h:243
FVector2D PivotOffset
Definition SLayoutComponent.h:236
virtual FVector2D ComputeDesiredSize(float LayoutScaleMultiplier) const override
Definition SLayoutComponent.h:134
Definition STouchInterface.h:43
Definition STouchInterfaceRuntime.h:30
FDecorator(TSharedPtr< SWidget > InDecorator, TEnumAsByte< EHorizontalAlignment > InHorizontalAlignment, TEnumAsByte< EVerticalAlignment > InVerticalAlignment, const int32 DesiredZOrder, const bool bVisibleOnPress, const bool bVisibleOnRelease)
Definition SLayoutComponent.h:36
bool bShowOnPress
Definition SLayoutComponent.h:22
TEnumAsByte< EHorizontalAlignment > HorizontalAlignment
Definition SLayoutComponent.h:19
bool bShowOnRelease
Definition SLayoutComponent.h:23
FDecorator(TSharedPtr< SWidget > InDecorator, const int32 DesiredZOrder, const bool bVisibleOnPress, const bool bVisibleOnRelease)
Definition SLayoutComponent.h:25
int32 ZOrder
Definition SLayoutComponent.h:21
TEnumAsByte< EVerticalAlignment > VerticalAlignment
Definition SLayoutComponent.h:20
TSharedPtr< SWidget > DecoratorWidget
Definition SLayoutComponent.h:18