Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonComponent.cpp
More file actions
Latest commit
167 lines (145 loc) · 3.91 KB
/
Copy pathButtonComponent.cpp
File metadata and controls
167 lines (145 loc) · 3.91 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),
isEnabled(true),
m_WasPressedInside(false)
{
}
ButtonComponent::~ButtonComponent()
{
}
voidButtonComponent::Start()
{
InputCollider* collider = inputCollider.Get();
if (!collider)
{
DEKI_LOG_WARNING("ButtonComponent: No InputCollider referenced on '%s'",
GetOwner()->GetName().c_str());
return;
}
// Register pointer callbacks on InputCollider
collider->onPointerDown.push_back([this](float x, float y) {
(void)x; (void)y;
if (!isEnabled) return;
m_WasPressedInside = true;
SetState(ButtonState::Pressed);
InvokeCallbacks(onPress);
});
collider->onPointerUp.push_back([this](float x, float y) {
(void)x; (void)y;
if (!isEnabled) return;
if (m_WasPressedInside)
{
InvokeCallbacks(onRelease);
// Click = was pressed inside and released inside collider
InputCollider* col = inputCollider.Get();
if (col && col->IsPointerInside())
{
SetState(ButtonState::Normal);
InvokeCallbacks(onClick);
}
else
{
SetState(ButtonState::Normal);
}
m_WasPressedInside = false;
}
});
collider->onPointerEnter.push_back([this](float x, float y) {
(void)x; (void)y;
if (!isEnabled) return;
if (!m_WasPressedInside)
{
SetState(ButtonState::Hovered);
}
});
collider->onPointerExit.push_back([this](float x, float y) {
(void)x; (void)y;
if (!isEnabled) return;
if (m_WasPressedInside)
{
SetState(ButtonState::Normal);
}
elseif (state == ButtonState::Hovered)
{
SetState(ButtonState::Normal);
}
});
}
voidButtonComponent::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(onHoverEnter);
}
elseif (old_state == ButtonState::Hovered && new_state != ButtonState::Hovered)
{
InvokeCallbacks(onHoverExit);
}
// Notify state change listeners
for (constauto& cb : on_state_changed)
{
if (cb) cb(new_state);
}
}
voidButtonComponent::SetEnabled(bool enabled)
{
isEnabled = enabled;
if (!enabled)
{
SetState(ButtonState::Disabled);
m_WasPressedInside = false;
}
elseif (state == ButtonState::Disabled)
{
SetState(ButtonState::Normal);
}
}
voidButtonComponent::AddOnClickCallback(const ButtonCallback& callback)
{
onClick.push_back(callback);
}
voidButtonComponent::AddOnPressCallback(const ButtonCallback& callback)
{
onPress.push_back(callback);
}
voidButtonComponent::AddOnReleaseCallback(const ButtonCallback& callback)
{
onRelease.push_back(callback);
}
voidButtonComponent::AddOnHoverEnterCallback(const ButtonCallback& callback)
{
onHoverEnter.push_back(callback);
}
voidButtonComponent::AddOnHoverExitCallback(const ButtonCallback& callback)
{
onHoverExit.push_back(callback);
}
voidButtonComponent::AddOnStateChangedCallback(const std::function<void(ButtonState)>& callback)
{
on_state_changed.push_back(callback);
}
voidButtonComponent::CancelPress()
{
if (m_WasPressedInside)
{
m_WasPressedInside = false;
SetState(ButtonState::Normal);
InvokeCallbacks(onRelease);
}
}
voidButtonComponent::InvokeCallbacks(const std::vector<ButtonCallback>& callbacks)
{
for (constauto& cb : callbacks)
{
if (cb) cb();
}
}