Input.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef INPUT_INPUT_H
  24. #define INPUT_INPUT_H
  25. #include "EventListener.h"
  26. #include "InputEvents.h"
  27. #include "SharedPtr.h"
  28. class Renderer;
  29. static const int MAX_KEYS = 256;
  30. //! Converts operating system window messages to input state and events
  31. class Input : public RefCounted, public EventListener
  32. {
  33. public:
  34. //! Construct with renderer subsystem pointer
  35. Input(Renderer* renderer);
  36. //! Destruct
  37. virtual ~Input();
  38. //! Poll for window messages through the Renderer
  39. void update();
  40. //! Set whether ALT-ENTER fullscreen toggle is enabled
  41. void setToggleFullscreen(bool enable);
  42. //! Check if a key is held down
  43. bool getKeyDown(int key) const;
  44. //! Check if a key has been pressed on this frame
  45. bool getKeyPress(int key) const;
  46. //! Check if a mouse button is held down
  47. bool getMouseButtonDown(int button) const;
  48. //! Check if a mouse button has been pressed on this frame
  49. bool getMouseButtonPress(int button) const;
  50. //! Return horizontal mouse movement since last frame
  51. int getMouseMoveX() const;
  52. //! Return vertical mouse movement since last frame
  53. int getMouseMoveY() const;
  54. //! Return whether fullscreen toggle is enabled
  55. bool getToggleFullscreen() const { return mToggleFullscreen; }
  56. //! Return whether application window is active
  57. bool isActive() { return mActive; }
  58. //! Return whether application window is minimized
  59. bool isMinimized() { return mMinimized; }
  60. private:
  61. //! Handle a window message event
  62. void handleWindowMessage(StringHash eventType, VariantMap& eventData);
  63. //! Activate the application
  64. void makeActive();
  65. //! Deactivate the application
  66. void makeInactive();
  67. //! Clear input state
  68. void clearState();
  69. //! Handle a mouse button change
  70. void mouseButtonChange(int button, bool newState);
  71. //! Handle a key change
  72. void keyChange(int key, bool newState);
  73. //! Renderer subsystem
  74. SharedPtr<Renderer> mRenderer;
  75. //! Key down state
  76. bool mKeyDown[MAX_KEYS];
  77. //! Key pressed state
  78. bool mKeyPress[MAX_KEYS];
  79. //! Mouse buttons' down state
  80. unsigned mMouseButtonDown;
  81. //! Mouse buttons' pressed state
  82. unsigned mMouseButtonPress;
  83. //! Horizontal mouse movement since last frame
  84. int mMouseMoveX;
  85. //! Vertical mouse movement since last frame
  86. int mMouseMoveY;
  87. //! Fullscreen toggle flag
  88. bool mToggleFullscreen;
  89. //! Active flag
  90. bool mActive;
  91. //! Minimized flag
  92. bool mMinimized;
  93. //! Activated on this frame flag
  94. bool mActivated;
  95. };
  96. //! Return Input instance
  97. Input* getInput();
  98. #endif // INPUT_INPUT_H