Skip to content

Commit

Permalink
Workaround false positive gamepad detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mechakotik committed Oct 7, 2024
1 parent 9a29c83 commit 2a74771
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/gamepad.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace TA { namespace gamepad {
void init(int index = 0);
void update();
bool connected();
bool oncePressed();
void quit();

TA_Point getDirectionVector();
Expand Down
2 changes: 1 addition & 1 deletion src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ bool TA_Controller::isJustPressed(TA_FunctionButton button)
bool TA_Controller::isTouchscreen()
{
#ifdef __ANDROID__
return !TA::gamepad::connected();
return !(TA::gamepad::connected() && TA::gamepad::oncePressed());
#else
return false;
#endif
Expand Down
7 changes: 7 additions & 0 deletions src/gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ namespace TA { namespace gamepad {
SDL_Gamepad *controller = nullptr;
std::array<bool, SDL_GAMEPAD_BUTTON_COUNT> pressed, justPressed;
bool isConnected = false;
bool isOncePressed = false;
}}

bool TA::gamepad::connected()
{
return isConnected;
}

bool TA::gamepad::oncePressed()
{
return isOncePressed;
}

void TA::gamepad::handleEvent(SDL_GamepadDeviceEvent event)
{
if(event.type == SDL_EVENT_GAMEPAD_ADDED && !isConnected) {
Expand Down Expand Up @@ -79,6 +85,7 @@ void TA::gamepad::update()
if(SDL_GetGamepadButton(controller, (SDL_GamepadButton)button)) {
if(!pressed[(int)button]) {
pressed[(int)button] = justPressed[(int)button] = true;
isOncePressed = true;
}
else {
justPressed[(int)button] = false;
Expand Down

0 comments on commit 2a74771

Please sign in to comment.