guiInputCtrl.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. IMPLEMENT_CONOBJECT(GuiInputCtrl);
  26. ConsoleDocClass( GuiInputCtrl,
  27. "@brief A control that locks the mouse and reports all keyboard input events to script.\n\n"
  28. "This is useful for implementing custom keyboard handling code, and most commonly "
  29. "used in Torque for a menu that allows a user to remap their in-game controls\n\n "
  30. "@tsexample\n"
  31. "new GuiInputCtrl(OptRemapInputCtrl)\n"
  32. "{\n"
  33. " lockMouse = \"0\";\n"
  34. " position = \"0 0\";\n"
  35. " extent = \"64 64\";\n"
  36. " minExtent = \"8 8\";\n"
  37. " horizSizing = \"center\";\n"
  38. " vertSizing = \"bottom\";\n"
  39. " profile = \"GuiInputCtrlProfile\";\n"
  40. " visible = \"1\";\n"
  41. " active = \"1\";\n"
  42. " tooltipProfile = \"GuiToolTipProfile\";\n"
  43. " hovertime = \"1000\";\n"
  44. " isContainer = \"0\";\n"
  45. " canSave = \"1\";\n"
  46. " canSaveDynamicFields = \"0\";\n"
  47. "};\n"
  48. "@endtsexample\n\n"
  49. "@see GuiMouseEventCtrl\n"
  50. "@ingroup GuiUtil\n");
  51. //------------------------------------------------------------------------------
  52. GuiInputCtrl::GuiInputCtrl()
  53. : mSendAxisEvents(false),
  54. mSendBreakEvents(false),
  55. mSendModifierEvents(false),
  56. mIgnoreMouseEvents(false)
  57. {
  58. }
  59. //------------------------------------------------------------------------------
  60. void GuiInputCtrl::initPersistFields()
  61. {
  62. addGroup("GuiInputCtrl");
  63. addField("sendAxisEvents", TypeBool, Offset(mSendAxisEvents, GuiInputCtrl),
  64. "If true, onAxisEvent callbacks will be sent for SI_AXIS Move events (Default false).");
  65. addField("sendBreakEvents", TypeBool, Offset(mSendBreakEvents, GuiInputCtrl),
  66. "If true, break events for all devices will generate callbacks (Default false).");
  67. addField("sendModifierEvents", TypeBool, Offset(mSendModifierEvents, GuiInputCtrl),
  68. "If true, Make events will be sent for modifier keys (Default false).");
  69. addField("ignoreMouseEvents", TypeBool, Offset(mIgnoreMouseEvents, GuiInputCtrl),
  70. "If true, any events from mouse devices will be passed through.");
  71. endGroup("GuiInputCtrl");
  72. Parent::initPersistFields();
  73. }
  74. //------------------------------------------------------------------------------
  75. bool GuiInputCtrl::onWake()
  76. {
  77. // Set the default profile on start-up:
  78. if( !mProfile )
  79. {
  80. GuiControlProfile* profile;
  81. Sim::findObject( "GuiInputCtrlProfile", profile);
  82. if( profile )
  83. setControlProfile( profile );
  84. }
  85. if ( !Parent::onWake() )
  86. return( false );
  87. if( !smDesignTime && !mIgnoreMouseEvents)
  88. mouseLock();
  89. setFirstResponder();
  90. return( true );
  91. }
  92. //------------------------------------------------------------------------------
  93. void GuiInputCtrl::onSleep()
  94. {
  95. Parent::onSleep();
  96. mouseUnlock();
  97. clearFirstResponder();
  98. }
  99. //------------------------------------------------------------------------------
  100. static bool isModifierKey( U16 keyCode )
  101. {
  102. switch ( keyCode )
  103. {
  104. case KEY_LCONTROL:
  105. case KEY_RCONTROL:
  106. case KEY_LALT:
  107. case KEY_RALT:
  108. case KEY_LSHIFT:
  109. case KEY_RSHIFT:
  110. case KEY_MAC_LOPT:
  111. case KEY_MAC_ROPT:
  112. return( true );
  113. }
  114. return( false );
  115. }
  116. IMPLEMENT_CALLBACK( GuiInputCtrl, onInputEvent, void, (const char* device, const char* action, bool state ),
  117. ( device, action, state),
  118. "@brief Callback that occurs when an input is triggered on this control\n\n"
  119. "@param device The device type triggering the input, such as keyboard, mouse, etc\n"
  120. "@param action The actual event occuring, such as a key or button\n"
  121. "@param state True if the action is being pressed, false if it is being release\n\n");
  122. IMPLEMENT_CALLBACK(GuiInputCtrl, onAxisEvent, void, (const char* device, const char* action, F32 axisValue),
  123. (device, action, axisValue),
  124. "@brief Callback that occurs when an axis event is triggered on this control\n\n"
  125. "@param device The device type triggering the input, such as mouse, joystick, gamepad, etc\n"
  126. "@param action The ActionMap code for the axis\n"
  127. "@param axisValue The current value of the axis\n\n");
  128. //------------------------------------------------------------------------------
  129. bool GuiInputCtrl::onInputEvent( const InputEventInfo &event )
  130. {
  131. if (mIgnoreMouseEvents && event.deviceType == MouseDeviceType)
  132. return false;
  133. char deviceString[32];
  134. if ( event.action == SI_MAKE )
  135. {
  136. if ( event.objType == SI_BUTTON
  137. || event.objType == SI_POV
  138. || event.objType == SI_KEY )
  139. {
  140. if ( !ActionMap::getDeviceName( event.deviceType, event.deviceInst, deviceString ) )
  141. return false;
  142. if ((event.objType == SI_KEY) && isModifierKey(event.objInst))
  143. {
  144. if (!mSendModifierEvents)
  145. return false;
  146. char keyString[32];
  147. if (!ActionMap::getKeyString(event.objInst, keyString))
  148. return false;
  149. onInputEvent_callback(deviceString, keyString, 1);
  150. }
  151. else
  152. {
  153. const char* actionString = ActionMap::buildActionString(&event);
  154. onInputEvent_callback(deviceString, actionString, 1);
  155. }
  156. return true;
  157. }
  158. }
  159. else if ( event.action == SI_BREAK )
  160. {
  161. if ( ( event.objType == SI_KEY ) && isModifierKey( event.objInst ) )
  162. {
  163. char keyString[32];
  164. if ( !ActionMap::getKeyString( event.objInst, keyString ) )
  165. return false;
  166. onInputEvent_callback("keyboard", keyString, 0);
  167. return true;
  168. }
  169. else if (mSendBreakEvents)
  170. {
  171. if (!ActionMap::getDeviceName(event.deviceType, event.deviceInst, deviceString))
  172. return false;
  173. const char* actionString = ActionMap::buildActionString(&event);
  174. onInputEvent_callback(deviceString, actionString, 0);
  175. return true;
  176. }
  177. }
  178. else if (mSendAxisEvents && ((event.objType == SI_AXIS) || (event.objType == SI_INT) || (event.objType == SI_FLOAT)))
  179. {
  180. F32 fValue = event.fValue;
  181. if (event.objType == SI_INT)
  182. fValue = (F32)event.iValue;
  183. if (!ActionMap::getDeviceName(event.deviceType, event.deviceInst, deviceString))
  184. return false;
  185. const char* actionString = ActionMap::buildActionString(&event);
  186. onAxisEvent_callback(deviceString, actionString, fValue);
  187. return (event.deviceType != MouseDeviceType); // Don't consume mouse move events
  188. }
  189. return false;
  190. }