EmscriptenInput.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. // Portions Copyright (c) 2014 James S Urquhart
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "platformEmscripten/platformEmscripten.h"
  24. #include "platform/platformInput.h"
  25. #include "platform/platformVideo.h"
  26. #include "platform/event.h"
  27. #include "game/gameInterface.h"
  28. #include "console/console.h"
  29. #include "platformEmscripten/EmscriptenInputManager.h"
  30. #include <SDL/SDL.h>
  31. #ifdef LOG_INPUT
  32. #include <time.h>
  33. #include <stdarg.h>
  34. #include <fcntl.h>
  35. #endif
  36. // Static class variables:
  37. InputManager* Input::smManager = NULL;
  38. // smActive is not maintained under unix. Use Input::isActive()
  39. // instead
  40. bool Input::smActive = false;
  41. CursorManager* Input::smCursorManager = 0;
  42. //extern AsciiData AsciiTable[NUM_KEYS];
  43. #ifdef LOG_INPUT
  44. S32 gInputLog = -1;
  45. #endif
  46. //------------------------------------------------------------------------------
  47. void Input::init()
  48. {
  49. Con::printf( "Input Init:" );
  50. destroy();
  51. smActive = false;
  52. smManager = NULL;
  53. UInputManager *uInputManager = new UInputManager;
  54. if ( !uInputManager->enable() )
  55. {
  56. Con::errorf( " Failed to enable Input Manager." );
  57. delete uInputManager;
  58. return;
  59. }
  60. uInputManager->init();
  61. smManager = uInputManager;
  62. Con::printf(" Input initialized");
  63. Con::printf(" ");
  64. }
  65. //------------------------------------------------------------------------------
  66. ConsoleFunction( isJoystickDetected, bool, 1, 1, "isJoystickDetected()" )
  67. {
  68. argc; argv;
  69. UInputManager* manager = dynamic_cast<UInputManager*>(Input::getManager());
  70. if (manager)
  71. return manager->joystickDetected();
  72. else
  73. return false;
  74. }
  75. //------------------------------------------------------------------------------
  76. ConsoleFunction( getJoystickAxes, const char*, 2, 2, "getJoystickAxes( instance )" )
  77. {
  78. argc; argv;
  79. UInputManager* manager = dynamic_cast<UInputManager*>(Input::getManager());
  80. if (manager)
  81. return manager->getJoystickAxesString(dAtoi(argv[1]));
  82. else
  83. return "";
  84. }
  85. //------------------------------------------------------------------------------
  86. U16 Input::getKeyCode( U16 asciiCode )
  87. {
  88. U16 keyCode = 0;
  89. U16 i;
  90. // This is done three times so the lowerkey will always
  91. // be found first. Some foreign keyboards have duplicate
  92. // chars on some keys.
  93. for ( i = KEY_FIRST; i < NUM_KEYS && !keyCode; i++ )
  94. {
  95. if ( AsciiTable[i].lower.ascii == asciiCode )
  96. {
  97. keyCode = i;
  98. break;
  99. };
  100. }
  101. for ( i = KEY_FIRST; i < NUM_KEYS && !keyCode; i++ )
  102. {
  103. if ( AsciiTable[i].upper.ascii == asciiCode )
  104. {
  105. keyCode = i;
  106. break;
  107. };
  108. }
  109. for ( i = KEY_FIRST; i < NUM_KEYS && !keyCode; i++ )
  110. {
  111. if ( AsciiTable[i].goofy.ascii == asciiCode )
  112. {
  113. keyCode = i;
  114. break;
  115. };
  116. }
  117. return( keyCode );
  118. }
  119. //-----------------------------------------------------------------------------
  120. U16 Input::getAscii( U16 keyCode, KEY_STATE keyState )
  121. {
  122. if ( keyCode >= NUM_KEYS )
  123. return 0;
  124. switch ( keyState )
  125. {
  126. case STATE_LOWER:
  127. return AsciiTable[keyCode].lower.ascii;
  128. case STATE_UPPER:
  129. return AsciiTable[keyCode].upper.ascii;
  130. case STATE_GOOFY:
  131. return AsciiTable[keyCode].goofy.ascii;
  132. default:
  133. return(0);
  134. }
  135. }
  136. //------------------------------------------------------------------------------
  137. void Input::destroy()
  138. {
  139. if ( smManager && smManager->isEnabled() )
  140. {
  141. smManager->disable();
  142. delete smManager;
  143. smManager = NULL;
  144. }
  145. }
  146. //------------------------------------------------------------------------------
  147. bool Input::enable()
  148. {
  149. if ( smManager && !smManager->isEnabled() )
  150. return( smManager->enable() );
  151. return( false );
  152. }
  153. //------------------------------------------------------------------------------
  154. void Input::disable()
  155. {
  156. if ( smManager && smManager->isEnabled() )
  157. smManager->disable();
  158. }
  159. //------------------------------------------------------------------------------
  160. void Input::activate()
  161. {
  162. if ( smManager && smManager->isEnabled() && !isActive())
  163. {
  164. #ifdef LOG_INPUT
  165. Input::log( "Activating Input...\n" );
  166. #endif
  167. UInputManager* uInputManager = dynamic_cast<UInputManager*>( smManager );
  168. if ( uInputManager )
  169. uInputManager->activate();
  170. }
  171. }
  172. //------------------------------------------------------------------------------
  173. void Input::deactivate()
  174. {
  175. if ( smManager && smManager->isEnabled() && isActive() )
  176. {
  177. #ifdef LOG_INPUT
  178. Input::log( "Deactivating Input...\n" );
  179. #endif
  180. UInputManager* uInputManager = dynamic_cast<UInputManager*>( smManager );
  181. if ( uInputManager )
  182. uInputManager->deactivate();
  183. }
  184. }
  185. //------------------------------------------------------------------------------
  186. void Input::reactivate()
  187. {
  188. Input::deactivate();
  189. Input::activate();
  190. }
  191. //------------------------------------------------------------------------------
  192. bool Input::isEnabled()
  193. {
  194. if ( smManager )
  195. return smManager->isEnabled();
  196. return false;
  197. }
  198. //------------------------------------------------------------------------------
  199. bool Input::isActive()
  200. {
  201. UInputManager* uInputManager = dynamic_cast<UInputManager*>( smManager );
  202. if ( uInputManager )
  203. return uInputManager->isActive();
  204. return false;
  205. }
  206. //------------------------------------------------------------------------------
  207. void Input::process()
  208. {
  209. if ( smManager )
  210. smManager->process();
  211. }
  212. //------------------------------------------------------------------------------
  213. InputManager* Input::getManager()
  214. {
  215. return smManager;
  216. }
  217. #ifdef LOG_INPUT
  218. //------------------------------------------------------------------------------
  219. void Input::log( const char* format, ... )
  220. {
  221. }
  222. ConsoleFunction( inputLog, void, 2, 2, "inputLog( string )" )
  223. {
  224. argc;
  225. Input::log( "%s\n", argv[1] );
  226. }
  227. #endif // LOG_INPUT
  228. //------------------------------------------------------------------------------
  229. const char* Platform::getClipboard()
  230. {
  231. return "";
  232. }
  233. //------------------------------------------------------------------------------
  234. bool Platform::setClipboard(const char *text)
  235. {
  236. return false;
  237. }
  238. #pragma mark ---- Cursor Functions ----
  239. //------------------------------------------------------------------------------
  240. void Input::pushCursor(S32 cursorID)
  241. {
  242. CursorManager* cm = getCursorManager();
  243. if(cm)
  244. cm->pushCursor(cursorID);
  245. }
  246. //------------------------------------------------------------------------------
  247. void Input::popCursor()
  248. {
  249. CursorManager* cm = getCursorManager();
  250. if(cm)
  251. cm->popCursor();
  252. }
  253. //------------------------------------------------------------------------------
  254. void Input::refreshCursor()
  255. {
  256. CursorManager* cm = getCursorManager();
  257. if(cm)
  258. cm->refreshCursor();
  259. }
  260. #pragma mark ---- DoubleClick Functions ----
  261. //------------------------------------------------------------------------------
  262. U32 Input::getDoubleClickTime()
  263. {
  264. return ( 50 * 60.0f * 1000.0f);
  265. }
  266. //------------------------------------------------------------------------------
  267. S32 Input::getDoubleClickWidth()
  268. {
  269. // this is an arbitrary value.
  270. return 10;
  271. }
  272. //------------------------------------------------------------------------------
  273. S32 Input::getDoubleClickHeight()
  274. {
  275. return getDoubleClickWidth();
  276. }
  277. #pragma mark -
  278. //------------------------------------------------------------------------------
  279. void Input::setCursorPos(S32 x, S32 y)
  280. {
  281. SDL_WarpMouse((S16)x, (S16)y);
  282. }