joystick_controller.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. Minetest
  3. Copyright (C) 2016 est31, <MTest31@outlook.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irrlichttypes_extrabloated.h"
  18. #include "keys.h"
  19. #include <bitset>
  20. #include <vector>
  21. enum JoystickAxis {
  22. JA_SIDEWARD_MOVE,
  23. JA_FORWARD_MOVE,
  24. JA_FRUSTUM_HORIZONTAL,
  25. JA_FRUSTUM_VERTICAL,
  26. // To know the count of enum values
  27. JA_COUNT,
  28. };
  29. struct JoystickAxisLayout {
  30. u16 axis_id;
  31. // -1 if to invert, 1 if to keep it.
  32. int invert;
  33. };
  34. struct JoystickCombination {
  35. virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const=0;
  36. GameKeyType key;
  37. };
  38. struct JoystickButtonCmb : public JoystickCombination {
  39. JoystickButtonCmb() = default;
  40. JoystickButtonCmb(GameKeyType key, u32 filter_mask, u32 compare_mask) :
  41. filter_mask(filter_mask),
  42. compare_mask(compare_mask)
  43. {
  44. this->key = key;
  45. }
  46. virtual ~JoystickButtonCmb() = default;
  47. virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const;
  48. u32 filter_mask;
  49. u32 compare_mask;
  50. };
  51. struct JoystickAxisCmb : public JoystickCombination {
  52. JoystickAxisCmb() = default;
  53. JoystickAxisCmb(GameKeyType key, u16 axis_to_compare, int direction, s16 thresh) :
  54. axis_to_compare(axis_to_compare),
  55. direction(direction),
  56. thresh(thresh)
  57. {
  58. this->key = key;
  59. }
  60. virtual ~JoystickAxisCmb() = default;
  61. bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const override;
  62. u16 axis_to_compare;
  63. // if -1, thresh must be smaller than the axis value in order to trigger
  64. // if 1, thresh must be bigger than the axis value in order to trigger
  65. int direction;
  66. s16 thresh;
  67. };
  68. struct JoystickLayout {
  69. std::vector<JoystickButtonCmb> button_keys;
  70. std::vector<JoystickAxisCmb> axis_keys;
  71. JoystickAxisLayout axes[JA_COUNT];
  72. s16 axes_deadzone;
  73. };
  74. class JoystickController {
  75. public:
  76. JoystickController();
  77. void onJoystickConnect(const std::vector<irr::SJoystickInfo> &joystick_infos);
  78. bool handleEvent(const irr::SEvent::SJoystickEvent &ev);
  79. void clear();
  80. bool wasKeyDown(GameKeyType b)
  81. {
  82. bool r = m_past_keys_pressed[b];
  83. m_past_keys_pressed[b] = false;
  84. return r;
  85. }
  86. bool wasKeyReleased(GameKeyType b)
  87. {
  88. return m_keys_released[b];
  89. }
  90. void clearWasKeyReleased(GameKeyType b)
  91. {
  92. m_keys_released[b] = false;
  93. }
  94. bool wasKeyPressed(GameKeyType b)
  95. {
  96. return m_keys_pressed[b];
  97. }
  98. void clearWasKeyPressed(GameKeyType b)
  99. {
  100. m_keys_pressed[b] = false;
  101. }
  102. bool isKeyDown(GameKeyType b)
  103. {
  104. return m_keys_down[b];
  105. }
  106. s16 getAxis(JoystickAxis axis)
  107. {
  108. return m_axes_vals[axis];
  109. }
  110. float getAxisWithoutDead(JoystickAxis axis);
  111. float getMovementDirection();
  112. float getMovementSpeed();
  113. f32 doubling_dtime;
  114. private:
  115. void setLayoutFromControllerName(const std::string &name);
  116. JoystickLayout m_layout;
  117. s16 m_axes_vals[JA_COUNT];
  118. u8 m_joystick_id = 0;
  119. std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_keys_down;
  120. std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_keys_pressed;
  121. f32 m_internal_time;
  122. f32 m_past_pressed_time[KeyType::INTERNAL_ENUM_COUNT];
  123. std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_past_keys_pressed;
  124. std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_keys_released;
  125. };