Mercurial > repos > blastem
comparison nuklear_ui/blastem_nuklear.c @ 1602:b452887f85b4
Basic UI for selecting layout variants + minor fix to SDL2 mapping generation UI
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Tue, 31 Jul 2018 22:19:56 -0700 |
parents | da04dd9c89ad |
children | 68b05322d971 |
comparison
equal
deleted
inserted
replaced
1601:da04dd9c89ad | 1602:b452887f85b4 |
---|---|
714 memcpy(mapping_string + mapping_pos, name, namesz); | 714 memcpy(mapping_string + mapping_pos, name, namesz); |
715 mapping_pos += namesz; | 715 mapping_pos += namesz; |
716 mapping_string[mapping_pos++] = ':'; | 716 mapping_string[mapping_pos++] = ':'; |
717 } | 717 } |
718 | 718 |
719 #define QUIET_FRAMES 5 | |
719 static void view_controller_mappings(struct nk_context *context) | 720 static void view_controller_mappings(struct nk_context *context) |
720 { | 721 { |
721 char buffer[512]; | 722 char buffer[512]; |
723 static int quiet; | |
722 uint8_t added_mapping = 0; | 724 uint8_t added_mapping = 0; |
723 if (nk_begin(context, "Controllers", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) { | 725 if (nk_begin(context, "Controllers", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) { |
724 nk_layout_row_static(context, render_height() - context->style.font->height, render_width() - context->style.font->height, 1); | 726 nk_layout_row_static(context, render_height() - context->style.font->height, render_width() - context->style.font->height, 1); |
725 if (current_button < SDL_CONTROLLER_BUTTON_MAX) { | 727 if (current_button < SDL_CONTROLLER_BUTTON_MAX) { |
726 snprintf(buffer, sizeof(buffer), "Press Button %s", get_button_label(&selected_controller_info, current_button)); | 728 snprintf(buffer, sizeof(buffer), "Press Button %s", get_button_label(&selected_controller_info, current_button)); |
727 } else { | 729 } else { |
728 snprintf(buffer, sizeof(buffer), "Move Axis %s", get_axis_label(&selected_controller_info, current_axis)); | 730 snprintf(buffer, sizeof(buffer), "Move Axis %s", get_axis_label(&selected_controller_info, current_axis)); |
729 } | 731 } |
730 nk_label(context, buffer, NK_TEXT_CENTERED); | 732 nk_label(context, buffer, NK_TEXT_CENTERED); |
731 if (button_pressed >= 0 && button_pressed != last_button) { | 733 if (quiet) { |
732 start_mapping(); | 734 --quiet; |
733 mapping_string[mapping_pos++] = 'b'; | 735 } else { |
734 if (button_pressed > 9) { | 736 if (button_pressed >= 0 && button_pressed != last_button) { |
735 mapping_string[mapping_pos++] = '0' + button_pressed / 10; | 737 start_mapping(); |
736 } | 738 mapping_string[mapping_pos++] = 'b'; |
737 mapping_string[mapping_pos++] = '0' + button_pressed % 10; | 739 if (button_pressed > 9) { |
738 added_mapping = 1; | 740 mapping_string[mapping_pos++] = '0' + button_pressed / 10; |
739 last_button = button_pressed; | 741 } |
740 } else if (hat_moved >= 0 && hat_value && (hat_moved != last_hat || hat_value != last_hat_value)) { | 742 mapping_string[mapping_pos++] = '0' + button_pressed % 10; |
741 start_mapping(); | 743 added_mapping = 1; |
742 mapping_string[mapping_pos++] = 'h'; | 744 last_button = button_pressed; |
743 mapping_string[mapping_pos++] = '0' + hat_moved; | 745 } else if (hat_moved >= 0 && hat_value && (hat_moved != last_hat || hat_value != last_hat_value)) { |
744 mapping_string[mapping_pos++] = '.'; | 746 start_mapping(); |
745 mapping_string[mapping_pos++] = '0' + hat_value; | 747 mapping_string[mapping_pos++] = 'h'; |
746 added_mapping = 1; | 748 mapping_string[mapping_pos++] = '0' + hat_moved; |
749 mapping_string[mapping_pos++] = '.'; | |
750 mapping_string[mapping_pos++] = '0' + hat_value; | |
751 added_mapping = 1; | |
752 | |
753 last_hat = hat_moved; | |
754 last_hat_value = hat_value; | |
755 } else if (axis_moved >= 0 && abs(axis_value) > 1000 && axis_moved != last_axis) { | |
756 start_mapping(); | |
757 mapping_string[mapping_pos++] = 'a'; | |
758 if (axis_moved > 9) { | |
759 mapping_string[mapping_pos++] = '0' + axis_moved / 10; | |
760 } | |
761 mapping_string[mapping_pos++] = '0' + axis_moved % 10; | |
762 added_mapping = 1; | |
763 last_axis = axis_moved; | |
764 } | |
765 } | |
747 | 766 |
748 last_hat = hat_moved; | |
749 last_hat_value = hat_value; | |
750 } else if (axis_moved >= 0 && abs(axis_value) > 1000 && axis_moved != last_axis) { | |
751 start_mapping(); | |
752 mapping_string[mapping_pos++] = 'a'; | |
753 if (axis_moved > 9) { | |
754 mapping_string[mapping_pos++] = '0' + axis_moved / 10; | |
755 } | |
756 mapping_string[mapping_pos++] = '0' + axis_moved % 10; | |
757 added_mapping = 1; | |
758 last_axis = axis_moved; | |
759 } | |
760 if (added_mapping) { | 767 if (added_mapping) { |
768 quiet = QUIET_FRAMES; | |
761 if (current_button < SDL_CONTROLLER_BUTTON_MAX) { | 769 if (current_button < SDL_CONTROLLER_BUTTON_MAX) { |
762 current_button++; | 770 current_button++; |
763 if (current_button == SDL_CONTROLLER_BUTTON_MAX) { | 771 if (current_button == SDL_CONTROLLER_BUTTON_MAX) { |
764 current_axis = 0; | 772 current_axis = 0; |
765 } | 773 } |
779 axis_moved = -1; | 787 axis_moved = -1; |
780 nk_end(context); | 788 nk_end(context); |
781 } | 789 } |
782 } | 790 } |
783 | 791 |
784 void controller_type_group(struct nk_context *context, char *name, int type_id, int first_subtype_id, const char **types, uint32_t num_types) | 792 static void view_controller_variant(struct nk_context *context) |
793 { | |
794 uint8_t selected = 0; | |
795 if (nk_begin(context, "Controller Type", nk_rect(0, 0, render_width(), render_height()), 0)) { | |
796 nk_layout_row_static(context, context->style.font->height*1.25, render_width() - context->style.font->height * 2, 1); | |
797 nk_label(context, "", NK_TEXT_CENTERED); | |
798 nk_label(context, "Select the layout that", NK_TEXT_CENTERED); | |
799 nk_label(context, "best matches your controller", NK_TEXT_CENTERED); | |
800 nk_label(context, "", NK_TEXT_CENTERED); | |
801 if (nk_button_label(context, "4 face buttons")) { | |
802 selected_controller_info.variant = VARIANT_NORMAL; | |
803 selected = 1; | |
804 } | |
805 char buffer[512]; | |
806 snprintf(buffer, sizeof(buffer), "6 face buttons including %s and %s", | |
807 get_button_label(&selected_controller_info, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER), | |
808 get_axis_label(&selected_controller_info, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) | |
809 ); | |
810 if (nk_button_label(context, buffer)) { | |
811 selected_controller_info.variant = VARIANT_6B_RIGHT; | |
812 selected = 1; | |
813 } | |
814 snprintf(buffer, sizeof(buffer), "6 face buttons including %s and %s", | |
815 get_button_label(&selected_controller_info, SDL_CONTROLLER_BUTTON_LEFTSHOULDER), | |
816 get_button_label(&selected_controller_info, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) | |
817 ); | |
818 if (nk_button_label(context, buffer)) { | |
819 selected_controller_info.variant = VARIANT_6B_BUMPERS; | |
820 selected = 1; | |
821 } | |
822 nk_end(context); | |
823 } | |
824 if (selected) { | |
825 save_controller_info(selected_controller, &selected_controller_info); | |
826 pop_view(); | |
827 SDL_GameController *controller = render_get_controller(selected_controller); | |
828 if (controller) { | |
829 push_view(view_controller_bindings); | |
830 SDL_GameControllerClose(controller); | |
831 } else { | |
832 current_button = SDL_CONTROLLER_BUTTON_A; | |
833 button_pressed = -1; | |
834 last_button = -1; | |
835 last_hat = -1; | |
836 axis_moved = -1; | |
837 last_axis = -1; | |
838 SDL_Joystick *joy = render_get_joystick(selected_controller); | |
839 const char *name = SDL_JoystickName(joy); | |
840 size_t namesz = strlen(name); | |
841 mapping_string = malloc(512 + namesz); | |
842 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joy), mapping_string, 33); | |
843 mapping_string[32] = ','; | |
844 memcpy(mapping_string + 33, name, namesz); | |
845 mapping_pos = 33+namesz; | |
846 | |
847 push_view(view_controller_mappings); | |
848 } | |
849 } | |
850 } | |
851 | |
852 static void controller_type_group(struct nk_context *context, char *name, int type_id, int first_subtype_id, const char **types, uint32_t num_types) | |
785 { | 853 { |
786 nk_layout_row_static(context, (context->style.font->height + 3) * num_types + context->style.font->height, render_width() - 80, 1); | 854 nk_layout_row_static(context, (context->style.font->height + 3) * num_types + context->style.font->height, render_width() - 80, 1); |
787 if (nk_group_begin(context, name, NK_WINDOW_TITLE)) { | 855 if (nk_group_begin(context, name, NK_WINDOW_TITLE)) { |
788 nk_layout_row_static(context, context->style.font->height, render_width()/2 - 80, 2); | 856 nk_layout_row_static(context, context->style.font->height, render_width()/2 - 80, 2); |
789 for (int i = 0; i < num_types; i++) | 857 for (int i = 0; i < num_types; i++) |
790 { | 858 { |
791 if (nk_button_label(context, types[i])) { | 859 if (nk_button_label(context, types[i])) { |
860 selected_controller_info.type = type_id; | |
792 selected_controller_info.subtype = first_subtype_id + i; | 861 selected_controller_info.subtype = first_subtype_id + i; |
793 save_controller_info(selected_controller, &selected_controller_info); | |
794 selected_controller_info = get_controller_info(selected_controller); | |
795 pop_view(); | 862 pop_view(); |
796 SDL_GameController *controller = render_get_controller(selected_controller); | 863 push_view(view_controller_variant); |
797 if (controller) { | |
798 push_view(view_controller_bindings); | |
799 SDL_GameControllerClose(controller); | |
800 } else { | |
801 current_button = SDL_CONTROLLER_BUTTON_A; | |
802 button_pressed = -1; | |
803 last_button = -1; | |
804 last_hat = -1; | |
805 axis_moved = -1; | |
806 last_axis = -1; | |
807 SDL_Joystick *joy = render_get_joystick(selected_controller); | |
808 const char *name = SDL_JoystickName(joy); | |
809 size_t namesz = strlen(name); | |
810 mapping_string = malloc(512 + namesz); | |
811 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joy), mapping_string, 33); | |
812 mapping_string[32] = ','; | |
813 memcpy(mapping_string + 33, name, namesz); | |
814 mapping_pos = 33+namesz; | |
815 | |
816 push_view(view_controller_mappings); | |
817 } | |
818 } | 864 } |
819 } | 865 } |
820 nk_group_end(context); | 866 nk_group_end(context); |
821 } | 867 } |
822 } | 868 } |