guiInputCtrl.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. docsURL;
  63. addGroup("GuiInputCtrl");
  64. addField("sendAxisEvents", TypeBool, Offset(mSendAxisEvents, GuiInputCtrl),
  65. "If true, onAxisEvent callbacks will be sent for SI_AXIS Move events (Default false).");
  66. addField("sendBreakEvents", TypeBool, Offset(mSendBreakEvents, GuiInputCtrl),
  67. "If true, break events for all devices will generate callbacks (Default false).");
  68. addField("sendModifierEvents", TypeBool, Offset(mSendModifierEvents, GuiInputCtrl),
  69. "If true, Make events will be sent for modifier keys (Default false).");
  70. addField("ignoreMouseEvents", TypeBool, Offset(mIgnoreMouseEvents, GuiInputCtrl),
  71. "If true, any events from mouse devices will be passed through.");
  72. endGroup("GuiInputCtrl");
  73. Parent::initPersistFields();
  74. }
  75. //------------------------------------------------------------------------------
  76. bool GuiInputCtrl::onWake()
  77. {
  78. // Set the default profile on start-up:
  79. if( !mProfile )
  80. {
  81. GuiControlProfile* profile;
  82. Sim::findObject( "GuiInputCtrlProfile", profile);
  83. if( profile )
  84. setControlProfile( profile );
  85. }
  86. if ( !Parent::onWake() )
  87. return( false );
  88. if( !smDesignTime && !mIgnoreMouseEvents)
  89. mouseLock();
  90. setFirstResponder();
  91. return( true );
  92. }
  93. //------------------------------------------------------------------------------
  94. void GuiInputCtrl::onSleep()
  95. {
  96. Parent::onSleep();
  97. mouseUnlock();
  98. clearFirstResponder();
  99. }
  100. //------------------------------------------------------------------------------
  101. static bool isModifierKey( U16 keyCode )
  102. {
  103. switch ( keyCode )
  104. {
  105. case KEY_LCONTROL:
  106. case KEY_RCONTROL:
  107. case KEY_LALT:
  108. case KEY_RALT:
  109. case KEY_LSHIFT:
  110. case KEY_RSHIFT:
  111. case KEY_MAC_LOPT:
  112. case KEY_MAC_ROPT:
  113. return( true );
  114. }
  115. return( false );
  116. }
  117. IMPLEMENT_CALLBACK( GuiInputCtrl, onInputEvent, void, (const char* device, const char* action, bool state ),
  118. ( device, action, state),
  119. "@brief Callback that occurs when an input is triggered on this control\n\n"
  120. "@param device The device type triggering the input, such as keyboard, mouse, etc\n"
  121. "@param action The actual event occuring, such as a key or button\n"
  122. "@param state True if the action is being pressed, false if it is being release\n\n");
  123. IMPLEMENT_CALLBACK(GuiInputCtrl, onAxisEvent, void, (const char* device, const char* action, F32 axisValue),
  124. (device, action, axisValue),
  125. "@brief Callback that occurs when an axis event is triggered on this control\n\n"
  126. "@param device The device type triggering the input, such as mouse, joystick, gamepad, etc\n"
  127. "@param action The ActionMap code for the axis\n"
  128. "@param axisValue The current value of the axis\n\n");
  129. //------------------------------------------------------------------------------
  130. bool GuiInputCtrl::onInputEvent( const InputEventInfo &event )
  131. {
  132. if (mIgnoreMouseEvents && event.deviceType == MouseDeviceType)
  133. return false;
  134. char deviceString[32];
  135. if ( event.action == SI_MAKE )
  136. {
  137. if ( event.objType == SI_BUTTON
  138. || event.objType == SI_POV
  139. || event.objType == SI_KEY )
  140. {
  141. if ( !ActionMap::getDeviceName( event.deviceType, event.deviceInst, deviceString ) )
  142. return false;
  143. if ((event.objType == SI_KEY) && isModifierKey(event.objInst))
  144. {
  145. if (!mSendModifierEvents)
  146. return false;
  147. char keyString[32];
  148. if (!ActionMap::getKeyString(event.objInst, keyString))
  149. return false;
  150. onInputEvent_callback(deviceString, keyString, 1);
  151. }
  152. else
  153. {
  154. const char* actionString = ActionMap::buildActionString(&event);
  155. onInputEvent_callback(deviceString, actionString, 1);
  156. }
  157. return true;
  158. }
  159. }
  160. else if ( event.action == SI_BREAK )
  161. {
  162. if ( ( event.objType == SI_KEY ) && isModifierKey( event.objInst ) )
  163. {
  164. char keyString[32];
  165. if ( !ActionMap::getKeyString( event.objInst, keyString ) )
  166. return false;
  167. onInputEvent_callback("keyboard", keyString, 0);
  168. return true;
  169. }
  170. else if (mSendBreakEvents)
  171. {
  172. if (!ActionMap::getDeviceName(event.deviceType, event.deviceInst, deviceString))
  173. return false;
  174. const char* actionString = ActionMap::buildActionString(&event);
  175. onInputEvent_callback(deviceString, actionString, 0);
  176. return true;
  177. }
  178. }
  179. else if (mSendAxisEvents && ((event.objType == SI_AXIS) || (event.objType == SI_INT) || (event.objType == SI_FLOAT)))
  180. {
  181. F32 fValue = event.fValue;
  182. if (event.objType == SI_INT)
  183. fValue = (F32)event.iValue;
  184. if (!ActionMap::getDeviceName(event.deviceType, event.deviceInst, deviceString))
  185. return false;
  186. const char* actionString = ActionMap::buildActionString(&event);
  187. onAxisEvent_callback(deviceString, actionString, fValue);
  188. return (event.deviceType != MouseDeviceType); // Don't consume mouse move events
  189. }
  190. return false;
  191. }