Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
event_listener.h
1 #pragma once
2 
3 #include <list>
4 #include "types.h"
5 #include "keycodes.h"
6 #include "macros.h"
7 #include "input/input_state.h"
8 
9 namespace smlt {
10 
11 /* EventListeners can directly handle events received by the window
12  * (e.g. input)
13  *
14  * An EventListener must be properly registered/unregistered with the window
15  * with window->register_event_listener() and window->unregister_event_listener()
16  */
17 
18 typedef uint32_t TouchPointID;
19 
20 enum TouchEventType {
21  TOUCH_EVENT_TYPE_FINGER_DOWN,
22  TOUCH_EVENT_TYPE_FINGER_UP,
23  TOUCH_EVENT_TYPE_FINGER_MOVE
24 };
25 
26 struct TouchEvent {
27  TouchEventType type;
28  TouchPointID touch_id;
29  Vec2 coord;
30  Vec2 normalized_coord;
31  Vec2 movement;
32  float pressure;
33 };
34 
35 enum KeyEventType {
36  KEY_EVENT_TYPE_KEY_DOWN,
37  KEY_EVENT_TYPE_KEY_UP
38 };
39 
41  bool lshift = false;
42  bool rshift = false;
43  bool lctrl = false;
44  bool rctrl = false;
45  bool lalt = false;
46  bool ralt = false;
47  bool lsuper = false;
48  bool rsuper = false;
49  bool num_lock = false;
50  bool caps_lock = false;
51  bool mode = false; // AltGr
52 
53  bool ctrl() const { return lctrl || rctrl; }
54  bool shift() const { return lshift || rshift; }
55  bool alt() const { return lalt || ralt; }
56  bool super() const { return lsuper || rsuper; }
57 };
58 
59 struct KeyEvent {
60  KeyEventType type;
61  KeyboardCode keyboard_code;
62  ModifierKeyState modifiers;
63 };
64 
65 enum GameControllerEventType {
66  GAME_CONTROLLER_EVENT_TYPE_BUTTON_DOWN,
67  GAME_CONTROLLER_EVENT_TYPE_BUTTON_UP
68 };
69 
71  GameControllerIndex index;
72  GameControllerEventType type;
73  JoystickButton button;
74 };
75 
77 public:
78  EventListener() {}
79  virtual ~EventListener() {}
80 
81  void handle_touch_begin(Window* window, TouchPointID touch_id, float normalized_x, float normalized_y, float pressure);
82  void handle_touch_end(Window* window, TouchPointID touch_id, float normalized_x, float normalized_y);
83  void handle_touch_move(Window* window, TouchPointID touch_id, float normalized_x, float normalized_y, float dx, float dy);
84 
85  void handle_key_down(Window* window, KeyboardCode code, ModifierKeyState modifiers);
86  void handle_key_up(Window* window, KeyboardCode code, ModifierKeyState modifiers);
87 
88  void handle_controller_button_down(GameControllerIndex controller, JoystickButton button);
89  void handle_controller_button_up(GameControllerIndex controller, JoystickButton button);
90 
91 private:
92  virtual void on_key_down(const KeyEvent& evt) {
93  _S_UNUSED(evt);
94  }
95 
96  virtual void on_key_up(const KeyEvent& evt) {
97  _S_UNUSED(evt);
98  }
99 
100  virtual void on_touch_begin(const TouchEvent& evt) {
101  _S_UNUSED(evt);
102  }
103 
104  virtual void on_touch_end(const TouchEvent& evt) {
105  _S_UNUSED(evt);
106  }
107 
108  virtual void on_touch_move(const TouchEvent& evt) {
109  _S_UNUSED(evt);
110  }
111 
112  virtual void on_game_controller_button_down(const GameControllerEvent& evt) {
113  _S_UNUSED(evt);
114  }
115 
116  virtual void on_game_controller_button_up(const GameControllerEvent& evt) {
117  _S_UNUSED(evt);
118  }
119 
120  virtual void on_window_focus() {}
121  virtual void on_window_blur() {}
122  virtual void on_window_minimize() {}
123  virtual void on_window_restore() {}
124 };
125 
127 public:
129  virtual ~EventListenerManager() {}
130 
131  void register_event_listener(EventListener* listener);
132  void unregister_event_listener(EventListener* listener);
133 
134  void each_event_listener(std::function<void (EventListener*)> callback);
135 
136 private:
137  std::list<EventListener*> listeners_;
138 };
139 
140 }
smlt::EventListener
Definition: event_listener.h:76
smlt::GameControllerEvent
Definition: event_listener.h:70
smlt
Definition: animation.cpp:25
smlt::Window
Definition: window.h:65
smlt::KeyEvent
Definition: event_listener.h:59
smlt::ModifierKeyState
Definition: event_listener.h:40
smlt::EventListenerManager
Definition: event_listener.h:126
smlt::TouchEvent
Definition: event_listener.h:26
smlt::Vec2
Definition: vec2.h:14