changeset 1804:34370330eaf3

Support controllers that have their dpad mapped to an axis
author Michael Pavone <pavone@retrodev.com>
date Tue, 26 Mar 2019 23:26:08 -0700
parents a851d36e24bb
children 930c11348874
files bindings.c nuklear_ui/blastem_nuklear.c render.h render_sdl.c
diffstat 4 files changed, 19 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/bindings.c	Tue Mar 26 22:34:41 2019 -0700
+++ b/bindings.c	Tue Mar 26 23:26:08 2019 -0700
@@ -825,7 +825,7 @@
 			bind_dpad(hostpadnum, render_dpad_part(hostbutton), render_direction_part(hostbutton), bindtype, subtype_a, subtype_b);
 			return;
 		} else if (hostbutton & RENDER_AXIS_BIT) {
-			bind_axis(hostpadnum, render_axis_part(hostbutton), 1, bindtype, subtype_a, subtype_b);
+			bind_axis(hostpadnum, render_axis_part(hostbutton), hostbutton & RENDER_AXIS_POS, bindtype, subtype_a, subtype_b);
 			return;
 		}
 	}
--- a/nuklear_ui/blastem_nuklear.c	Tue Mar 26 22:34:41 2019 -0700
+++ b/nuklear_ui/blastem_nuklear.c	Tue Mar 26 23:26:08 2019 -0700
@@ -1128,7 +1128,7 @@
 static int current_axis;
 static int button_pressed, last_button;
 static int hat_moved, hat_value, last_hat, last_hat_value;
-static int axis_moved, axis_value, last_axis;
+static int axis_moved, axis_value, last_axis, last_axis_value;
 static char *mapping_string;
 static size_t mapping_pos;
 
@@ -1206,7 +1206,11 @@
 				
 				last_hat = hat_moved;
 				last_hat_value = hat_value;
-			} else if (axis_moved >= 0 && abs(axis_value) > 1000 && axis_moved != last_axis) {
+			} else if (axis_moved >= 0 && abs(axis_value) > 1000 && (
+					axis_moved != last_axis || (
+						axis_value/abs(axis_value) != last_axis_value/abs(axis_value) && current_button >= SDL_CONTROLLER_BUTTON_DPAD_UP
+					)
+				)) {
 				if (current_button <= SDL_CONTROLLER_BUTTON_B || axis_moved != button_a_axis) {
 					start_mapping();
 					mapping_string[mapping_pos++] = 'a';
@@ -1214,7 +1218,11 @@
 						mapping_string[mapping_pos++] = '0' + axis_moved / 10;
 					}
 					mapping_string[mapping_pos++] = '0' + axis_moved % 10;
+					if (current_button >= SDL_CONTROLLER_BUTTON_DPAD_UP) {
+						mapping_string[mapping_pos++] = axis_value >= 0 ? '+' : '-';
+					}
 					last_axis = axis_moved;
+					last_axis_value = axis_value;
 				}
 				added_mapping = 1;
 			}
@@ -1295,6 +1303,7 @@
 			last_hat = -1;
 			axis_moved = -1;
 			last_axis = -1;
+			last_axis_value = 0;
 			SDL_Joystick *joy = render_get_joystick(selected_controller);
 			const char *name = SDL_JoystickName(joy);
 			size_t namesz = strlen(name);
--- a/render.h	Tue Mar 26 22:34:41 2019 -0700
+++ b/render.h	Tue Mar 26 23:26:08 2019 -0700
@@ -86,6 +86,7 @@
 
 #define RENDER_DPAD_BIT 0x40000000
 #define RENDER_AXIS_BIT 0x20000000
+#define RENDER_AXIS_POS 0x10000000
 #define RENDER_INVALID_NAME -1
 #define RENDER_NOT_MAPPED -2
 #define RENDER_NOT_PLUGGED_IN -3
--- a/render_sdl.c	Tue Mar 26 22:34:41 2019 -0700
+++ b/render_sdl.c	Tue Mar 26 23:26:08 2019 -0700
@@ -1864,6 +1864,7 @@
 	}
 	
 	SDL_GameControllerButtonBind cbind;
+	int32_t is_positive = RENDER_AXIS_POS;
 	if (is_axis) {
 		
 		int sdl_axis = render_lookup_axis(name);
@@ -1878,6 +1879,10 @@
 			SDL_GameControllerClose(control);
 			return RENDER_INVALID_NAME;
 		}
+		if (sdl_button == SDL_CONTROLLER_BUTTON_DPAD_UP || sdl_button == SDL_CONTROLLER_BUTTON_DPAD_LEFT) {
+			//assume these will be negative if they are an axis
+			is_positive = 0;
+		}
 		cbind = SDL_GameControllerGetBindForButton(control, sdl_button);
 	}
 	SDL_GameControllerClose(control);
@@ -1886,7 +1891,7 @@
 	case SDL_CONTROLLER_BINDTYPE_BUTTON:
 		return cbind.value.button;
 	case SDL_CONTROLLER_BINDTYPE_AXIS:
-		return RENDER_AXIS_BIT | cbind.value.axis;
+		return RENDER_AXIS_BIT | cbind.value.axis | is_positive;
 	case SDL_CONTROLLER_BINDTYPE_HAT:
 		return RENDER_DPAD_BIT | (cbind.value.hat.hat << 4) | cbind.value.hat.hat_mask;
 	}