windowInputGenerator.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. event.modifier = modifier;
  127. event.ascii = 0;
  128. // Generate delta movement along each axis
  129. Point2F cursDelta;
  130. if(isRelative)
  131. {
  132. cursDelta.x = F32(x) * mPixelsPerMickey;
  133. cursDelta.y = F32(y) * mPixelsPerMickey;
  134. }
  135. else
  136. {
  137. cursDelta.x = F32(x - mLastCursorPos.x);
  138. cursDelta.y = F32(y - mLastCursorPos.y);
  139. }
  140. // If X axis changed, generate a relative event
  141. if(mFabs(cursDelta.x) > 0.1)
  142. {
  143. event.objInst = SI_XAXIS;
  144. event.action = SI_MOVE;
  145. event.fValue = cursDelta.x;
  146. generateInputEvent(event);
  147. }
  148. // If Y axis changed, generate a relative event
  149. if(mFabs(cursDelta.y) > 0.1)
  150. {
  151. event.objInst = SI_YAXIS;
  152. event.action = SI_MOVE;
  153. event.fValue = cursDelta.y;
  154. generateInputEvent(event);
  155. }
  156. // CodeReview : If we're not relative, pass along a positional update
  157. // so that the canvas can update it's internal cursor tracking
  158. // point. [7/6/2007 justind]
  159. if( !isRelative )
  160. {
  161. if( mClampToWindow )
  162. {
  163. Point2I winExtent = mWindow->getClientExtent();
  164. x = mClampF(x, 0.0f, F32(winExtent.x - 1));
  165. y = mClampF(y, 0.0f, F32(winExtent.y - 1));
  166. }
  167. // When the window gains focus, we send a cursor position event
  168. if( mNotifyPosition )
  169. {
  170. mNotifyPosition = false;
  171. // We use SI_MAKE to signify that the position is being set, not relatively moved.
  172. event.action = SI_MAKE;
  173. // X Axis
  174. event.objInst = SI_XAXIS;
  175. event.fValue = (F32)x;
  176. generateInputEvent(event);
  177. // Y Axis
  178. event.objInst = SI_YAXIS;
  179. event.fValue = (F32)y;
  180. generateInputEvent(event);
  181. }
  182. mLastCursorPos = Point2I(x,y);
  183. }
  184. else
  185. {
  186. mLastCursorPos += Point2I(x,y);
  187. mNotifyPosition = true;
  188. }
  189. }
  190. void WindowInputGenerator::handleMouseButton( WindowId did, U32 modifiers, U32 action, U16 button )
  191. {
  192. if( !mInputController || !mFocused )
  193. return;
  194. InputEventInfo event;
  195. event.deviceType = MouseDeviceType;
  196. event.deviceInst = 0;
  197. event.objType = SI_BUTTON;
  198. event.objInst = (InputObjectInstances)(KEY_BUTTON0 + button);
  199. event.modifier = modifiers;
  200. event.ascii = 0;
  201. event.action = (action==IA_MAKE) ? SI_MAKE : SI_BREAK;
  202. event.fValue = (action==IA_MAKE) ? 1.0 : 0.0;
  203. generateInputEvent(event);
  204. }
  205. void WindowInputGenerator::handleMouseWheel( WindowId did, U32 modifiers, S32 wheelDeltaX, S32 wheelDeltaY )
  206. {
  207. if( !mInputController || !mFocused )
  208. return;
  209. InputEventInfo event;
  210. event.deviceType = MouseDeviceType;
  211. event.deviceInst = 0;
  212. event.objType = SI_AXIS;
  213. event.modifier = modifiers;
  214. event.ascii = 0;
  215. event.action = SI_MOVE;
  216. if( wheelDeltaY ) // Vertical
  217. {
  218. event.objInst = SI_ZAXIS;
  219. event.fValue = (F32)wheelDeltaY;
  220. generateInputEvent(event);
  221. }
  222. if( wheelDeltaX ) // Horizontal
  223. {
  224. event.objInst = SI_RZAXIS;
  225. event.fValue = (F32)wheelDeltaX;
  226. generateInputEvent(event);
  227. }
  228. }
  229. //-----------------------------------------------------------------------------
  230. // Key/Character Input
  231. //-----------------------------------------------------------------------------
  232. void WindowInputGenerator::handleCharInput( WindowId did, U32 modifier, U16 key )
  233. {
  234. if( !mInputController || !mFocused )
  235. return;
  236. InputEventInfo event;
  237. event.deviceType = KeyboardDeviceType;
  238. event.deviceInst = 0;
  239. event.objType = SI_KEY;
  240. event.objInst = KEY_NULL;
  241. event.modifier = modifier;
  242. event.ascii = key;
  243. event.action = SI_MAKE;
  244. event.fValue = 1.0;
  245. generateInputEvent(event);
  246. event.action = SI_BREAK;
  247. event.fValue = 0.f;
  248. generateInputEvent(event);
  249. }
  250. void WindowInputGenerator::handleKeyboard( WindowId did, U32 modifier, U32 action, U16 key )
  251. {
  252. if( !mInputController || !mFocused )
  253. return;
  254. InputEventInfo event;
  255. event.deviceType = KeyboardDeviceType;
  256. event.deviceInst = 0;
  257. event.objType = SI_KEY;
  258. event.objInst = (InputObjectInstances)key;
  259. event.modifier = modifier;
  260. event.ascii = 0;
  261. switch(action)
  262. {
  263. case IA_MAKE:
  264. event.action = SI_MAKE;
  265. event.fValue = 1.f;
  266. break;
  267. case IA_REPEAT:
  268. event.action = SI_REPEAT;
  269. event.fValue = 1.f;
  270. break;
  271. case IA_BREAK:
  272. event.action = SI_BREAK;
  273. event.fValue = 0.f;
  274. break;
  275. // If we encounter an unknown don't submit the event.
  276. default:
  277. //Con::warnf("GuiCanvas::handleKeyboard - got an unknown action type %d!", action);
  278. return;
  279. }
  280. generateInputEvent(event);
  281. }
  282. //-----------------------------------------------------------------------------
  283. // Raw input
  284. //-----------------------------------------------------------------------------
  285. 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 )
  286. {
  287. // Skip it if we don't have focus.
  288. if(!mInputController)// || !mFocused)
  289. return;
  290. // Convert to an InputEventInfo and pass it around for processing.
  291. InputEventInfo event;
  292. event.deviceInst = deviceInst;
  293. event.fValue = fValue;
  294. event.fValue2 = fValue2;
  295. event.fValue3 = fValue3;
  296. event.fValue4 = fValue4;
  297. event.iValue = iValue;
  298. event.deviceType = (InputDeviceTypes)deviceType;
  299. event.objType = (InputEventType)objType;
  300. event.ascii = ascii;
  301. event.objInst = (InputObjectInstances)objInst;
  302. event.action = (InputActionType)action;
  303. event.modifier = (InputModifiers)modifier;
  304. generateInputEvent(event);
  305. }
  306. //-----------------------------------------------------------------------------
  307. // Window Events
  308. //-----------------------------------------------------------------------------
  309. void WindowInputGenerator::handleAppEvent( WindowId did, S32 event )
  310. {
  311. if(event == LoseFocus)
  312. {
  313. // Fire all breaks; this will prevent issues with dangling keys.
  314. ActionMap::clearAllBreaks();
  315. mFocused = false;
  316. }
  317. else if(event == GainFocus)
  318. {
  319. // Set an update flag to notify the consumer of the absolute mouse position next move
  320. mNotifyPosition = true;
  321. mFocused = true;
  322. }
  323. // always focused with offscreen rendering
  324. if (mWindow->getOffscreenRender())
  325. mFocused = true;
  326. }
  327. //-----------------------------------------------------------------------------
  328. // Character Input Mapping
  329. //-----------------------------------------------------------------------------
  330. bool WindowInputGenerator::wantAsKeyboardEvent( U32 modifiers, U32 keyCode )
  331. {
  332. // Disallow translation on keys that are bound in the global action map.
  333. return ActionMap::getGlobalMap()->isAction(
  334. KeyboardDeviceType,
  335. 0,
  336. modifiers,
  337. keyCode
  338. );
  339. }