windowInputGenerator.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #include "windowManager/windowInputGenerator.h"
  23. #include "windowManager/platformWindow.h"
  24. #include "sim/actionMap.h"
  25. #include "platform/input/IProcessInput.h"
  26. extern InputModifiers convertModifierBits(const U32 in);
  27. //-----------------------------------------------------------------------------
  28. // Constructor/Destructor
  29. //-----------------------------------------------------------------------------
  30. WindowInputGenerator::WindowInputGenerator( PlatformWindow *window ) :
  31. mNotifyPosition(true),
  32. mWindow(window),
  33. mInputController(NULL),
  34. mLastCursorPos(0,0),
  35. mClampToWindow(true),
  36. mFocused(false),
  37. mPixelsPerMickey(1.0f),
  38. mLastPressWasGlobalActionMap(false)
  39. {
  40. AssertFatal(mWindow, "NULL PlatformWindow on WindowInputGenerator creation");
  41. #ifdef TORQUE_OS_XENON
  42. mFocused = true;
  43. #endif
  44. if (mWindow->getOffscreenRender())
  45. mFocused = true;
  46. mWindow->appEvent.notify(this, &WindowInputGenerator::handleAppEvent);
  47. mWindow->mouseEvent.notify(this, &WindowInputGenerator::handleMouseMove);
  48. mWindow->wheelEvent.notify(this, &WindowInputGenerator::handleMouseWheel);
  49. mWindow->buttonEvent.notify(this, &WindowInputGenerator::handleMouseButton);
  50. mWindow->keyEvent.notify(this, &WindowInputGenerator::handleKeyboard);
  51. mWindow->charEvent.notify(this, &WindowInputGenerator::handleCharInput);
  52. // We also want to subscribe to input events.
  53. Input::smInputEvent.notify(this, &WindowInputGenerator::handleInputEvent);
  54. }
  55. WindowInputGenerator::~WindowInputGenerator()
  56. {
  57. if( mWindow )
  58. {
  59. mWindow->mouseEvent.remove(this, &WindowInputGenerator::handleMouseMove);
  60. mWindow->buttonEvent.remove(this, &WindowInputGenerator::handleMouseButton);
  61. mWindow->wheelEvent.remove(this, &WindowInputGenerator::handleMouseWheel);
  62. mWindow->keyEvent.remove(this, &WindowInputGenerator::handleKeyboard);
  63. mWindow->charEvent.remove(this, &WindowInputGenerator::handleCharInput);
  64. mWindow->appEvent.remove(this, &WindowInputGenerator::handleAppEvent);
  65. }
  66. Input::smInputEvent.remove(this, &WindowInputGenerator::handleInputEvent);
  67. }
  68. //-----------------------------------------------------------------------------
  69. // Process an input event and pass it on.
  70. // Respect the action map.
  71. //-----------------------------------------------------------------------------
  72. void WindowInputGenerator::generateInputEvent( InputEventInfo &inputEvent )
  73. {
  74. // Reset last press being global
  75. mLastPressWasGlobalActionMap = false;
  76. if (!mInputController)// || !mFocused)
  77. return;
  78. if (inputEvent.action == SI_MAKE && inputEvent.deviceType == KeyboardDeviceType)
  79. {
  80. for (int i = 0; i < mAcceleratorMap.size(); ++i)
  81. {
  82. const AccKeyMap &acc = mAcceleratorMap[i];
  83. if (!mWindow->getKeyboardTranslation() &&
  84. ((acc.modifier == inputEvent.modifier && acc.modifier != 0) || (acc.modifier == 0 && inputEvent.modifier == 0))
  85. && acc.keyCode == inputEvent.objInst)
  86. {
  87. Con::evaluatef(acc.cmd);
  88. return;
  89. }
  90. }
  91. }
  92. // Give the ActionMap first shot.
  93. if (ActionMap::handleEventGlobal(&inputEvent))
  94. {
  95. mLastPressWasGlobalActionMap = true;
  96. return;
  97. }
  98. if (mInputController->processInputEvent(inputEvent))
  99. return;
  100. if (mWindow->getKeyboardTranslation())
  101. return;
  102. // If we get here we failed to process it with anything prior... so let
  103. // the ActionMap handle it.
  104. ActionMap::handleEvent(&inputEvent);
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Mouse Events
  108. //-----------------------------------------------------------------------------
  109. void WindowInputGenerator::handleMouseMove( WindowId did, U32 modifier, S32 x, S32 y, bool isRelative )
  110. {
  111. if( !mInputController || !mFocused )
  112. return;
  113. // jddTODO : Clean this up
  114. // CodeReview currently the Torque GuiCanvas deals with mouse input
  115. // as relative movement, even when the cursor is visible. Because
  116. // of this there is an asinine bit of code in there that manages
  117. // updating the cursor position on the class based on relative movement.
  118. // Because of this we always have to generate and send off for processing
  119. // relative events, even if the mouse is not locked.
  120. // I'm considering removing this in the Canvas refactor, thoughts? [7/6/2007 justind]
  121. // Generate a base Movement along and Axis event
  122. InputEventInfo event;
  123. event.deviceType = MouseDeviceType;
  124. event.deviceInst = 0;
  125. event.objType = SI_AXIS;
  126. #ifdef TORQUE_SDL
  127. event.modifier = modifier;
  128. #else
  129. event.modifier = convertModifierBits(modifier);
  130. #endif
  131. event.ascii = 0;
  132. // Generate delta movement along each axis
  133. Point2F cursDelta;
  134. if(isRelative)
  135. {
  136. cursDelta.x = F32(x) * mPixelsPerMickey;
  137. cursDelta.y = F32(y) * mPixelsPerMickey;
  138. }
  139. else
  140. {
  141. cursDelta.x = F32(x - mLastCursorPos.x);
  142. cursDelta.y = F32(y - mLastCursorPos.y);
  143. }
  144. // If X axis changed, generate a relative event
  145. if(mFabs(cursDelta.x) > 0.1)
  146. {
  147. event.objInst = SI_XAXIS;
  148. event.action = SI_MOVE;
  149. event.fValue = cursDelta.x;
  150. generateInputEvent(event);
  151. }
  152. // If Y axis changed, generate a relative event
  153. if(mFabs(cursDelta.y) > 0.1)
  154. {
  155. event.objInst = SI_YAXIS;
  156. event.action = SI_MOVE;
  157. event.fValue = cursDelta.y;
  158. generateInputEvent(event);
  159. }
  160. // CodeReview : If we're not relative, pass along a positional update
  161. // so that the canvas can update it's internal cursor tracking
  162. // point. [7/6/2007 justind]
  163. if( !isRelative )
  164. {
  165. if( mClampToWindow )
  166. {
  167. Point2I winExtent = mWindow->getClientExtent();
  168. x = mClampF(x, 0.0f, F32(winExtent.x - 1));
  169. y = mClampF(y, 0.0f, F32(winExtent.y - 1));
  170. }
  171. // When the window gains focus, we send a cursor position event
  172. if( mNotifyPosition )
  173. {
  174. mNotifyPosition = false;
  175. // We use SI_MAKE to signify that the position is being set, not relatively moved.
  176. event.action = SI_MAKE;
  177. // X Axis
  178. event.objInst = SI_XAXIS;
  179. event.fValue = (F32)x;
  180. generateInputEvent(event);
  181. // Y Axis
  182. event.objInst = SI_YAXIS;
  183. event.fValue = (F32)y;
  184. generateInputEvent(event);
  185. }
  186. mLastCursorPos = Point2I(x,y);
  187. }
  188. else
  189. {
  190. mLastCursorPos += Point2I(x,y);
  191. mNotifyPosition = true;
  192. }
  193. }
  194. void WindowInputGenerator::handleMouseButton( WindowId did, U32 modifiers, U32 action, U16 button )
  195. {
  196. if( !mInputController || !mFocused )
  197. return;
  198. InputEventInfo event;
  199. event.deviceType = MouseDeviceType;
  200. event.deviceInst = 0;
  201. event.objType = SI_BUTTON;
  202. event.objInst = (InputObjectInstances)(KEY_BUTTON0 + button);
  203. #ifdef TORQUE_SDL
  204. event.modifier = modifiers;
  205. #else
  206. event.modifier = convertModifierBits(modifiers);
  207. #endif
  208. event.ascii = 0;
  209. event.action = (action==IA_MAKE) ? SI_MAKE : SI_BREAK;
  210. event.fValue = (action==IA_MAKE) ? 1.0 : 0.0;
  211. generateInputEvent(event);
  212. }
  213. void WindowInputGenerator::handleMouseWheel( WindowId did, U32 modifiers, S32 wheelDeltaX, S32 wheelDeltaY )
  214. {
  215. if( !mInputController || !mFocused )
  216. return;
  217. InputEventInfo event;
  218. event.deviceType = MouseDeviceType;
  219. event.deviceInst = 0;
  220. event.objType = SI_AXIS;
  221. #ifdef TORQUE_SDL
  222. event.modifier = modifiers;
  223. #else
  224. event.modifier = convertModifierBits(modifiers);
  225. #endif
  226. event.ascii = 0;
  227. event.action = SI_MOVE;
  228. if( wheelDeltaY ) // Vertical
  229. {
  230. event.objInst = SI_ZAXIS;
  231. event.fValue = (F32)wheelDeltaY;
  232. generateInputEvent(event);
  233. }
  234. if( wheelDeltaX ) // Horizontal
  235. {
  236. event.objInst = SI_RZAXIS;
  237. event.fValue = (F32)wheelDeltaX;
  238. generateInputEvent(event);
  239. }
  240. }
  241. //-----------------------------------------------------------------------------
  242. // Key/Character Input
  243. //-----------------------------------------------------------------------------
  244. void WindowInputGenerator::handleCharInput( WindowId did, U32 modifier, U16 key )
  245. {
  246. if( !mInputController || !mFocused )
  247. return;
  248. InputEventInfo event;
  249. event.deviceType = KeyboardDeviceType;
  250. event.deviceInst = 0;
  251. event.objType = SI_KEY;
  252. event.objInst = KEY_NULL;
  253. #ifdef TORQUE_SDL
  254. event.modifier = modifier;
  255. #else
  256. event.modifier = convertModifierBits(modifier);
  257. #endif
  258. event.ascii = key;
  259. event.action = SI_MAKE;
  260. event.fValue = 1.0;
  261. generateInputEvent(event);
  262. event.action = SI_BREAK;
  263. event.fValue = 0.f;
  264. generateInputEvent(event);
  265. }
  266. void WindowInputGenerator::handleKeyboard( WindowId did, U32 modifier, U32 action, U16 key )
  267. {
  268. if( !mInputController || !mFocused )
  269. return;
  270. InputEventInfo event;
  271. event.deviceType = KeyboardDeviceType;
  272. event.deviceInst = 0;
  273. event.objType = SI_KEY;
  274. event.objInst = (InputObjectInstances)key;
  275. #ifdef TORQUE_SDL
  276. event.modifier = modifier;
  277. #else
  278. event.modifier = convertModifierBits(modifier);
  279. #endif
  280. event.ascii = 0;
  281. switch(action)
  282. {
  283. case IA_MAKE:
  284. event.action = SI_MAKE;
  285. event.fValue = 1.f;
  286. break;
  287. case IA_REPEAT:
  288. event.action = SI_REPEAT;
  289. event.fValue = 1.f;
  290. break;
  291. case IA_BREAK:
  292. event.action = SI_BREAK;
  293. event.fValue = 0.f;
  294. break;
  295. // If we encounter an unknown don't submit the event.
  296. default:
  297. //Con::warnf("GuiCanvas::handleKeyboard - got an unknown action type %d!", action);
  298. return;
  299. }
  300. generateInputEvent(event);
  301. }
  302. //-----------------------------------------------------------------------------
  303. // Raw input
  304. //-----------------------------------------------------------------------------
  305. void WindowInputGenerator::handleInputEvent( U32 deviceInst, F32 fValue, F32 fValue2, F32 fValue3, F32 fValue4, S32 iValue, U16 deviceType, U16 objType, U16 ascii, U16 objInst, U8 action, U8 modifier )
  306. {
  307. // Skip it if we don't have focus.
  308. if(!mInputController)// || !mFocused)
  309. return;
  310. // Convert to an InputEventInfo and pass it around for processing.
  311. InputEventInfo event;
  312. event.deviceInst = deviceInst;
  313. event.fValue = fValue;
  314. event.fValue2 = fValue2;
  315. event.fValue3 = fValue3;
  316. event.fValue4 = fValue4;
  317. event.iValue = iValue;
  318. event.deviceType = (InputDeviceTypes)deviceType;
  319. event.objType = (InputEventType)objType;
  320. event.ascii = ascii;
  321. event.objInst = (InputObjectInstances)objInst;
  322. event.action = (InputActionType)action;
  323. event.modifier = (InputModifiers)modifier;
  324. generateInputEvent(event);
  325. }
  326. //-----------------------------------------------------------------------------
  327. // Window Events
  328. //-----------------------------------------------------------------------------
  329. void WindowInputGenerator::handleAppEvent( WindowId did, S32 event )
  330. {
  331. if(event == LoseFocus)
  332. {
  333. // Fire all breaks; this will prevent issues with dangling keys.
  334. ActionMap::clearAllBreaks();
  335. mFocused = false;
  336. }
  337. else if(event == GainFocus)
  338. {
  339. // Set an update flag to notify the consumer of the absolute mouse position next move
  340. mNotifyPosition = true;
  341. mFocused = true;
  342. }
  343. // always focused with offscreen rendering
  344. if (mWindow->getOffscreenRender())
  345. mFocused = true;
  346. }
  347. //-----------------------------------------------------------------------------
  348. // Character Input Mapping
  349. //-----------------------------------------------------------------------------
  350. bool WindowInputGenerator::wantAsKeyboardEvent( U32 modifiers, U32 keyCode )
  351. {
  352. // Disallow translation on keys that are bound in the global action map.
  353. return ActionMap::getGlobalMap()->isAction(
  354. KeyboardDeviceType,
  355. 0,
  356. modifiers,
  357. keyCode
  358. );
  359. }