Castlekeep
A isometric castle build simulator
 All Classes Namespaces
input_manager.h
1 
23 #ifndef INPUT_MANAGER_H
24 #define INPUT_MANAGER_H
25 
26 #include <map>
27 #include <forward_list>
28 
29 #include "types.h"
30 #include "keycodes.h"
31 
32 namespace CastleKeep
33 {
34  class Window;
35  class Game;
36  enum class EventType
37  {
38  MOUSE_BUTTON_PRESS,
39  MOUSE_BUTTON_RELEASE,
40  MOUSE_MOTION,
41  MOUSE_WHEEL,
42  KEY_PRESS,
43  KEY_RELEASE,
44  QUIT,
45  };
46 
47  enum class MouseButton { LEFT,MIDDLE,RIGHT };
48 
50  {
51  bool left;
52  bool middle;
53  bool right;
54  };
55 
56  class Event
57  {
58  public:
59  EventType type;
60  };
61 
62  class MouseButtonEvent : public Event
63  {
64  public:
65  MouseButton button;
66  Pos pos;
67  };
68 
69  class MouseMotionEvent : public Event
70  {
71  public:
72  Pos pos;
73  Pos relative_pos;
74  MouseButtonState button_state;
75  };
76 
77  class MouseWheelEvent : public Event
78  {
79  public:
80  int amount;
81  };
82 
83  class KeyEvent : public Event
84  {
85  public:
86  KeyCode key;
87  };
88 
90  {
91  public:
92  virtual void onMouseButtonPress(const MouseButtonEvent &event)=0;
93  virtual void onMouseButtonRelease(const MouseButtonEvent &event)=0;
94  };
95 
97  {
98  public:
99  virtual void onMouseMove(const MouseMotionEvent &event)=0;
100  };
101 
103  {
104  public:
105  virtual void onMouseScroll(const MouseWheelEvent &event)=0;
106  };
107 
109  {
110  public:
111  virtual void onKeyPress(const KeyEvent &event)=0;
112  virtual void onKeyRelease(const KeyEvent &event)=0;
113  };
114 
116  {
117  public:
118  virtual void onQuit()=0;
119  };
120 
122  {
123  public:
124  InputManager(Game *game, Window *window);
125  virtual ~InputManager();
126 
127  void addMouseButtonEventListener(MouseButtonEventListener *listener);
128 
129  void addMouseMotionEventListener(MouseMotionEventListener *listener);
130 
131  void addMouseWheelEventListener(MouseWheelEventListener *listener);
132 
133  void addKeyEventListener(KeyEventListener *listener);
134 
135  void addQuitEventListener(QuitEventListener *listener);
136 
137  bool getKeyState(KeyCode key);
138 
139  bool getMouseButtonState(MouseButton button);
140 
141  const Pos& getMousePosition();
142 
143  const MouseButtonState& getMouseButtonState();
144 
145  virtual void processInput();
146 
147  protected:
148 
149  void notifyMouseButtonEventListeners(const MouseButtonEvent &event);
150 
151  void notifyMouseMotionEventListeners(const MouseMotionEvent &event);
152 
153  void notifyMouseWheelEventListeners(const MouseWheelEvent &event);
154 
155  void notifyKeyEventListeners(const KeyEvent &event);
156 
157  bool notifyQuitEventListener();
158 
159  MouseButtonState _mouse_state;
160  Pos _mouse_position;
161  std::map<KeyCode,bool> _key_state;
162  std::forward_list<MouseButtonEventListener*> _mouse_button_listeners;
163  std::forward_list<MouseMotionEventListener*> _mouse_motion_listeners;
164  std::forward_list<MouseWheelEventListener*> _mouse_wheel_listeners;
165  std::forward_list<KeyEventListener*> _key_listeners;
166  QuitEventListener *_quit_listener;
167  Game *_game;
168  Window *_window;
169  };
170 }
171 #endif
Definition: input_manager.h:89
Definition: input_manager.h:77
Definition: input_manager.h:96
Definition: types.h:36
Definition: input_manager.h:62
Definition: input_manager.h:102
Definition: input_manager.h:108
Definition: input_manager.h:83
Definition: input_manager.h:121
Definition: input_manager.h:49
Definition: input_manager.h:69
Definition: input_manager.h:115
Definition: input_manager.h:56
Definition: window.h:29
Definition: game.h:34