Repository navigation
Expand file tree
/
Copy pathMsgboxComponent.cpp
More file actions
340 lines (312 loc) · 9.63 KB
/
Copy pathMsgboxComponent.cpp
File metadata and controls
340 lines (312 loc) · 9.63 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "MsgboxComponent.h"
#include <deki/Object.h>
#include "ButtonComponent.h"
#include "deki-input/InputCollider.h"
#include "deki-input/Keys.h"
#include "deki-rendering/Fingerprint.h"
#include "deki-rendering/RendererComponent.h"
#include "WidgetShapes.h"
#include "WidgetText.h"
#include <algorithm>
#include <cstdio>
namespace Deki2D
{
namespace
{
constexpr int32_t kMaxMsgboxButtons = 3;
// The dialog draws above everything else, open dropdown lists included.
constexpr int32_t kMsgboxSortingOrder = 2000;
// The backdrop covers far more than any screen, so every tap outside the
// panel lands on it.
constexpr float kBackdropSize = 4000.0f;
uint32_t HashText(const std::string& text)
{
uint32_t h = 2166136261u;
for (char c : text)
{
h = (h ^ static_cast<uint8_t>(c)) * 16777619u;
}
return h;
}
} // namespace
MsgboxComponent::~MsgboxComponent()
{
if (m_ScopeOpen && m_Box)
{
FocusSystem::PopScope(m_Box);
}
}
void MsgboxComponent::Start()
{
SyncParts();
WireButtons();
m_Started = true;
if (isOpen)
{
isOpen = false;
Open();
}
}
void MsgboxComponent::WireButtons()
{
for (size_t i = 0; i < m_Buttons.size(); ++i)
{
auto* button = m_Buttons[i]->GetComponent<ButtonComponent>();
auto* shape = WidgetParts::Shape(m_Buttons[i]);
if (!button)
{
continue;
}
const int32_t index = static_cast<int32_t>(i);
button->AddOnClickCallback([this, index]() { Close(index); });
if (shape)
{
// Lighter while hovered or focused, darker while held.
const Deki::Color base = shape->color;
button->AddOnStateChangedCallback(
[shape, base](ButtonState state)
{
const Deki::Color white(255, 255, 255, base.a);
const Deki::Color black(0, 0, 0, base.a);
if (state == ButtonState::Hovered)
{
shape->color = WidgetParts::LerpColor(base, white, 0.2f);
}
else if (state == ButtonState::Pressed)
{
shape->color = WidgetParts::LerpColor(base, black, 0.25f);
}
else
{
shape->color = base;
}
});
}
}
}
void MsgboxComponent::Open()
{
if (isOpen)
{
return;
}
isOpen = true;
ShowBox();
if (m_Box && m_Started)
{
// Someone steering with keys gets the first button focused; a touch
// user gets no focus ring.
const bool usingKeys = FocusSystem::GetFocused() != nullptr;
FocusSystem::PushScope(m_Box, this);
m_ScopeOpen = true;
if (usingKeys && !m_Buttons.empty())
{
FocusSystem::SetFocus(m_Buttons[0]->GetComponent<ButtonComponent>());
}
}
}
void MsgboxComponent::Close(int32_t button)
{
if (!isOpen)
{
return;
}
isOpen = false;
if (m_ScopeOpen && m_Box)
{
FocusSystem::SetFocus(nullptr);
FocusSystem::PopScope(m_Box);
m_ScopeOpen = false;
}
ShowBox();
for (const auto& callback : onClosed)
{
callback(button);
}
}
void MsgboxComponent::AddOnClosedCallback(const std::function<void(int32_t)>& callback)
{
onClosed.push_back(callback);
}
void MsgboxComponent::ShowBox()
{
// Its buttons start when it first shows.
if (m_Box)
{
m_Box->SetActive(isOpen);
}
}
bool MsgboxComponent::OnFocusKey(uint32_t key, uint32_t character)
{
(void)character;
if (key == DekiInput::Keys::Esc && closeOnEsc && isOpen)
{
Close(-1);
return true;
}
return false;
}
void MsgboxComponent::OnPropertyChanged(const char* propertyName)
{
(void)propertyName;
m_LastLayout = 0;
SyncParts();
}
void MsgboxComponent::RuntimeUpdate(float deltaTime)
{
(void)deltaTime;
SyncParts();
}
void MsgboxComponent::SyncParts()
{
Deki::Object* owner = GetOwner();
if (!owner)
{
return;
}
const size_t boxChildren = m_Box ? m_Box->GetChildren().size() : static_cast<size_t>(-1);
if (owner->GetChildren().size() != m_KnownChildren || boxChildren != m_KnownBoxChildren)
{
EnsureParts();
m_KnownChildren = owner->GetChildren().size();
m_KnownBoxChildren = m_Box ? m_Box->GetChildren().size() : static_cast<size_t>(-1);
m_LastLayout = 0;
}
DekiRendering::Fingerprint f;
f.Add(width);
f.Add(height);
f.Add(static_cast<uint32_t>(isOpen));
f.Add(HashText(title));
f.Add(HashText(message));
f.Add(static_cast<uint32_t>(buttons.size()));
for (const std::string& label : buttons)
{
f.Add(HashText(label));
}
if (f.Value() == m_LastLayout)
{
return;
}
m_LastLayout = f.Value();
LayoutParts();
}
void MsgboxComponent::EnsureParts()
{
Deki::Object* owner = GetOwner();
m_Box = WidgetParts::Find(owner, "Box");
#ifdef DEKI_EDITOR
if (!m_Box)
{
m_Box = WidgetParts::MakeChild(owner, "Box");
}
#endif
if (!m_Box)
{
return;
}
bool created = false;
m_Backdrop = WidgetParts::ShapePart(m_Box, "Backdrop", ShapeKind::Rectangle, Deki::Color(0, 0, 0, 150));
if (m_Backdrop && !m_Backdrop->GetComponent<DekiInput::InputCollider>())
{
#ifdef DEKI_EDITOR
auto* blocker = m_Backdrop->AddComponent<DekiInput::InputCollider>();
blocker->width = kBackdropSize;
blocker->height = kBackdropSize;
#endif
}
m_Panel = WidgetParts::ShapePart(m_Box, "Panel", ShapeKind::Rectangle, WidgetColors::Panel, 0.6f);
m_Title = WidgetParts::TextPart(m_Box, "Title", WidgetColors::Text);
m_Message = WidgetParts::TextPart(m_Box, "Message", WidgetColors::DimText);
m_Buttons.clear();
for (int32_t i = 0; i < kMaxMsgboxButtons; ++i)
{
char name[16];
std::snprintf(name, sizeof(name), "Button%d", static_cast<int>(i));
const Deki::Color color = i == 0 ? WidgetColors::Accent : WidgetColors::Track;
Deki::Object* button = WidgetParts::FindOrCreate<ShapeComponent>(m_Box, name, created);
if (!button)
{
break; // on the device: the buttons the scene has
}
if (created)
{
if (ShapeComponent* shape = WidgetParts::Shape(button))
{
shape->color = color;
shape->cornerRadius = 0.4f;
}
auto* collider = button->AddComponent<DekiInput::InputCollider>();
(void)collider;
auto* press = button->AddComponent<ButtonComponent>();
press->inputCollider.Set(button);
}
WidgetParts::TextPart(button, "Label", WidgetColors::Text);
Deki::Object* ring = WidgetParts::ShapePart(button, "Focus", ShapeKind::Rectangle, WidgetColors::Focus, 0.5f);
if (ring && created)
{
ring->SetActive(false);
}
m_Buttons.push_back(button);
}
for (Deki::Object* part : m_Box->GetChildren())
{
if (auto* renderer = part->GetComponent<DekiRendering::RendererComponent>())
{
renderer->sortingOrder = kMsgboxSortingOrder;
renderer->ignoreClip = true;
}
}
ShowBox();
}
void MsgboxComponent::LayoutParts()
{
if (!m_Box)
{
return;
}
ShowBox();
WidgetParts::PlaceShape(m_Backdrop, 0.0f, 0.0f, kBackdropSize, kBackdropSize);
WidgetParts::PlaceShape(m_Panel, 0.0f, 0.0f, width, height);
const float pad = 0.6f;
const float titleHeight = 1.6f;
const float buttonHeight = 1.75f;
const float inner = std::max(0.0f, width - pad * 2.0f);
WidgetParts::PlaceText(m_Title, 0.0f, height * 0.5f - pad - titleHeight * 0.5f, inner, titleHeight);
WidgetParts::SetPartText(m_Title, title);
const float messageTop = height * 0.5f - pad - titleHeight;
const float messageBottom = -height * 0.5f + pad * 2.0f + buttonHeight;
const float messageHeight = std::max(0.0f, messageTop - messageBottom);
WidgetParts::PlaceText(m_Message, 0.0f, (messageTop + messageBottom) * 0.5f, inner, messageHeight);
WidgetParts::SetPartText(m_Message, message);
const int32_t count =
std::min<int32_t>(static_cast<int32_t>(buttons.size()), static_cast<int32_t>(m_Buttons.size()));
const float buttonWidth =
count > 0 ? (inner - pad * static_cast<float>(count - 1)) / static_cast<float>(count) : 0.0f;
const float buttonY = -height * 0.5f + pad + buttonHeight * 0.5f;
for (int32_t i = 0; i < static_cast<int32_t>(m_Buttons.size()); ++i)
{
Deki::Object* button = m_Buttons[i];
button->SetActive(i < count);
if (i >= count)
{
continue;
}
const float x = -inner * 0.5f + buttonWidth * 0.5f + (buttonWidth + pad) * static_cast<float>(i);
WidgetParts::PlaceShape(button, x, buttonY, buttonWidth, buttonHeight);
if (auto* collider = button->GetComponent<DekiInput::InputCollider>())
{
collider->width = buttonWidth;
collider->height = buttonHeight;
}
Deki::Object* label = WidgetParts::Find(button, "Label");
WidgetParts::PlaceText(label, 0.0f, 0.0f, buttonWidth, buttonHeight);
WidgetParts::SetPartText(label, buttons[i]);
Deki::Object* ring = WidgetParts::Find(button, "Focus");
WidgetParts::PlaceShape(ring, 0.0f, 0.0f, buttonWidth + 0.45f, buttonHeight + 0.45f);
if (ShapeComponent* shape = WidgetParts::Shape(ring))
{
shape->lineWidth = 0.14f;
}
}
}
} // namespace Deki2D