Mercurial > repos > blastem
comparison nuklear_ui/blastem_nuklear.c @ 1599:1fc61c844ec5
Allow selecting controller type when controllers have an SDL 2 mapping, but heuristics fail to idenify details
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 27 Jul 2018 22:40:56 -0700 |
parents | 5e2af89c3467 |
children | 7f39c40b4b25 |
comparison
equal
deleted
inserted
replaced
1598:5e2af89c3467 | 1599:1fc61c844ec5 |
---|---|
514 #define LEFTSTICK 0x10000000 | 514 #define LEFTSTICK 0x10000000 |
515 #define RIGHTSTICK 0x20000000 | 515 #define RIGHTSTICK 0x20000000 |
516 enum { | 516 enum { |
517 UP,DOWN,LEFT,RIGHT | 517 UP,DOWN,LEFT,RIGHT |
518 }; | 518 }; |
519 void binding_box(struct nk_context *context, char *name, float x, float y, float width, int num_binds, int *binds) | 519 |
520 static char * config_ps_names[] = { | |
521 [SDL_CONTROLLER_BUTTON_A] = "cross", | |
522 [SDL_CONTROLLER_BUTTON_B] = "circle", | |
523 [SDL_CONTROLLER_BUTTON_X] = "square", | |
524 [SDL_CONTROLLER_BUTTON_Y] = "triangle", | |
525 [SDL_CONTROLLER_BUTTON_BACK] = "share", | |
526 [SDL_CONTROLLER_BUTTON_START] = "options", | |
527 [SDL_CONTROLLER_BUTTON_LEFTSHOULDER] = "l1", | |
528 [SDL_CONTROLLER_BUTTON_RIGHTSHOULDER] = "r1", | |
529 [SDL_CONTROLLER_BUTTON_LEFTSTICK] = "l3", | |
530 [SDL_CONTROLLER_BUTTON_RIGHTSTICK] = "r3", | |
531 }; | |
532 | |
533 static void binding_box(struct nk_context *context, char *name, float x, float y, float width, int num_binds, int *binds) | |
520 { | 534 { |
521 const struct nk_user_font *font = context->style.font; | 535 const struct nk_user_font *font = context->style.font; |
522 float row_height = font->height * 2; | 536 float row_height = font->height * 2; |
523 | 537 |
524 nk_layout_space_push(context, nk_rect(x, y, width, num_binds * (row_height + 4) + 4)); | |
525 nk_group_begin(context, name, NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR); | |
526 | |
527 char const **labels = calloc(sizeof(char *), num_binds); | 538 char const **labels = calloc(sizeof(char *), num_binds); |
539 char **conf_keys = calloc(sizeof(char *), num_binds); | |
528 float max_width = 0.0f; | 540 float max_width = 0.0f; |
541 | |
542 static const char base_path[] = "bindings\0pads"; | |
543 static const char buttons[] = "buttons"; | |
544 char padkey[] = {'0' + selected_controller, 0}; | |
545 int skipped = 0; | |
529 for (int i = 0; i < num_binds; i++) | 546 for (int i = 0; i < num_binds; i++) |
530 { | 547 { |
531 if (binds[i] & AXIS) { | 548 if (binds[i] & AXIS) { |
532 labels[i] = get_axis_label(&selected_controller_info, binds[i] & ~AXIS); | 549 labels[i] = get_axis_label(&selected_controller_info, binds[i] & ~AXIS); |
533 } else if (binds[i] & STICKDIR) { | 550 } else if (binds[i] & STICKDIR) { |
534 static char const * dirs[] = {"Up", "Down", "Left", "Right"}; | 551 static char const * dirs[] = {"Up", "Down", "Left", "Right"}; |
535 labels[i] = dirs[binds[i] & 3]; | 552 labels[i] = dirs[binds[i] & 3]; |
536 } else { | 553 } else { |
537 labels[i] = get_button_label(&selected_controller_info, binds[i]); | 554 labels[i] = get_button_label(&selected_controller_info, binds[i]); |
555 char template[] = "bindings\0pads\00\0buttons\0"; | |
556 const char *but_name = SDL_GameControllerGetStringForButton(binds[i]); | |
557 size_t namelen = strlen(but_name); | |
558 conf_keys[i] = malloc(sizeof(base_path) + sizeof(padkey) + sizeof(buttons) + namelen + 2); | |
559 memcpy(conf_keys[i], base_path, sizeof(base_path)); | |
560 memcpy(conf_keys[i] + sizeof(base_path), padkey, sizeof(padkey)); | |
561 memcpy(conf_keys[i] + sizeof(base_path) + sizeof(padkey), buttons, sizeof(buttons)); | |
562 | |
563 memcpy(conf_keys[i] + sizeof(base_path) + sizeof(padkey) + sizeof(buttons), but_name, namelen+1); | |
564 conf_keys[i][sizeof(base_path) + sizeof(padkey) + sizeof(buttons) + namelen + 1] = 0; | |
565 } | |
566 if (!labels[i]) { | |
567 skipped++; | |
568 continue; | |
538 } | 569 } |
539 float lb_width = font->width(font->userdata, font->height, labels[i], strlen(labels[i])); | 570 float lb_width = font->width(font->userdata, font->height, labels[i], strlen(labels[i])); |
540 max_width = max_width < lb_width ? lb_width : max_width; | 571 max_width = max_width < lb_width ? lb_width : max_width; |
541 } | 572 } |
573 nk_layout_space_push(context, nk_rect(x, y, width, (num_binds - skipped) * (row_height + 4) + 4)); | |
574 nk_group_begin(context, name, NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR); | |
575 | |
542 float widths[] = {max_width + 3, width - (max_width + 6)}; | 576 float widths[] = {max_width + 3, width - (max_width + 6)}; |
543 nk_layout_row(context, NK_STATIC, row_height, 2, widths); | 577 nk_layout_row(context, NK_STATIC, row_height, 2, widths); |
544 for (int i = 0; i < num_binds; i++) | 578 for (int i = 0; i < num_binds; i++) |
545 { | 579 { |
580 if (!labels[i]) { | |
581 continue; | |
582 } | |
546 nk_label(context, labels[i], NK_TEXT_LEFT); | 583 nk_label(context, labels[i], NK_TEXT_LEFT); |
547 nk_button_label(context, i & 1 ? "Internal Screenshot" : "A"); | 584 nk_button_label(context, i & 1 ? "Internal Screenshot" : "A"); |
585 free(conf_keys[i]); | |
548 } | 586 } |
549 nk_group_end(context); | 587 nk_group_end(context); |
550 } | 588 } |
551 | 589 |
552 void view_controller_bindings(struct nk_context *context) | 590 void view_controller_bindings(struct nk_context *context) |
649 def_font->handle.height = orig_height; | 687 def_font->handle.height = orig_height; |
650 nk_layout_row_static(context, orig_height + 4, (render_width() - 2*orig_height) / 4, 1); | 688 nk_layout_row_static(context, orig_height + 4, (render_width() - 2*orig_height) / 4, 1); |
651 if (nk_button_label(context, "Back")) { | 689 if (nk_button_label(context, "Back")) { |
652 pop_view(); | 690 pop_view(); |
653 } | 691 } |
692 nk_end(context); | |
693 } | |
694 } | |
695 | |
696 void controller_type_group(struct nk_context *context, char *name, int type_id, int first_subtype_id, const char **types, uint32_t num_types) | |
697 { | |
698 nk_layout_row_static(context, (context->style.font->height + 3) * num_types + context->style.font->height, render_width() - 80, 1); | |
699 if (nk_group_begin(context, name, NK_WINDOW_TITLE)) { | |
700 nk_layout_row_static(context, context->style.font->height, render_width()/2 - 80, 2); | |
701 for (int i = 0; i < num_types; i++) | |
702 { | |
703 if (nk_button_label(context, types[i])) { | |
704 selected_controller_info.subtype = first_subtype_id + i; | |
705 save_controller_info(selected_controller, &selected_controller_info); | |
706 pop_view(); | |
707 push_view(view_controller_bindings); | |
708 } | |
709 } | |
710 nk_group_end(context); | |
711 } | |
712 } | |
713 | |
714 void view_controller_type(struct nk_context *context) | |
715 { | |
716 if (nk_begin(context, "Controller Type", nk_rect(0, 0, render_width(), render_height()), 0)) { | |
717 controller_type_group(context, "Xbox", TYPE_XBOX, SUBTYPE_XBOX, (const char *[]){ | |
718 "Original", "Xbox 360", "Xbox One" | |
719 }, 3); | |
720 controller_type_group(context, "Playstation", TYPE_PSX, SUBTYPE_PS3, (const char *[]){ | |
721 "PS3", "PS4" | |
722 }, 2); | |
723 controller_type_group(context, "Sega", TYPE_SEGA, SUBTYPE_GENESIS, (const char *[]){ | |
724 "Genesis", "Saturn" | |
725 }, 2); | |
726 controller_type_group(context, "Nintendo", TYPE_NINTENDO, SUBTYPE_WIIU, (const char *[]){ | |
727 "WiiU", "Switch" | |
728 }, 2); | |
654 nk_end(context); | 729 nk_end(context); |
655 } | 730 } |
656 } | 731 } |
657 | 732 |
658 void view_controllers(struct nk_context *context) | 733 void view_controllers(struct nk_context *context) |
665 SDL_Joystick *joy = render_get_joystick(i); | 740 SDL_Joystick *joy = render_get_joystick(i); |
666 if (joy) { | 741 if (joy) { |
667 controller_info info = get_controller_info(i); | 742 controller_info info = get_controller_info(i); |
668 nk_layout_row_begin(context, NK_STATIC, height, 2); | 743 nk_layout_row_begin(context, NK_STATIC, height, 2); |
669 nk_layout_row_push(context, image_width); | 744 nk_layout_row_push(context, image_width); |
670 nk_image(context, controller_360_image); | 745 if (info.type == TYPE_UNKNOWN || info.type == TYPE_GENERIC_MAPPING) { |
746 nk_label(context, "?", NK_TEXT_CENTERED); | |
747 } else { | |
748 nk_image(context, controller_360_image); | |
749 } | |
671 nk_layout_row_push(context, render_width() - image_width - 2 * context->style.font->height); | 750 nk_layout_row_push(context, render_width() - image_width - 2 * context->style.font->height); |
672 if (nk_button_label(context, info.name)) { | 751 if (nk_button_label(context, info.name)) { |
673 selected_controller = i; | 752 selected_controller = i; |
674 selected_controller_info = info; | 753 selected_controller_info = info; |
675 push_view(view_controller_bindings); | 754 if (info.type == TYPE_UNKNOWN || info.type == TYPE_GENERIC_MAPPING) { |
755 push_view(view_controller_type); | |
756 } else { | |
757 push_view(view_controller_bindings); | |
758 } | |
759 | |
676 } | 760 } |
677 nk_layout_row_end(context); | 761 nk_layout_row_end(context); |
678 } | 762 } |
679 } | 763 } |
680 nk_layout_row_static(context, context->style.font->height, (render_width() - 2 * context->style.font->height) / 2, 2); | 764 nk_layout_row_static(context, context->style.font->height, (render_width() - 2 * context->style.font->height) / 2, 2); |