Repository navigation
Expand file tree
/
Copy pathSwitchComponent.cpp
More file actions
185 lines (166 loc) · 4.56 KB
/
Copy pathSwitchComponent.cpp
File metadata and controls
185 lines (166 loc) · 4.56 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
#include "SwitchComponent.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>
namespace Deki2D
{
SwitchComponent::~SwitchComponent()
{
FocusSystem::Unregister(this);
}
void SwitchComponent::Start()
{
m_Shown = isOn ? 1.0f : 0.0f;
SyncParts();
FocusSystem::Register(this);
DekiInput::InputCollider* collider = inputCollider.Get();
if (!collider)
{
return;
}
collider->onPointerDown.push_back(
[this](float x, float y)
{
(void)x;
(void)y;
m_Pressed = isEnabled && !FocusSystem::IsInputBlocked(GetOwner());
});
// A tap is a press and a release both inside, as for a button. A scroll
// that takes the drag over cancels the press, with the pointer outside.
collider->onPointerUp.push_back(
[this](float x, float y)
{
(void)x;
(void)y;
DekiInput::InputCollider* col = inputCollider.Get();
if (m_Pressed && isEnabled && col && col->IsPointerInside())
{
Toggle();
}
m_Pressed = false;
});
}
void SwitchComponent::SetOn(bool on, bool notify)
{
if (on == isOn)
{
return;
}
isOn = on;
if (notify)
{
for (const ToggleCallback& callback : onToggled)
{
callback(isOn);
}
}
}
void SwitchComponent::AddOnToggledCallback(const ToggleCallback& callback)
{
onToggled.push_back(callback);
}
void SwitchComponent::OnPropertyChanged(const char* propertyName)
{
(void)propertyName;
m_Shown = isOn ? 1.0f : 0.0f;
m_LastLayout = 0;
SyncParts();
}
void SwitchComponent::RuntimeUpdate(float deltaTime)
{
m_Shown = WidgetParts::Glide(m_Shown, isOn ? 1.0f : 0.0f, glideSpeed, deltaTime);
SyncParts();
}
void SwitchComponent::OnFocusChanged(bool focused)
{
m_Focused = focused;
WidgetParts::ShowFocus(GetOwner(), focused);
}
bool SwitchComponent::OnFocusKey(uint32_t key, uint32_t character)
{
(void)character;
if (key == DekiInput::Keys::Enter || key == DekiInput::Keys::Space)
{
Toggle();
return true;
}
return false;
}
void SwitchComponent::SyncParts()
{
Deki::Object* owner = GetOwner();
if (!owner)
{
return;
}
if (owner->GetChildren().size() != m_KnownChildren)
{
EnsureParts();
m_KnownChildren = owner->GetChildren().size();
m_LastLayout = 0;
}
if (m_Shown < 0.0f)
{
m_Shown = isOn ? 1.0f : 0.0f;
}
DekiRendering::Fingerprint f;
f.Add(width);
f.Add(height);
f.Add(m_Shown);
f.Add(onColor.ToRGBA8888());
f.Add(offColor.ToRGBA8888());
if (f.Value() == m_LastLayout)
{
return;
}
m_LastLayout = f.Value();
LayoutParts();
}
void SwitchComponent::EnsureParts()
{
Deki::Object* owner = GetOwner();
m_Track = WidgetParts::ShapePart(owner, "Track", ShapeKind::Rectangle, offColor, 100.0f);
m_Knob = WidgetParts::ShapePart(owner, "Knob", ShapeKind::Ellipse, WidgetColors::Knob);
m_Focus = WidgetParts::ShapePart(owner, "Focus", ShapeKind::Rectangle, WidgetColors::Focus, 100.0f);
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 SwitchComponent::LayoutParts()
{
WidgetParts::PlaceShape(m_Track, 0.0f, 0.0f, width, height);
WidgetParts::TintShape(m_Track, WidgetParts::LerpColor(offColor, onColor, m_Shown));
// The knob sits inside the track with a small margin all round.
const float margin = height * 0.12f;
const float knob = std::max(0.0f, height - margin * 2.0f);
const float travel = std::max(0.0f, width - margin * 2.0f - knob);
const float x = -travel * 0.5f + travel * m_Shown;
WidgetParts::PlaceShape(m_Knob, x, 0.0f, knob, knob);
const float ring = height * 0.3f;
WidgetParts::PlaceShape(m_Focus, 0.0f, 0.0f, width + ring, height + ring);
if (ShapeComponent* shape = WidgetParts::Shape(m_Focus))
{
shape->lineWidth = height * 0.08f;
}
DekiInput::InputCollider* collider = inputCollider.Get();
if (collider && inputCollider.GetObject() == GetOwner())
{
collider->width = width;
collider->height = height;
}
}
} // namespace Deki2D