guiInputCtrl.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 "gui/utility/guiInputCtrl.h"
  23. #include "sim/actionMap.h"
  24. #include "console/engineAPI.h"
  25. #include "gui/core/guiCanvas.h"
  26. IMPLEMENT_CONOBJECT(GuiInputCtrl);
  27. ConsoleDocClass( GuiInputCtrl,
  28. "@brief A control that locks the mouse and reports all keyboard input events to script.\n\n"
  29. "This is useful for implementing custom keyboard handling code, and most commonly "
  30. "used in Torque for a menu that allows a user to remap their in-game controls\n\n "
  31. "@tsexample\n"
  32. "new GuiInputCtrl(OptRemapInputCtrl)\n"
  33. "{\n"
  34. " lockMouse = \"0\";\n"
  35. " position = \"0 0\";\n"
  36. " extent = \"64 64\";\n"
  37. " minExtent = \"8 8\";\n"
  38. " horizSizing = \"center\";\n"
  39. " vertSizing = \"bottom\";\n"
  40. " profile = \"GuiInputCtrlProfile\";\n"
  41. " visible = \"1\";\n"
  42. " active = \"1\";\n"
  43. " tooltipProfile = \"GuiToolTipProfile\";\n"
  44. " hovertime = \"1000\";\n"
  45. " isContainer = \"0\";\n"
  46. " canSave = \"1\";\n"
  47. " canSaveDynamicFields = \"0\";\n"
  48. "};\n"
  49. "@endtsexample\n\n"
  50. "@see GuiMouseEventCtrl\n"
  51. "@ingroup GuiUtil\n");
  52. //------------------------------------------------------------------------------
  53. GuiInputCtrl::GuiInputCtrl()
  54. : mSendAxisEvents(false),
  55. mSendBreakEvents(false),
  56. mSendModifierEvents(false),
  57. mIgnoreMouseEvents(false)
  58. {
  59. mActionmap = nullptr;
  60. }
  61. //------------------------------------------------------------------------------
  62. void GuiInputCtrl::initPersistFields()
  63. {
  64. docsURL;
  65. addGroup("GuiInputCtrl");
  66. addField("sendAxisEvents", TypeBool, Offset(mSendAxisEvents, GuiInputCtrl),
  67. "If true, onAxisEvent callbacks will be sent for SI_AXIS Move events (Default false).");
  68. addField("sendBreakEvents", TypeBool, Offset(mSendBreakEvents, GuiInputCtrl),
  69. "If true, break events for all devices will generate callbacks (Default false).");
  70. addField("sendModifierEvents", TypeBool, Offset(mSendModifierEvents, GuiInputCtrl),
  71. "If true, Make events will be sent for modifier keys (Default false).");
  72. addField("ignoreMouseEvents", TypeBool, Offset(mIgnoreMouseEvents, GuiInputCtrl),
  73. "If true, any events from mouse devices will be passed through.");
  74. addField("actionMap", TYPEID<ActionMap>(), Offset(mActionmap, GuiInputCtrl), "The name of an action map to push/pop on the input stack alongside the wake/sleep of this control.");
  75. endGroup("GuiInputCtrl");
  76. Parent::initPersistFields();
  77. }
  78. //------------------------------------------------------------------------------
  79. bool GuiInputCtrl::onAdd()
  80. {
  81. if (!Parent::onAdd())
  82. return false;
  83. GuiCanvas::getCanvasSetActiveSignal().notify(this, &GuiInputCtrl::handleCanvasSetActive);
  84. }
  85. bool GuiInputCtrl::onWake()
  86. {
  87. // Set the default profile on start-up:
  88. if( !mProfile )
  89. {
  90. GuiControlProfile* profile;
  91. Sim::findObject( "GuiInputCtrlProfile", profile);
  92. if( profile )
  93. setControlProfile( profile );
  94. }
  95. if ( !Parent::onWake() )
  96. return( false );
  97. if( !smDesignTime && !mIgnoreMouseEvents)
  98. mouseLock();
  99. if(mActionmap != nullptr)
  100. {
  101. if (getRoot()->isActive())
  102. {
  103. SimSet* actionMapSet = Sim::getActiveActionMapSet();
  104. actionMapSet->pushObject(mActionmap);
  105. }
  106. }
  107. setFirstResponder();
  108. return( true );
  109. }
  110. //------------------------------------------------------------------------------
  111. void GuiInputCtrl::onSleep()
  112. {
  113. Parent::onSleep();
  114. mouseUnlock();
  115. if (mActionmap != nullptr)
  116. {
  117. SimSet* actionMapSet = Sim::getActiveActionMapSet();
  118. actionMapSet->removeObject(mActionmap);
  119. }
  120. clearFirstResponder();
  121. }
  122. void GuiInputCtrl::setActive(bool value)
  123. {
  124. Parent::setActive(value);
  125. if (value)
  126. {
  127. if (!smDesignTime && !mIgnoreMouseEvents)
  128. mouseLock();
  129. setFirstResponder();
  130. }
  131. else
  132. {
  133. mouseUnlock();
  134. clearFirstResponder();
  135. }
  136. }
  137. void GuiInputCtrl::handleCanvasSetActive(GuiCanvas* canvas, bool isActive)
  138. {
  139. if (mActionmap == nullptr)
  140. return;
  141. if (getRoot() == canvas)
  142. {
  143. if (isActive)
  144. {
  145. SimSet* actionMapSet = Sim::getActiveActionMapSet();
  146. actionMapSet->pushObject(mActionmap);
  147. }
  148. else
  149. {
  150. SimSet* actionMapSet = Sim::getActiveActionMapSet();
  151. actionMapSet->removeObject(mActionmap);
  152. }
  153. }
  154. }
  155. //------------------------------------------------------------------------------
  156. static bool isModifierKey( U16 keyCode )
  157. {
  158. switch ( keyCode )
  159. {
  160. case KEY_LCONTROL:
  161. case KEY_RCONTROL:
  162. case KEY_LALT:
  163. case KEY_RALT:
  164. case KEY_LSHIFT:
  165. case KEY_RSHIFT:
  166. case KEY_MAC_LOPT:
  167. case KEY_MAC_ROPT:
  168. return( true );
  169. }
  170. return( false );
  171. }
  172. IMPLEMENT_CALLBACK( GuiInputCtrl, onInputEvent, void, (const char* device, const char* action, bool state ),
  173. ( device, action, state),
  174. "@brief Callback that occurs when an input is triggered on this control\n\n"
  175. "@param device The device type triggering the input, such as keyboard, mouse, etc\n"
  176. "@param action The actual event occuring, such as a key or button\n"
  177. "@param state True if the action is being pressed, false if it is being release\n\n");
  178. IMPLEMENT_CALLBACK(GuiInputCtrl, onAxisEvent, void, (const char* device, const char* action, F32 axisValue),
  179. (device, action, axisValue),
  180. "@brief Callback that occurs when an axis event is triggered on this control\n\n"
  181. "@param device The device type triggering the input, such as mouse, joystick, gamepad, etc\n"
  182. "@param action The ActionMap code for the axis\n"
  183. "@param axisValue The current value of the axis\n\n");
  184. //------------------------------------------------------------------------------
  185. bool GuiInputCtrl::onInputEvent( const InputEventInfo &event )
  186. {
  187. if (mIgnoreMouseEvents && event.deviceType == MouseDeviceType)
  188. return false;
  189. if (mActionmap != nullptr)
  190. return false;
  191. char deviceString[32];
  192. if ( event.action == SI_MAKE )
  193. {
  194. if ( event.objType == SI_BUTTON
  195. || event.objType == SI_POV
  196. || event.objType == SI_KEY )
  197. {
  198. if ( !ActionMap::getDeviceName( event.deviceType, event.deviceInst, deviceString ) )
  199. return false;
  200. if ((event.objType == SI_KEY) && isModifierKey(event.objInst))
  201. {
  202. if (!mSendModifierEvents)
  203. return false;
  204. char keyString[32];
  205. if (!ActionMap::getKeyString(event.objInst, keyString))
  206. return false;
  207. onInputEvent_callback(deviceString, keyString, 1);
  208. }
  209. else
  210. {
  211. const char* actionString = ActionMap::buildActionString(&event);
  212. onInputEvent_callback(deviceString, actionString, 1);
  213. }
  214. return true;
  215. }
  216. }
  217. else if ( event.action == SI_BREAK )
  218. {
  219. if ( ( event.objType == SI_KEY ) && isModifierKey( event.objInst ) )
  220. {
  221. char keyString[32];
  222. if ( !ActionMap::getKeyString( event.objInst, keyString ) )
  223. return false;
  224. onInputEvent_callback("keyboard", keyString, 0);
  225. return true;
  226. }
  227. else if (mSendBreakEvents)
  228. {
  229. if (!ActionMap::getDeviceName(event.deviceType, event.deviceInst, deviceString))
  230. return false;
  231. const char* actionString = ActionMap::buildActionString(&event);
  232. onInputEvent_callback(deviceString, actionString, 0);
  233. return true;
  234. }
  235. }
  236. else if (mSendAxisEvents && ((event.objType == SI_AXIS) || (event.objType == SI_INT) || (event.objType == SI_FLOAT)))
  237. {
  238. F32 fValue = event.fValue;
  239. if (event.objType == SI_INT)
  240. fValue = (F32)event.iValue;
  241. if (!ActionMap::getDeviceName(event.deviceType, event.deviceInst, deviceString))
  242. return false;
  243. const char* actionString = ActionMap::buildActionString(&event);
  244. onAxisEvent_callback(deviceString, actionString, fValue);
  245. return (event.deviceType != MouseDeviceType); // Don't consume mouse move events
  246. }
  247. return false;
  248. }