Input.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #pragma once
  24. #include "HashSet.h"
  25. #include "InputEvents.h"
  26. #include "Object.h"
  27. class Graphics;
  28. /// %Input subsystem. Converts operating system window messages to input state and events.
  29. class Input : public Object
  30. {
  31. #ifdef USE_OPENGL
  32. friend void KeyCallback(GLFWwindow, int, int);
  33. friend void CharCallback(GLFWwindow, int);
  34. friend void MouseButtonCallback(GLFWwindow, int, int);
  35. friend void MouseScrollCallback(GLFWwindow, int, int);
  36. #endif
  37. OBJECT(Input);
  38. public:
  39. /// Construct.
  40. Input(Context* context);
  41. /// Destruct.
  42. virtual ~Input();
  43. /// Poll for window messages. Called by HandleBeginFrame().
  44. void Update();
  45. /// %Set whether ALT-ENTER fullscreen toggle is enabled.
  46. void SetToggleFullscreen(bool enable);
  47. /// Suppress the next char message.
  48. void SuppressNextChar();
  49. /// Check if a key is held down.
  50. bool GetKeyDown(int key) const;
  51. /// Check if a key has been pressed on this frame.
  52. bool GetKeyPress(int key) const;
  53. /// Check if a mouse button is held down.
  54. bool GetMouseButtonDown(int button) const;
  55. /// Check if a mouse button has been pressed on this frame.
  56. bool GetMouseButtonPress(int button) const;
  57. /// Check if a qualifier key is held down.
  58. bool GetQualifierDown(int qualifier) const;
  59. /// Check if a qualifier key has been pressed on this frame.
  60. bool GetQualifierPress(int qualifier) const;
  61. /// Return the currently held down qualifiers.
  62. int GetQualifiers() const;
  63. /// Return mouse movement since last frame.
  64. const IntVector2& GetMouseMove() const { return mouseMove_; }
  65. /// Return horizontal mouse movement since last frame.
  66. int GetMouseMoveX() const { return mouseMove_.x_; }
  67. /// Return vertical mouse movement since last frame.
  68. int GetMouseMoveY() const { return mouseMove_.y_; }
  69. /// Return mouse wheel movement since last frame.
  70. int GetMouseMoveWheel() const { return mouseMoveWheel_; }
  71. /// Return whether fullscreen toggle is enabled.
  72. bool GetToggleFullscreen() const { return toggleFullscreen_; }
  73. /// Return whether application window is active.
  74. bool IsActive() { return active_; }
  75. /// Return whether application window is minimized.
  76. bool IsMinimized() { return minimized_; }
  77. private:
  78. /// Initialize when screen mode initially set.
  79. void Initialize();
  80. /// Activate the application.
  81. void MakeActive();
  82. /// Deactivate the application.
  83. void MakeInactive();
  84. /// Clear input state.
  85. void ResetState();
  86. /// Handle a mouse button change.
  87. void SetMouseButton(int button, bool newState);
  88. /// Handle a key change.
  89. void SetKey(int key, bool newState);
  90. /// Handle mousewheel change.
  91. void SetMouseWheel(int delta);
  92. #ifndef USE_OPENGL
  93. /// Internal function to clip the mouse cursor to the window.
  94. void SetClipCursor(bool enable);
  95. /// Internal function to set the mouse cursor position.
  96. void SetCursorPosition(const IntVector2& position);
  97. /// Internal function to show/hide the mouse cursor.
  98. void SetCursorVisible(bool enable);
  99. /// Handle window message event.
  100. void HandleWindowMessage(StringHash eventType, VariantMap& eventData);
  101. #endif
  102. /// Internal function to get the mouse cursor position.
  103. IntVector2 GetCursorPosition() const;
  104. /// Handle screen mode event.
  105. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  106. /// Handle frame start event.
  107. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  108. /// Graphics subsystem.
  109. WeakPtr<Graphics> graphics_;
  110. /// Key down state.
  111. HashSet<int> keyDown_;
  112. /// Key pressed state.
  113. HashSet<int> keyPress_;
  114. /// Mouse buttons' down state.
  115. unsigned mouseButtonDown_;
  116. /// Mouse buttons' pressed state.
  117. unsigned mouseButtonPress_;
  118. /// Last mouse position for calculating movement.
  119. IntVector2 lastCursorPosition_;
  120. /// Mouse movement since last frame.
  121. IntVector2 mouseMove_;
  122. /// Mouse wheel movement since last frame.
  123. int mouseMoveWheel_;
  124. /// Mouse cursor show/hide flag.
  125. bool showCursor_;
  126. /// Fullscreen toggle flag.
  127. bool toggleFullscreen_;
  128. /// Active flag.
  129. bool active_;
  130. /// Minimized flag.
  131. bool minimized_;
  132. /// Activated on this frame flag.
  133. bool activated_;
  134. /// Next char message suppress flag.
  135. bool suppressNextChar_;
  136. /// Initialized flag.
  137. bool initialized_;
  138. };