Repository navigation
Expand file tree
/
Copy pathSpinnerComponent.cpp
More file actions
85 lines (76 loc) · 2.01 KB
/
Copy pathSpinnerComponent.cpp
File metadata and controls
85 lines (76 loc) · 2.01 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
#include "SpinnerComponent.h"
#include <deki/Object.h>
#include "deki-rendering/Fingerprint.h"
#include "WidgetShapes.h"
#include <cmath>
namespace Deki2D
{
void SpinnerComponent::OnPropertyChanged(const char* propertyName)
{
(void)propertyName;
m_LastLayout = 0;
SyncParts();
}
void SpinnerComponent::RuntimeUpdate(float deltaTime)
{
SyncParts();
if (!isSpinning || !m_Arc)
{
return;
}
// Clockwise is a falling angle.
m_Angle = std::fmod(m_Angle - turnsPerSecond * kTwoPi * deltaTime, kTwoPi);
m_Arc->SetRotation(m_Angle);
}
void SpinnerComponent::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(size);
f.Add(thickness);
f.Add(arcLength);
if (f.Value() == m_LastLayout)
{
return;
}
m_LastLayout = f.Value();
LayoutParts();
}
void SpinnerComponent::EnsureParts()
{
Deki::Object* owner = GetOwner();
m_Track = WidgetParts::ShapePart(owner, "Track", ShapeKind::Arc, WidgetColors::Track);
m_Arc = WidgetParts::ShapePart(owner, "Arc", ShapeKind::Arc, WidgetColors::Accent);
if (ShapeComponent* arc = WidgetParts::Shape(m_Arc))
{
arc->roundCaps = true;
}
}
void SpinnerComponent::LayoutParts()
{
WidgetParts::PlaceShape(m_Track, 0.0f, 0.0f, size, size);
if (ShapeComponent* track = WidgetParts::Shape(m_Track))
{
track->SetArc(0.0f, kTwoPi);
track->lineWidth = thickness;
}
WidgetParts::PlaceShape(m_Arc, 0.0f, 0.0f, size, size);
if (ShapeComponent* arc = WidgetParts::Shape(m_Arc))
{
// Centred on up, so the turn is the only thing that moves it.
const float up = kTwoPi * 0.25f;
arc->SetArc(up - arcLength * 0.5f, up + arcLength * 0.5f);
arc->lineWidth = thickness;
}
}
} // namespace Deki2D