guiInputCtrl.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. return true;
  85. }
  86. void GuiInputCtrl::onRemove()
  87. {
  88. GuiCanvas::getCanvasSetActiveSignal().remove(this, &GuiInputCtrl::handleCanvasSetActive);
  89. Parent::onRemove();
  90. }
  91. bool GuiInputCtrl::onWake()
  92. {
  93. // Set the default profile on start-up:
  94. if( !mProfile )
  95. {
  96. GuiControlProfile* profile;
  97. Sim::findObject( "GuiInputCtrlProfile", profile);
  98. if( profile )
  99. setControlProfile( profile );
  100. }
  101. if ( !Parent::onWake() )
  102. return( false );
  103. if( !smDesignTime && !mIgnoreMouseEvents)
  104. mouseLock();
  105. if(mActionmap != nullptr)
  106. {
  107. if (getRoot()->isActive())
  108. {
  109. SimSet* actionMapSet = Sim::getActiveActionMapSet();
  110. actionMapSet->pushObject(mActionmap);
  111. }
  112. }
  113. setFirstResponder();
  114. return( true );
  115. }
  116. //------------------------------------------------------------------------------
  117. void GuiInputCtrl::onSleep()
  118. {
  119. Parent::onSleep();
  120. mouseUnlock();
  121. if (mActionmap != nullptr)
  122. {
  123. SimSet* actionMapSet = Sim::getActiveActionMapSet();
  124. actionMapSet->removeObject(mActionmap);
  125. }
  126. clearFirstResponder();
  127. }
  128. void GuiInputCtrl::setActive(bool value)
  129. {
  130. Parent::setActive(value);
  131. if (value)
  132. {
  133. if (!smDesignTime && !mIgnoreMouseEvents)
  134. mouseLock();
  135. setFirstResponder();
  136. }
  137. else
  138. {
  139. mouseUnlock();
  140. clearFirstResponder();
  141. }
  142. }
  143. void GuiInputCtrl::handleCanvasSetActive(GuiCanvas* canvas, bool isActive)
  144. {
  145. if (mActionmap == nullptr)
  146. return;
  147. if (getRoot() == canvas)
  148. {
  149. if (isActive)
  150. {
  151. SimSet* actionMapSet = Sim::getActiveActionMapSet();
  152. actionMapSet->pushObject(mActionmap);
  153. }
  154. else
  155. {
  156. SimSet* actionMapSet = Sim::getActiveActionMapSet();
  157. actionMapSet->removeObject(mActionmap);
  158. }
  159. }
  160. }
  161. //------------------------------------------------------------------------------
  162. static bool isModifierKey( U16 keyCode )
  163. {
  164. switch ( keyCode )
  165. {
  166. case KEY_LCONTROL:
  167. case KEY_RCONTROL:
  168. case KEY_LALT:
  169. case KEY_RALT:
  170. case KEY_LSHIFT:
  171. case KEY_RSHIFT:
  172. case KEY_MAC_LOPT:
  173. case KEY_MAC_ROPT:
  174. return( true );
  175. }
  176. return( false );
  177. }
  178. IMPLEMENT_CALLBACK( GuiInputCtrl, onInputEvent, void, (const char* device, const char* action, bool state ),
  179. ( device, action, state),
  180. "@brief Callback that occurs when an input is triggered on this control\n\n"
  181. "@param device The device type triggering the input, such as keyboard, mouse, etc\n"
  182. "@param action The actual event occuring, such as a key or button\n"
  183. "@param state True if the action is being pressed, false if it is being release\n\n");
  184. IMPLEMENT_CALLBACK(GuiInputCtrl, onAxisEvent, void, (const char* device, const char* action, F32 axisValue),
  185. (device, action, axisValue),
  186. "@brief Callback that occurs when an axis event is triggered on this control\n\n"
  187. "@param device The device type triggering the input, such as mouse, joystick, gamepad, etc\n"
  188. "@param action The ActionMap code for the axis\n"
  189. "@param axisValue The current value of the axis\n\n");
  190. //------------------------------------------------------------------------------
  191. bool GuiInputCtrl::onInputEvent( const InputEventInfo &event )
  192. {
  193. if (mIgnoreMouseEvents && event.deviceType == MouseDeviceType)
  194. return false;
  195. if (mActionmap != nullptr)
  196. return false;
  197. char deviceString[32];
  198. if ( event.action == SI_MAKE )
  199. {
  200. if ( event.objType == SI_BUTTON
  201. || event.objType == SI_POV
  202. || event.objType == SI_KEY )
  203. {
  204. if ( !ActionMap::getDeviceName( event.deviceType, event.deviceInst, deviceString ) )
  205. return false;
  206. if ((event.objType == SI_KEY) && isModifierKey(event.objInst))
  207. {
  208. if (!mSendModifierEvents)
  209. return false;
  210. char keyString[32];
  211. if (!ActionMap::getKeyString(event.objInst, keyString))
  212. return false;
  213. onInputEvent_callback(deviceString, keyString, 1);
  214. }
  215. else
  216. {
  217. const char* actionString = ActionMap::buildActionString(&event);
  218. onInputEvent_callback(deviceString, actionString, 1);
  219. }
  220. return true;
  221. }
  222. }
  223. else if ( event.action == SI_BREAK )
  224. {
  225. if ( ( event.objType == SI_KEY ) && isModifierKey( event.objInst ) )
  226. {
  227. char keyString[32];
  228. if ( !ActionMap::getKeyString( event.objInst, keyString ) )
  229. return false;
  230. onInputEvent_callback("keyboard", keyString, 0);
  231. return true;
  232. }
  233. else if (mSendBreakEvents)
  234. {
  235. if (!ActionMap::getDeviceName(event.deviceType, event.deviceInst, deviceString))
  236. return false;
  237. const char* actionString = ActionMap::buildActionString(&event);
  238. onInputEvent_callback(deviceString, actionString, 0);
  239. return true;
  240. }
  241. }
  242. else if (mSendAxisEvents && ((event.objType == SI_AXIS) || (event.objType == SI_INT) || (event.objType == SI_FLOAT)))
  243. {
  244. F32 fValue = event.fValue;
  245. if (event.objType == SI_INT)
  246. fValue = (F32)event.iValue;
  247. if (!ActionMap::getDeviceName(event.deviceType, event.deviceInst, deviceString))
  248. return false;
  249. const char* actionString = ActionMap::buildActionString(&event);
  250. onAxisEvent_callback(deviceString, actionString, fValue);
  251. return (event.deviceType != MouseDeviceType); // Don't consume mouse move events
  252. }
  253. return false;
  254. }