Repository navigation
Expand file tree
/
Copy pathCheckboxComponent.h
More file actions
120 lines (95 loc) · 3.2 KB
/
Copy pathCheckboxComponent.h
File metadata and controls
120 lines (95 loc) · 3.2 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
#pragma once
#include <stdint.h>
#include <functional>
#include <string>
#include <vector>
#include <deki/Color.h>
#include <deki/Component.h>
#include <deki/reflection/ObjectRef.h>
#include <deki/reflection/Property.h>
#include "FocusSystem.h"
namespace Deki
{
class Object;
}
namespace DekiInput
{
class InputCollider;
}
namespace Deki2D
{
using CheckCallback = std::function<void(bool checked)>;
/// A box that ticks on and off, with a label beside it. A tap anywhere on
/// it flips it, and so do Enter and Space while it has keyboard focus.
///
/// Its parts are children the editor makes: Box, Check (the tick), Label
/// and Focus.
DEKI_CATEGORY("2D")
DEKI_DESCRIPTION("A box that ticks on and off, with a label.")
class CheckboxComponent : public Deki::Component, public IFocusable
{
public:
DEKI_EXPORT
DEKI_TOOLTIP("The hit area that catches the tap. The editor adds one on the same object and sizes it to the "
"box and label.")
Deki::ObjectRef<DekiInput::InputCollider> inputCollider;
DEKI_EXPORT
DEKI_TOOLTIP("Whether the box is ticked.")
bool isChecked = false;
DEKI_EXPORT
DEKI_TOOLTIP("A disabled checkbox ignores taps and keys.")
bool isEnabled = true;
DEKI_EXPORT
DEKI_TOOLTIP("The text beside the box.")
std::string label = "Checkbox";
DEKI_EXPORT
DEKI_TOOLTIP("Width of the box and its label together, in meters.")
DEKI_UNIT(Distance)
float width = 8.0f;
DEKI_EXPORT
DEKI_TOOLTIP("Size of the box in meters. The checkbox is this tall.")
DEKI_UNIT(Distance)
float boxSize = 1.5f;
DEKI_EXPORT
DEKI_TOOLTIP("Box colour while ticked.")
Deki::Color checkedColor = Deki::Color(64, 140, 255);
DEKI_EXPORT
DEKI_TOOLTIP("Outline colour while not ticked.")
Deki::Color uncheckedColor = Deki::Color(140, 146, 160);
// Runtime-only listeners
std::vector<CheckCallback> onToggled;
CheckboxComponent() = default;
~CheckboxComponent() override;
/// Ticks or clears it. With `notify`, the toggled callbacks run.
void SetChecked(bool checked, bool notify = true);
void Toggle() { SetChecked(!isChecked); }
void AddOnToggledCallback(const CheckCallback& callback);
/// Places and sizes the parts, making any the editor is missing.
void SyncParts();
void Start() override;
bool NeedsRuntimeUpdate() const override { return true; }
void RuntimeUpdate(float deltaTime) override;
void OnPropertyChanged(const char* propertyName) override;
// IFocusable
Deki::Object* GetFocusObject() const override { return GetOwner(); }
bool CanFocus() const override { return isEnabled; }
void GetFocusSize(float& w, float& h) const override
{
w = width;
h = boxSize;
}
void OnFocusChanged(bool focused) override;
bool OnFocusKey(uint32_t key, uint32_t character) override;
private:
Deki::Object* m_Box = nullptr;
Deki::Object* m_Check = nullptr;
Deki::Object* m_Label = nullptr;
Deki::Object* m_Focus = nullptr;
size_t m_KnownChildren = static_cast<size_t>(-1);
uint64_t m_LastLayout = 0;
bool m_Focused = false;
bool m_Pressed = false;
void EnsureParts();
void LayoutParts();
};
} // namespace Deki2D