Input.h 5.7 KB

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