windowInputGenerator.cpp 13 KB

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