BARE2D
InputManager.cpp
Go to the documentation of this file.
1
#include "
InputManager.hpp
"
2
3
#include <SDL2/SDL_keycode.h>
4
5
namespace
BARE2D
{
6
7
InputManager::InputManager
()
8
{}
9
10
InputManager::~InputManager
()
11
{}
12
13
void
InputManager::update
()
14
{
15
// Update the previous keymap
16
for
(
auto
& iterator :
m_keyMap
)
17
{
18
m_previousKeyMap
[iterator.first] = iterator.second;
19
}
20
21
// As this is essentially the \delta mouseScrollWheel variable, it needs to be set to zero each update
22
m_mouseScrollwheelPosition
= 0;
23
}
24
25
void
InputManager::pressKey
(
unsigned
int
keyID)
26
{
27
// True represents pressed
28
m_keyMap
[keyID] =
true
;
29
}
30
31
void
InputManager::releaseKey
(
unsigned
int
keyID)
32
{
33
// False represents released
34
m_keyMap
[keyID] =
false
;
35
}
36
37
void
InputManager::setMousePosition
(
float
x,
float
y)
38
{
39
m_mousePosition
.x = x;
40
m_mousePosition
.y = y;
41
}
42
43
void
InputManager::setMousePosition
(glm::vec2 position)
44
{
45
m_mousePosition
= position;
46
}
47
48
void
InputManager::setMouseScrollwheelPosition
(
int
position)
49
{
50
m_mouseScrollwheelPosition
= position;
51
}
52
53
bool
InputManager::isKeyDown
(
unsigned
int
keyID)
const
54
{
55
// We dont want to use the associative array approach here
56
// because we don't want to create a key if it doesnt exist.
57
// So we do it manually
58
auto
it =
m_keyMap
.find(keyID);
59
if
(it !=
m_keyMap
.end())
60
{
61
// Found the key
62
return
it->second;
63
}
else
64
{
65
// Didn't find the key
66
return
false
;
67
}
68
}
69
70
bool
InputManager::isKeyPressed
(
unsigned
int
keyID)
const
71
{
72
// Should return true if it is pressed now and wasn't before.
73
return
(
isKeyDown
(keyID) && !
wasKeyDown
(keyID));
74
}
75
76
char
InputManager::getPressedCharacter
(
bool
alpha,
bool
nums,
bool
all)
const
77
{
78
if
(all)
79
{
80
for
(
unsigned
int
i = 33; i <= 126; i++)
81
{
82
if
(
isKeyPressed
(SDLK_EXCLAIM + i))
83
{
84
return
'!'
+ i;
85
}
86
}
87
}
88
return
'\0'
;
89
if
(alpha)
90
{
91
for
(
unsigned
int
i = 0; i <= 26; i++)
92
{
93
if
(
isKeyPressed
(SDLK_a + i))
94
{
95
if
(
isKeyDown
(SDLK_LSHIFT) ||
isKeyDown
(SDLK_RSHIFT))
96
{
97
return
'A'
+ i;
98
}
99
return
'a'
+ i;
100
}
101
}
102
}
103
if
(nums)
104
{
105
for
(
unsigned
int
i = 0; i <= 9; i++)
106
{
107
if
(
isKeyPressed
(SDLK_0 + i))
108
{
109
return
'0'
+ i;
110
}
111
}
112
}
113
}
114
115
glm::vec2
InputManager::getMousePosition
()
const
116
{
117
return
m_mousePosition
;
118
}
119
120
int
InputManager::getMouseScrollwheelPosition
()
const
121
{
122
return
m_mouseScrollwheelPosition
;
123
}
124
125
bool
InputManager::wasKeyDown
(
unsigned
int
keyID)
const
126
{
127
// We dont want to use the associative array approach here
128
// because we don't want to create a key if it doesnt exist.
129
// So we do it manually
130
auto
it =
m_previousKeyMap
.find(keyID);
131
if
(it !=
m_previousKeyMap
.end())
132
{
133
// Found the key
134
return
it->second;
135
}
else
136
{
137
// Didn't find the key
138
return
false
;
139
}
140
}
141
142
}
// namespace BARE2D
BARE2D
Definition:
App.cpp:13
BARE2D::InputManager::update
void update()
Updates the inputmanager. Should be called every frame.
Definition:
InputManager.cpp:13
BARE2D::InputManager::~InputManager
~InputManager()
Definition:
InputManager.cpp:10
BARE2D::InputManager::wasKeyDown
bool wasKeyDown(unsigned int keyID) const
Checks if the key was down last frame.
Definition:
InputManager.cpp:125
BARE2D::InputManager::InputManager
InputManager()
Definition:
InputManager.cpp:7
BARE2D::InputManager::getPressedCharacter
char getPressedCharacter(bool alpha=true, bool nums=true, bool all=true) const
Gets entered character (This just follows the first character that has been pressed this update)
Definition:
InputManager.cpp:76
BARE2D::InputManager::m_previousKeyMap
std::unordered_map< unsigned int, bool > m_previousKeyMap
Definition:
InputManager.hpp:98
BARE2D::InputManager::pressKey
void pressKey(unsigned int keyID)
Sets a key as 'pressed'.
Definition:
InputManager.cpp:25
BARE2D::InputManager::getMouseScrollwheelPosition
int getMouseScrollwheelPosition() const
Gets the position of the mousewheel.
Definition:
InputManager.cpp:120
BARE2D::InputManager::getMousePosition
glm::vec2 getMousePosition() const
Gets the position of the mouse in the window.
Definition:
InputManager.cpp:115
InputManager.hpp
BARE2D::InputManager::m_keyMap
std::unordered_map< unsigned int, bool > m_keyMap
Definition:
InputManager.hpp:97
BARE2D::InputManager::isKeyDown
bool isKeyDown(unsigned int keyID) const
Checks if the key is down at all.
Definition:
InputManager.cpp:53
BARE2D::InputManager::m_mousePosition
glm::vec2 m_mousePosition
Definition:
InputManager.hpp:99
BARE2D::InputManager::m_mouseScrollwheelPosition
int m_mouseScrollwheelPosition
Definition:
InputManager.hpp:100
BARE2D::InputManager::releaseKey
void releaseKey(unsigned int keyID)
Sets a key as 'released'.
Definition:
InputManager.cpp:31
BARE2D::InputManager::isKeyPressed
bool isKeyPressed(unsigned int keyID) const
Checks if the key has just been pressed.
Definition:
InputManager.cpp:70
BARE2D::InputManager::setMousePosition
void setMousePosition(float x, float y)
Sets the mouse position to (x,y)
Definition:
InputManager.cpp:37
BARE2D::InputManager::setMouseScrollwheelPosition
void setMouseScrollwheelPosition(int position)
Sets the "position" of the scrollwheel - essentially, how much it has moved since the last frame.
Definition:
InputManager.cpp:48
Source
InputManager.cpp
Generated by
1.8.17