windowInputGenerator.cpp 12 KB

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