Repository navigation
Expand file tree
/
Copy pathTabviewComponent.cpp
More file actions
247 lines (226 loc) · 6.95 KB
/
Copy pathTabviewComponent.cpp
File metadata and controls
247 lines (226 loc) · 6.95 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
#include "TabviewComponent.h"
#include <deki/Object.h>
#include "deki-input/InputCollider.h"
#include "deki-input/Keys.h"
#include "deki-rendering/Fingerprint.h"
#include "WidgetShapes.h"
#include "WidgetText.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
namespace Deki2D
{
TabviewComponent::~TabviewComponent()
{
FocusSystem::Unregister(this);
}
void TabviewComponent::Start()
{
m_Shown = static_cast<float>(selectedTab);
SyncParts();
FocusSystem::Register(this);
auto* collider = m_Bar ? m_Bar->GetComponent<DekiInput::InputCollider>() : nullptr;
if (!collider)
{
return;
}
collider->onPointerUp.push_back(
[this](float x, float y)
{
(void)y;
auto* col = m_Bar ? m_Bar->GetComponent<DekiInput::InputCollider>() : nullptr;
const Deki::Object* owner = GetOwner();
if (!isEnabled || tabs.empty() || !owner || !col || !col->IsPointerInside() || width <= 0.0f ||
FocusSystem::IsInputBlocked(owner))
{
return;
}
const float local = x - owner->GetWorldX() + width * 0.5f;
const int32_t count = static_cast<int32_t>(tabs.size());
SelectTab(std::clamp<int32_t>(static_cast<int32_t>(local / width * count), 0, count - 1));
});
}
void TabviewComponent::SelectTab(int32_t index, bool notify)
{
if (tabs.empty())
{
return;
}
index = std::clamp<int32_t>(index, 0, static_cast<int32_t>(tabs.size()) - 1);
if (index == selectedTab)
{
return;
}
selectedTab = index;
SyncParts();
if (notify)
{
for (const auto& callback : onTabChanged)
{
callback(selectedTab);
}
}
}
void TabviewComponent::AddOnTabChangedCallback(const std::function<void(int32_t)>& callback)
{
onTabChanged.push_back(callback);
}
Deki::Object* TabviewComponent::GetPage(int32_t index) const
{
return index >= 0 && index < static_cast<int32_t>(m_Pages.size()) ? m_Pages[index] : nullptr;
}
Deki::Object* TabviewComponent::GetFocusObject() const
{
return m_Bar ? m_Bar : GetOwner();
}
void TabviewComponent::OnPropertyChanged(const char* propertyName)
{
(void)propertyName;
m_Shown = static_cast<float>(selectedTab);
m_LastLayout = 0;
SyncParts();
}
void TabviewComponent::RuntimeUpdate(float deltaTime)
{
m_Shown = WidgetParts::Glide(m_Shown, static_cast<float>(selectedTab), glideSpeed, deltaTime);
SyncParts();
}
void TabviewComponent::OnFocusChanged(bool focused)
{
m_Focused = focused;
WidgetParts::ShowFocus(GetOwner(), focused);
}
bool TabviewComponent::OnFocusKey(uint32_t key, uint32_t character)
{
(void)character;
namespace Keys = DekiInput::Keys;
if (key != Keys::Left && key != Keys::Right)
{
return false;
}
const int32_t next = selectedTab + (key == Keys::Right ? 1 : -1);
if (next < 0 || next >= static_cast<int32_t>(tabs.size()))
{
return false; // past the last tab: the focus moves on
}
SelectTab(next);
return true;
}
void TabviewComponent::SyncParts()
{
Deki::Object* owner = GetOwner();
if (!owner)
{
return;
}
if (owner->GetChildren().size() != m_KnownChildren || m_TabParts.size() < tabs.size())
{
EnsureParts();
m_KnownChildren = owner->GetChildren().size();
m_LastLayout = 0;
}
if (m_Shown < 0.0f)
{
m_Shown = static_cast<float>(selectedTab);
}
DekiRendering::Fingerprint f;
f.Add(width);
f.Add(height);
f.Add(barHeight);
f.Add(selectedTab);
f.Add(m_Shown);
f.Add(static_cast<uint32_t>(tabs.size()));
for (const std::string& tab : tabs)
{
uint32_t h = 2166136261u;
for (char c : tab)
{
h = (h ^ static_cast<uint8_t>(c)) * 16777619u;
}
f.Add(h);
}
if (f.Value() == m_LastLayout)
{
return;
}
m_LastLayout = f.Value();
LayoutParts();
}
void TabviewComponent::EnsureParts()
{
Deki::Object* owner = GetOwner();
m_Bar = WidgetParts::ShapePart(owner, "Bar", ShapeKind::Rectangle, WidgetColors::Panel);
m_Indicator = WidgetParts::ShapePart(owner, "Indicator", ShapeKind::Rectangle, WidgetColors::Accent, 100.0f);
m_Focus = WidgetParts::ShapePart(owner, "Focus", ShapeKind::Rectangle, WidgetColors::Focus);
if (m_Focus)
{
m_Focus->SetActive(m_Focused);
}
#ifdef DEKI_EDITOR
// The bar carries the hit area, as it is the part that takes taps.
if (m_Bar && !m_Bar->GetComponent<DekiInput::InputCollider>())
{
m_Bar->AddComponent<DekiInput::InputCollider>();
}
#endif
m_TabParts.clear();
m_Pages.clear();
for (size_t i = 0; i < tabs.size(); ++i)
{
char name[16];
std::snprintf(name, sizeof(name), "Tab%d", static_cast<int>(i));
Deki::Object* tab = WidgetParts::TextPart(owner, name, WidgetColors::DimText);
std::snprintf(name, sizeof(name), "Page%d", static_cast<int>(i));
Deki::Object* page = WidgetParts::Find(owner, name);
#ifdef DEKI_EDITOR
if (!page)
{
page = WidgetParts::MakeChild(owner, name);
}
#endif
if (!tab || !page)
{
break; // on the device: the tabs the scene has
}
m_TabParts.push_back(tab);
m_Pages.push_back(page);
}
}
void TabviewComponent::LayoutParts()
{
const int32_t count = static_cast<int32_t>(m_TabParts.size());
const float barY = height * 0.5f - barHeight * 0.5f;
WidgetParts::PlaceShape(m_Bar, 0.0f, barY, width, barHeight);
if (auto* collider = m_Bar ? m_Bar->GetComponent<DekiInput::InputCollider>() : nullptr)
{
collider->width = width;
collider->height = barHeight;
}
const float tabWidth = count > 0 ? width / static_cast<float>(count) : width;
for (int32_t i = 0; i < count; ++i)
{
const float x = -width * 0.5f + tabWidth * (static_cast<float>(i) + 0.5f);
WidgetParts::PlaceText(m_TabParts[i], x, barY, tabWidth, barHeight);
if (i < static_cast<int32_t>(tabs.size()))
{
WidgetParts::SetPartText(m_TabParts[i], tabs[i]);
}
if (TextComponent* text = WidgetParts::Text(m_TabParts[i]))
{
text->color = i == selectedTab ? WidgetColors::Text : WidgetColors::DimText;
}
// Pages fill the space under the bar, centred in it.
m_Pages[i]->SetLocalPosition(0.0f, -barHeight * 0.5f);
m_Pages[i]->SetActive(i == selectedTab);
}
const float line = barHeight * 0.1f;
const float x = -width * 0.5f + tabWidth * (m_Shown + 0.5f);
WidgetParts::PlaceShape(m_Indicator, x, barY - barHeight * 0.5f + line * 0.5f, tabWidth * 0.6f, line);
const float ring = barHeight * 0.3f;
WidgetParts::PlaceShape(m_Focus, 0.0f, barY, width + ring, barHeight + ring);
if (ShapeComponent* shape = WidgetParts::Shape(m_Focus))
{
shape->lineWidth = barHeight * 0.08f;
}
}
} // namespace Deki2D