windowInputGenerator.cpp 13 KB

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