comparison nuklear_ui/blastem_nuklear.c @ 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 c4256ce2c45a
comparison
equal deleted inserted replaced
2677:2df04125ac78 2678:844ca8377b45
1415 static uint8_t initial_controller_config; 1415 static uint8_t initial_controller_config;
1416 #define QUIET_FRAMES 9 1416 #define QUIET_FRAMES 9
1417 static void view_controller_mappings(struct nk_context *context) 1417 static void view_controller_mappings(struct nk_context *context)
1418 { 1418 {
1419 char buffer[512]; 1419 char buffer[512];
1420 static int quiet, button_a = -1, button_a_axis = -1; 1420 static int button_a = -1, button_a_axis = -1;
1421 static uint32_t quiet_start;
1421 uint8_t added_mapping = 0; 1422 uint8_t added_mapping = 0;
1422 if (nk_begin(context, "Controllers", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) { 1423 if (nk_begin(context, "Controllers", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) {
1423 1424
1424 nk_layout_space_begin(context, NK_STATIC, render_height() - context->style.font->height, 3); 1425 nk_layout_space_begin(context, NK_STATIC, render_height() - context->style.font->height, 3);
1425 1426
1443 snprintf(buffer, sizeof(buffer), "Press Button %s to skip", get_button_label(&selected_controller_info, SDL_CONTROLLER_BUTTON_A)); 1444 snprintf(buffer, sizeof(buffer), "Press Button %s to skip", get_button_label(&selected_controller_info, SDL_CONTROLLER_BUTTON_A));
1444 nk_label(context, buffer, NK_TEXT_CENTERED); 1445 nk_label(context, buffer, NK_TEXT_CENTERED);
1445 } 1446 }
1446 1447
1447 nk_layout_space_end(context); 1448 nk_layout_space_end(context);
1448 if (quiet) { 1449 if (quiet_start) {
1449 --quiet; 1450 if (render_elapsed_ms() - quiet_start > 1000 * QUIET_FRAMES / 60) {
1451 quiet_start = 0;
1452 }
1450 } else { 1453 } else {
1451 if (button_pressed >= 0 && button_pressed != last_button) { 1454 if (button_pressed >= 0 && button_pressed != last_button) {
1452 if (current_button <= SDL_CONTROLLER_BUTTON_B || button_pressed != button_a) { 1455 if (current_button <= SDL_CONTROLLER_BUTTON_B || button_pressed != button_a) {
1453 start_mapping(); 1456 start_mapping();
1454 mapping_string[mapping_pos++] = 'b'; 1457 mapping_string[mapping_pos++] = 'b';
1493 added_mapping = 1; 1496 added_mapping = 1;
1494 } 1497 }
1495 } 1498 }
1496 1499
1497 while (added_mapping) { 1500 while (added_mapping) {
1498 quiet = QUIET_FRAMES; 1501 quiet_start = render_elapsed_ms();
1499 if (current_button < SDL_CONTROLLER_BUTTON_MAX) { 1502 if (current_button < SDL_CONTROLLER_BUTTON_MAX) {
1500 current_button++; 1503 current_button++;
1501 if (current_button == SDL_CONTROLLER_BUTTON_MAX) { 1504 if (current_button == SDL_CONTROLLER_BUTTON_MAX) {
1502 current_axis = 0; 1505 current_axis = 0;
1503 if (get_axis_label(&selected_controller_info, current_axis)) { 1506 if (get_axis_label(&selected_controller_info, current_axis)) {
2669 ui_exit(); 2672 ui_exit();
2670 #endif 2673 #endif
2671 } 2674 }
2672 static void handle_event(SDL_Event *event) 2675 static void handle_event(SDL_Event *event)
2673 { 2676 {
2677 SDL_Joystick *joy = render_get_joystick(selected_controller);
2674 if (event->type == SDL_KEYDOWN) { 2678 if (event->type == SDL_KEYDOWN) {
2675 keycode = event->key.keysym.sym; 2679 keycode = event->key.keysym.sym;
2676 }
2677 else if (event->type == SDL_JOYBUTTONDOWN) {
2678 button_pressed = event->jbutton.button;
2679 }
2680 else if (event->type == SDL_JOYHATMOTION) {
2681 hat_moved = event->jhat.hat;
2682 hat_value = event->jhat.value;
2683 }
2684 else if (event->type == SDL_JOYAXISMOTION) {
2685 if (event->jaxis.axis == axis_moved || abs(event->jaxis.value) > abs(axis_value) || abs(event->jaxis.value) > 4000) {
2686 axis_moved = event->jaxis.axis;
2687 axis_value = event->jaxis.value;
2688 }
2689 } else if (event->type == SDL_MOUSEBUTTONDOWN && event->button.button == 0) { 2680 } else if (event->type == SDL_MOUSEBUTTONDOWN && event->button.button == 0) {
2690 click = 1; 2681 click = 1;
2691 } else if (event->type == SDL_MOUSEBUTTONUP && event->button.button == 0) { 2682 } else if (event->type == SDL_MOUSEBUTTONUP && event->button.button == 0) {
2692 click = 0; 2683 click = 0;
2684 } else if (joy) {
2685 if (event->type == SDL_JOYBUTTONDOWN && event->jbutton.which == SDL_JoystickInstanceID(joy)) {
2686 button_pressed = event->jbutton.button;
2687 }
2688 else if (event->type == SDL_JOYHATMOTION && event->jhat.which == SDL_JoystickInstanceID(joy)) {
2689 hat_moved = event->jhat.hat;
2690 hat_value = event->jhat.value;
2691 }
2692 else if (event->type == SDL_JOYAXISMOTION && event->jaxis.which == SDL_JoystickInstanceID(joy)) {
2693 if (event->jaxis.axis == axis_moved || abs(event->jaxis.value) > abs(axis_value) || abs(event->jaxis.value) > 4000) {
2694 axis_moved = event->jaxis.axis;
2695 axis_value = event->jaxis.value;
2696 }
2697 }
2693 } 2698 }
2694 if (stick_nav_disabled && event->type == SDL_CONTROLLERAXISMOTION) { 2699 if (stick_nav_disabled && event->type == SDL_CONTROLLERAXISMOTION) {
2695 return; 2700 return;
2696 } 2701 }
2697 nk_sdl_handle_event(event); 2702 nk_sdl_handle_event(event);