JugiMap Framework
jmInput.h
1 #ifndef JUGIMAP_INPUT_H
2 #define JUGIMAP_INPUT_H
3 
4 #include <assert.h>
5 #include <algorithm>
6 #include <cmath>
7 #include <vector>
8 #include <array>
9 #include <string>
10 
11 #include "jmCommon.h"
12 
13 
14 namespace jugimap{
15 
16 class Sprite;
17 
18 
21 
22 
23 
24 
25 struct ButtonState
26 {
27  bool pressed = false;
28  bool down = false;
29  bool released = false;
30 
31  void _Set(bool _down);
32  void Reset(){ pressed = down = released = false; }
33  void ResetPerUpdateFlags(){ pressed = released = false; }
34 
35 };
36 
37 
39 enum class KeyCode
40 {
41 
42  UNKNOWN = 0,
43 
44  BACKSPACE=8,
45  TAB=9,
46  ENTER=13,
47  PAUSE=19,
48  ESCAPE=27,
49  SPACE=32,
50 
51  PAGEUP=33,
52  PAGEDOWN=34,
53  END=35,
54  HOME=36,
55 
56  LEFT=37,
57  UP=38,
58  RIGHT=39,
59  DOWN=40,
60 
61  PRINT=42,
62  INSERT=45,
63  DELETEkey=46,
64 
65  NUM_0=48,
66  NUM_1=49,
67  NUM_2=50,
68  NUM_3=51,
69  NUM_4=52,
70  NUM_5=53,
71  NUM_6=54,
72  NUM_7=55,
73  NUM_8=56,
74  NUM_9=57,
75 
76  A=65,
77  B=66,
78  C=67,
79  D=68,
80  E=69,
81  F=70,
82  G=71,
83  H=72,
84  I=73,
85  J=74,
86  K=75,
87  L=76,
88  M=77,
89  N=78,
90  O=79,
91  P=80,
92  Q=81,
93  R=82,
94  S=83,
95  T=84,
96  U=85,
97  V=86,
98  W=87,
99  X=88,
100  Y=89,
101  Z=90,
102 
103  /*
104  KEY_NUM0=96,
105  KEY_NUM1=97,
106  KEY_NUM2=98,
107  KEY_NUM3=99,
108  KEY_NUM4=100,
109  KEY_NUM5=101,
110  KEY_NUM6=102,
111  KEY_NUM7=103,
112  KEY_NUM8=104,
113  KEY_NUM9=105,
114 
115  KEY_NUMMULTIPLY=106,
116  KEY_NUMADD=107,
117  KEY_NUMSUBTRACT=109,
118  KEY_NUMDECIMAL=110,
119  KEY_NUMDIVIDE=111,
120  */
121 
122  F1=112,
123  F2=113,
124  F3=114,
125  F4=115,
126  F5=116,
127  F6=117,
128  F7=118,
129  F8=119,
130  F9=120,
131  F10=121,
132  F11=122,
133  F12=123,
134 
135 
136  // 165 - 190 - reserved for upper case letters using printable characters table
137  TILDE=192,
138  HYPHEN=193,
139  EQUAL=194,
140  SEMICOLON=195,
141  QUOTE=196,
142  COMMA=197,
143  PERIOD=198,
144  SLASH=199,
145  LEFT_BRACKET=200,
146  RIGHT_BRACKET=201,
147  BACKSLASH=202,
148 
149  LEFT_SHIFT=210,
150  RIGHT_SHIFT=211,
151  LEFT_CONTROL=212,
152  RIGHT_CONTROL=213,
153  LEFT_ALT=214,
154  RIGHT_ALT=215,
155 
156 };
157 
158 
160 class Keyboard
161 {
162 public:
163 
164  Keyboard();
165 
167  bool IsKeyPressed(KeyCode _keyCode);
168 
170  bool IsKeyDown(KeyCode _keyCode);
171 
173  bool IsKeyReleased(KeyCode _keyCode);
174 
176  char GetCharPressed(){ return charPressed; }
177 
179  void Reset();
180 
181  //-------------------------------------------------------
182 
183  void ResetPerUpdateFlags();
184  void _SetKeyState(KeyCode _keyCode, bool _keyDown);
185  std::array<ButtonState,256> & GetKeys(){ return keys; }
186  std::vector<std::string> & GetKeysNames(){ return keyNames; }
187 
188 private:
189  std::array<ButtonState,256>keys;
190  std::vector<char>printableASCIIcodes;
191  std::vector<std::string>keyNames;
192  char charPressed = 0;
193 
194 };
195 
196 
198 extern Keyboard keyboard;
199 
200 
201 
202 //-------------------------------------------------------------------------------------
203 
204 
206 enum class MouseButton
207 {
208  NONE = 0,
209  LEFT = 1,
210  MIDDLE = 2,
211  RIGHT = 3,
212 };
213 
214 
216 class Mouse
217 {
218 public:
219 
221  int GetX(){ return pos.x; }
222 
224  int GetY(){ return pos.y; }
225 
227  Vec2i GetPos(){ return pos; }
228 
230  bool IsPositionChanged(){ return posChanged; }
231 
232 
234  int GetWheelX(){ return wheel.x; }
235 
237  int GetWheelY(){ return wheel.y; }
238 
240  Vec2i GetWheel(){ return wheel; }
241 
243  bool IsWheelXChanged(){ return wheelXChanged; }
244 
246  bool IsWheelYChanged(){ return wheelYChanged; }
247 
248 
250  bool IsButtonPressed(MouseButton _mouseButton){ return buttons[(int)_mouseButton].pressed; }
251 
253  bool IsButtonDown(MouseButton _mouseButton){ return buttons[(int)_mouseButton].down; }
254 
256  bool IsButtonReleased(MouseButton _mouseButton){ return buttons[(int)_mouseButton].released; }
257 
261  void SetCursorSprite(jugimap::Sprite *_cursorSprite);
262 
266  void ClearCursorSprite();
267 
269  Sprite* GetCursorSprite(){ return cursorSprite; }
270 
272  void Reset();
273 
274  //----
275 
276  void ResetPerUpdateFlags();
277  void _SetPosition(Vec2i _pos) { posChanged = ! _pos.Equals(pos); pos = _pos; }
278  void _SetWheel(Vec2i _wheel);
279  void _SetButtonState(MouseButton _mouseButton, bool _down);
280 
281 
282 private:
283 
284  Vec2i pos;
285  Vec2i wheel;
286  std::array<ButtonState,4> buttons;
287 
288  bool wheelYChanged = false;
289  bool wheelXChanged = false;
290  bool posChanged =false;
291 
292  //----
293  Sprite *cursorSprite = nullptr; // LINK
294 
295 };
296 
297 
299 extern Mouse mouse;
300 
301 
302 
303 //-------------------------------------------------------------------------------------
304 
305 
306 
307 struct Finger : public ButtonState
308 {
309  int id = 0;
310  Vec2i position;
311 };
312 
313 
315 class Touch
316 {
317 public:
318 
319 
321  bool IsFingerPressed(int _finger){ return fingers[_finger].pressed; }
322 
324  bool IsFingerDown(int _finger){ return fingers[_finger].down; }
325 
327  bool IsFingerReleased(int _finger){ return fingers[_finger].released; }
328 
330  int GetFingerX(int _finger){ return fingers[_finger].position.x; }
331 
333  int GetFingerY(int _finger){ return fingers[_finger].position.y; }
334 
336  Vec2i GetFingerPos(int _finger){ return fingers[_finger].position; }
337 
339  std::array<Finger, 10> & GetFingers(){ return fingers;}
340 
342  void Reset();
343 
344  //----
345  void ResetPerUpdateFlags();
346 
347  //----
348  void _SetFingerState(int _finger, bool _down, Vec2i _position);
349 
350 private:
351 
352  std::array<Finger, 10>fingers;
353 
354 };
355 
357 extern Touch touch;
358 
359 
360 //-------------------------------------------------------------------------------------
361 
362 
363 
365 class Joystick
366 {
367 public:
368 
370  enum class POV_X
371  {
372  NONE,
373  RIGHT,
374  LEFT
375  };
376 
378  enum class POV_Y
379  {
380  NONE,
381  UP,
382  DOWN,
383  };
384 
385 
386  static std::string DbgGetPOV_Xstring(POV_X _pov);
387  static std::string DbgGetPOV_Ystring(POV_Y _pov);
388 
389 
391  bool IsButtonPressed(int _buttonIndex){ return buttons[_buttonIndex].pressed; }
392 
394  bool IsButtonDown(int _buttonIndex){ return buttons[_buttonIndex].down; }
395 
397  bool IsButtonReleased(int _buttonIndex){ return buttons[_buttonIndex].released; }
398 
400  bool IsPOV_XPressed(POV_X _povX){ return povXButtons[(int)_povX].pressed; }
401 
403  bool IsPOV_XDown(POV_X _povX){ return povXButtons[(int)_povX].down; }
404 
406  bool IsPOV_XReleased(POV_X _povX){ return povXButtons[(int)_povX].released; }
407 
409  bool IsPOV_YPressed(POV_Y _povY){ return povYButtons[(int)_povY].pressed; }
410 
412  bool IsPOV_YDown(POV_Y _povY){ return povYButtons[(int)_povY].down; }
413 
415  bool IsPOV_YReleased(POV_Y _povY){ return povYButtons[(int)_povY].released; }
416 
418  float GetXaxis(){ return xAxis; }
419 
421  float GetYaxis(){ return yAxis; }
422 
424  float GetZaxis(){ return zAxis; }
425 
427  bool IsConnected(){ return connected; }
428 
430  void Reset();
431 
433  std::array<ButtonState,16>& GetButtons(){ return buttons;}
434 
438  std::string GetName(){ return name; }
439 
440 
441  void ResetPerUpdateFlags();
442 
443  //----
444  void _SetPOV_X(POV_X _povX);
445  void _SetPOV_Y(POV_Y _povY);
446  void _SetXaxis(float _xAxis){ xAxis = _xAxis;}
447  void _SetYaxis(float _yAxis){ yAxis = _yAxis;}
448  void _SetZaxis(float _zAxis){ zAxis = _zAxis;}
449  void _SetButtonState(int _buttonIndex, bool _down);
450  void _SetConnected(bool _connected){ connected = _connected; }
451  void _SetName(const std::string &_name){ name = _name; }
452 
453 
454 private:
455  std::array<ButtonState,16>buttons;
456  std::array<ButtonState,3>povXButtons;
457  std::array<ButtonState,3>povYButtons;
458 
459 
460  float xAxis = 0.0;
461  float yAxis = 0.0;
462  float zAxis = 0.0;
463  bool connected = false;
464  std::string name = "Generic joystick";
465 };
466 
467 
469 extern std::array<Joystick, 4>joysticks;
470 
471 
474 
475 
476 }
477 
478 
479 
480 #endif // JUGIMAP_INPUT_H
jugimap::Joystick::IsButtonPressed
bool IsButtonPressed(int _buttonIndex)
Returns true if a button with the given *_buttonIndex* is pressed; otherwise returns false.
Definition: jmInput.h:391
jugimap::MouseButton::RIGHT
Right button.
jugimap::KeyCode::NUM_0
0
jugimap::KeyCode::LEFT_CONTROL
Left Control.
jugimap::Keyboard::GetCharPressed
char GetCharPressed()
Returns the ascii code of a printable character if pressed; otherwise returns 0;.
Definition: jmInput.h:176
jugimap::KeyCode::NUM_4
4
jugimap::KeyCode::DELETEkey
Delete.
jugimap::KeyCode::LEFT
Left.
jugimap::Joystick::IsConnected
bool IsConnected()
Returns true if this joystick is connected; otherwise returns false.
Definition: jmInput.h:427
jugimap::Joystick::IsPOV_XPressed
bool IsPOV_XPressed(POV_X _povX)
Returns true if the given *_povX* button is pressed; otherwise returns false.
Definition: jmInput.h:400
jugimap::KeyCode::L
L.
jugimap::KeyCode::RIGHT_SHIFT
Right Shift.
jugimap::KeyCode::QUOTE
Quote.
jugimap::KeyCode::F
F.
jugimap::Keyboard::IsKeyReleased
bool IsKeyReleased(KeyCode _keyCode)
Returns true if a key with the given *_keyCode* is released; otherwise returns false.
Definition: jmInput.cpp:241
jugimap::KeyCode::COMMA
Comma.
jugimap::Mouse::IsButtonReleased
bool IsButtonReleased(MouseButton _mouseButton)
Returns true if the given *_mouse button* is released; otherwise returns false.
Definition: jmInput.h:256
jugimap::KeyCode::G
G.
jugimap::KeyCode::F4
F4.
jugimap::KeyCode::J
J.
jugimap::Joystick::IsButtonDown
bool IsButtonDown(int _buttonIndex)
Returns true if a button with the given *_buttonIndex* is hold down; otherwise returns false.
Definition: jmInput.h:394
jugimap::Joystick::POV_X
POV_X
POV X values.
Definition: jmInput.h:370
jugimap::Joystick::POV_Y
POV_Y
POV Y values.
Definition: jmInput.h:378
jugimap::MouseButton::LEFT
Left button.
jugimap::KeyCode::SPACE
Space.
jugimap::KeyCode::NUM_2
2
jugimap::KeyCode::END
End.
jugimap::Mouse::IsButtonDown
bool IsButtonDown(MouseButton _mouseButton)
Returns true if the given *_mouse button* is hold down; otherwise returns false.
Definition: jmInput.h:253
jugimap::KeyCode::A
A.
jugimap::KeyCode::Z
Z.
jugimap::Joystick::IsPOV_YDown
bool IsPOV_YDown(POV_Y _povY)
Returns true if the given *_povY* button is hold down; otherwise returns false.
Definition: jmInput.h:412
jugimap::Touch::GetFingerX
int GetFingerX(int _finger)
Returns the x coordinate of the given *_finger* on screen.
Definition: jmInput.h:330
jugimap::Joystick::IsButtonReleased
bool IsButtonReleased(int _buttonIndex)
Returns true if a button with the given *_buttonIndex* is released; otherwise returns false.
Definition: jmInput.h:397
jugimap::joysticks
std::array< Joystick, 4 > joysticks
Global Joystick objects used for querying joysticks input.
Definition: jmInput.cpp:492
jugimap::KeyCode::NUM_6
6
jugimap::KeyCode::NUM_1
1
jugimap::Vec2i
Vec2< int > Vec2i
Vec2 struct in integer precision.
Definition: jmCommon.h:166
jugimap::KeyCode::UNKNOWN
Unknown.
jugimap::MouseButton::NONE
None.
jugimap::KeyCode::F6
F6.
jugimap::Joystick::IsPOV_XReleased
bool IsPOV_XReleased(POV_X _povX)
Returns true if the given *_povX* button is released; otherwise returns false.
Definition: jmInput.h:406
jugimap::KeyCode::BACKSLASH
Backslash.
jugimap::Mouse::IsButtonPressed
bool IsButtonPressed(MouseButton _mouseButton)
Returns true if the given *_mouse button* is pressed; otherwise returns false.
Definition: jmInput.h:250
jugimap::KeyCode::Y
Y.
jugimap::KeyCode::TAB
Tab.
jugimap::Mouse::IsWheelXChanged
bool IsWheelXChanged()
Returns true if the value of horizontal wheel changed; otherwise returns false.
Definition: jmInput.h:243
jugimap::KeyCode
KeyCode
The key codes of keyboard keys.
Definition: jmInput.h:39
jugimap::KeyCode::RIGHT_BRACKET
Right Bracket.
jugimap::KeyCode::UP
Up.
jugimap::KeyCode::P
P.
jugimap::Mouse::GetX
int GetX()
Returns the x coordinate of the mouse cursor on screen.
Definition: jmInput.h:221
jugimap::KeyCode::R
R.
jugimap::KeyCode::LEFT_BRACKET
Left Bracket.
jugimap::KeyCode::I
I.
jugimap::KeyCode::E
E.
jugimap::KeyCode::M
M.
jugimap::Joystick
The Joystick class provide information about joystick input.
Definition: jmInput.h:365
jugimap::KeyCode::F12
F12.
jugimap::KeyCode::PRINT
Print.
jugimap::KeyCode::INSERT
Insert.
jugimap::keyboard
Keyboard keyboard
Global Keyboard object used for querying keyboard input.
Definition: jmInput.cpp:267
jugimap::Mouse::GetY
int GetY()
Returns the y coordinate of the mouse cursor on screen.
Definition: jmInput.h:224
jugimap::Keyboard::IsKeyDown
bool IsKeyDown(KeyCode _keyCode)
Returns true if a key with the given *_keyCode* is hold down; otherwise returns false.
Definition: jmInput.cpp:229
jugimap::Mouse::IsPositionChanged
bool IsPositionChanged()
Returns true if the position of the cursor changed; otherwise returns false.
Definition: jmInput.h:230
jugimap::Mouse::GetCursorSprite
Sprite * GetCursorSprite()
Returns the cursor sprite if exists; otherwise returns nullptr.
Definition: jmInput.h:269
jugimap::KeyCode::O
O.
jugimap::Touch
The Touch class provide information about touch input.
Definition: jmInput.h:315
jugimap::KeyCode::NUM_3
3
jugimap::Vec2::x
T x
The x coordinate.
Definition: jmCommon.h:25
jugimap::Joystick::GetName
std::string GetName()
Return the joystick name.
Definition: jmInput.h:438
jugimap::Sprite
The Sprite is the base sprite class.
Definition: jmSprites.h:32
jugimap::Mouse::GetWheelY
int GetWheelY()
Returns the value of vertical wheel.
Definition: jmInput.h:237
jugimap::Mouse::IsWheelYChanged
bool IsWheelYChanged()
Returns true if the value of horizontal wheel changed; otherwise returns false.
Definition: jmInput.h:246
jugimap::touch
Touch touch
Global Touch object used for querying touch input.
Definition: jmInput.cpp:378
jugimap::KeyCode::S
S.
jugimap::KeyCode::F7
F7.
jugimap::Touch::IsFingerDown
bool IsFingerDown(int _finger)
Returns true if the given *_finger* is hold down; otherwise returns false.
Definition: jmInput.h:324
jugimap::Mouse::SetCursorSprite
void SetCursorSprite(jugimap::Sprite *_cursorSprite)
Set the cursor sprite to the given *_cursorSprite*.
Definition: jmInput.cpp:321
jugimap::KeyCode::DOWN
Down.
jugimap::KeyCode::HYPHEN
Hyphen.
jugimap::Touch::GetFingers
std::array< Finger, 10 > & GetFingers()
Returns a reference to the vector of finger states.
Definition: jmInput.h:339
jugimap::Keyboard
The Keyboard class provide information about keyboard input.
Definition: jmInput.h:160
jugimap::mouse
Mouse mouse
Global Mouse object used for querying mouse input.
Definition: jmInput.cpp:343
jugimap::KeyCode::NUM_8
8
jugimap::KeyCode::H
G.
jugimap::KeyCode::V
V.
jugimap::Touch::IsFingerPressed
bool IsFingerPressed(int _finger)
Returns true if the given *_finger* is pressed; otherwise returns false.
Definition: jmInput.h:321
jugimap::Vec2::Equals
bool Equals(const Vec2< T > &v, float epsilon=0.00001f) const
Returns true if the given vectors v is equal to this vector; otherwise returns false....
Definition: jmCommon.h:46
jugimap::Vec2::y
T y
The y coordinate.
Definition: jmCommon.h:26
jugimap::KeyCode::RIGHT
Right.
jugimap::KeyCode::TILDE
Tilde.
jugimap::KeyCode::SLASH
Slash.
jugimap::Mouse::ClearCursorSprite
void ClearCursorSprite()
Clear the cursor sprite.
Definition: jmInput.cpp:329
jugimap::KeyCode::X
X.
jugimap::KeyCode::PAUSE
Pause.
jugimap::KeyCode::C
C.
jugimap::Mouse::GetPos
Vec2i GetPos()
Returns the position of the mouse cursor on screen.
Definition: jmInput.h:227
jugimap::KeyCode::LEFT_ALT
Left Alt.
jugimap::KeyCode::B
B.
jugimap::KeyCode::D
D.
jugimap::Mouse
The Mouse class provide information about mouse input.
Definition: jmInput.h:216
jugimap::KeyCode::F8
F8.
jugimap::Joystick::IsPOV_YReleased
bool IsPOV_YReleased(POV_Y _povY)
Returns true if the given *_povY* button is released; otherwise returns false.
Definition: jmInput.h:415
jugimap::Mouse::GetWheelX
int GetWheelX()
Returns the value of horizontal wheel.
Definition: jmInput.h:234
jugimap::KeyCode::RIGHT_CONTROL
Right Control.
jugimap::Joystick::GetYaxis
float GetYaxis()
Returns the value of y axis.
Definition: jmInput.h:421
jugimap::KeyCode::BACKSPACE
Backspace.
jugimap::KeyCode::HOME
Home.
jugimap::MouseButton
MouseButton
The mouse buttons.
Definition: jmInput.h:206
jugimap::Keyboard::Reset
void Reset()
Reset all key states.
Definition: jmInput.cpp:247
jugimap::Joystick::GetButtons
std::array< ButtonState, 16 > & GetButtons()
Returns a reference to the array of button states.
Definition: jmInput.h:433
jugimap::KeyCode::F9
F9.
jugimap::KeyCode::NUM_7
7
jugimap::AlignX::LEFT
Align left.
jugimap::Mouse::GetWheel
Vec2i GetWheel()
Returns the values of vertical and horizonatal wheel.
Definition: jmInput.h:240
jugimap::Joystick::GetXaxis
float GetXaxis()
Returns the value of x axis.
Definition: jmInput.h:418
jugimap::KeyCode::F1
F1.
jugimap::Touch::Reset
void Reset()
Reset all finger states.
Definition: jmInput.cpp:349
jugimap::KeyCode::RIGHT_ALT
Right Alt.
jugimap::AlignX::RIGHT
Align right.
jugimap::KeyCode::SEMICOLON
Semicolon.
jugimap::Touch::IsFingerReleased
bool IsFingerReleased(int _finger)
Returns true if the given *_finger* is released; otherwise returns false.
Definition: jmInput.h:327
jugimap::KeyCode::F2
F2.
jugimap::KeyCode::T
T.
jugimap::KeyCode::F10
F10.
jugimap::Vec2< int >
jugimap::Joystick::IsPOV_XDown
bool IsPOV_XDown(POV_X _povX)
Returns true if the given *_povX* button is hold down; otherwise returns false.
Definition: jmInput.h:403
jugimap::KeyCode::LEFT_SHIFT
Left Shift.
jugimap::KeyCode::F3
F3.
jugimap::KeyCode::EQUAL
Equal.
jugimap::KeyCode::ESCAPE
Escape.
jugimap::Mouse::Reset
void Reset()
Reset all buttons states.
Definition: jmInput.cpp:273
jugimap::KeyCode::PAGEUP
Page Up.
jugimap::Joystick::IsPOV_YPressed
bool IsPOV_YPressed(POV_Y _povY)
Returns true if the given *_povY* button is pressed; otherwise returns false.
Definition: jmInput.h:409
jugimap::Keyboard::IsKeyPressed
bool IsKeyPressed(KeyCode _keyCode)
Returns true if a key with the given *_keyCode* is pressed; otherwise returns false.
Definition: jmInput.cpp:235
jugimap::AlignX::MIDDLE
Align to middle.
jugimap::KeyCode::K
K.
jugimap::KeyCode::F11
F11.
jugimap::KeyCode::NUM_5
5
jugimap::Touch::GetFingerY
int GetFingerY(int _finger)
Returns the y coordinate of the given *_finger* on screen.
Definition: jmInput.h:333
jugimap::KeyCode::W
W.
jugimap::KeyCode::PERIOD
Period.
jugimap::Joystick::GetZaxis
float GetZaxis()
Returns the value of z axis.
Definition: jmInput.h:424
jugimap::KeyCode::ENTER
Enter.
jugimap::KeyCode::F5
F5.
jugimap::Touch::GetFingerPos
Vec2i GetFingerPos(int _finger)
Returns position of the given *_finger* on screen.
Definition: jmInput.h:336
jugimap::KeyCode::PAGEDOWN
Page Down.
jugimap::Joystick::Reset
void Reset()
Reset all button states.
Definition: jmInput.cpp:416
jugimap::KeyCode::Q
Q.
jugimap::KeyCode::N
N.
jugimap::KeyCode::NUM_9
9
jugimap::KeyCode::U
U.