Mercurial > repos > blastem
comparison nuklear_ui/blastem_nuklear.c @ 1601:da04dd9c89ad
SDL2 mapping UI now handles axes
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Tue, 31 Jul 2018 09:07:23 -0700 |
parents | 7f39c40b4b25 |
children | b452887f85b4 |
comparison
equal
deleted
inserted
replaced
1600:7f39c40b4b25 | 1601:da04dd9c89ad |
---|---|
692 nk_end(context); | 692 nk_end(context); |
693 } | 693 } |
694 } | 694 } |
695 | 695 |
696 static int current_button; | 696 static int current_button; |
697 static int current_axis; | |
697 static int button_pressed, last_button; | 698 static int button_pressed, last_button; |
698 static int hat_moved, hat_value, last_hat, last_hat_value; | 699 static int hat_moved, hat_value, last_hat, last_hat_value; |
700 static int axis_moved, axis_value, last_axis; | |
699 static char *mapping_string; | 701 static char *mapping_string; |
700 static size_t mapping_pos; | 702 static size_t mapping_pos; |
701 void view_controller_mappings(struct nk_context *context) | 703 |
704 static void start_mapping(void) | |
705 { | |
706 const char *name; | |
707 mapping_string[mapping_pos++] = ','; | |
708 if (current_button != SDL_CONTROLLER_BUTTON_MAX) { | |
709 name = SDL_GameControllerGetStringForButton(current_button); | |
710 } else { | |
711 name = SDL_GameControllerGetStringForAxis(current_axis); | |
712 } | |
713 size_t namesz = strlen(name); | |
714 memcpy(mapping_string + mapping_pos, name, namesz); | |
715 mapping_pos += namesz; | |
716 mapping_string[mapping_pos++] = ':'; | |
717 } | |
718 | |
719 static void view_controller_mappings(struct nk_context *context) | |
702 { | 720 { |
703 char buffer[512]; | 721 char buffer[512]; |
704 uint8_t added_mapping = 0; | 722 uint8_t added_mapping = 0; |
705 if (nk_begin(context, "Controllers", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) { | 723 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); | 724 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)); | 725 if (current_button < SDL_CONTROLLER_BUTTON_MAX) { |
726 snprintf(buffer, sizeof(buffer), "Press Button %s", get_button_label(&selected_controller_info, current_button)); | |
727 } else { | |
728 snprintf(buffer, sizeof(buffer), "Move Axis %s", get_axis_label(&selected_controller_info, current_axis)); | |
729 } | |
708 nk_label(context, buffer, NK_TEXT_CENTERED); | 730 nk_label(context, buffer, NK_TEXT_CENTERED); |
709 if (button_pressed >= 0 && button_pressed != last_button) { | 731 if (button_pressed >= 0 && button_pressed != last_button) { |
710 mapping_string[mapping_pos++] = ','; | 732 start_mapping(); |
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'; | 733 mapping_string[mapping_pos++] = 'b'; |
717 if (button_pressed > 9) { | 734 if (button_pressed > 9) { |
718 mapping_string[mapping_pos++] = '0' + button_pressed / 10; | 735 mapping_string[mapping_pos++] = '0' + button_pressed / 10; |
719 } | 736 } |
720 mapping_string[mapping_pos++] = '0' + button_pressed % 10; | 737 mapping_string[mapping_pos++] = '0' + button_pressed % 10; |
721 added_mapping = 1; | 738 added_mapping = 1; |
722 last_button = button_pressed; | 739 last_button = button_pressed; |
723 } | 740 } else if (hat_moved >= 0 && hat_value && (hat_moved != last_hat || hat_value != last_hat_value)) { |
724 else if (hat_moved >= 0 && hat_value && (hat_moved != last_hat || hat_value != last_hat_value)) { | 741 start_mapping(); |
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'; | 742 mapping_string[mapping_pos++] = 'h'; |
732 mapping_string[mapping_pos++] = '0' + hat_moved; | 743 mapping_string[mapping_pos++] = '0' + hat_moved; |
733 mapping_string[mapping_pos++] = '.'; | 744 mapping_string[mapping_pos++] = '.'; |
734 mapping_string[mapping_pos++] = '0' + hat_value; | 745 mapping_string[mapping_pos++] = '0' + hat_value; |
735 added_mapping = 1; | 746 added_mapping = 1; |
736 | 747 |
737 last_hat = hat_moved; | 748 last_hat = hat_moved; |
738 last_hat_value = hat_value; | 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; | |
739 } | 759 } |
740 if (added_mapping) { | 760 if (added_mapping) { |
741 current_button++; | 761 if (current_button < SDL_CONTROLLER_BUTTON_MAX) { |
742 if (current_button == SDL_CONTROLLER_BUTTON_MAX) { | 762 current_button++; |
743 mapping_string[mapping_pos] = 0; | 763 if (current_button == SDL_CONTROLLER_BUTTON_MAX) { |
744 save_controller_mapping(selected_controller, mapping_string); | 764 current_axis = 0; |
745 free(mapping_string); | 765 } |
746 pop_view(); | 766 } else { |
747 push_view(view_controller_bindings); | 767 current_axis++; |
748 | 768 if (current_axis == SDL_CONTROLLER_AXIS_MAX) { |
769 mapping_string[mapping_pos] = 0; | |
770 save_controller_mapping(selected_controller, mapping_string); | |
771 free(mapping_string); | |
772 pop_view(); | |
773 push_view(view_controller_bindings); | |
774 } | |
749 } | 775 } |
750 } | 776 } |
751 button_pressed = -1; | 777 button_pressed = -1; |
752 hat_moved = -1; | 778 hat_moved = -1; |
779 axis_moved = -1; | |
753 nk_end(context); | 780 nk_end(context); |
754 } | 781 } |
755 } | 782 } |
756 | 783 |
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) | 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) |
772 SDL_GameControllerClose(controller); | 799 SDL_GameControllerClose(controller); |
773 } else { | 800 } else { |
774 current_button = SDL_CONTROLLER_BUTTON_A; | 801 current_button = SDL_CONTROLLER_BUTTON_A; |
775 button_pressed = -1; | 802 button_pressed = -1; |
776 last_button = -1; | 803 last_button = -1; |
804 last_hat = -1; | |
805 axis_moved = -1; | |
806 last_axis = -1; | |
777 SDL_Joystick *joy = render_get_joystick(selected_controller); | 807 SDL_Joystick *joy = render_get_joystick(selected_controller); |
778 const char *name = SDL_JoystickName(joy); | 808 const char *name = SDL_JoystickName(joy); |
779 size_t namesz = strlen(name); | 809 size_t namesz = strlen(name); |
780 mapping_string = malloc(512 + namesz); | 810 mapping_string = malloc(512 + namesz); |
781 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joy), mapping_string, 33); | 811 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joy), mapping_string, 33); |
1307 } | 1337 } |
1308 else if (event->type == SDL_JOYBUTTONDOWN) { | 1338 else if (event->type == SDL_JOYBUTTONDOWN) { |
1309 button_pressed = event->jbutton.button; | 1339 button_pressed = event->jbutton.button; |
1310 } | 1340 } |
1311 else if (event->type == SDL_JOYHATMOTION) { | 1341 else if (event->type == SDL_JOYHATMOTION) { |
1312 hat_moved = event->jhat.which; | 1342 hat_moved = event->jhat.hat; |
1313 hat_value = event->jhat.value; | 1343 hat_value = event->jhat.value; |
1344 } | |
1345 else if (event->type == SDL_JOYAXISMOTION) { | |
1346 if (event->jaxis.axis == axis_moved || abs(event->jaxis.value) > abs(axis_value) || abs(event->jaxis.value) > 1000) { | |
1347 axis_moved = event->jaxis.axis; | |
1348 axis_value = event->jaxis.value; | |
1349 } | |
1314 } | 1350 } |
1315 nk_sdl_handle_event(event); | 1351 nk_sdl_handle_event(event); |
1316 } | 1352 } |
1317 | 1353 |
1318 static void context_destroyed(void) | 1354 static void context_destroyed(void) |