Elevate Engine 1
Loading...
Searching...
No Matches
InputBuffer.cpp
Go to the documentation of this file.
1#include "eepch.h"
2
6#include "InputBuffer.h"
7
8namespace Elevate
9{
11 // Keyboard
12 bool InputBuffer::IsKeyPressed(int keycode)
13 {
14 return GetKeyState(keycode) == EE_STATE_PRESSED;
15 }
16 bool InputBuffer::IsKeyDown(int keycode)
17 {
18 return GetKeyState(keycode) == EE_STATE_DOWN;
19 }
20 bool InputBuffer::IsKeyUp(int keycode)
21 {
22 return GetKeyState(keycode) == EE_STATE_UP;
23 }
24 // Mouse
25 bool InputBuffer::IsMouseButtonPressed(int mouseButtonCode)
26 {
27 return GetMouseButtonState(mouseButtonCode) == EE_STATE_PRESSED;
28 }
29 bool InputBuffer::IsMouseButtonDown(int mouseButtonCode)
30 {
31 return GetMouseButtonState(mouseButtonCode) == EE_STATE_DOWN;
32 }
33 bool InputBuffer::IsMouseButtonUp(int mouseButtonCode)
34 {
35 return GetMouseButtonState(mouseButtonCode) == EE_STATE_UP;
36 }
37
39 // Keyboard
40 void InputBuffer::SetKeyPressed(int keycode, int repeatCount)
41 {
42 // if key is not down and not repeating -> down
43 if (GetKeyState(keycode) != EE_STATE_DOWN && repeatCount == 0)
44 {
45 SetKeyState(keycode, EE_STATE_DOWN);
46 }
47 else // else -> pressed
48 {
49 SetKeyState(keycode, EE_STATE_PRESSED);
50 }
51 }
53 {
54 // if key is down or pressed -> up
55 if (GetKeyState(keycode) == EE_STATE_DOWN || GetKeyState(keycode) == EE_STATE_PRESSED)
56 {
57 SetKeyState(keycode, EE_STATE_UP);
58 }
59 }
60
61 // Mousse
62 void InputBuffer::SetMouseButtonPressed(int mouseButtonCode)
63 {
64 // if key is not down
65 if (GetMouseButtonState(mouseButtonCode) != EE_STATE_DOWN)
66 {
67 SetMouseButtonState(mouseButtonCode, EE_STATE_DOWN);
68 }
69 else // else -> pressed
70 {
71 SetMouseButtonState(mouseButtonCode, EE_STATE_PRESSED);
72 }
73 }
74 void InputBuffer::SetMouseButtonReleased(int mouseButtonCode)
75 {
76 // if key is down or pressed -> up
77 if (GetMouseButtonState(mouseButtonCode) == EE_STATE_DOWN || GetMouseButtonState(mouseButtonCode) == EE_STATE_PRESSED)
78 {
79 SetMouseButtonState(mouseButtonCode, EE_STATE_UP);
80 }
81 }
82
84 {
85 keyStates.reset();
86 mouseButtonStates.reset();
87 }
88
90 {
91 // Keyboard
92 for (size_t i = 0; i < keyStates.size() / 2; i++)
93 {
94 uint8_t state = GetKeyState(i);
95 switch (state)
96 {
97 case EE_STATE_DOWN:
98 SetKeyState(i, EE_STATE_PRESSED);
99 break;
100 case EE_STATE_UP:
101 SetKeyState(i, EE_STATE_RELEASED);
102 break;
103 }
104 }
105
106 // Mouse
107 for (size_t i = 0; i < mouseButtonStates.size() / 2; i++)
108 {
109 uint8_t state = GetMouseButtonState(i);
110 switch (state)
111 {
112 case EE_STATE_DOWN:
113 SetMouseButtonState(i, EE_STATE_PRESSED);
114 break;
115 case EE_STATE_UP:
116 SetMouseButtonState(i, EE_STATE_RELEASED);
117 break;
118 }
119 }
120 }
121
122 // Bitset operation helper
123 std::uint8_t InputBuffer::GetKeyState(size_t index) const
124 {
125 size_t bitPos = index * 2;
126 EE_CORE_ASSERT(index >= 0 && bitPos < keyStates.size(), "InputBuffer::GetKeyState() : Invalid key index.");
127
128 return (keyStates[bitPos] ? 2 : 0) | (keyStates[bitPos + 1] ? 1 : 0);
129 }
130
131 void InputBuffer::SetKeyState(size_t index, std::uint8_t value)
132 {
133 size_t bitPos = index * 2;
134 EE_CORE_ASSERT(index >= 0 && bitPos < keyStates.size(), "InputBuffer::SetKeyState() : Invalid key index.");
135
136 keyStates[bitPos] = (value & 0x02) != 0;
137 keyStates[bitPos + 1] = (value & 0x01) != 0;
138 }
139
140 std::uint8_t InputBuffer::GetMouseButtonState(size_t index) const
141 {
142 size_t bitPos = index * 2;
143 EE_CORE_ASSERT(index >= 0 && bitPos < keyStates.size(), "InputBuffer::GetMouseButtonState() : Invalid buton index.");
144
145 return (mouseButtonStates[bitPos] ? 2 : 0) | (mouseButtonStates[bitPos + 1] ? 1 : 0);
146 }
147 void InputBuffer::SetMouseButtonState(size_t index, std::uint8_t value)
148 {
149 size_t bitPos = index * 2;
150 EE_CORE_ASSERT(index >= 0 && bitPos < keyStates.size(), "InputBuffer::SetMouseButtonState() : Invalid buton index.");
151
152 mouseButtonStates[bitPos] = (value & 0x02) != 0;
153 mouseButtonStates[bitPos + 1] = (value & 0x01) != 0;
154 }
155}
#define EE_STATE_UP
Definition Input.h:22
#define EE_STATE_RELEASED
Definition Input.h:19
#define EE_STATE_PRESSED
Definition Input.h:21
#define EE_STATE_DOWN
Definition Input.h:20
void SetKeyReleased(int keycode)
bool IsKeyPressed(int keycode)
Getter.
void SetKeyPressed(int keycode, int repeatCount)
Setter.
void SetMouseButtonReleased(int mouseButtonCode)
bool IsKeyUp(int keycode)
bool IsKeyDown(int keycode)
bool IsMouseButtonPressed(int mouseButtonCode)
void SetMouseButtonPressed(int mouseButtonCode)
bool IsMouseButtonUp(int mouseButtonCode)
bool IsMouseButtonDown(int mouseButtonCode)