Input.h 5.9 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 "InputEvents.h"
  25. #include "Object.h"
  26. class Graphics;
  27. static const int MAX_KEYS = 256;
  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 mouse cursor is confined inside the window. Mouse delta movement is sent only when enabled, which is default.
  40. void SetClipCursor(bool enable);
  41. /// Set whether ALT-ENTER fullscreen Toggle is enabled
  42. void SetToggleFullscreen(bool enable);
  43. /// Set absolute mouse cursor position within the window. Only useful when the cursor is not confined
  44. void SetMousePosition(const IntVector2& position);
  45. /// Set absolute mouse cursor position within the window. Only useful when the cursor is not confined
  46. void SetMousePosition(int x, int y);
  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 absolute mouse cursor position within the window. Only useful when the cursor is not confined
  64. IntVector2 GetMousePosition() const;
  65. /// Return mouse movement since last frame
  66. const IntVector2& GetMouseMove() const { return mouseMove_; }
  67. /// Return horizontal mouse movement since last frame
  68. int GetMouseMoveX() const { return mouseMove_.x_; }
  69. /// Return vertical mouse movement since last frame
  70. int GetMouseMoveY() const { return mouseMove_.y_; }
  71. /// Return mouse wheel movement since last frame
  72. int GetMouseMoveWheel() const { return mouseMoveWheel_; }
  73. /// Return whether mouse cursor is confined inside the window
  74. bool GetClipCursor() const { return clipCursor_; }
  75. /// Return whether fullscreen Toggle is enabled
  76. bool GetToggleFullscreen() const { return toggleFullscreen_; }
  77. /// Return whether application window is active
  78. bool IsActive() { return active_; }
  79. /// Return whether application window is minimized
  80. bool IsMinimized() { return minimized_; }
  81. private:
  82. /// Initialize when screen mode initially set
  83. void Initialize();
  84. /// Activate the application
  85. void MakeActive();
  86. /// Deactivate the application
  87. void MakeInactive();
  88. /// Clear input state
  89. void clearState();
  90. /// Handle a mouse button change
  91. void SetMouseButton(int button, bool newState);
  92. /// Handle a key change
  93. void SetKey(int key, bool newState);
  94. /// Handle mousewheel change
  95. void SetMouseWheel(int delta);
  96. /// Check for mouse move and send event if moved
  97. void CheckMouseMove();
  98. /// Internal function to show/hide the operating system mouse cursor
  99. void SetCursorVisible(bool enable);
  100. /// Handle window message event
  101. void HandleWindowMessage(StringHash eventType, VariantMap& eventData);
  102. /// Handle screen mode event
  103. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  104. /// Handle frame start event
  105. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  106. /// Graphics
  107. WeakPtr<Graphics> graphics_;
  108. /// Key down state
  109. bool keyDown_[MAX_KEYS];
  110. /// Key pressed state
  111. bool keyPress_[MAX_KEYS];
  112. /// Mouse buttons' down state
  113. unsigned mouseButtonDown_;
  114. /// Mouse buttons' pressed state
  115. unsigned mouseButtonPress_;
  116. /// Last mouse position for calculating deltas
  117. IntVector2 lastMousePosition_;
  118. /// Mouse movement since last frame
  119. IntVector2 mouseMove_;
  120. /// Mouse wheel movement since last frame
  121. int mouseMoveWheel_;
  122. /// Mouse cursor confine flag
  123. bool clipCursor_;
  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. };