changeset 2678:844ca8377b45

Make the controller remap UI less twitchy on high refresh rate screens and filter out events from other controllers
author Michael Pavone <pavone@retrodev.com>
date Wed, 26 Mar 2025 01:15:52 -0700
parents 2df04125ac78
children 69c1093f8726
files nuklear_ui/blastem_nuklear.c
diffstat 1 files changed, 22 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/nuklear_ui/blastem_nuklear.c	Sat Mar 15 23:31:30 2025 -0700
+++ b/nuklear_ui/blastem_nuklear.c	Wed Mar 26 01:15:52 2025 -0700
@@ -1417,7 +1417,8 @@
 static void view_controller_mappings(struct nk_context *context)
 {
 	char buffer[512];
-	static int quiet, button_a = -1, button_a_axis = -1;
+	static int button_a = -1, button_a_axis = -1;
+	static uint32_t quiet_start;
 	uint8_t added_mapping = 0;
 	if (nk_begin(context, "Controllers", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) {
 
@@ -1445,8 +1446,10 @@
 		}
 
 		nk_layout_space_end(context);
-		if (quiet) {
-			--quiet;
+		if (quiet_start) {
+			if (render_elapsed_ms() - quiet_start > 1000 * QUIET_FRAMES / 60) {
+				quiet_start = 0;
+			}
 		} else {
 			if (button_pressed >= 0 && button_pressed != last_button) {
 				if (current_button <= SDL_CONTROLLER_BUTTON_B || button_pressed != button_a) {
@@ -1495,7 +1498,7 @@
 		}
 
 		while (added_mapping) {
-			quiet = QUIET_FRAMES;
+			quiet_start = render_elapsed_ms();
 			if (current_button < SDL_CONTROLLER_BUTTON_MAX) {
 				current_button++;
 				if (current_button == SDL_CONTROLLER_BUTTON_MAX) {
@@ -2671,25 +2674,27 @@
 }
 static void handle_event(SDL_Event *event)
 {
+	SDL_Joystick *joy = render_get_joystick(selected_controller);
 	if (event->type == SDL_KEYDOWN) {
 		keycode = event->key.keysym.sym;
-	}
-	else if (event->type == SDL_JOYBUTTONDOWN) {
-		button_pressed = event->jbutton.button;
-	}
-	else if (event->type == SDL_JOYHATMOTION) {
-		hat_moved = event->jhat.hat;
-		hat_value = event->jhat.value;
-	}
-	else if (event->type == SDL_JOYAXISMOTION) {
-		if (event->jaxis.axis == axis_moved || abs(event->jaxis.value) > abs(axis_value) || abs(event->jaxis.value) > 4000) {
-			axis_moved = event->jaxis.axis;
-			axis_value = event->jaxis.value;
-		}
 	} else if (event->type == SDL_MOUSEBUTTONDOWN && event->button.button == 0) {
 		click = 1;
 	} else if (event->type == SDL_MOUSEBUTTONUP && event->button.button == 0) {
 		click = 0;
+	} else if (joy) {
+		if (event->type == SDL_JOYBUTTONDOWN && event->jbutton.which == SDL_JoystickInstanceID(joy)) {
+			button_pressed = event->jbutton.button;
+		}
+		else if (event->type == SDL_JOYHATMOTION && event->jhat.which == SDL_JoystickInstanceID(joy)) {
+			hat_moved = event->jhat.hat;
+			hat_value = event->jhat.value;
+		}
+		else if (event->type == SDL_JOYAXISMOTION && event->jaxis.which == SDL_JoystickInstanceID(joy)) {
+			if (event->jaxis.axis == axis_moved || abs(event->jaxis.value) > abs(axis_value) || abs(event->jaxis.value) > 4000) {
+				axis_moved = event->jaxis.axis;
+				axis_value = event->jaxis.value;
+			}
+		}
 	}
 	if (stick_nav_disabled && event->type == SDL_CONTROLLERAXISMOTION) {
 		return;