guiInputCtrl.cc 3.5 KB

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