-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadarChartWidget.cpp
More file actions
325 lines (277 loc) · 9.8 KB
/
RadarChartWidget.cpp
File metadata and controls
325 lines (277 loc) · 9.8 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "RadarChartWidget.h"
void URadarChartWidget::PostLoad()
{
Super::PostLoad();
SyncEditorData();
}
#if WITH_EDITOR
void URadarChartWidget::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
Super::PostEditChangeProperty(PropertyChangedEvent);
const FName PropertyName = PropertyChangedEvent.Property
? PropertyChangedEvent.Property->GetFName()
: NAME_None;
bool bIsNumAxesChanged = PropertyName == GET_MEMBER_NAME_CHECKED(URadarChartWidget, NumAxes);
bool bIsValuesMapLengthChanged = AxesValuesMap.Num() != NumAxes;
if (bIsNumAxesChanged || bIsValuesMapLengthChanged)
{
SyncEditorData();
}
}
#endif
void URadarChartWidget::SyncEditorData()
{
// update axis names
int32 OldSize = AxesOrderFromTop.Num();
AxesOrderFromTop.SetNum(NumAxes);
for (int32 i = OldSize; i < NumAxes; i++)
{
FName AxisName = FName(FString::Printf(TEXT("axis%i"), i + 1));
AxesOrderFromTop[i] = AxisName;
}
// update values map
TMap<FName, float> NewMap;
for (const FName& Axis : AxesOrderFromTop)
{
if (AxesValuesMap.Contains(Axis))
{
NewMap.Add(Axis, AxesValuesMap[Axis]);
}
else
{
NewMap.Add(Axis, FMath::RandRange(0.5f, 1.f));
}
}
AxesValuesMap = MoveTemp(NewMap);
// recalculate angles for new polygon
constexpr float StartAngle = -PI / 2.f;
AxisDirections.Empty();
for (int32 i = 0; i < NumAxes; ++i)
{
float Angle = StartAngle + i * (2 * PI / NumAxes);
AxisDirections.Add(
FVector2f(FMath::Cos(Angle), FMath::Sin(Angle))
);
}
}
void URadarChartWidget::SetAxisValue(const FName& AxisName, float Value)
{
if (!AxesValuesMap.Contains(AxisName)) {
UE_LOG(LogTemp, Error, TEXT("[RadarChartWidget::SetAxisValue] Axis '%s' doesnt exist!"), *AxisName.ToString());
return;
}
float AxisValue = AxesValuesMap[AxisName];
AxisValue = FMath::Clamp(AxisValue + Value, 0, 1);
AxesValuesMap[AxisName] = AxisValue;
}
void URadarChartWidget::StartFilledPolygonAnimation(const TMap<FName, float>& AxesDiff)
{
StartValuesMap = AxesValuesMap;
DiffValuesMap.Empty();
for (const auto& Pair : AxesDiff)
{
if (AxesValuesMap.Contains(Pair.Key))
{
DiffValuesMap.Add(Pair);
}
}
AnimElapsed = 0.f;
bAnimating = true;
}
void URadarChartWidget::NativeTick(const FGeometry& MyGeometry, float InDeltaTime)
{
Super::NativeTick(MyGeometry, InDeltaTime);
if (!bAnimating)
return;
AnimElapsed += InDeltaTime;
float Alpha = FMath::Clamp(AnimElapsed / AnimDuration, 0.f, 1.f);
for (const auto& Pair : DiffValuesMap)
{
float Start = StartValuesMap[Pair.Key];
AxesValuesMap[Pair.Key] = Start + Pair.Value * Alpha;
}
if (Alpha >= 1.f)
{
bAnimating = false;
}
Invalidate(EInvalidateWidgetReason::Paint);
}
int32 URadarChartWidget::NativePaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const
{
Super::NativePaint(Args, AllottedGeometry, MyCullingRect, OutDrawElements, LayerId, InWidgetStyle, bParentEnabled);
if (NumAxes == 0)
return LayerId;
if (bDrawFilledPolygon)
{
DrawFilledPolygon(AllottedGeometry, OutDrawElements, LayerId, InWidgetStyle);
DrawFilledPolygonOutline(AllottedGeometry, OutDrawElements, LayerId + 1, InWidgetStyle);
}
else
{
DrawRadarChart(AllottedGeometry, OutDrawElements, LayerId);
}
return LayerId;
}
void URadarChartWidget::DrawWiredPolygon(const FGeometry& AllottedGeometry, FSlateWindowElementList& OutDrawElements, int32 LayerId, float Scale) const
{
TArray<FVector2f> PolygonPointsLocal;
PolygonPointsLocal.Reserve(NumAxes);
const FVector2f Size = AllottedGeometry.GetLocalSize();
const FVector2f Center = Size * 0.5f;
const float Radius = FMath::Min(Size.X, Size.Y) * 0.5f;
for (const FVector2f& Dir : AxisDirections)
{
PolygonPointsLocal.Add(Center + Dir * Radius * Scale);
}
PolygonPointsLocal.Add(FVector2f(PolygonPointsLocal[0]));
FSlateDrawElement::MakeLines(
OutDrawElements,
LayerId,
AllottedGeometry.ToPaintGeometry(),
PolygonPointsLocal,
ESlateDrawEffect::None,
OutlineColor,
true,
OutlineThickness
);
}
void URadarChartWidget::DrawAxes(const FGeometry& AllottedGeometry, FSlateWindowElementList& OutDrawElements, int32 LayerId) const
{
TArray<FVector2f> AxisLinePointsLocal;
AxisLinePointsLocal.Reserve(2);
const FVector2f Size = AllottedGeometry.GetLocalSize();
const FVector2f Center = Size * 0.5f;
const float Radius = FMath::Min(Size.X, Size.Y) * 0.5f;
for (const FVector2f& Dir : AxisDirections)
{
AxisLinePointsLocal.Add(Center);
AxisLinePointsLocal.Add(Center + Dir * Radius);
FSlateDrawElement::MakeLines(
OutDrawElements,
LayerId,
AllottedGeometry.ToPaintGeometry(),
AxisLinePointsLocal,
ESlateDrawEffect::None,
OutlineColor,
true,
OutlineThickness
);
AxisLinePointsLocal.Empty();
}
}
void URadarChartWidget::DrawFilledPolygon(const FGeometry& AllottedGeometry, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle) const
{
FLinearColor LinearColor = GetColorAndOpacity() * InWidgetStyle.GetColorAndOpacityTint() * Brush.GetTint(InWidgetStyle);
FColor FinalColorAndOpacity = LinearColor.ToFColor(true);
TArray<FVector2f> Points;
Points.Reserve(NumAxes);
const FVector2f Size = AllottedGeometry.GetLocalSize();
const FVector2f Center = Size * 0.5f;
const float Radius = FMath::Min(Size.X, Size.Y) * 0.5f;
for (int32 i = 0; i < NumAxes; ++i)
{
const FName& AxisName = AxesOrderFromTop[i];
FVector2f Dir = AxisDirections[i];
Points.Add(Center + Dir * Radius * GetAxisValue(AxisName));
}
TArray<FSlateVertex> Vertices;
TArray<SlateIndex> Indices;
// Vertex at center
{
Vertices.Add(FSlateVertex::Make<ESlateVertexRounding::Disabled>(
AllottedGeometry.GetAccumulatedRenderTransform(),
Center,
FVector2f(0.0f, 0.0f),
FinalColorAndOpacity
));
}
// Outer vertices
for (const FVector2f& P : Points)
{
Vertices.Add(FSlateVertex::Make<ESlateVertexRounding::Disabled>(
AllottedGeometry.GetAccumulatedRenderTransform(),
P,
FVector2f(0.0f, 0.0f),
FinalColorAndOpacity
));
}
// Build triangle fan
for (int32 i = 1; i < Points.Num(); ++i)
{
Indices.Add(0);
Indices.Add(i);
Indices.Add(i + 1);
}
// Close fan
Indices.Add(0);
Indices.Add(Points.Num());
Indices.Add(1);
const FSlateResourceHandle Handle = FSlateApplication::Get().GetRenderer()->GetResourceHandle(Brush);
FSlateDrawElement::MakeCustomVerts(
OutDrawElements,
LayerId,
Handle,
Vertices,
Indices,
nullptr,
0,
0
);
}
void URadarChartWidget::DrawFilledPolygonOutline(const FGeometry& AllottedGeometry, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle) const
{
TArray<FVector2f> PolygonPointsLocal;
PolygonPointsLocal.Reserve(NumAxes);
const FVector2f Size = AllottedGeometry.GetLocalSize();
const FVector2f Center = Size * 0.5f;
const float Radius = FMath::Min(Size.X, Size.Y) * 0.5f;
float DotRadius = DotBrush.GetImageSize().X * 0.5f;
FLinearColor DotLinearColor = GetColorAndOpacity() * InWidgetStyle.GetColorAndOpacityTint() * DotBrush.GetTint(InWidgetStyle);
for (int32 i = 0; i < NumAxes; ++i)
{
const FName& AxisName = AxesOrderFromTop[i];
FVector2f Dir = AxisDirections[i];
FVector2f Point = Center + Dir * Radius * GetAxisValue(AxisName);
PolygonPointsLocal.Add(Point);
FSlateDrawElement::MakeBox(
OutDrawElements,
LayerId + 1,
AllottedGeometry.ToPaintGeometry(
FVector2f(Point - FVector2f(DotRadius)),
FVector2f(DotRadius * 2.f)
),
&DotBrush,
ESlateDrawEffect::None,
DotLinearColor
);
}
PolygonPointsLocal.Add(FVector2f(PolygonPointsLocal[0]));
FSlateDrawElement::MakeLines(
OutDrawElements,
LayerId,
AllottedGeometry.ToPaintGeometry(),
PolygonPointsLocal,
ESlateDrawEffect::None,
FilledOutlineColor,
true,
OutlineThickness
);
}
void URadarChartWidget::DrawRadarChart(const FGeometry& AllottedGeometry, FSlateWindowElementList& OutDrawElements, int32 LayerId) const
{
DrawWiredPolygon(AllottedGeometry, OutDrawElements, LayerId + 1, 1);
if (bDrawValueMarks)
{
for (float Mark = MarkingStep;
Mark <= 1.f - MarkingStep || FMath::IsNearlyEqual(Mark, 1.f - MarkingStep, 0.01f);
Mark += MarkingStep)
{
DrawWiredPolygon(AllottedGeometry, OutDrawElements, LayerId, Mark);
}
}
DrawAxes(AllottedGeometry, OutDrawElements, LayerId);
}
float URadarChartWidget::GetAxisValue(const FName& Axis) const
{
return FMath::Clamp(AxesValuesMap[Axis], 0.f, 1.f);
}