Input.pkg 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. $#include "Input.h"
  2. /// %Input state for a finger touch.
  3. struct TouchState
  4. {
  5. /// Touch (finger) ID.
  6. int touchID_ @ touchID;
  7. /// Position in screen coordinates.
  8. IntVector2 position_ @ position;
  9. /// Last position in screen coordinates.
  10. IntVector2 lastPosition_ @ lastPosition;
  11. /// Movement since last frame.
  12. IntVector2 delta_ @ delta;
  13. /// Finger pressure.
  14. float pressure_ @ pressure;
  15. };
  16. /// %Input state for a joystick.
  17. struct JoystickState
  18. {
  19. /// Return number of buttons.
  20. unsigned GetNumButtons() const { return buttons_.Size(); }
  21. /// Return number of axes.
  22. unsigned GetNumAxes() const { return axes_.Size(); }
  23. /// Return number of hats.
  24. unsigned GetNumHats() const { return hats_.Size(); }
  25. /// Check if a button is held down.
  26. bool GetButtonDown(unsigned index) const
  27. {
  28. if (index < buttons_.Size())
  29. return buttons_[index];
  30. else
  31. return false;
  32. }
  33. /// Check if a button has been pressed on this frame.
  34. bool GetButtonPress(unsigned index) const
  35. {
  36. if (index < buttons_.Size())
  37. return buttonPress_[index];
  38. else
  39. return false;
  40. }
  41. /// Return axis position.
  42. float GetAxisPosition(unsigned index) const
  43. {
  44. if (index < axes_.Size())
  45. return axes_[index];
  46. else
  47. return 0.0f;
  48. }
  49. /// Return hat position.
  50. int GetHatPosition(unsigned index) const
  51. {
  52. if (index < hats_.Size())
  53. return hats_[index];
  54. else
  55. return HAT_CENTER;
  56. }
  57. };
  58. /// %Input subsystem. Converts operating system window messages to input state and events.
  59. class Input : public Object
  60. {
  61. public:
  62. /// Set whether the operating system mouse cursor is visible. When not visible (default), is kept centered to prevent leaving the window.
  63. void SetMouseVisible(bool enable);
  64. /// Open a joystick. Return true if successful.
  65. bool OpenJoystick(unsigned index);
  66. /// Close a joystick.
  67. void CloseJoystick(unsigned index);
  68. /// Redetect joysticks. Return true if successful.
  69. bool DetectJoysticks();
  70. /// Check if a key is held down.
  71. bool GetKeyDown(int key) const;
  72. /// Check if a key has been pressed on this frame.
  73. bool GetKeyPress(int key) const;
  74. /// Check if a mouse button is held down.
  75. bool GetMouseButtonDown(int button) const;
  76. /// Check if a mouse button has been pressed on this frame.
  77. bool GetMouseButtonPress(int button) const;
  78. /// Check if a qualifier key is held down.
  79. bool GetQualifierDown(int qualifier) const;
  80. /// Check if a qualifier key has been pressed on this frame.
  81. bool GetQualifierPress(int qualifier) const;
  82. /// Return the currently held down qualifiers.
  83. int GetQualifiers() const;
  84. /// Return mouse position within window. Should only be used with a visible mouse cursor.
  85. IntVector2 GetMousePosition() const;
  86. /// Return mouse movement since last frame.
  87. const IntVector2& GetMouseMove() const { return mouseMove_; }
  88. /// Return horizontal mouse movement since last frame.
  89. int GetMouseMoveX() const { return mouseMove_.x_; }
  90. /// Return vertical mouse movement since last frame.
  91. int GetMouseMoveY() const { return mouseMove_.y_; }
  92. /// Return mouse wheel movement since last frame.
  93. int GetMouseMoveWheel() const { return mouseMoveWheel_; }
  94. /// Return number of active finger touches.
  95. unsigned GetNumTouches() const { return touches_.Size(); }
  96. /// Return active finger touch by index.
  97. TouchState* GetTouch(unsigned index) const;
  98. /// Return number of connected joysticks.
  99. unsigned GetNumJoysticks() const { return joysticks_.Size(); }
  100. /// Return joystick name by index.
  101. const String& GetJoystickName(unsigned index) const;
  102. /// Return joystick state by index. Automatically open if not opened yet.
  103. JoystickState* GetJoystick(unsigned index);
  104. /// Return whether fullscreen toggle is enabled.
  105. bool GetToggleFullscreen() const { return toggleFullscreen_; }
  106. /// Return whether the operating system mouse cursor is visible.
  107. bool IsMouseVisible() const { return mouseVisible_; }
  108. /// Return whether application window has input focus.
  109. bool HasFocus() { return inputFocus_; }
  110. /// Return whether application window is minimized.
  111. bool IsMinimized() const;
  112. };