comparison nuklear_ui/blastem_nuklear.c @ 1666:a1e0ed70ad82

Allow skipping buttons/axes in controller SDL2 mapping UI
author Mike Pavone <pavone@retrodev.com>
date Mon, 31 Dec 2018 22:51:05 -0800
parents cc5107def372
children 12d0c7c4ad80
comparison
equal deleted inserted replaced
1665:2721949b372d 1666:a1e0ed70ad82
1147 1147
1148 #define QUIET_FRAMES 9 1148 #define QUIET_FRAMES 9
1149 static void view_controller_mappings(struct nk_context *context) 1149 static void view_controller_mappings(struct nk_context *context)
1150 { 1150 {
1151 char buffer[512]; 1151 char buffer[512];
1152 static int quiet; 1152 static int quiet, button_a = -1, button_a_axis = -1;
1153 uint8_t added_mapping = 0; 1153 uint8_t added_mapping = 0;
1154 if (nk_begin(context, "Controllers", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) { 1154 if (nk_begin(context, "Controllers", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) {
1155 nk_layout_row_static(context, render_height() - context->style.font->height, render_width() - context->style.font->height, 1); 1155
1156 nk_layout_space_begin(context, NK_STATIC, render_height() - context->style.font->height, 3);
1157
1156 if (current_button < SDL_CONTROLLER_BUTTON_MAX) { 1158 if (current_button < SDL_CONTROLLER_BUTTON_MAX) {
1157 snprintf(buffer, sizeof(buffer), "Press Button %s", get_button_label(&selected_controller_info, current_button)); 1159 snprintf(buffer, sizeof(buffer), "Press Button %s", get_button_label(&selected_controller_info, current_button));
1158 } else { 1160 } else {
1159 snprintf(buffer, sizeof(buffer), "Move Axis %s", get_axis_label(&selected_controller_info, current_axis)); 1161 snprintf(buffer, sizeof(buffer), "Move Axis %s", get_axis_label(&selected_controller_info, current_axis));
1160 } 1162 }
1163
1164 float height = context->style.font->height * 1.25;
1165 float top = render_height()/2 - 1.5 * height;
1166 float width = render_width() - context->style.font->height;
1167
1168 nk_layout_space_push(context, nk_rect(0, top, width, height));
1161 nk_label(context, buffer, NK_TEXT_CENTERED); 1169 nk_label(context, buffer, NK_TEXT_CENTERED);
1170 if (current_button > SDL_CONTROLLER_BUTTON_B) {
1171 nk_layout_space_push(context, nk_rect(0, top + height, width, height));
1172 nk_label(context, "OR", NK_TEXT_CENTERED);
1173
1174 nk_layout_space_push(context, nk_rect(0, top + 2.0 * height, width, height));
1175 snprintf(buffer, sizeof(buffer), "Press Button %s to skip", get_button_label(&selected_controller_info, SDL_CONTROLLER_BUTTON_A));
1176 nk_label(context, buffer, NK_TEXT_CENTERED);
1177 }
1178
1179 nk_layout_space_end(context);
1162 if (quiet) { 1180 if (quiet) {
1163 --quiet; 1181 --quiet;
1164 } else { 1182 } else {
1165 if (button_pressed >= 0 && button_pressed != last_button) { 1183 if (button_pressed >= 0 && button_pressed != last_button) {
1166 start_mapping(); 1184 if (current_button <= SDL_CONTROLLER_BUTTON_B || button_pressed != button_a) {
1167 mapping_string[mapping_pos++] = 'b'; 1185 start_mapping();
1168 if (button_pressed > 9) { 1186 mapping_string[mapping_pos++] = 'b';
1169 mapping_string[mapping_pos++] = '0' + button_pressed / 10; 1187 if (button_pressed > 9) {
1188 mapping_string[mapping_pos++] = '0' + button_pressed / 10;
1189 }
1190 mapping_string[mapping_pos++] = '0' + button_pressed % 10;
1191 last_button = button_pressed;
1192 if (current_button == SDL_CONTROLLER_BUTTON_A) {
1193 button_a = button_pressed;
1194 }
1170 } 1195 }
1171 mapping_string[mapping_pos++] = '0' + button_pressed % 10;
1172 added_mapping = 1; 1196 added_mapping = 1;
1173 last_button = button_pressed;
1174 } else if (hat_moved >= 0 && hat_value && (hat_moved != last_hat || hat_value != last_hat_value)) { 1197 } else if (hat_moved >= 0 && hat_value && (hat_moved != last_hat || hat_value != last_hat_value)) {
1175 start_mapping(); 1198 start_mapping();
1176 mapping_string[mapping_pos++] = 'h'; 1199 mapping_string[mapping_pos++] = 'h';
1177 mapping_string[mapping_pos++] = '0' + hat_moved; 1200 mapping_string[mapping_pos++] = '0' + hat_moved;
1178 mapping_string[mapping_pos++] = '.'; 1201 mapping_string[mapping_pos++] = '.';
1180 added_mapping = 1; 1203 added_mapping = 1;
1181 1204
1182 last_hat = hat_moved; 1205 last_hat = hat_moved;
1183 last_hat_value = hat_value; 1206 last_hat_value = hat_value;
1184 } else if (axis_moved >= 0 && abs(axis_value) > 1000 && axis_moved != last_axis) { 1207 } else if (axis_moved >= 0 && abs(axis_value) > 1000 && axis_moved != last_axis) {
1185 start_mapping(); 1208 if (current_button <= SDL_CONTROLLER_BUTTON_B || axis_moved != button_a_axis) {
1186 mapping_string[mapping_pos++] = 'a'; 1209 start_mapping();
1187 if (axis_moved > 9) { 1210 mapping_string[mapping_pos++] = 'a';
1188 mapping_string[mapping_pos++] = '0' + axis_moved / 10; 1211 if (axis_moved > 9) {
1212 mapping_string[mapping_pos++] = '0' + axis_moved / 10;
1213 }
1214 mapping_string[mapping_pos++] = '0' + axis_moved % 10;
1215 last_axis = axis_moved;
1189 } 1216 }
1190 mapping_string[mapping_pos++] = '0' + axis_moved % 10;
1191 added_mapping = 1; 1217 added_mapping = 1;
1192 last_axis = axis_moved;
1193 } 1218 }
1194 } 1219 }
1195 1220
1196 if (added_mapping) { 1221 if (added_mapping) {
1197 quiet = QUIET_FRAMES; 1222 quiet = QUIET_FRAMES;
1201 current_axis = 0; 1226 current_axis = 0;
1202 } 1227 }
1203 } else { 1228 } else {
1204 current_axis++; 1229 current_axis++;
1205 if (current_axis == SDL_CONTROLLER_AXIS_MAX) { 1230 if (current_axis == SDL_CONTROLLER_AXIS_MAX) {
1231 button_a = -1;
1232 button_a_axis = -1;
1206 mapping_string[mapping_pos] = 0; 1233 mapping_string[mapping_pos] = 0;
1207 save_controller_mapping(selected_controller, mapping_string); 1234 save_controller_mapping(selected_controller, mapping_string);
1208 free(mapping_string); 1235 free(mapping_string);
1209 pop_view(); 1236 pop_view();
1210 push_view(view_controller_bindings); 1237 push_view(view_controller_bindings);