joypad_uwp.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**************************************************************************/
  2. /* joypad_uwp.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "joypad_uwp.h"
  31. #include "core/os/os.h"
  32. using namespace Windows::Gaming::Input;
  33. using namespace Windows::Foundation;
  34. void JoypadUWP::register_events() {
  35. Gamepad::GamepadAdded +=
  36. ref new EventHandler<Gamepad ^>(this, &JoypadUWP::OnGamepadAdded);
  37. Gamepad::GamepadRemoved +=
  38. ref new EventHandler<Gamepad ^>(this, &JoypadUWP::OnGamepadRemoved);
  39. }
  40. void JoypadUWP::process_controllers() {
  41. for (int i = 0; i < MAX_CONTROLLERS; i++) {
  42. ControllerDevice &joy = controllers[i];
  43. if (!joy.connected)
  44. break;
  45. switch (joy.type) {
  46. case ControllerType::GAMEPAD_CONTROLLER: {
  47. GamepadReading reading = ((Gamepad ^) joy.controller_reference)->GetCurrentReading();
  48. int button_mask = (int)GamepadButtons::Menu;
  49. for (int j = 0; j < 14; j++) {
  50. input->joy_button(joy.id, j, (int)reading.Buttons & button_mask);
  51. button_mask *= 2;
  52. }
  53. input->joy_axis(joy.id, JOY_AXIS_0, axis_correct(reading.LeftThumbstickX));
  54. input->joy_axis(joy.id, JOY_AXIS_1, axis_correct(reading.LeftThumbstickY, true));
  55. input->joy_axis(joy.id, JOY_AXIS_2, axis_correct(reading.RightThumbstickX));
  56. input->joy_axis(joy.id, JOY_AXIS_3, axis_correct(reading.RightThumbstickY, true));
  57. input->joy_axis(joy.id, JOY_AXIS_4, axis_correct(reading.LeftTrigger, false, true));
  58. input->joy_axis(joy.id, JOY_AXIS_5, axis_correct(reading.RightTrigger, false, true));
  59. uint64_t timestamp = input->get_joy_vibration_timestamp(joy.id);
  60. if (timestamp > joy.ff_timestamp) {
  61. Vector2 strength = input->get_joy_vibration_strength(joy.id);
  62. float duration = input->get_joy_vibration_duration(joy.id);
  63. if (strength.x == 0 && strength.y == 0) {
  64. joypad_vibration_stop(i, timestamp);
  65. } else {
  66. joypad_vibration_start(i, strength.x, strength.y, duration, timestamp);
  67. }
  68. } else if (joy.vibrating && joy.ff_end_timestamp != 0) {
  69. uint64_t current_time = OS::get_singleton()->get_ticks_usec();
  70. if (current_time >= joy.ff_end_timestamp)
  71. joypad_vibration_stop(i, current_time);
  72. }
  73. break;
  74. }
  75. }
  76. }
  77. }
  78. JoypadUWP::JoypadUWP() {
  79. for (int i = 0; i < MAX_CONTROLLERS; i++)
  80. controllers[i].id = i;
  81. }
  82. JoypadUWP::JoypadUWP(InputDefault *p_input) {
  83. input = p_input;
  84. JoypadUWP();
  85. }
  86. void JoypadUWP::OnGamepadAdded(Platform::Object ^ sender, Windows::Gaming::Input::Gamepad ^ value) {
  87. short idx = -1;
  88. for (int i = 0; i < MAX_CONTROLLERS; i++) {
  89. if (!controllers[i].connected) {
  90. idx = i;
  91. break;
  92. }
  93. }
  94. ERR_FAIL_COND(idx == -1);
  95. controllers[idx].connected = true;
  96. controllers[idx].controller_reference = value;
  97. controllers[idx].id = idx;
  98. controllers[idx].type = ControllerType::GAMEPAD_CONTROLLER;
  99. input->joy_connection_changed(controllers[idx].id, true, "Xbox Controller", "__UWP_GAMEPAD__");
  100. }
  101. void JoypadUWP::OnGamepadRemoved(Platform::Object ^ sender, Windows::Gaming::Input::Gamepad ^ value) {
  102. short idx = -1;
  103. for (int i = 0; i < MAX_CONTROLLERS; i++) {
  104. if (controllers[i].controller_reference == value) {
  105. idx = i;
  106. break;
  107. }
  108. }
  109. ERR_FAIL_COND(idx == -1);
  110. controllers[idx] = ControllerDevice();
  111. input->joy_connection_changed(idx, false, "Xbox Controller");
  112. }
  113. float JoypadUWP::axis_correct(double p_val, bool p_negate, bool p_trigger) const {
  114. if (p_trigger) {
  115. // Convert to a value between -1.0f and 1.0f.
  116. return 2.0f * p_val - 1.0f;
  117. }
  118. return (float)(p_negate ? -p_val : p_val);
  119. }
  120. void JoypadUWP::joypad_vibration_start(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp) {
  121. ControllerDevice &joy = controllers[p_device];
  122. if (joy.connected) {
  123. GamepadVibration vibration;
  124. vibration.LeftMotor = p_strong_magnitude;
  125. vibration.RightMotor = p_weak_magnitude;
  126. ((Gamepad ^) joy.controller_reference)->Vibration = vibration;
  127. joy.ff_timestamp = p_timestamp;
  128. joy.ff_end_timestamp = p_duration == 0 ? 0 : p_timestamp + (uint64_t)(p_duration * 1000000.0);
  129. joy.vibrating = true;
  130. }
  131. }
  132. void JoypadUWP::joypad_vibration_stop(int p_device, uint64_t p_timestamp) {
  133. ControllerDevice &joy = controllers[p_device];
  134. if (joy.connected) {
  135. GamepadVibration vibration;
  136. vibration.LeftMotor = 0.0;
  137. vibration.RightMotor = 0.0;
  138. ((Gamepad ^) joy.controller_reference)->Vibration = vibration;
  139. joy.ff_timestamp = p_timestamp;
  140. joy.vibrating = false;
  141. }
  142. }