Input.h 5.5 KB

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