comparison nuklear_ui/blastem_nuklear.c @ 1600:7f39c40b4b25

WIP UI for creating an SDL2 mapping for controllers that don't have one
author Michael Pavone <pavone@retrodev.com>
date Mon, 30 Jul 2018 09:38:01 -0700
parents 1fc61c844ec5
children da04dd9c89ad
comparison
equal deleted inserted replaced
1599:1fc61c844ec5 1600:7f39c40b4b25
691 } 691 }
692 nk_end(context); 692 nk_end(context);
693 } 693 }
694 } 694 }
695 695
696 static int current_button;
697 static int button_pressed, last_button;
698 static int hat_moved, hat_value, last_hat, last_hat_value;
699 static char *mapping_string;
700 static size_t mapping_pos;
701 void view_controller_mappings(struct nk_context *context)
702 {
703 char buffer[512];
704 uint8_t added_mapping = 0;
705 if (nk_begin(context, "Controllers", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) {
706 nk_layout_row_static(context, render_height() - context->style.font->height, render_width() - context->style.font->height, 1);
707 snprintf(buffer, sizeof(buffer), "Press Button %s", get_button_label(&selected_controller_info, current_button));
708 nk_label(context, buffer, NK_TEXT_CENTERED);
709 if (button_pressed >= 0 && button_pressed != last_button) {
710 mapping_string[mapping_pos++] = ',';
711 const char *name = SDL_GameControllerGetStringForButton(current_button);
712 size_t namesz = strlen(name);
713 memcpy(mapping_string + mapping_pos, name, namesz);
714 mapping_pos += namesz;
715 mapping_string[mapping_pos++] = ':';
716 mapping_string[mapping_pos++] = 'b';
717 if (button_pressed > 9) {
718 mapping_string[mapping_pos++] = '0' + button_pressed / 10;
719 }
720 mapping_string[mapping_pos++] = '0' + button_pressed % 10;
721 added_mapping = 1;
722 last_button = button_pressed;
723 }
724 else if (hat_moved >= 0 && hat_value && (hat_moved != last_hat || hat_value != last_hat_value)) {
725 mapping_string[mapping_pos++] = ',';
726 const char *name = SDL_GameControllerGetStringForButton(current_button);
727 size_t namesz = strlen(name);
728 memcpy(mapping_string + mapping_pos, name, namesz);
729 mapping_pos += namesz;
730 mapping_string[mapping_pos++] = ':';
731 mapping_string[mapping_pos++] = 'h';
732 mapping_string[mapping_pos++] = '0' + hat_moved;
733 mapping_string[mapping_pos++] = '.';
734 mapping_string[mapping_pos++] = '0' + hat_value;
735 added_mapping = 1;
736
737 last_hat = hat_moved;
738 last_hat_value = hat_value;
739 }
740 if (added_mapping) {
741 current_button++;
742 if (current_button == SDL_CONTROLLER_BUTTON_MAX) {
743 mapping_string[mapping_pos] = 0;
744 save_controller_mapping(selected_controller, mapping_string);
745 free(mapping_string);
746 pop_view();
747 push_view(view_controller_bindings);
748
749 }
750 }
751 button_pressed = -1;
752 hat_moved = -1;
753 nk_end(context);
754 }
755 }
756
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) 757 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 { 758 {
698 nk_layout_row_static(context, (context->style.font->height + 3) * num_types + context->style.font->height, render_width() - 80, 1); 759 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)) { 760 if (nk_group_begin(context, name, NK_WINDOW_TITLE)) {
700 nk_layout_row_static(context, context->style.font->height, render_width()/2 - 80, 2); 761 nk_layout_row_static(context, context->style.font->height, render_width()/2 - 80, 2);
701 for (int i = 0; i < num_types; i++) 762 for (int i = 0; i < num_types; i++)
702 { 763 {
703 if (nk_button_label(context, types[i])) { 764 if (nk_button_label(context, types[i])) {
704 selected_controller_info.subtype = first_subtype_id + i; 765 selected_controller_info.subtype = first_subtype_id + i;
705 save_controller_info(selected_controller, &selected_controller_info); 766 save_controller_info(selected_controller, &selected_controller_info);
767 selected_controller_info = get_controller_info(selected_controller);
706 pop_view(); 768 pop_view();
707 push_view(view_controller_bindings); 769 SDL_GameController *controller = render_get_controller(selected_controller);
770 if (controller) {
771 push_view(view_controller_bindings);
772 SDL_GameControllerClose(controller);
773 } else {
774 current_button = SDL_CONTROLLER_BUTTON_A;
775 button_pressed = -1;
776 last_button = -1;
777 SDL_Joystick *joy = render_get_joystick(selected_controller);
778 const char *name = SDL_JoystickName(joy);
779 size_t namesz = strlen(name);
780 mapping_string = malloc(512 + namesz);
781 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joy), mapping_string, 33);
782 mapping_string[32] = ',';
783 memcpy(mapping_string + 33, name, namesz);
784 mapping_pos = 33+namesz;
785
786 push_view(view_controller_mappings);
787 }
708 } 788 }
709 } 789 }
710 nk_group_end(context); 790 nk_group_end(context);
711 } 791 }
712 } 792 }
1223 static void handle_event(SDL_Event *event) 1303 static void handle_event(SDL_Event *event)
1224 { 1304 {
1225 if (event->type == SDL_KEYDOWN) { 1305 if (event->type == SDL_KEYDOWN) {
1226 keycode = event->key.keysym.sym; 1306 keycode = event->key.keysym.sym;
1227 } 1307 }
1308 else if (event->type == SDL_JOYBUTTONDOWN) {
1309 button_pressed = event->jbutton.button;
1310 }
1311 else if (event->type == SDL_JOYHATMOTION) {
1312 hat_moved = event->jhat.which;
1313 hat_value = event->jhat.value;
1314 }
1228 nk_sdl_handle_event(event); 1315 nk_sdl_handle_event(event);
1229 } 1316 }
1230 1317
1231 static void context_destroyed(void) 1318 static void context_destroyed(void)
1232 { 1319 {