windowInputGenerator.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifndef _WINDOW_INPUTGENERATOR_H_
  23. #define _WINDOW_INPUTGENERATOR_H_
  24. #ifndef _PLATFORMINPUT_H_
  25. #include "platform/platformInput.h"
  26. #endif
  27. #ifndef _MPOINT2_H_
  28. #include "math/mPoint2.h"
  29. #endif
  30. class IProcessInput;
  31. class PlatformWindow;
  32. class WindowInputGenerator
  33. {
  34. protected:
  35. PlatformWindow *mWindow;
  36. IProcessInput *mInputController;
  37. Point2I mLastCursorPos;
  38. bool mClampToWindow;
  39. bool mFocused; ///< We store this off to avoid polling the OS constantly
  40. /// This is the scale factor which relates mouse movement in pixels
  41. /// (one unit of mouse movement is a mickey) to units in the GUI.
  42. F32 mPixelsPerMickey;
  43. /// This tells us if the last key we pressed was used from the global action map.
  44. bool mLastPressWasGlobalActionMap;
  45. // Event Handlers
  46. void handleMouseButton(WindowId did, U32 modifier, U32 action, U16 button);
  47. void handleMouseWheel (WindowId did, U32 modifier, S32 wheelDeltaX, S32 wheelDeltaY);
  48. void handleMouseMove (WindowId did, U32 modifier, S32 x, S32 y, bool isRelative);
  49. void handleKeyboard (WindowId did, U32 modifier, U32 action, U16 key);
  50. void handleCharInput (WindowId did, U32 modifier, U16 key);
  51. void handleAppEvent (WindowId did, S32 event);
  52. void handleInputEvent (U32 deviceInst, F32 fValue, F32 fValue2, F32 fValue3, F32 fValue4, S32 iValue, U16 deviceType, U16 objType, U16 ascii, U16 objInst, U8 action, U8 modifier);
  53. void generateInputEvent( InputEventInfo &inputEvent );
  54. /// Accelerator key map
  55. struct AccKeyMap
  56. {
  57. void *hnd;
  58. String cmd;
  59. U32 keyCode;
  60. U32 modifier;
  61. };
  62. Vector <AccKeyMap> mAcceleratorMap;
  63. public:
  64. WindowInputGenerator( PlatformWindow *window );
  65. virtual ~WindowInputGenerator();
  66. void setInputController( IProcessInput *inputController ) { mInputController = inputController; };
  67. /// Returns true if the given keypress event should be send as a raw keyboard
  68. /// event even if it maps to a character input event.
  69. bool wantAsKeyboardEvent( U32 modifiers, U32 key );
  70. /// Tells us if the last key was used within the global action map.
  71. /// @return true if the key was a global action map key, false otherwise.
  72. /// @note Useful and currently used to tell if we just opened the console
  73. /// by using the console key. Currently this is used to fix a bug in SDL
  74. /// but it is not limited to that use.
  75. bool lastKeyWasGlobalActionMap() const { return mLastPressWasGlobalActionMap; }
  76. void addAcceleratorKey( void *hnd, const String &cmd, U32 keycode, U32 modifier)
  77. {
  78. AccKeyMap acc;
  79. acc.hnd = hnd;
  80. acc.cmd = cmd;
  81. acc.keyCode = keycode;
  82. acc.modifier = modifier;
  83. mAcceleratorMap.push_back(acc);
  84. }
  85. void removeAcceleratorKeys( void *hnd )
  86. {
  87. for( int i = 0; i < mAcceleratorMap.size(); )
  88. {
  89. if( mAcceleratorMap[i].hnd == hnd )
  90. {
  91. mAcceleratorMap.erase( i, 1 );
  92. continue;
  93. }
  94. ++i;
  95. }
  96. }
  97. void removeAcceleratorKey( void *hnd, U32 keycode, U32 modifier )
  98. {
  99. for( int i = 0; i < mAcceleratorMap.size(); ++i )
  100. {
  101. if( mAcceleratorMap[i].hnd == hnd && mAcceleratorMap[i].keyCode == keycode && mAcceleratorMap[i].modifier == modifier )
  102. {
  103. mAcceleratorMap.erase( i, 1 );
  104. return;
  105. }
  106. }
  107. }
  108. };
  109. #endif // _WINDOW_INPUTGENERATOR_H_