The Game Engine  1
Input.h
Go to the documentation of this file.
1 #ifndef __INPUT_H__
2 #define __INPUT_H__
3 
4 #include "fns.h"
5 #include <SDL/SDL.h>
6 #include <map>
7 #include <set>
8 using std::map;
9 using std::set;
10 
14 enum JoyStick {
15  JoyLeft, JoyRight, DPad, Triggers/*for xbox triggers*/
16 };
17 
21 enum JoyButton {
23 };
24 
32 };
33 
34 //LeftJoyStick,
35 // RigthJoyStick,
36 // Dpad,
37 
39  static const int TRIGGERTIME = 250;
40 
42  static const int DOUBLECLICKTRIGGERTIME = 500;
43 
45  static const int THRESHHOLD = 5000;
46 
48  static const float JOYSTICKRANGE = 32767.0;
49 
62 class Input{
63 public:
64  Input(bool isJoy = false);
65 
69  static int getNumInputs();
70 
74  struct MouseState{
75  MouseState(bool s=false, Point2D p=Point2D(), Uint32 t=0, Point2D rp=Point2D()):pressed(s),pos(p),time(t),relPos(rp){}
77  bool pressed;
83  Uint32 time;
84  };
85 
89  static int updateJoysticks();
90 
95  static Input* getInput(unsigned int index=0);
96 
100  static void updateInputs(SDL_Event event);
101 
106  static pair<int,int> getDeviceCounts();
107 
112  static int getNumJoysticks();
113 
117  static void addInput(Input* i);
118 
122  static void removeInput(Input* i);
123 
128  static void addKeyDownCallback(void (*fn)(SDLKey, SDLMod));
129 
134  static void removeKeyDownCallback(void (*fn)(SDLKey, SDLMod));
135 
140  static void addKeyUpCallback(void (*fn)(SDLKey, SDLMod));
141 
146  static void removeKeyUpCallback(void (*fn)(SDLKey, SDLMod));
147 
152  static void addMouseDownCallback(void (*fn)(Uint8, MouseState));
153 
158  static void removeMouseDownCallback(void (*fn)(Uint8, MouseState));
159 
164  static void addMouseUpCallback(void (*fn)(Uint8, MouseState));
165 
170  static void removeMouseUpCallback(void (*fn)(Uint8, MouseState));
171 
176  static void addMouseMoveCallback(void (*fn)(MouseState));
177 
182  static void removeMouseMoveCallback(void (*fn)(MouseState));
183 
188  static void addJoyButtonDownCallback(void (*fn)(Uint8, JoyButton));
189 
194  static void removeJoyButtonDownCallback(void (*fn)(Uint8, JoyButton));
195 
200  static void addJoyButtonUpCallback(void (*fn)(Uint8, JoyButton));
201 
206  static void removeJoyButtonUpCallback(void (*fn)(Uint8, JoyButton));
207 
212  static void addJoyMoveCallback(void (*fn)(Uint8, JoyStick, Point2D));
213 
218  static void removeJoyMoveCallback(void (*fn)(Uint8, JoyStick, Point2D));
219 
224  static bool getAllPressed(JoyButton b);
225 
230  static bool getAllTriggered(JoyButton b);
231 
237 
241  static Point2D getMousePos();
242 
246  static Point2D getMouseRelPos();
247 
256 
263  bool pressed(SDLKey key, SDLMod modifier = KMOD_NONE);
264 
271  bool triggered(SDLKey key, int ms=-1);
272 
279  bool doubleTapped(SDLKey key, int ms=-1);
280 
286  bool pressed(JoyButton button);
287 
295  bool triggered(JoyButton button, int ms=-1);
296 
304  bool doubleTapped(JoyButton button, int ms=-1);
305 
312  static bool pressed(MouseButton b, int button=0);
313 
321  static bool triggered(MouseButton b, int button=0, int ms=-1);
322 
330  static bool doubleTapped(MouseButton b, int button=0, int ms=-1);
331 
332 protected:
333 
335  int mIndex;
336 
338  static vector<Input*> inputs;
339 
341  static set<void (*)(SDLKey, SDLMod)> mKeyDownCallbacks;
342 
344  static set<void (*)(SDLKey, SDLMod)> mKeyUpCallbacks;
345 
347  static set<void (*)(Uint8, MouseState)> mMouseDownCallbacks;
348 
350  static set<void (*)(Uint8, MouseState)> mMouseUpCallbacks;
351 
353  static set<void (*)(MouseState)> mMouseMoveCallbacks;
354 
356  static set<void (*)(Uint8, JoyButton)> mJoyButtonDownCallbacks;
357 
359  static set<void (*)(Uint8, JoyButton)> mJoyButtonUpCallbacks;
360 
362  static set<void (*)(Uint8, JoyStick, Point2D)> mJoyMoveCallbacks;
363 
366 
368  SDL_Joystick* mJoystick;
369 
372 
375 
378 
381 
383  map<SDLKey, pair<bool,SDLMod> > mKeyStates;
384 
386  map<SDLKey, Uint32> mLastKeyStates;
387 
389  map<SDLKey, Uint32> mLastKeyDoubleStates;
390 
392  vector<bool> mJoyStates;
393 
395  vector<Uint32> mLastJoyStates;
396 
398  vector<Uint32> mLastJoyDoubleStates;
399 
401  vector<MouseState> mMouseButtonStates;
402 
404  vector<MouseState> mLastMouseStates;
405 
407  vector<MouseState> mLastMouseDoubleStates;
408 };
409 
410 #endif