guiInputCtrl.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. void GuiInputCtrl::initPersistFields()
  53. {
  54. Parent::initPersistFields();
  55. }
  56. //------------------------------------------------------------------------------
  57. bool GuiInputCtrl::onWake()
  58. {
  59. // Set the default profile on start-up:
  60. if( !mProfile )
  61. {
  62. GuiControlProfile* profile;
  63. Sim::findObject( "GuiInputCtrlProfile", profile);
  64. if( profile )
  65. setControlProfile( profile );
  66. }
  67. if ( !Parent::onWake() )
  68. return( false );
  69. if( !smDesignTime )
  70. mouseLock();
  71. setFirstResponder();
  72. return( true );
  73. }
  74. //------------------------------------------------------------------------------
  75. void GuiInputCtrl::onSleep()
  76. {
  77. Parent::onSleep();
  78. mouseUnlock();
  79. clearFirstResponder();
  80. }
  81. //------------------------------------------------------------------------------
  82. static bool isModifierKey( U16 keyCode )
  83. {
  84. switch ( keyCode )
  85. {
  86. case KEY_LCONTROL:
  87. case KEY_RCONTROL:
  88. case KEY_LALT:
  89. case KEY_RALT:
  90. case KEY_LSHIFT:
  91. case KEY_RSHIFT:
  92. return( true );
  93. }
  94. return( false );
  95. }
  96. IMPLEMENT_CALLBACK( GuiInputCtrl, onInputEvent, void, (const char* device, const char* action, bool state ),
  97. ( device, action, state),
  98. "@brief Callback that occurs when an input is triggered on this control\n\n"
  99. "@param device The device type triggering the input, such as keyboard, mouse, etc\n"
  100. "@param action The actual event occuring, such as a key or button\n"
  101. "@param state True if the action is being pressed, false if it is being release\n\n"
  102. );
  103. //------------------------------------------------------------------------------
  104. bool GuiInputCtrl::onInputEvent( const InputEventInfo &event )
  105. {
  106. // TODO - add POV support...
  107. if ( event.action == SI_MAKE )
  108. {
  109. if ( event.objType == SI_BUTTON
  110. || event.objType == SI_POV
  111. || ( ( event.objType == SI_KEY ) && !isModifierKey( event.objInst ) ) )
  112. {
  113. char deviceString[32];
  114. if ( !ActionMap::getDeviceName( event.deviceType, event.deviceInst, deviceString ) )
  115. return( false );
  116. const char* actionString = ActionMap::buildActionString( &event );
  117. //Con::executef( this, "onInputEvent", deviceString, actionString, "1" );
  118. onInputEvent_callback(deviceString, actionString, 1);
  119. return( true );
  120. }
  121. }
  122. else if ( event.action == SI_BREAK )
  123. {
  124. if ( ( event.objType == SI_KEY ) && isModifierKey( event.objInst ) )
  125. {
  126. char keyString[32];
  127. if ( !ActionMap::getKeyString( event.objInst, keyString ) )
  128. return( false );
  129. //Con::executef( this, "onInputEvent", "keyboard", keyString, "0" );
  130. onInputEvent_callback("keyboard", keyString, 0);
  131. return( true );
  132. }
  133. }
  134. return( false );
  135. }