Input.h 6.1 KB

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