-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputState.cpp
More file actions
155 lines (127 loc) · 3.17 KB
/
Copy pathInputState.cpp
File metadata and controls
155 lines (127 loc) · 3.17 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
/**
*
* @file InputState.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira.
* All rights reserved.
* https://github.com/vixcpp/game
*
* Use of this source code is governed by a MIT license
* that can be found in the LICENSE file.
*
* Vix.cpp
*
*/
#include <vix/game/InputState.hpp>
namespace vix::game
{
void InputState::begin_frame()
{
previous_keys_ = current_keys_;
previous_buttons_ = current_buttons_;
pointer_delta_ = PointerDelta{};
wheel_delta_ = PointerDelta{};
}
void InputState::press_key(InputKey key)
{
if (key == InputKey::Unknown)
{
return;
}
current_keys_.insert(key);
}
void InputState::release_key(InputKey key)
{
if (key == InputKey::Unknown)
{
return;
}
current_keys_.erase(key);
}
void InputState::press_button(InputButton button)
{
if (!is_valid_input_button(button))
{
return;
}
current_buttons_.insert(button);
}
void InputState::release_button(InputButton button)
{
if (!is_valid_input_button(button))
{
return;
}
current_buttons_.erase(button);
}
void InputState::set_pointer_position(double x, double y) noexcept
{
pointer_.x = x;
pointer_.y = y;
}
void InputState::add_pointer_delta(double dx, double dy) noexcept
{
pointer_delta_.x += dx;
pointer_delta_.y += dy;
}
void InputState::add_wheel_delta(double dx, double dy) noexcept
{
wheel_delta_.x += dx;
wheel_delta_.y += dy;
}
bool InputState::key_down(InputKey key) const
{
return current_keys_.find(key) != current_keys_.end();
}
bool InputState::key_pressed(InputKey key) const
{
return current_keys_.find(key) != current_keys_.end() &&
previous_keys_.find(key) == previous_keys_.end();
}
bool InputState::key_released(InputKey key) const
{
return current_keys_.find(key) == current_keys_.end() &&
previous_keys_.find(key) != previous_keys_.end();
}
bool InputState::button_down(InputButton button) const
{
return current_buttons_.find(button) != current_buttons_.end();
}
bool InputState::button_pressed(InputButton button) const
{
return current_buttons_.find(button) != current_buttons_.end() &&
previous_buttons_.find(button) == previous_buttons_.end();
}
bool InputState::button_released(InputButton button) const
{
return current_buttons_.find(button) == current_buttons_.end() &&
previous_buttons_.find(button) != previous_buttons_.end();
}
PointerPosition InputState::pointer_position() const noexcept
{
return pointer_;
}
PointerDelta InputState::pointer_delta() const noexcept
{
return pointer_delta_;
}
PointerDelta InputState::wheel_delta() const noexcept
{
return wheel_delta_;
}
bool InputState::any_down() const noexcept
{
return !current_keys_.empty() || !current_buttons_.empty();
}
void InputState::clear()
{
current_keys_.clear();
previous_keys_.clear();
current_buttons_.clear();
previous_buttons_.clear();
pointer_ = PointerPosition{};
pointer_delta_ = PointerDelta{};
wheel_delta_ = PointerDelta{};
}
} // namespace vix::game