joystick_uwp.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*************************************************************************/
  2. /* joypad_uwp.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "joypad_uwp.h"
  30. using namespace Windows::Gaming::Input;
  31. using namespace Windows::Foundation;
  32. void JoypadUWP::register_events() {
  33. Gamepad::GamepadAdded +=
  34. ref new EventHandler<Gamepad^>(this, &JoypadUWP::OnGamepadAdded);
  35. Gamepad::GamepadRemoved +=
  36. ref new EventHandler<Gamepad^>(this, &JoypadUWP::OnGamepadRemoved);
  37. }
  38. uint32_t JoypadUWP::process_controllers(uint32_t p_last_id) {
  39. for (int i = 0; i < MAX_CONTROLLERS; i++) {
  40. if (!controllers[i].connected) break;
  41. switch (controllers[i].type) {
  42. case ControllerType::GAMEPAD_CONTROLLER: {
  43. GamepadReading reading = ((Gamepad^)controllers[i].controller_reference)->GetCurrentReading();
  44. int button_mask = (int)GamepadButtons::Menu;
  45. for (int j = 0; j < 14; j++) {
  46. p_last_id = input->joy_button(p_last_id, controllers[i].id, j,(int)reading.Buttons & button_mask);
  47. button_mask *= 2;
  48. }
  49. p_last_id = input->joy_axis(p_last_id, controllers[i].id, JOY_AXIS_0, axis_correct(reading.LeftThumbstickX));
  50. p_last_id = input->joy_axis(p_last_id, controllers[i].id, JOY_AXIS_1, axis_correct(reading.LeftThumbstickY, true));
  51. p_last_id = input->joy_axis(p_last_id, controllers[i].id, JOY_AXIS_2, axis_correct(reading.RightThumbstickX));
  52. p_last_id = input->joy_axis(p_last_id, controllers[i].id, JOY_AXIS_3, axis_correct(reading.RightThumbstickY, true));
  53. p_last_id = input->joy_axis(p_last_id, controllers[i].id, JOY_AXIS_4, axis_correct(reading.LeftTrigger, false, true));
  54. p_last_id = input->joy_axis(p_last_id, controllers[i].id, JOY_AXIS_5, axis_correct(reading.RightTrigger, false, true));
  55. break;
  56. }
  57. }
  58. }
  59. return p_last_id;
  60. }
  61. JoypadUWP::JoypadUWP() {
  62. for (int i = 0; i < MAX_CONTROLLERS; i++)
  63. controllers[i].id = i;
  64. }
  65. JoypadUWP::JoypadUWP(InputDefault * p_input) {
  66. input = p_input;
  67. JoypadUWP();
  68. }
  69. void JoypadUWP::OnGamepadAdded(Platform::Object ^ sender, Windows::Gaming::Input::Gamepad ^ value) {
  70. short idx = -1;
  71. for (int i = 0; i < MAX_CONTROLLERS; i++) {
  72. if (!controllers[i].connected) {
  73. idx = i;
  74. break;
  75. }
  76. }
  77. ERR_FAIL_COND(idx == -1);
  78. controllers[idx].connected = true;
  79. controllers[idx].controller_reference = value;
  80. controllers[idx].id = idx;
  81. controllers[idx].type = ControllerType::GAMEPAD_CONTROLLER;
  82. input->joy_connection_changed(controllers[idx].id, true, "Xbox Controller", "__UWP_GAMEPAD__");
  83. }
  84. void JoypadUWP::OnGamepadRemoved(Platform::Object ^ sender, Windows::Gaming::Input::Gamepad ^ value) {
  85. short idx = -1;
  86. for (int i = 0; i < MAX_CONTROLLERS; i++) {
  87. if (controllers[i].controller_reference == value) {
  88. idx = i;
  89. break;
  90. }
  91. }
  92. ERR_FAIL_COND(idx == -1);
  93. for (int i = idx + 1; i < MAX_CONTROLLERS - 1; i++) {
  94. if (!controllers[i].connected) {
  95. break;
  96. }
  97. controllers[i - 1] = controllers[i];
  98. }
  99. controllers[MAX_CONTROLLERS - 1] = ControllerDevice();
  100. input->joy_connection_changed(idx, false, "Xbox Controller");
  101. }
  102. InputDefault::JoyAxis JoypadUWP::axis_correct(double p_val, bool p_negate, bool p_trigger) const {
  103. InputDefault::JoyAxis jx;
  104. jx.min = p_trigger ? 0 : -1;
  105. jx.value = (float)(p_negate ? -p_val : p_val);
  106. return jx;
  107. }