-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonComponent.cpp
More file actions
167 lines (145 loc) · 3.94 KB
/
ButtonComponent.cpp
File metadata and controls
167 lines (145 loc) · 3.94 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
#include "ButtonComponent.h"
#include "deki-input/InputCollider.h"
#include "DekiObject.h"
#include "DekiLogSystem.h"
ButtonComponent::ButtonComponent()
: state(ButtonState::Normal),
is_enabled(true),
m_WasPressedInside(false)
{
}
ButtonComponent::~ButtonComponent()
{
}
void ButtonComponent::Start()
{
InputCollider* collider = input_collider.Get();
if (!collider)
{
DEKI_LOG_WARNING("ButtonComponent: No InputCollider referenced on '%s'",
GetOwner()->GetName().c_str());
return;
}
// Register pointer callbacks on InputCollider
collider->on_pointer_down.push_back([this](float x, float y) {
(void)x; (void)y;
if (!is_enabled) return;
m_WasPressedInside = true;
SetState(ButtonState::Pressed);
InvokeCallbacks(on_press);
});
collider->on_pointer_up.push_back([this](float x, float y) {
(void)x; (void)y;
if (!is_enabled) return;
if (m_WasPressedInside)
{
InvokeCallbacks(on_release);
// Click = was pressed inside and released inside collider
InputCollider* col = input_collider.Get();
if (col && col->IsPointerInside())
{
SetState(ButtonState::Normal);
InvokeCallbacks(on_click);
}
else
{
SetState(ButtonState::Normal);
}
m_WasPressedInside = false;
}
});
collider->on_pointer_enter.push_back([this](float x, float y) {
(void)x; (void)y;
if (!is_enabled) return;
if (!m_WasPressedInside)
{
SetState(ButtonState::Hovered);
}
});
collider->on_pointer_exit.push_back([this](float x, float y) {
(void)x; (void)y;
if (!is_enabled) return;
if (m_WasPressedInside)
{
SetState(ButtonState::Normal);
}
else if (state == ButtonState::Hovered)
{
SetState(ButtonState::Normal);
}
});
}
void ButtonComponent::SetState(ButtonState new_state)
{
if (state == new_state)
return;
ButtonState old_state = state;
state = new_state;
// Trigger hover callbacks
if (old_state != ButtonState::Hovered && new_state == ButtonState::Hovered)
{
InvokeCallbacks(on_hover_enter);
}
else if (old_state == ButtonState::Hovered && new_state != ButtonState::Hovered)
{
InvokeCallbacks(on_hover_exit);
}
// Notify state change listeners
for (const auto& cb : on_state_changed)
{
if (cb) cb(new_state);
}
}
void ButtonComponent::SetEnabled(bool enabled)
{
is_enabled = enabled;
if (!enabled)
{
SetState(ButtonState::Disabled);
m_WasPressedInside = false;
}
else if (state == ButtonState::Disabled)
{
SetState(ButtonState::Normal);
}
}
void ButtonComponent::AddOnClickCallback(const ButtonCallback& callback)
{
on_click.push_back(callback);
}
void ButtonComponent::AddOnPressCallback(const ButtonCallback& callback)
{
on_press.push_back(callback);
}
void ButtonComponent::AddOnReleaseCallback(const ButtonCallback& callback)
{
on_release.push_back(callback);
}
void ButtonComponent::AddOnHoverEnterCallback(const ButtonCallback& callback)
{
on_hover_enter.push_back(callback);
}
void ButtonComponent::AddOnHoverExitCallback(const ButtonCallback& callback)
{
on_hover_exit.push_back(callback);
}
void ButtonComponent::AddOnStateChangedCallback(const std::function<void(ButtonState)>& callback)
{
on_state_changed.push_back(callback);
}
void ButtonComponent::CancelPress()
{
if (m_WasPressedInside)
{
m_WasPressedInside = false;
SetState(ButtonState::Normal);
InvokeCallbacks(on_release);
}
}
void ButtonComponent::InvokeCallbacks(const std::vector<ButtonCallback>& callbacks)
{
for (const auto& cb : callbacks)
{
if (cb) cb();
}
}