comparison nuklear_ui/blastem_nuklear.c @ 1606:aa7533000161

Show current config value for controller butotn and axis mappings
author Michael Pavone <pavone@retrodev.com>
date Thu, 02 Aug 2018 19:05:48 -0700
parents f7b1d983d5c0
children 9c8f58740450
comparison
equal deleted inserted replaced
1605:f7b1d983d5c0 1606:aa7533000161
539 char **conf_keys = calloc(sizeof(char *), num_binds); 539 char **conf_keys = calloc(sizeof(char *), num_binds);
540 float max_width = 0.0f; 540 float max_width = 0.0f;
541 541
542 static const char base_path[] = "bindings\0pads"; 542 static const char base_path[] = "bindings\0pads";
543 static const char buttons[] = "buttons"; 543 static const char buttons[] = "buttons";
544 static const char dpads[] = "dpads\00";
545 static const char axes[] = "axes";
546 static const char positive[] = ".positive";
547 static const char negative[] = ".negative";
544 char padkey[] = {'0' + selected_controller, 0}; 548 char padkey[] = {'0' + selected_controller, 0};
545 int skipped = 0; 549 int skipped = 0;
546 for (int i = 0; i < num_binds; i++) 550 for (int i = 0; i < num_binds; i++)
547 { 551 {
548 if (binds[i] & AXIS) { 552 if (binds[i] & AXIS) {
549 labels[i] = get_axis_label(&selected_controller_info, binds[i] & ~AXIS); 553 labels[i] = get_axis_label(&selected_controller_info, binds[i] & ~AXIS);
554 const char *axname = SDL_GameControllerGetStringForAxis(binds[i] & ~AXIS);
555 size_t namelen = strlen(axname);
556 conf_keys[i] = malloc(sizeof(base_path) + sizeof(padkey) + sizeof(axes) + namelen + 2);
557 memcpy(conf_keys[i], base_path, sizeof(base_path));
558 memcpy(conf_keys[i] + sizeof(base_path), padkey, sizeof(padkey));
559 memcpy(conf_keys[i] + sizeof(base_path) + sizeof(padkey), axes, sizeof(axes));
560 memcpy(conf_keys[i] + sizeof(base_path) + sizeof(padkey) + sizeof(axes), axname, namelen+1);
561 conf_keys[i][sizeof(base_path) + sizeof(padkey) + sizeof(axes) + namelen + 1] = 0;
550 } else if (binds[i] & STICKDIR) { 562 } else if (binds[i] & STICKDIR) {
551 static char const * dirs[] = {"Up", "Down", "Left", "Right"}; 563 static char const * dirs[] = {"Up", "Down", "Left", "Right"};
552 labels[i] = dirs[binds[i] & 3]; 564 labels[i] = dirs[binds[i] & 3];
565 uint8_t is_pos = (binds[i] & 3) == 3 || !(binds[i] & 3);
566 int sdl_axis = (binds[i] & LEFTSTICK ? SDL_CONTROLLER_AXIS_LEFTX : SDL_CONTROLLER_AXIS_RIGHTX) + !(binds[i] & 2);
567 const char *axname = SDL_GameControllerGetStringForAxis(sdl_axis);
568 size_t namelen = strlen(axname);
569 conf_keys[i] = malloc(sizeof(base_path) + sizeof(padkey) + sizeof(axes) + namelen + sizeof(positive) + 1);
570 memcpy(conf_keys[i], base_path, sizeof(base_path));
571 memcpy(conf_keys[i] + sizeof(base_path), padkey, sizeof(padkey));
572 memcpy(conf_keys[i] + sizeof(base_path) + sizeof(padkey), axes, sizeof(axes));
573 memcpy(conf_keys[i] + sizeof(base_path) + sizeof(padkey) + sizeof(axes), axname, namelen);
574 memcpy(conf_keys[i] + sizeof(base_path) + sizeof(padkey) + sizeof(axes) + namelen, is_pos ? positive : negative, sizeof(positive));
575 conf_keys[i][sizeof(base_path) + sizeof(padkey) + sizeof(axes) + namelen + sizeof(positive)] = 0;
553 } else { 576 } else {
554 labels[i] = get_button_label(&selected_controller_info, binds[i]); 577 labels[i] = get_button_label(&selected_controller_info, binds[i]);
555 char template[] = "bindings\0pads\00\0buttons\0"; 578 static const char* dpdirs[] = {"up", "down", "left", "right"};
556 const char *but_name = SDL_GameControllerGetStringForButton(binds[i]); 579 const char *but_name, *mid;
580 size_t midsz;
581 if (binds[i] < SDL_CONTROLLER_BUTTON_DPAD_UP) {
582 but_name = SDL_GameControllerGetStringForButton(binds[i]);
583 mid = buttons;
584 midsz = sizeof(buttons);
585 } else {
586 but_name = dpdirs[binds[i] - SDL_CONTROLLER_BUTTON_DPAD_UP];
587 mid = dpads;
588 midsz = sizeof(dpads);
589 }
557 size_t namelen = strlen(but_name); 590 size_t namelen = strlen(but_name);
558 conf_keys[i] = malloc(sizeof(base_path) + sizeof(padkey) + sizeof(buttons) + namelen + 2); 591 conf_keys[i] = malloc(sizeof(base_path) + sizeof(padkey) + midsz + namelen + 2);
559 memcpy(conf_keys[i], base_path, sizeof(base_path)); 592 memcpy(conf_keys[i], base_path, sizeof(base_path));
560 memcpy(conf_keys[i] + sizeof(base_path), padkey, sizeof(padkey)); 593 memcpy(conf_keys[i] + sizeof(base_path), padkey, sizeof(padkey));
561 memcpy(conf_keys[i] + sizeof(base_path) + sizeof(padkey), buttons, sizeof(buttons)); 594 memcpy(conf_keys[i] + sizeof(base_path) + sizeof(padkey), mid, midsz);
562 595
563 memcpy(conf_keys[i] + sizeof(base_path) + sizeof(padkey) + sizeof(buttons), but_name, namelen+1); 596 memcpy(conf_keys[i] + sizeof(base_path) + sizeof(padkey) + midsz, but_name, namelen+1);
564 conf_keys[i][sizeof(base_path) + sizeof(padkey) + sizeof(buttons) + namelen + 1] = 0; 597 conf_keys[i][sizeof(base_path) + sizeof(padkey) + sizeof(buttons) + namelen + 1] = 0;
565 } 598 }
566 if (!labels[i]) { 599 if (!labels[i]) {
567 skipped++; 600 skipped++;
568 continue; 601 continue;
579 { 612 {
580 if (!labels[i]) { 613 if (!labels[i]) {
581 continue; 614 continue;
582 } 615 }
583 nk_label(context, labels[i], NK_TEXT_LEFT); 616 nk_label(context, labels[i], NK_TEXT_LEFT);
584 nk_button_label(context, i & 1 ? "Internal Screenshot" : "A"); 617 if (conf_keys[i]) {
618 char *assigned = tern_find_path(config, conf_keys[i], TVAL_PTR).ptrval;
619 if (!assigned) {
620 assigned = "None";
621 } else if (!memcmp("gamepads.", assigned, strlen("gamepads."))) {
622 assigned += strlen("gamepads.0.");
623 }
624 nk_button_label(context, assigned);
625 } else {
626 nk_button_label(context, i & 1 ? "Internal Screenshot" : "A");
627 }
585 free(conf_keys[i]); 628 free(conf_keys[i]);
586 } 629 }
587 nk_group_end(context); 630 nk_group_end(context);
588 } 631 }
589 632