-
Notifications
You must be signed in to change notification settings - Fork 9
/
SCustomShapeButton.cpp
289 lines (237 loc) · 8.01 KB
/
SCustomShapeButton.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (c) Yevhenii Selivanov
#include "SCustomShapeButton.h"
//---
#include "RenderingThread.h"
#include "RHICommandList.h"
#include "RHIResources.h"
#include "TextureResource.h"
#include "Engine/Texture2D.h"
#include "Engine/World.h"
#include "Kismet/KismetRenderingLibrary.h"
#include "Materials/MaterialInterface.h"
// Virtual destructor, unregister data
SCustomShapeButton::~SCustomShapeButton()
{
if (IsValid(RenderTarget.Get()))
{
RenderTarget->ConditionalBeginDestroy();
RenderTarget.Reset();
}
}
/** Allows button to be hovered. */
void SCustomShapeButton::SetCanHover(bool bAllow)
{
if (bCanHover == bAllow)
{
return;
}
bCanHover = bAllow;
TAttribute<bool> bHovered = false;
if (bAllow)
{
bHovered = MakeAttributeLambda([WeakThisPtr = StaticCastWeakPtr<SCustomShapeButton>(AsWeak())]() -> bool
{
const TSharedPtr<SCustomShapeButton> This = WeakThisPtr.Pin();
return This.IsValid() && This->IsAlphaPixelHovered();
});
}
SetHover(bHovered);
TryDetectOnHovered();
}
// Updates the internal texture size
void SCustomShapeButton::SetTextureSize(const FIntPoint& InSize)
{
if (!ensureMsgf(InSize.X > 0 && InSize.Y > 0, TEXT("ASSERT: [%i] %hs:\nTexture Size is not valid!"), __LINE__, __FUNCTION__)
|| TextureRes == InSize)
{
// Is already set
return;
}
TextureRes = InSize;
}
// Forces to update the Raw Colors (pixels data) about current image
void SCustomShapeButton::ForceUpdateImage()
{
RawColors.Empty();
TryUpdateRawColorsOnce();
}
// Calculates the index of the pixel under the cursor
uint32 SCustomShapeButton::GetCurrentPointIndex() const
{
const FVector2D CurrentGeometrySize = CurrentGeometry.GetLocalSize();
if (CurrentGeometrySize.X <= 0.f || CurrentGeometrySize.Y <= 0.f)
{
return INDEX_NONE;
}
FVector2D LocalPosition = CurrentGeometry.AbsoluteToLocal(CurrentMouseEvent.GetScreenSpacePosition());
LocalPosition /= CurrentGeometrySize;
LocalPosition.X *= TextureRes.X;
LocalPosition.Y *= TextureRes.Y;
const uint32 LocalPositionX = FMath::FloorToInt(LocalPosition.X);
const uint32 LocalPositionY = FMath::FloorToInt(LocalPosition.Y);
const uint32 PixelRow = LocalPositionY * TextureRes.X;
return PixelRow + LocalPositionX;
}
FReply SCustomShapeButton::OnMouseButtonDown(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent)
{
UpdateMouseData(MyGeometry, MouseEvent);
if (!IsHovered())
{
// Avoid press event
return FReply::Unhandled();
}
return SButton::OnMouseButtonDown(MyGeometry, MouseEvent);
}
FReply SCustomShapeButton::OnMouseButtonDoubleClick(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent)
{
UpdateMouseData(MyGeometry, MouseEvent);
if (!IsHovered())
{
// Avoid press event
return FReply::Unhandled();
}
return SButton::OnMouseButtonDoubleClick(MyGeometry, MouseEvent);
}
FReply SCustomShapeButton::OnMouseButtonUp(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent)
{
UpdateMouseData(MyGeometry, MouseEvent);
FReply Reply = FReply::Unhandled();
if (IsHovered())
{
Reply = SButton::OnMouseButtonUp(MyGeometry, MouseEvent);
}
SetCanHover(false);
return Reply;
}
FReply SCustomShapeButton::OnMouseMove(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent)
{
UpdateMouseData(MyGeometry, MouseEvent);
if (!bCanHover
&& IsAlphaPixelHovered())
{
// Allow to be hovered since mouse is hovered on alpha pixel
SetCanHover(true);
}
TryDetectOnHovered();
return SButton::OnMouseMove(MyGeometry, MouseEvent);
}
void SCustomShapeButton::OnMouseLeave(const FPointerEvent& MouseEvent)
{
SButton::OnMouseLeave(MouseEvent);
SetCanHover(false);
}
void SCustomShapeButton::OnMouseEnter(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent)
{
SButton::OnMouseEnter(MyGeometry, MouseEvent);
TryUpdateRawColorsOnce();
SetCanHover(true);
}
// Returns true if cursor is hovered on a texture
bool SCustomShapeButton::IsAlphaPixelHovered() const
{
if (RawColors.IsEmpty())
{
// Raw Colors are not set
return false;
}
const uint32 BufferPosition = GetCurrentPointIndex();
if (!RawColors.IsValidIndex(BufferPosition))
{
return false;
}
const uint8 HoveredPixel = RawColors[BufferPosition].A;
const uint8 HoveredPixelNormalized = HoveredPixel > 0 ? 1 : 0;
constexpr uint8 TextureAlpha = 1;
constexpr uint8 MaterialAlpha = 0;
return HoveredPixelNormalized == (RenderTarget ? MaterialAlpha : TextureAlpha);
}
// Set once on render thread the buffer data about all pixels of current image if was not set before
void SCustomShapeButton::TryUpdateRawColorsOnce()
{
if (!RawColors.IsEmpty())
{
// Buffer data is already cached, use ForceUpdateImage to refresh it
return;
}
const FSlateBrush* ImageBrush = GetBorderImage();
UObject* InImage = ImageBrush ? ImageBrush->GetResourceObject() : nullptr;
if (!ensureMsgf(InImage, TEXT("%s: 'InImage' is null, most likely no texture is set in the Button Style"), *FString(__FUNCTION__)))
{
return;
}
if (const UTexture2D* Texture = Cast<UTexture2D>(InImage))
{
UpdateRawColors_Texture(*Texture);
}
else if (UMaterialInterface* Material = Cast<UMaterialInterface>(InImage))
{
UpdateRawColors_Material(*Material);
}
else
{
ensureMsgf(false, TEXT("ASSERT: [%i] %hs:\n'No image' is set!"), __LINE__, __FUNCTION__);
}
}
// Copies the buffer data from the texture
void SCustomShapeButton::UpdateRawColors_Texture(const UTexture2D& Texture)
{
SetTextureSize(FIntPoint(Texture.GetSizeX(), Texture.GetSizeY()));
// Get Raw Colors data on Render thread
TWeakPtr<SCustomShapeButton> WeakThisPtr = StaticCastWeakPtr<SCustomShapeButton>(AsWeak());
checkf(WeakThisPtr.IsValid(), TEXT("ERROR: [%i] %hs:\n'WeakThis' is not valid!"), __LINE__, __FUNCTION__);
const TWeakObjectPtr<const UTexture2D> WeakTexture = &Texture;
const FIntRect TextureSize(0, 0, TextureRes.X, TextureRes.Y);
ENQUEUE_RENDER_COMMAND(TryUpdateRawColorsOnce)([WeakThisPtr, WeakTexture, TextureSize](FRHICommandListImmediate& RHICmdList)
{
SCustomShapeButton* This = WeakThisPtr.Pin().Get();
if (!ensureMsgf(This, TEXT("ASSERT: [%i] %hs:\n'This' is not valid!"), __LINE__, __FUNCTION__))
{
return;
}
const UTexture2D* Texture2D = WeakTexture.Get();
const FTextureResource* TextureResource = Texture2D ? Texture2D->GetResource() : nullptr;
FRHITexture2D* RHITexture2D = TextureResource ? TextureResource->GetTexture2DRHI() : nullptr;
if (ensureMsgf(RHITexture2D, TEXT("%hs: 'RHITexture2D' is not valid"), __FUNCTION__))
{
// Copy data to cache
RHICmdList.ReadSurfaceData(RHITexture2D, TextureSize, /*out*/This->RawColors, FReadSurfaceDataFlags());
}
});
}
// Copies the buffer data from the material
void SCustomShapeButton::UpdateRawColors_Material(UMaterialInterface& Material)
{
checkf(GWorld, TEXT("ERROR: [%i] %hs:\n'GWorld' is null!"), __LINE__, __FUNCTION__);
const FSlateBrush* Image = GetBorderImage();
checkf(Image, TEXT("ERROR: [%i] %hs:\n'Image' is null!"), __LINE__, __FUNCTION__);
const FVector2f ImageSize = Image->GetImageSize();
SetTextureSize(FIntPoint(ImageSize.X, ImageSize.Y));
// Create new Render Target
if (!RenderTarget)
{
RenderTarget = TStrongObjectPtr(UKismetRenderingLibrary::CreateRenderTarget2D(GWorld, TextureRes.X, TextureRes.Y));
}
// Clear created Render Target now before rendering material
UKismetRenderingLibrary::ClearRenderTarget2D(GWorld, RenderTarget.Get());
// Render our material first before copying pixels data
UKismetRenderingLibrary::DrawMaterialToRenderTarget(GWorld, RenderTarget.Get(), &Material);
// Copy pixels data from Render Target to our cache
UKismetRenderingLibrary::ReadRenderTarget(GWorld, RenderTarget.Get(), /*out*/RawColors);
}
// Try register On Hovered and On Unhovered events
void SCustomShapeButton::TryDetectOnHovered()
{
const bool bIsHoveredNow = bCanHover && IsHovered();
if (bIsHovered != bIsHoveredNow)
{
// Call OnHovered\OnUnhovered event
ExecuteHoverStateChanged(true);
}
bIsHovered = bIsHoveredNow;
}
// Caching current geometry and last mouse event
void SCustomShapeButton::UpdateMouseData(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent)
{
CurrentGeometry = MyGeometry;
CurrentMouseEvent = MouseEvent;
}