windowInputGenerator.cpp 13 KB

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