diff options
Diffstat (limited to 'platform/uwp/joypad_uwp.cpp')
-rw-r--r-- | platform/uwp/joypad_uwp.cpp | 75 |
1 files changed, 56 insertions, 19 deletions
diff --git a/platform/uwp/joypad_uwp.cpp b/platform/uwp/joypad_uwp.cpp index 34e36f7b66..f3d4eb99c8 100644 --- a/platform/uwp/joypad_uwp.cpp +++ b/platform/uwp/joypad_uwp.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "joypad_uwp.h" +#include "core/os/os.h" using namespace Windows::Gaming::Input; using namespace Windows::Foundation; @@ -45,27 +46,44 @@ void JoypadUWP::process_controllers() { for (int i = 0; i < MAX_CONTROLLERS; i++) { - if (!controllers[i].connected) break; + ControllerDevice &joy = controllers[i]; - switch (controllers[i].type) { + if (!joy.connected) break; + + switch (joy.type) { case ControllerType::GAMEPAD_CONTROLLER: { - GamepadReading reading = ((Gamepad ^)controllers[i].controller_reference)->GetCurrentReading(); + GamepadReading reading = ((Gamepad ^) joy.controller_reference)->GetCurrentReading(); int button_mask = (int)GamepadButtons::Menu; for (int j = 0; j < 14; j++) { - input->joy_button(controllers[i].id, j, (int)reading.Buttons & button_mask); + input->joy_button(joy.id, j, (int)reading.Buttons & button_mask); button_mask *= 2; } - input->joy_axis(controllers[i].id, JOY_AXIS_0, axis_correct(reading.LeftThumbstickX)); - input->joy_axis(controllers[i].id, JOY_AXIS_1, axis_correct(reading.LeftThumbstickY, true)); - input->joy_axis(controllers[i].id, JOY_AXIS_2, axis_correct(reading.RightThumbstickX)); - input->joy_axis(controllers[i].id, JOY_AXIS_3, axis_correct(reading.RightThumbstickY, true)); - input->joy_axis(controllers[i].id, JOY_AXIS_4, axis_correct(reading.LeftTrigger, false, true)); - input->joy_axis(controllers[i].id, JOY_AXIS_5, axis_correct(reading.RightTrigger, false, true)); + input->joy_axis(joy.id, JOY_AXIS_0, axis_correct(reading.LeftThumbstickX)); + input->joy_axis(joy.id, JOY_AXIS_1, axis_correct(reading.LeftThumbstickY, true)); + input->joy_axis(joy.id, JOY_AXIS_2, axis_correct(reading.RightThumbstickX)); + input->joy_axis(joy.id, JOY_AXIS_3, axis_correct(reading.RightThumbstickY, true)); + input->joy_axis(joy.id, JOY_AXIS_4, axis_correct(reading.LeftTrigger, false, true)); + input->joy_axis(joy.id, JOY_AXIS_5, axis_correct(reading.RightTrigger, false, true)); + + uint64_t timestamp = input->get_joy_vibration_timestamp(joy.id); + if (timestamp > joy.ff_timestamp) { + Vector2 strength = input->get_joy_vibration_strength(joy.id); + float duration = input->get_joy_vibration_duration(joy.id); + if (strength.x == 0 && strength.y == 0) { + joypad_vibration_stop(i, timestamp); + } else { + joypad_vibration_start(i, strength.x, strength.y, duration, timestamp); + } + } else if (joy.vibrating && joy.ff_end_timestamp != 0) { + uint64_t current_time = OS::get_singleton()->get_ticks_usec(); + if (current_time >= joy.ff_end_timestamp) + joypad_vibration_stop(i, current_time); + } break; } @@ -122,15 +140,7 @@ void JoypadUWP::OnGamepadRemoved(Platform::Object ^ sender, Windows::Gaming::Inp ERR_FAIL_COND(idx == -1); - for (int i = idx + 1; i < MAX_CONTROLLERS - 1; i++) { - - if (!controllers[i].connected) { - break; - } - - controllers[i - 1] = controllers[i]; - } - controllers[MAX_CONTROLLERS - 1] = ControllerDevice(); + controllers[idx] = ControllerDevice(); input->joy_connection_changed(idx, false, "Xbox Controller"); } @@ -144,3 +154,30 @@ InputDefault::JoyAxis JoypadUWP::axis_correct(double p_val, bool p_negate, bool return jx; } + +void JoypadUWP::joypad_vibration_start(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp) { + ControllerDevice &joy = controllers[p_device]; + if (joy.connected) { + GamepadVibration vibration; + vibration.LeftMotor = p_strong_magnitude; + vibration.RightMotor = p_weak_magnitude; + ((Gamepad ^) joy.controller_reference)->Vibration = vibration; + + joy.ff_timestamp = p_timestamp; + joy.ff_end_timestamp = p_duration == 0 ? 0 : p_timestamp + (uint64_t)(p_duration * 1000000.0); + joy.vibrating = true; + } +} + +void JoypadUWP::joypad_vibration_stop(int p_device, uint64_t p_timestamp) { + ControllerDevice &joy = controllers[p_device]; + if (joy.connected) { + GamepadVibration vibration; + vibration.LeftMotor = 0.0; + vibration.RightMotor = 0.0; + ((Gamepad ^) joy.controller_reference)->Vibration = vibration; + + joy.ff_timestamp = p_timestamp; + joy.vibrating = false; + } +} |