SDL: Allow clicking on the gamepad image to bind elements

From 08db0e8f644821f670211cf7d833775ef12fc096 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 16 Jul 2023 12:39:05 -0700
Subject: [PATCH] Allow clicking on the gamepad image to bind elements

---
 test/testcontroller.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/test/testcontroller.c b/test/testcontroller.c
index 1b32166f6d65..666f24a5baef 100644
--- a/test/testcontroller.c
+++ b/test/testcontroller.c
@@ -220,10 +220,15 @@ static void UpdateButtonHighlights(float x, float y, SDL_bool button_down)
     if (display_mode == CONTROLLER_MODE_TESTING) {
         SetGamepadButtonHighlight(setup_mapping_button, GamepadButtonContains(setup_mapping_button, x, y), button_down);
     } else if (display_mode == CONTROLLER_MODE_BINDING) {
-        int gamepad_highlight_element;
+        int gamepad_highlight_element = SDL_GAMEPAD_ELEMENT_INVALID;
         char *joystick_highlight_element;
 
-        gamepad_highlight_element = GetGamepadDisplayElementAt(gamepad_elements, controller->gamepad, x, y);
+        if (controller->joystick != virtual_joystick) {
+            gamepad_highlight_element = GetGamepadImageElementAt(image, x, y);
+        }
+        if (gamepad_highlight_element == SDL_GAMEPAD_ELEMENT_INVALID) {
+            gamepad_highlight_element = GetGamepadDisplayElementAt(gamepad_elements, controller->gamepad, x, y);
+        }
         SetGamepadDisplayHighlight(gamepad_elements, gamepad_highlight_element, button_down);
 
         joystick_highlight_element = GetJoystickDisplayElementAt(joystick_elements, controller->joystick, x, y);
@@ -1353,10 +1358,15 @@ static void loop(void *arg)
                 } else if (GamepadButtonContains(paste_button, event.button.x, event.button.y)) {
                     PasteMapping();
                 } else {
-                    int gamepad_element;
+                    int gamepad_element = SDL_GAMEPAD_ELEMENT_INVALID;
                     char *joystick_element;
 
-                    gamepad_element = GetGamepadDisplayElementAt(gamepad_elements, controller->gamepad, event.button.x, event.button.y);
+                    if (controller->joystick != virtual_joystick) {
+                        gamepad_element = GetGamepadImageElementAt(image, event.button.x, event.button.y);
+                    }
+                    if (gamepad_element == SDL_GAMEPAD_ELEMENT_INVALID) {
+                        gamepad_element = GetGamepadDisplayElementAt(gamepad_elements, controller->gamepad, event.button.x, event.button.y);
+                    }
                     if (gamepad_element != SDL_GAMEPAD_ELEMENT_INVALID) {
                         /* Set this to SDL_FALSE if you don't want to start the binding flow at this point */
                         const SDL_bool should_start_flow = SDL_TRUE;