guiInputCtrl.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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/guiInputCtrl.h"
  23. #include "input/actionMap.h"
  24. IMPLEMENT_CONOBJECT(GuiInputCtrl);
  25. GuiInputCtrl::GuiInputCtrl()
  26. {
  27. mRendersChildren = false;
  28. mIsContainer = false;
  29. }
  30. //------------------------------------------------------------------------------
  31. bool GuiInputCtrl::onWake()
  32. {
  33. // Set the default profile on start-up:
  34. if ( !mProfile )
  35. {
  36. SimObject *obj = Sim::findObject("GuiInputCtrlProfile");
  37. if ( obj )
  38. mProfile = dynamic_cast<GuiControlProfile*>( obj );
  39. }
  40. if ( !Parent::onWake() )
  41. return( false );
  42. mouseLock();
  43. setFirstResponder();
  44. return( true );
  45. }
  46. //------------------------------------------------------------------------------
  47. void GuiInputCtrl::onSleep()
  48. {
  49. Parent::onSleep();
  50. mouseUnlock();
  51. clearFirstResponder();
  52. }
  53. //------------------------------------------------------------------------------
  54. static bool isModifierKey( U16 keyCode )
  55. {
  56. switch ( keyCode )
  57. {
  58. case KEY_LCONTROL:
  59. case KEY_RCONTROL:
  60. case KEY_LALT:
  61. case KEY_RALT:
  62. case KEY_LSHIFT:
  63. case KEY_RSHIFT:
  64. return( true );
  65. }
  66. return( false );
  67. }
  68. //------------------------------------------------------------------------------
  69. bool GuiInputCtrl::onInputEvent( const InputEvent &event )
  70. {
  71. if ( event.action == SI_MAKE )
  72. {
  73. if ( event.objType == SI_BUTTON
  74. || event.objType == SI_POV
  75. || ( ( event.objType == SI_KEY ) && !isModifierKey( event.objInst ) ) )
  76. {
  77. char deviceString[32];
  78. if ( !ActionMap::getDeviceName( event.deviceType, event.deviceInst, deviceString ) )
  79. return( false );
  80. const char* actionString = ActionMap::buildActionString( &event );
  81. Con::executef( this, 4, "onInputEvent", deviceString, actionString, "1" );
  82. return( true );
  83. }
  84. }
  85. else if ( event.action == SI_BREAK )
  86. {
  87. if ( ( event.objType == SI_KEY ) && isModifierKey( event.objInst ) )
  88. {
  89. char keyString[32];
  90. if ( !ActionMap::getKeyString( event.objInst, keyString ) )
  91. return( false );
  92. Con::executef( this, 4, "onInputEvent", "keyboard", keyString, "0" );
  93. return( true );
  94. }
  95. }
  96. return( false );
  97. }