diff options
| author | german77 <juangerman-13@hotmail.com> | 2021-05-14 21:17:08 -0500 | 
|---|---|---|
| committer | german77 <juangerman-13@hotmail.com> | 2021-05-14 21:17:08 -0500 | 
| commit | 85eeae7aad96fd5272f77ce0e24608417ad5c68e (patch) | |
| tree | 81f0537f4141b5d43b802c76037a0af7f119e2a7 | |
| parent | 904584e4ba0b87e3f1555e29e9bf31109b65e301 (diff) | |
input_common: Rewrite sdl analog mapping
| -rw-r--r-- | src/input_common/sdl/sdl_impl.cpp | 50 | 
1 files changed, 25 insertions, 25 deletions
| diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index f682a6db4..c918a333d 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp @@ -1168,51 +1168,51 @@ public:      void Start(const std::string& device_id) override {          SDLPoller::Start(device_id);          // Reset stored axes -        analog_x_axis = -1; -        analog_y_axis = -1; +        first_axis = -1;      }      Common::ParamPackage GetNextInput() override {          SDL_Event event;          while (state.event_queue.Pop(event)) { -            // Filter out axis events that are below a threshold -            if (event.type == SDL_JOYAXISMOTION && std::abs(event.jaxis.value / 32767.0) < 0.5) { -                continue; -            } -            if (event.type == SDL_JOYAXISMOTION) { -                const auto axis = event.jaxis.axis; -                // In order to return a complete analog param, we need inputs for both axes. -                // First we take the x-axis (horizontal) input, then the y-axis (vertical) input. -                if (analog_x_axis == -1) { -                    analog_x_axis = axis; -                } else if (analog_y_axis == -1 && analog_x_axis != axis) { -                    analog_y_axis = axis; -                } -            } else { -                // If the press wasn't accepted as a joy axis, check for a button press +            if (event.type != SDL_JOYAXISMOTION) { +                // Check for a button press                  auto button_press = button_poller.FromEvent(event);                  if (button_press) {                      return *button_press;                  } +                continue; +            } +            const auto axis = event.jaxis.axis; + +            // Filter out axis events that are below a threshold +            if (std::abs(event.jaxis.value / 32767.0) < 0.5) { +                continue; +            } + +            // Filter out axis events that are the same +            if (first_axis == axis) { +                continue; +            } + +            // In order to return a complete analog param, we need inputs for both axes. +            // If the first axis isn't set we set the value then wait till next event +            if (first_axis == -1) { +                first_axis = axis; +                continue;              } -        } -        if (analog_x_axis != -1 && analog_y_axis != -1) {              if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) {                  auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), -                                                         analog_x_axis, analog_y_axis); -                analog_x_axis = -1; -                analog_y_axis = -1; +                                                         first_axis, axis); +                first_axis = -1;                  return params;              }          } -          return {};      }  private: -    int analog_x_axis = -1; -    int analog_y_axis = -1; +    int first_axis = -1;      SDLButtonPoller button_poller;  };  } // namespace Polling | 
