Skip to content

Commit

Permalink
input: wait for 200ms before detecting button/axis input when remapping
Browse files Browse the repository at this point in the history
  • Loading branch information
flyinghead committed Mar 28, 2019
1 parent 47201b9 commit 8751e55
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
22 changes: 20 additions & 2 deletions core/input/gamepad_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <limits.h>
#include "gamepad_device.h"
#include "rend/gui.h"
#include "oslib/oslib.h"

extern void dc_exit();

Expand All @@ -32,7 +33,8 @@ std::mutex GamepadDevice::_gamepads_mutex;

bool GamepadDevice::gamepad_btn_input(u32 code, bool pressed)
{
if (_input_detected != NULL && _detecting_button && pressed)
if (_input_detected != NULL && _detecting_button
&& os_GetSeconds() >= _detection_start_time && pressed)
{
_input_detected(code);
_input_detected = NULL;
Expand Down Expand Up @@ -84,7 +86,8 @@ bool GamepadDevice::gamepad_axis_input(u32 code, int value)
v = (get_axis_min_value(code) + get_axis_range(code) - value) * 255 / get_axis_range(code) - 128;
else
v = (value - get_axis_min_value(code)) * 255 / get_axis_range(code) - 128; //-128 ... + 127 range
if (_input_detected != NULL && !_detecting_button && (v >= 64 || v <= -64))
if (_input_detected != NULL && !_detecting_button
&& os_GetSeconds() >= _detection_start_time && (v >= 64 || v <= -64))
{
_input_detected(code);
_input_detected = NULL;
Expand Down Expand Up @@ -235,3 +238,18 @@ void UpdateVibration(u32 port, float power, float inclination, u32 duration_ms)
gamepad->rumble(power, inclination, duration_ms);
}
}

void GamepadDevice::detect_btn_input(input_detected_cb button_pressed)
{
_input_detected = button_pressed;
_detecting_button = true;
_detection_start_time = os_GetSeconds() + 0.2;
}

void GamepadDevice::detect_axis_input(input_detected_cb axis_moved)
{
_input_detected = axis_moved;
_detecting_button = false;
_detection_start_time = os_GetSeconds() + 0.2;
}

13 changes: 3 additions & 10 deletions core/input/gamepad_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,8 @@ class GamepadDevice
bool gamepad_axis_input(u32 code, int value);
virtual ~GamepadDevice() {}

void detect_btn_input(input_detected_cb button_pressed)
{
_input_detected = button_pressed;
_detecting_button = true;
}
void detect_axis_input(input_detected_cb axis_moved)
{
_input_detected = axis_moved;
_detecting_button = false;
}
void detect_btn_input(input_detected_cb button_pressed);
void detect_axis_input(input_detected_cb axis_moved);
void cancel_detect_input()
{
_input_detected = NULL;
Expand Down Expand Up @@ -104,6 +96,7 @@ class GamepadDevice
std::string _api_name;
int _maple_port;
bool _detecting_button = false;
double _detection_start_time;
input_detected_cb _input_detected;
bool _remappable;
float _dead_zone = 0.1f;
Expand Down

0 comments on commit 8751e55

Please sign in to comment.