Repository navigation
Expand file tree
/
Copy pathArcComponent.cpp
More file actions
266 lines (245 loc) · 6.93 KB
/
Copy pathArcComponent.cpp
File metadata and controls
266 lines (245 loc) · 6.93 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
#include "ArcComponent.h"
#include <deki/Object.h>
#include "deki-input/InputCollider.h"
#include "deki-input/Keys.h"
#include "deki-rendering/Fingerprint.h"
#include "WidgetShapes.h"
#include <algorithm>
#include <cmath>
namespace Deki2D
{
ArcComponent::~ArcComponent()
{
FocusSystem::Unregister(this);
}
void ArcComponent::Start()
{
SyncParts();
FocusSystem::Register(this);
DekiInput::InputCollider* collider = inputCollider.Get();
if (!collider)
{
return;
}
collider->onPointerDown.push_back(
[this](float x, float y)
{
if (!interactive || FocusSystem::IsInputBlocked(GetOwner()))
{
return;
}
m_Dragging = true;
SetValueFromInput(ValueAtPoint(x, y, value));
});
collider->onPointerMove.push_back(
[this](float x, float y)
{
if (m_Dragging)
{
SetValueFromInput(ValueAtPoint(x, y, value));
}
});
collider->onPointerUp.push_back(
[this](float x, float y)
{
(void)x;
(void)y;
m_Dragging = false;
});
}
void ArcComponent::SetValue(float newValue)
{
value = std::clamp(newValue, std::min(minValue, maxValue), std::max(minValue, maxValue));
}
float ArcComponent::GetFraction() const
{
const float range = maxValue - minValue;
if (range == 0.0f)
{
return 0.0f;
}
return std::clamp((value - minValue) / range, 0.0f, 1.0f);
}
void ArcComponent::AddOnValueChangedCallback(const std::function<void(float)>& callback)
{
onValueChanged.push_back(callback);
}
float ArcComponent::ValueAtPoint(float worldX, float worldY, float previous) const
{
const Deki::Object* owner = GetOwner();
if (!owner || sweepAngle <= 0.0f)
{
return previous;
}
const float angle = std::atan2(worldY - owner->GetWorldY(), worldX - owner->GetWorldX());
// How far clockwise from the start, 0 to a full turn.
float along = std::fmod(startAngle - angle, kTwoPi);
if (along < 0.0f)
{
along += kTwoPi;
}
float fraction = along / sweepAngle;
if (along > sweepAngle)
{
// In the gap: the nearer end.
const float pastEnd = along - sweepAngle;
const float beforeStart = kTwoPi - along;
fraction = pastEnd < beforeStart ? 1.0f : 0.0f;
}
// A drag does not jump across the gap from one end to the other.
const float range = maxValue - minValue;
const float previousFraction = range != 0.0f ? (previous - minValue) / range : 0.0f;
if (std::fabs(fraction - previousFraction) > 0.5f)
{
fraction = previousFraction > 0.5f ? 1.0f : 0.0f;
}
return minValue + range * fraction;
}
void ArcComponent::SetValueFromInput(float newValue)
{
if (step > 0.0f)
{
newValue = minValue + std::round((newValue - minValue) / step) * step;
}
const float old = value;
SetValue(newValue);
if (value == old)
{
return;
}
SyncParts();
for (const auto& callback : onValueChanged)
{
callback(value);
}
}
void ArcComponent::OnPropertyChanged(const char* propertyName)
{
(void)propertyName;
m_LastLayout = 0;
SyncParts();
}
void ArcComponent::RuntimeUpdate(float deltaTime)
{
(void)deltaTime;
SyncParts();
}
void ArcComponent::OnFocusChanged(bool focused)
{
m_Focused = focused;
WidgetParts::ShowFocus(GetOwner(), focused);
}
bool ArcComponent::OnFocusKey(uint32_t key, uint32_t character)
{
(void)character;
namespace Keys = DekiInput::Keys;
if (key != Keys::Left && key != Keys::Right)
{
return false;
}
const float amount = step > 0.0f ? step : (maxValue - minValue) / 20.0f;
SetValueFromInput(value + (key == Keys::Right ? amount : -amount));
return true;
}
void ArcComponent::SyncParts()
{
Deki::Object* owner = GetOwner();
if (!owner)
{
return;
}
if (owner->GetChildren().size() != m_KnownChildren)
{
EnsureParts();
m_KnownChildren = owner->GetChildren().size();
m_LastLayout = 0;
}
DekiRendering::Fingerprint f;
f.Add(GetFraction());
f.Add(size);
f.Add(thickness);
f.Add(startAngle);
f.Add(sweepAngle);
f.Add(static_cast<uint32_t>(interactive));
if (f.Value() == m_LastLayout)
{
return;
}
m_LastLayout = f.Value();
LayoutParts();
}
void ArcComponent::EnsureParts()
{
Deki::Object* owner = GetOwner();
m_Track = WidgetParts::ShapePart(owner, "Track", ShapeKind::Arc, WidgetColors::Track);
m_Indicator = WidgetParts::ShapePart(owner, "Indicator", ShapeKind::Arc, WidgetColors::Accent);
m_Knob = WidgetParts::ShapePart(owner, "Knob", ShapeKind::Ellipse, WidgetColors::Knob);
m_Focus = WidgetParts::ShapePart(owner, "Focus", ShapeKind::Ellipse, WidgetColors::Focus);
for (Deki::Object* part : { m_Track, m_Indicator })
{
if (ShapeComponent* shape = WidgetParts::Shape(part))
{
shape->roundCaps = true;
}
}
if (m_Focus)
{
m_Focus->SetActive(m_Focused);
}
#ifdef DEKI_EDITOR
if (owner && !inputCollider.GetObject())
{
if (!owner->GetComponent<DekiInput::InputCollider>())
{
owner->AddComponent<DekiInput::InputCollider>();
}
inputCollider.Set(owner);
}
#endif
}
void ArcComponent::LayoutParts()
{
// Shapes run counter-clockwise from start to end; the value runs
// clockwise from startAngle, so each arc ends at startAngle.
const float fraction = GetFraction();
const float endOfValue = startAngle - sweepAngle * fraction;
WidgetParts::PlaceShape(m_Track, 0.0f, 0.0f, size, size);
if (ShapeComponent* track = WidgetParts::Shape(m_Track))
{
track->SetArc(startAngle - sweepAngle, startAngle);
track->lineWidth = thickness;
}
WidgetParts::PlaceShape(m_Indicator, 0.0f, 0.0f, size, size);
if (ShapeComponent* indicator = WidgetParts::Shape(m_Indicator))
{
indicator->SetArc(endOfValue, startAngle);
indicator->lineWidth = thickness;
}
if (m_Indicator)
{
m_Indicator->SetActive(fraction > 0.0f);
}
// The knob rides the middle of the ring.
const float radius = (size - thickness) * 0.5f;
const float knob = thickness * 1.6f;
const float x = std::cos(endOfValue) * radius;
const float y = std::sin(endOfValue) * radius;
WidgetParts::PlaceShape(m_Knob, x, y, knob, knob);
if (m_Knob)
{
m_Knob->SetActive(interactive);
}
const float ring = knob * 1.5f;
WidgetParts::PlaceShape(m_Focus, x, y, ring, ring);
if (ShapeComponent* shape = WidgetParts::Shape(m_Focus))
{
shape->lineWidth = knob * 0.12f;
}
DekiInput::InputCollider* collider = inputCollider.Get();
if (collider && inputCollider.GetObject() == GetOwner())
{
collider->width = size;
collider->height = size;
}
}
} // namespace Deki2D