Input.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifndef INPUT_INPUT_H
  24. #define INPUT_INPUT_H
  25. #include "EventListener.h"
  26. #include "InputEvents.h"
  27. #include "SharedPtr.h"
  28. class Renderer;
  29. static const int MAX_KEYS = 256;
  30. //! Converts operating system window messages to input state and events
  31. class Input : public RefCounted, public EventListener
  32. {
  33. public:
  34. //! Construct with renderer subsystem pointer
  35. Input(Renderer* renderer);
  36. //! Destruct
  37. virtual ~Input();
  38. //! Poll for window messages. Called by Engine each frame
  39. void update();
  40. //! Set whether mouse cursor is confined inside the window. Mouse delta movement is sent only when enabled, which is default.
  41. void setClipCursor(bool enable);
  42. //! Set whether ALT-ENTER fullscreen toggle is enabled
  43. void setToggleFullscreen(bool enable);
  44. //! Set absolute mouse cursor position within the window. Only useful when the cursor is not confined
  45. void setMousePosition(const IntVector2& position);
  46. //! Set absolute mouse cursor position within the window. Only useful when the cursor is not confined
  47. void setMousePosition(int x, int y);
  48. //! Suppress the next char message
  49. void suppressNextChar();
  50. //! Check if a key is held down
  51. bool getKeyDown(int key) const;
  52. //! Check if a key has been pressed on this frame
  53. bool getKeyPress(int key) const;
  54. //! Check if a mouse button is held down
  55. bool getMouseButtonDown(int button) const;
  56. //! Check if a mouse button has been pressed on this frame
  57. bool getMouseButtonPress(int button) const;
  58. //! Check if a qualifier key is held down
  59. bool getQualifierDown(int qualifier) const;
  60. //! Check if a qualifier key has been pressed on this frame
  61. bool getQualifierPress(int qualifier) const;
  62. //! Return the currently held down qualifiers
  63. int getQualifiers() const;
  64. //! Return absolute mouse cursor position within the window. Only useful when the cursor is not confined
  65. IntVector2 getMousePosition() const;
  66. //! Return mouse movement since last frame
  67. const IntVector2& getMouseMove() const { return mMouseMove; }
  68. //! Return horizontal mouse movement since last frame
  69. int getMouseMoveX() const { return mMouseMove.mX; }
  70. //! Return vertical mouse movement since last frame
  71. int getMouseMoveY() const { return mMouseMove.mY; }
  72. //! Return mouse wheel movement since last frame
  73. int getMouseMoveWheel() const { return mMouseMoveWheel; }
  74. //! Return whether mouse cursor is confined inside the window
  75. bool getClipCursor() const { return mClipCursor; }
  76. //! Return whether fullscreen toggle is enabled
  77. bool getToggleFullscreen() const { return mToggleFullscreen; }
  78. //! Return whether application window is active
  79. bool isActive() { return mActive; }
  80. //! Return whether application window is minimized
  81. bool isMinimized() { return mMinimized; }
  82. private:
  83. //! Handle a window message
  84. void handleWindowMessage(StringHash eventType, VariantMap& eventData);
  85. //! Handle screen mode change
  86. void handleScreenMode(StringHash eventType, VariantMap& eventData);
  87. //! Activate the application
  88. void makeActive();
  89. //! Deactivate the application
  90. void makeInactive();
  91. //! Clear input state
  92. void clearState();
  93. //! Handle a mouse button change
  94. void mouseButtonChange(int button, bool newState);
  95. //! Handle a key change
  96. void keyChange(int key, bool newState);
  97. //! Handle mousewheel change
  98. void mouseWheelChange(int delta);
  99. //! Check for mouse move and send event if moved
  100. void checkMouseMove();
  101. //! Internal function to show/hide the operating system mouse cursor
  102. void setCursorVisible(bool enable);
  103. //! Renderer subsystem
  104. SharedPtr<Renderer> mRenderer;
  105. //! Key down state
  106. bool mKeyDown[MAX_KEYS];
  107. //! Key pressed state
  108. bool mKeyPress[MAX_KEYS];
  109. //! Mouse buttons' down state
  110. unsigned mMouseButtonDown;
  111. //! Mouse buttons' pressed state
  112. unsigned mMouseButtonPress;
  113. //! Last mouse position for calculating deltas
  114. IntVector2 mLastMousePosition;
  115. //! Mouse movement since last frame
  116. IntVector2 mMouseMove;
  117. //! Mouse wheel movement since last frame
  118. int mMouseMoveWheel;
  119. //! Mouse cursor confine flag
  120. bool mClipCursor;
  121. //! Mouse cursor show/hide flag
  122. bool mShowCursor;
  123. //! Fullscreen toggle flag
  124. bool mToggleFullscreen;
  125. //! Active flag
  126. bool mActive;
  127. //! Minimized flag
  128. bool mMinimized;
  129. //! Activated on this frame flag
  130. bool mActivated;
  131. //! Next char message suppress flag
  132. bool mSuppressNextChar;
  133. };
  134. #endif // INPUT_INPUT_H