EmscriptenInputManager.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _EMSCRIPTENINPUTMANAGER_H_
  23. #define _EMSCRIPTENINPUTMANAGER_H_
  24. #include "collection/vector.h"
  25. #include "platform/platformInput.h"
  26. #include "platformEmscripten/platformEmscripten.h"
  27. #include <SDL/SDL_events.h>
  28. #define NUM_KEYS ( KEY_BUTTON31 + 1 )
  29. #define KEY_FIRST KEY_ESCAPE
  30. struct AsciiData
  31. {
  32. struct KeyData
  33. {
  34. U16 ascii;
  35. bool isDeadChar;
  36. };
  37. KeyData upper;
  38. KeyData lower;
  39. KeyData goofy;
  40. };
  41. extern AsciiData AsciiTable[NUM_KEYS];
  42. typedef struct _SDL_Joystick;
  43. struct JoystickAxisInfo
  44. {
  45. S32 type;
  46. S32 minValue;
  47. S32 maxValue;
  48. };
  49. //------------------------------------------------------------------------------
  50. class JoystickInputDevice : public InputDevice
  51. {
  52. public:
  53. JoystickInputDevice(U8 deviceID);
  54. ~JoystickInputDevice();
  55. bool activate();
  56. bool deactivate();
  57. bool isActive() { return( mActive ); }
  58. U8 getDeviceType() { return( JoystickDeviceType ); }
  59. U8 getDeviceID() { return( mDeviceID ); }
  60. const char* getName();
  61. const char* getJoystickAxesString();
  62. void loadJoystickInfo();
  63. void loadAxisInfo();
  64. JoystickAxisInfo& getAxisInfo(int axisNum) { return mAxisList[axisNum]; }
  65. bool process();
  66. void reset();
  67. private:
  68. bool mActive;
  69. U8 mDeviceID;
  70. SDL_Joystick* mStick;
  71. Vector<JoystickAxisInfo> mAxisList;
  72. Vector<bool> mButtonState;
  73. Vector<U8> mHatState;
  74. S32 mNumAxes;
  75. S32 mNumButtons;
  76. S32 mNumHats;
  77. S32 mNumBalls;
  78. };
  79. //------------------------------------------------------------------------------
  80. class UInputManager : public InputManager
  81. {
  82. friend bool JoystickInputDevice::process(); // for joystick event funcs
  83. friend void JoystickInputDevice::reset();
  84. public:
  85. UInputManager();
  86. void init();
  87. bool enable();
  88. void disable();
  89. void activate();
  90. void deactivate();
  91. void setWindowLocked(bool locked);
  92. bool isActive() { return( mActive ); }
  93. void onDeleteNotify( SimObject* object );
  94. bool onAdd();
  95. void onRemove();
  96. void process();
  97. bool enableKeyboard();
  98. void disableKeyboard();
  99. bool isKeyboardEnabled() { return( mKeyboardEnabled ); }
  100. bool activateKeyboard();
  101. void deactivateKeyboard();
  102. bool isKeyboardActive() { return( mKeyboardActive ); }
  103. bool enableMouse();
  104. void disableMouse();
  105. bool isMouseEnabled() { return( mMouseEnabled ); }
  106. bool activateMouse();
  107. void deactivateMouse();
  108. bool isMouseActive() { return( mMouseActive ); }
  109. bool enableJoystick();
  110. void disableJoystick();
  111. bool isJoystickEnabled() { return( mJoystickEnabled ); }
  112. bool activateJoystick();
  113. void deactivateJoystick();
  114. bool isJoystickActive() { return( mJoystickActive ); }
  115. void setLocking(bool enabled);
  116. bool getLocking() { return mLocking; }
  117. const char* getJoystickAxesString( U32 deviceID );
  118. bool joystickDetected() { return mJoystickList.size() > 0; }
  119. private:
  120. typedef SimGroup Parent;
  121. // the following vector is just for quick access during event processing.
  122. // it does not manage the cleanup of the JoystickInputDevice objects
  123. Vector<JoystickInputDevice*> mJoystickList;
  124. bool mKeyboardEnabled;
  125. bool mMouseEnabled;
  126. bool mJoystickEnabled;
  127. bool mKeyboardActive;
  128. bool mMouseActive;
  129. bool mJoystickActive;
  130. bool mActive;
  131. // Device state variables
  132. S32 mModifierKeys;
  133. bool mKeyboardState[256];
  134. bool mMouseButtonState[3];
  135. // last mousex and y are maintained when window is unlocked
  136. S32 mLastMouseX;
  137. S32 mLastMouseY;
  138. void initJoystick();
  139. void resetKeyboardState();
  140. void resetMouseState();
  141. void resetInputState();
  142. void lockInput();
  143. void unlockInput();
  144. bool mLocking;
  145. void joyHatEvent(U8 deviceID, U8 hatNum,
  146. U8 prevHatState, U8 currHatState);
  147. void joyButtonEvent(U8 deviceID, U8 buttonNum, bool pressed);
  148. void joyButtonEvent(const SDL_Event& event);
  149. void joyAxisEvent(const SDL_Event& event);
  150. void joyAxisEvent(U8 deviceID, U8 axisNum, S16 axisValue);
  151. void mouseButtonEvent(const SDL_Event& event);
  152. void mouseMotionEvent(const SDL_Event& event);
  153. void keyEvent(const SDL_Event& event);
  154. bool processKeyEvent(InputEvent &event);
  155. };
  156. #endif // _EMSCRIPTENINPUTMANAGER_H_