guiInputCtrl.cpp 7.3 KB

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