Hummingbird Framework
 All Classes Functions Variables Modules Pages
InputManager.h
1 #ifndef HB_INPUT_MANAGER_H
2 #define HB_INPUT_MANAGER_H
3 #include <memory>
4 #include <map>
5 #include <SFML/Window.hpp>
6 #include <SFML/Graphics.hpp>
7 #include "../Core/MessageManager.h"
8 #include "../Core/Game.h"
9 #include "Renderer.h"
10 
11 namespace hb
12 {
17  typedef sf::Keyboard Keyboard;
18  typedef sf::Joystick Joystick;
19  typedef sf::Mouse Mouse;
20  typedef sf::Event Event;
21 
23  {
24  Mouse::Button button;
25  int x, y;
26  explicit MouseButtonWindow(const Event::MouseButtonEvent& ev):
27  button(ev.button),
28  x(ev.x),
29  y(ev.y)
30  {}
31  };
32 
34  {
35  Mouse::Button button;
36  double x, y;
37  explicit MouseButtonWorld(const Event::MouseButtonEvent& ev):
38  button(ev.button),
39  x(ev.x),
40  y(ev.y)
41  {
42  x += Renderer::getWindow().getView().getCenter().x - Renderer::getWindow().getSize().x / 2;
43  y += Renderer::getWindow().getView().getCenter().y - Renderer::getWindow().getSize().y / 2;
44  Vector3d v = x * Renderer::getCamera().getInverseAxisX() + y * Renderer::getCamera().getInverseAxisY();
45  x = v.x;
46  y = v.y;
47  }
48  };
49 
50  struct KeyPressed
51  {
52  Keyboard::Key code;
53  bool alt, control, shift, system;
54  explicit KeyPressed(const Event::KeyEvent& ev):
55  code(ev.code),
56  alt(ev.alt),
57  control(ev.control),
58  shift(ev.shift),
59  system(ev.system)
60  {}
61  };
62 
63  struct KeyReleased
64  {
65  Keyboard::Key code;
66  bool alt, control, shift, system;
67  explicit KeyReleased(const Event::KeyEvent& ev):
68  code(ev.code),
69  alt(ev.alt),
70  control(ev.control),
71  shift(ev.shift),
72  system(ev.system)
73  {}
74  };
75 
76 
78  {
79  unsigned int joystickId, button;
80  explicit JoyButtonPressed(const Event::JoystickButtonEvent& ev):
81  joystickId(ev.joystickId),
82  button(ev.button)
83  {}
84  };
85 
86 
88  {
89  unsigned int joystickId, button;
90  explicit JoyButtonReleased(const Event::JoystickButtonEvent& ev):
91  joystickId(ev.joystickId),
92  button(ev.button)
93  {}
94  };
95 
96 
97  struct JoyAxis
98  {
99  unsigned int joystickId;
100  Joystick::Axis axis;
101  float position;
102  explicit JoyAxis(const Event::JoystickMoveEvent& ev):
103  joystickId(ev.joystickId),
104  axis(ev.axis),
105  position(ev.position)
106  {}
107  };
108 
110  {
111  unsigned int width, height;
112  explicit WindowResized(const Event::SizeEvent& ev):
113  width(ev.width),
114  height(ev.height)
115  {}
116  };
117 
118  class InputManager : public hb::MessageManager<MouseButtonWindow, MouseButtonWorld, KeyPressed, KeyReleased, JoyButtonPressed, JoyButtonReleased, JoyAxis, WindowResized>
119  {
120  public:
121  static InputManager* instance();
122 
123  void update();
124 
125  private:
126  static std::unique_ptr<InputManager> s_instance;
127 
128  std::map<Keyboard::Key, bool> m_pressed_keys;
129  std::map<std::pair<int, int>, bool> m_pressed_buttons;
130  };
132 }
133 #endif
Definition: InputManager.h:87
Generic message manager class.This class allows to define callbacks for a set of predefined events wi...
Definition: MessageManager.h:87
Definition: InputManager.h:63
A 3D vector.
Definition: Vector3d.h:14
Definition: InputManager.h:50
Definition: InputManager.h:77
Definition: Box2DPlugin.h:6
Definition: InputManager.h:22
Definition: InputManager.h:97
double y
y component of the vector.
Definition: Vector3d.h:18
Definition: InputManager.h:33
Definition: InputManager.h:109
double x
x component of the vector.
Definition: Vector3d.h:17
Definition: InputManager.h:118