💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
7.3 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "gamepad.hpp"
  20. #include "event/input-events.hpp"
  21. #include "event/event-dispatcher.hpp"
  22. #include "math/map.hpp"
  23. #include <algorithm>
  24. #include <cmath>
  25. namespace input {
  26. gamepad::gamepad():
  27. connected(true),
  28. left_deadzone_cross(true),
  29. right_deadzone_cross(true),
  30. left_deadzone_roundness(0.0f),
  31. right_deadzone_roundness(0.0f)
  32. {
  33. for (int i = 0; i < 6; ++i)
  34. {
  35. axis_values[i] = 0.0f;
  36. axis_activation_min[i] = 0.0f;
  37. axis_activation_max[i] = 1.0f;
  38. axis_response_curves[i] = gamepad_response_curve::linear;
  39. }
  40. }
  41. void gamepad::set_activation_threshold(gamepad_axis axis, float min, float max)
  42. {
  43. axis_activation_min[static_cast<int>(axis)] = min;
  44. axis_activation_max[static_cast<int>(axis)] = max;
  45. }
  46. void gamepad::set_response_curve(gamepad_axis axis, gamepad_response_curve curve)
  47. {
  48. axis_response_curves[static_cast<int>(axis)] = curve;
  49. }
  50. void gamepad::set_left_deadzone_cross(bool cross)
  51. {
  52. left_deadzone_cross = cross;
  53. }
  54. void gamepad::set_right_deadzone_cross(bool cross)
  55. {
  56. right_deadzone_cross = cross;
  57. }
  58. void gamepad::set_left_deadzone_roundness(float roundness)
  59. {
  60. left_deadzone_roundness = roundness;
  61. }
  62. void gamepad::set_right_deadzone_roundness(float roundness)
  63. {
  64. right_deadzone_roundness = roundness;
  65. }
  66. void gamepad::press(gamepad_button button)
  67. {
  68. if (!device::event_dispatcher)
  69. {
  70. return;
  71. }
  72. gamepad_button_pressed_event event;
  73. event.controller = this;
  74. event.button = button;
  75. device::event_dispatcher->queue(event);
  76. }
  77. void gamepad::release(gamepad_button button)
  78. {
  79. if (!device::event_dispatcher)
  80. {
  81. return;
  82. }
  83. gamepad_button_released_event event;
  84. event.controller = this;
  85. event.button = button;
  86. device::event_dispatcher->queue(event);
  87. }
  88. void gamepad::move(gamepad_axis axis, float value)
  89. {
  90. // Update axis value
  91. axis_values[static_cast<int>(axis)] = value;
  92. if (!device::event_dispatcher)
  93. {
  94. return;
  95. }
  96. switch (axis)
  97. {
  98. case gamepad_axis::left_x:
  99. case gamepad_axis::left_y:
  100. if (left_deadzone_cross)
  101. handle_axial_motion(axis);
  102. else
  103. handle_biaxial_motion(gamepad_axis::left_x, gamepad_axis::left_y);
  104. break;
  105. case gamepad_axis::right_x:
  106. case gamepad_axis::right_y:
  107. if (right_deadzone_cross)
  108. handle_axial_motion(axis);
  109. else
  110. handle_biaxial_motion(gamepad_axis::right_x, gamepad_axis::right_y);
  111. break;
  112. default:
  113. handle_axial_motion(axis);
  114. break;
  115. }
  116. }
  117. void gamepad::handle_axial_motion(gamepad_axis axis)
  118. {
  119. // Get axis parameters
  120. const int axis_index = static_cast<int>(axis);
  121. const float activation_min = axis_activation_min[axis_index];
  122. const float activation_max = axis_activation_max[axis_index];
  123. const float axis_value = axis_values[axis_index];
  124. const gamepad_response_curve response_curve = axis_response_curves[axis_index];
  125. // Build event
  126. gamepad_axis_moved_event event;
  127. event.controller = this;
  128. event.axis = axis;
  129. if (std::abs(axis_value) > activation_min)
  130. {
  131. // Remap response value according to activation thresholds and clamp to `[0, 1]`.
  132. float response = math::map(std::abs(axis_value), activation_min, activation_max, 0.0f, 1.0f);
  133. response = std::clamp(response, 0.0f, 1.0f);
  134. // Remap response value according to axis response curve
  135. response = curve_response(axis, response);
  136. // Restore sign of axis motion
  137. response = (axis_value < 0.0f) ? -response : response;
  138. event.value = response;
  139. }
  140. else
  141. {
  142. event.value = 0.0f;
  143. }
  144. // Dispatch event
  145. device::event_dispatcher->queue(event);
  146. }
  147. void gamepad::handle_biaxial_motion(gamepad_axis axis_x, gamepad_axis axis_y)
  148. {
  149. // Get axis parameters
  150. const int x_axis_index = static_cast<int>(axis_x);
  151. const int y_axis_index = static_cast<int>(axis_y);
  152. const float x_activation_min = axis_activation_min[x_axis_index];
  153. const float x_activation_max = axis_activation_max[x_axis_index];
  154. const float y_activation_min = axis_activation_min[y_axis_index];
  155. const float y_activation_max = axis_activation_max[y_axis_index];
  156. const float x_axis_value = axis_values[x_axis_index];
  157. const float y_axis_value = axis_values[y_axis_index];
  158. const gamepad_response_curve x_response_curve = axis_response_curves[x_axis_index];
  159. const gamepad_response_curve y_response_curve = axis_response_curves[y_axis_index];
  160. const float deadzone_roundness = (axis_x == gamepad_axis::left_x) ? left_deadzone_roundness : right_deadzone_roundness;
  161. const float radius = std::min<float>(x_activation_min, y_activation_min) * deadzone_roundness;
  162. const float dx = std::max<float>(0.0f, std::abs(x_axis_value) - x_activation_min + radius);
  163. const float dy = std::max<float>(0.0f, std::abs(y_axis_value) - y_activation_min + radius);
  164. const float distance = std::sqrt(dx * dx + dy * dy) - radius;
  165. // Build event
  166. gamepad_axis_moved_event event;
  167. event.controller = this;
  168. if (distance > 0.0f)
  169. {
  170. const float nx = std::abs(x_axis_value) / distance;
  171. const float ny = std::abs(y_axis_value) / distance;
  172. const float ndx = (distance - x_activation_min) / (x_activation_max - x_activation_min);
  173. const float ndy = (distance - y_activation_min) / (y_activation_max - y_activation_min);
  174. float response_x = std::clamp(nx * ndx, 0.0f, 1.0f);
  175. float response_y = std::clamp(ny * ndy, 0.0f, 1.0f);
  176. response_x = curve_response(axis_x, response_x);
  177. response_y = curve_response(axis_y, response_y);
  178. // Restore signs of axis motions
  179. response_x = (x_axis_value < 0.0f) ? -response_x : response_x;
  180. response_y = (y_axis_value < 0.0f) ? -response_y : response_y;
  181. event.value = response_x;
  182. event.axis = axis_x;
  183. device::event_dispatcher->queue(event);
  184. event.value = response_y;
  185. event.axis = axis_y;
  186. device::event_dispatcher->queue(event);
  187. }
  188. else
  189. {
  190. event.value = 0.0f;
  191. event.axis = axis_x;
  192. device::event_dispatcher->queue(event);
  193. event.axis = axis_y;
  194. device::event_dispatcher->queue(event);
  195. }
  196. }
  197. float gamepad::curve_response(gamepad_axis axis, float response) const
  198. {
  199. const gamepad_response_curve response_curve = axis_response_curves[static_cast<int>(axis)];
  200. switch (response_curve)
  201. {
  202. case gamepad_response_curve::linear:
  203. break;
  204. case gamepad_response_curve::square:
  205. response = response * response;
  206. break;
  207. case gamepad_response_curve::cube:
  208. response = response * response * response;
  209. break;
  210. }
  211. return response;
  212. }
  213. void gamepad::connect(bool reconnected)
  214. {
  215. connected = true;
  216. gamepad_connected_event event;
  217. event.controller = this;
  218. event.reconnected = reconnected;
  219. device::event_dispatcher->queue(event);
  220. }
  221. void gamepad::disconnect()
  222. {
  223. connected = false;
  224. gamepad_disconnected_event event;
  225. event.controller = this;
  226. device::event_dispatcher->queue(event);
  227. }
  228. } // namespace input