analog_from_button.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <atomic>
  5. #include <chrono>
  6. #include <cmath>
  7. #include <thread>
  8. #include "common/math_util.h"
  9. #include "common/settings.h"
  10. #include "input_common/analog_from_button.h"
  11. namespace InputCommon {
  12. class Analog final : public Input::AnalogDevice {
  13. public:
  14. using Button = std::unique_ptr<Input::ButtonDevice>;
  15. Analog(Button up_, Button down_, Button left_, Button right_, Button modifier_,
  16. float modifier_scale_, float modifier_angle_)
  17. : up(std::move(up_)), down(std::move(down_)), left(std::move(left_)),
  18. right(std::move(right_)), modifier(std::move(modifier_)), modifier_scale(modifier_scale_),
  19. modifier_angle(modifier_angle_) {
  20. Input::InputCallback<bool> callbacks{
  21. [this]([[maybe_unused]] bool status) { UpdateStatus(); }};
  22. up->SetCallback(callbacks);
  23. down->SetCallback(callbacks);
  24. left->SetCallback(callbacks);
  25. right->SetCallback(callbacks);
  26. modifier->SetCallback(callbacks);
  27. }
  28. bool IsAngleGreater(float old_angle, float new_angle) const {
  29. constexpr float TAU = Common::PI * 2.0f;
  30. // Use wider angle to ease the transition.
  31. constexpr float aperture = TAU * 0.15f;
  32. const float top_limit = new_angle + aperture;
  33. return (old_angle > new_angle && old_angle <= top_limit) ||
  34. (old_angle + TAU > new_angle && old_angle + TAU <= top_limit);
  35. }
  36. bool IsAngleSmaller(float old_angle, float new_angle) const {
  37. constexpr float TAU = Common::PI * 2.0f;
  38. // Use wider angle to ease the transition.
  39. constexpr float aperture = TAU * 0.15f;
  40. const float bottom_limit = new_angle - aperture;
  41. return (old_angle >= bottom_limit && old_angle < new_angle) ||
  42. (old_angle - TAU >= bottom_limit && old_angle - TAU < new_angle);
  43. }
  44. float GetAngle(std::chrono::time_point<std::chrono::steady_clock> now) const {
  45. constexpr float TAU = Common::PI * 2.0f;
  46. float new_angle = angle;
  47. auto time_difference = static_cast<float>(
  48. std::chrono::duration_cast<std::chrono::microseconds>(now - last_update).count());
  49. time_difference /= 1000.0f * 1000.0f;
  50. if (time_difference > 0.5f) {
  51. time_difference = 0.5f;
  52. }
  53. if (IsAngleGreater(new_angle, goal_angle)) {
  54. new_angle -= modifier_angle * time_difference;
  55. if (new_angle < 0) {
  56. new_angle += TAU;
  57. }
  58. if (!IsAngleGreater(new_angle, goal_angle)) {
  59. return goal_angle;
  60. }
  61. } else if (IsAngleSmaller(new_angle, goal_angle)) {
  62. new_angle += modifier_angle * time_difference;
  63. if (new_angle >= TAU) {
  64. new_angle -= TAU;
  65. }
  66. if (!IsAngleSmaller(new_angle, goal_angle)) {
  67. return goal_angle;
  68. }
  69. } else {
  70. return goal_angle;
  71. }
  72. return new_angle;
  73. }
  74. void SetGoalAngle(bool r, bool l, bool u, bool d) {
  75. // Move to the right
  76. if (r && !u && !d) {
  77. goal_angle = 0.0f;
  78. }
  79. // Move to the upper right
  80. if (r && u && !d) {
  81. goal_angle = Common::PI * 0.25f;
  82. }
  83. // Move up
  84. if (u && !l && !r) {
  85. goal_angle = Common::PI * 0.5f;
  86. }
  87. // Move to the upper left
  88. if (l && u && !d) {
  89. goal_angle = Common::PI * 0.75f;
  90. }
  91. // Move to the left
  92. if (l && !u && !d) {
  93. goal_angle = Common::PI;
  94. }
  95. // Move to the bottom left
  96. if (l && !u && d) {
  97. goal_angle = Common::PI * 1.25f;
  98. }
  99. // Move down
  100. if (d && !l && !r) {
  101. goal_angle = Common::PI * 1.5f;
  102. }
  103. // Move to the bottom right
  104. if (r && !u && d) {
  105. goal_angle = Common::PI * 1.75f;
  106. }
  107. }
  108. void UpdateStatus() {
  109. const float coef = modifier->GetStatus() ? modifier_scale : 1.0f;
  110. bool r = right->GetStatus();
  111. bool l = left->GetStatus();
  112. bool u = up->GetStatus();
  113. bool d = down->GetStatus();
  114. // Eliminate contradictory movements
  115. if (r && l) {
  116. r = false;
  117. l = false;
  118. }
  119. if (u && d) {
  120. u = false;
  121. d = false;
  122. }
  123. // Move if a key is pressed
  124. if (r || l || u || d) {
  125. amplitude = coef;
  126. } else {
  127. amplitude = 0;
  128. }
  129. const auto now = std::chrono::steady_clock::now();
  130. const auto time_difference = static_cast<u64>(
  131. std::chrono::duration_cast<std::chrono::milliseconds>(now - last_update).count());
  132. if (time_difference < 10) {
  133. // Disable analog mode if inputs are too fast
  134. SetGoalAngle(r, l, u, d);
  135. angle = goal_angle;
  136. } else {
  137. angle = GetAngle(now);
  138. SetGoalAngle(r, l, u, d);
  139. }
  140. last_update = now;
  141. }
  142. std::tuple<float, float> GetStatus() const override {
  143. if (Settings::values.emulate_analog_keyboard) {
  144. const auto now = std::chrono::steady_clock::now();
  145. float angle_ = GetAngle(now);
  146. return std::make_tuple(std::cos(angle_) * amplitude, std::sin(angle_) * amplitude);
  147. }
  148. constexpr float SQRT_HALF = 0.707106781f;
  149. int x = 0, y = 0;
  150. if (right->GetStatus()) {
  151. ++x;
  152. }
  153. if (left->GetStatus()) {
  154. --x;
  155. }
  156. if (up->GetStatus()) {
  157. ++y;
  158. }
  159. if (down->GetStatus()) {
  160. --y;
  161. }
  162. const float coef = modifier->GetStatus() ? modifier_scale : 1.0f;
  163. return std::make_tuple(static_cast<float>(x) * coef * (y == 0 ? 1.0f : SQRT_HALF),
  164. static_cast<float>(y) * coef * (x == 0 ? 1.0f : SQRT_HALF));
  165. }
  166. Input::AnalogProperties GetAnalogProperties() const override {
  167. return {modifier_scale, 1.0f, 0.5f};
  168. }
  169. bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override {
  170. switch (direction) {
  171. case Input::AnalogDirection::RIGHT:
  172. return right->GetStatus();
  173. case Input::AnalogDirection::LEFT:
  174. return left->GetStatus();
  175. case Input::AnalogDirection::UP:
  176. return up->GetStatus();
  177. case Input::AnalogDirection::DOWN:
  178. return down->GetStatus();
  179. }
  180. return false;
  181. }
  182. private:
  183. Button up;
  184. Button down;
  185. Button left;
  186. Button right;
  187. Button modifier;
  188. float modifier_scale;
  189. float modifier_angle;
  190. float angle{};
  191. float goal_angle{};
  192. float amplitude{};
  193. std::chrono::time_point<std::chrono::steady_clock> last_update;
  194. };
  195. std::unique_ptr<Input::AnalogDevice> AnalogFromButton::Create(const Common::ParamPackage& params) {
  196. const std::string null_engine = Common::ParamPackage{{"engine", "null"}}.Serialize();
  197. auto up = Input::CreateDevice<Input::ButtonDevice>(params.Get("up", null_engine));
  198. auto down = Input::CreateDevice<Input::ButtonDevice>(params.Get("down", null_engine));
  199. auto left = Input::CreateDevice<Input::ButtonDevice>(params.Get("left", null_engine));
  200. auto right = Input::CreateDevice<Input::ButtonDevice>(params.Get("right", null_engine));
  201. auto modifier = Input::CreateDevice<Input::ButtonDevice>(params.Get("modifier", null_engine));
  202. auto modifier_scale = params.Get("modifier_scale", 0.5f);
  203. auto modifier_angle = params.Get("modifier_angle", 5.5f);
  204. return std::make_unique<Analog>(std::move(up), std::move(down), std::move(left),
  205. std::move(right), std::move(modifier), modifier_scale,
  206. modifier_angle);
  207. }
  208. } // namespace InputCommon