joystick_controller.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #ifndef JOYSTICK_HEADER
  17. #define JOYSTICK_HEADER
  18. #include "irrlichttypes_extrabloated.h"
  19. #include "keys.h"
  20. #include <bitset>
  21. #include <vector>
  22. enum JoystickAxis {
  23. JA_SIDEWARD_MOVE,
  24. JA_FORWARD_MOVE,
  25. JA_FRUSTUM_HORIZONTAL,
  26. JA_FRUSTUM_VERTICAL,
  27. // To know the count of enum values
  28. JA_COUNT,
  29. };
  30. struct JoystickAxisLayout {
  31. u16 axis_id;
  32. // -1 if to invert, 1 if to keep it.
  33. int invert;
  34. };
  35. struct JoystickCombination {
  36. virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const=0;
  37. GameKeyType key;
  38. };
  39. struct JoystickButtonCmb : public JoystickCombination {
  40. JoystickButtonCmb() {}
  41. JoystickButtonCmb(GameKeyType key, u32 filter_mask, u32 compare_mask) :
  42. filter_mask(filter_mask),
  43. compare_mask(compare_mask)
  44. {
  45. this->key = key;
  46. }
  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() {}
  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 bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const;
  61. u16 axis_to_compare;
  62. // if -1, thresh must be smaller than the axis value in order to trigger
  63. // if 1, thresh must be bigger than the axis value in order to trigger
  64. int direction;
  65. s16 thresh;
  66. };
  67. struct JoystickLayout {
  68. std::vector<JoystickButtonCmb> button_keys;
  69. std::vector<JoystickAxisCmb> axis_keys;
  70. JoystickAxisLayout axes[JA_COUNT];
  71. s16 axes_dead_border;
  72. };
  73. class JoystickController {
  74. public:
  75. JoystickController();
  76. bool handleEvent(const irr::SEvent::SJoystickEvent &ev);
  77. void clear();
  78. bool wasKeyDown(GameKeyType b)
  79. {
  80. bool r = m_past_pressed_keys[b];
  81. m_past_pressed_keys[b] = false;
  82. return r;
  83. }
  84. bool getWasKeyDown(GameKeyType b)
  85. {
  86. return m_past_pressed_keys[b];
  87. }
  88. void clearWasKeyDown(GameKeyType b)
  89. {
  90. m_past_pressed_keys[b] = false;
  91. }
  92. bool wasKeyReleased(GameKeyType b)
  93. {
  94. bool r = m_past_released_keys[b];
  95. m_past_released_keys[b] = false;
  96. return r;
  97. }
  98. bool getWasKeyReleased(GameKeyType b)
  99. {
  100. return m_past_pressed_keys[b];
  101. }
  102. void clearWasKeyReleased(GameKeyType b)
  103. {
  104. m_past_pressed_keys[b] = false;
  105. }
  106. bool isKeyDown(GameKeyType b)
  107. {
  108. return m_pressed_keys[b];
  109. }
  110. s16 getAxis(JoystickAxis axis)
  111. {
  112. return m_axes_vals[axis];
  113. }
  114. s16 getAxisWithoutDead(JoystickAxis axis);
  115. f32 doubling_dtime;
  116. private:
  117. const JoystickLayout *m_layout;
  118. s16 m_axes_vals[JA_COUNT];
  119. std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_pressed_keys;
  120. f32 m_internal_time;
  121. f32 m_past_pressed_time[KeyType::INTERNAL_ENUM_COUNT];
  122. std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_past_pressed_keys;
  123. std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_past_released_keys;
  124. };
  125. #endif