guiInputCtrl.cc 3.5 KB

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