jubilant-funicular
InputManager.cpp
1 #include "nta/InputManager.h"
2 
3 namespace nta {
4  glm::vec2 InputManager::getMouseCoords() const {
5  return m_mouseCoords;
6  }
7  glm::vec2 InputManager::getMouseCoordsStandard(int height) const {
8  return glm::vec2(m_mouseCoords.x,height-m_mouseCoords.y);
9  }
11  return m_mouseWheelMotion;
12  }
13  bool InputManager::isPressed(unsigned int key) const {
14  auto it = m_keyMap.find(key);
15  return it == m_keyMap.end() ? false : it->second;
16  }
17  bool InputManager::justPressed(unsigned int key) const {
18  auto it = m_prevKeyMap.find(key);
19  return it == m_prevKeyMap.end() ? isPressed(key) : isPressed(key) && !it->second;
20  }
21  bool InputManager::justReleased(unsigned int key) const {
22  auto it = m_prevKeyMap.find(key);
23  return it == m_prevKeyMap.end() ? false : !isPressed(key) && it->second;
24  }
25  void InputManager::pressKey(unsigned int key) {
26  m_keyMap[key] = true;
27  }
28  void InputManager::releaseKey(unsigned int key) {
29  m_keyMap[key] = false;
30  }
31  void InputManager::setMouseCoords(float x, float y) {
32  m_mouseCoords.x = x;
33  m_mouseCoords.y = y;
34  }
36  m_mouseWheelMotion = motion;
37  }
38  void InputManager::update(SDL_Event& event) {
39  update_keys(event);
40  update_mouse(event);
41  }
42  void InputManager::update_keys(SDL_Event& event) {
43  switch(event.type) {
44  case SDL_KEYDOWN: pressKey(event.key.keysym.sym); break;
45  case SDL_KEYUP: releaseKey(event.key.keysym.sym); break;
46  }
47  }
48  void InputManager::update_mouse(SDL_Event& event) {
49  switch(event.type) {
50  case SDL_MOUSEMOTION: setMouseCoords(event.motion.x, event.motion.y); break;
51  case SDL_MOUSEBUTTONDOWN: pressKey(event.button.button); break;
52  case SDL_MOUSEBUTTONUP: releaseKey(event.button.button); break;
53  case SDL_MOUSEWHEEL: setMouseWheelMotion((nta::MouseWheelMotion)glm::sign(event.wheel.y)); break;
54  }
55  }
57  m_prevKeyMap.clear();
58  m_prevKeyMap.insert(m_keyMap.begin(), m_keyMap.end());
59  }
60 };
nta::InputManager::m_mouseWheelMotion
MouseWheelMotion m_mouseWheelMotion
stores the motion of the mouse wheel
Definition: InputManager.h:31
nta::InputManager::getMouseCoordsStandard
glm::vec2 getMouseCoordsStandard(int height) const
returns the mouse's coordinates with the y axis flipped (0 represents the bottom of the screen instea...
Definition: InputManager.cpp:7
nta::InputManager::getMouseWheelMotion
MouseWheelMotion getMouseWheelMotion() const
returns the mouse wheel's motion
Definition: InputManager.cpp:10
nta::InputManager::update
void update(SDL_Event &event)
updates internal state
Definition: InputManager.cpp:38
nta::InputManager::update_mouse
void update_mouse(SDL_Event &event)
update mouse information
Definition: InputManager.cpp:48
nta::InputManager::updatePrev
void updatePrev()
updates the state of m_prevKeyMap
Definition: InputManager.cpp:56
nta::InputManager::m_keyMap
std::unordered_map< key_type, bool > m_keyMap
stores whether each key is pressed or not
Definition: InputManager.h:25
nta::InputManager::pressKey
void pressKey(key_type key)
tells InputManager that specified key was pressed
Definition: InputManager.cpp:25
nta::InputManager::getMouseCoords
glm::vec2 getMouseCoords() const
returns the mouse's coordinates
Definition: InputManager.cpp:4
nta
Definition: Animation2D.h:6
nta::InputManager::update_keys
void update_keys(SDL_Event &event)
updates key information
Definition: InputManager.cpp:42
nta::InputManager::m_mouseCoords
glm::vec2 m_mouseCoords
stores the location of the mouse in mouse coordinates
Definition: InputManager.h:29
nta::MouseWheelMotion
MouseWheelMotion
represents the way a mouse wheel was rolled
Definition: InputManager.h:18
nta::InputManager::setMouseWheelMotion
void setMouseWheelMotion(const MouseWheelMotion &motion)
tells InputManager how the wheel is rolling
Definition: InputManager.cpp:35
nta::InputManager::justPressed
bool justPressed(key_type key) const
returns whether or not the key was just pressed this frame
Definition: InputManager.cpp:17
nta::InputManager::isPressed
bool isPressed(key_type key) const
returns whether or not specified key is pressed
Definition: InputManager.cpp:13
nta::InputManager::justReleased
bool justReleased(key_type key) const
returns whether or not the key was just released this frame
Definition: InputManager.cpp:21
nta::InputManager::releaseKey
void releaseKey(key_type key)
tells InputManager that specified key was released
Definition: InputManager.cpp:28
nta::InputManager::setMouseCoords
void setMouseCoords(float x, float y)
tells InputManager where the mouse is
Definition: InputManager.cpp:31
nta::InputManager::m_prevKeyMap
std::unordered_map< key_type, bool > m_prevKeyMap
stores whether the key was pressed last frame or not
Definition: InputManager.h:27