windowInputGenerator.cpp 13 KB

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