comparison nuklear_ui/blastem_nuklear.c @ 1859:52f136052ab0

Allow changing SDL2 mapping and controller type after initial configuration
author Michael Pavone <pavone@retrodev.com>
date Fri, 03 May 2019 18:33:26 -0700
parents dda7479f3bbb
children e07fc3d473b2
comparison
equal deleted inserted replaced
1858:dda7479f3bbb 1859:52f136052ab0
1173 memcpy(mapping_string + mapping_pos, name, namesz); 1173 memcpy(mapping_string + mapping_pos, name, namesz);
1174 mapping_pos += namesz; 1174 mapping_pos += namesz;
1175 mapping_string[mapping_pos++] = ':'; 1175 mapping_string[mapping_pos++] = ':';
1176 } 1176 }
1177 1177
1178 static uint8_t initial_controller_config;
1178 #define QUIET_FRAMES 9 1179 #define QUIET_FRAMES 9
1179 static void view_controller_mappings(struct nk_context *context) 1180 static void view_controller_mappings(struct nk_context *context)
1180 { 1181 {
1181 char buffer[512]; 1182 char buffer[512];
1182 static int quiet, button_a = -1, button_a_axis = -1; 1183 static int quiet, button_a = -1, button_a_axis = -1;
1275 button_a_axis = -1; 1276 button_a_axis = -1;
1276 mapping_string[mapping_pos] = 0; 1277 mapping_string[mapping_pos] = 0;
1277 save_controller_mapping(selected_controller, mapping_string); 1278 save_controller_mapping(selected_controller, mapping_string);
1278 free(mapping_string); 1279 free(mapping_string);
1279 pop_view(); 1280 pop_view();
1280 push_view(view_controller_bindings); 1281 if (initial_controller_config) {
1281 controller_binding_changed = 0; 1282 push_view(view_controller_bindings);
1283 controller_binding_changed = 0;
1284 }
1282 added_mapping = 0; 1285 added_mapping = 0;
1283 } else if (get_axis_label(&selected_controller_info, current_axis)) { 1286 } else if (get_axis_label(&selected_controller_info, current_axis)) {
1284 added_mapping = 0; 1287 added_mapping = 0;
1285 } 1288 }
1286 } 1289 }
1288 button_pressed = -1; 1291 button_pressed = -1;
1289 hat_moved = -1; 1292 hat_moved = -1;
1290 axis_moved = -1; 1293 axis_moved = -1;
1291 nk_end(context); 1294 nk_end(context);
1292 } 1295 }
1296 }
1297
1298 static void show_mapping_view(void)
1299 {
1300 current_button = SDL_CONTROLLER_BUTTON_A;
1301 button_pressed = -1;
1302 last_button = -1;
1303 last_hat = -1;
1304 axis_moved = -1;
1305 last_axis = -1;
1306 last_axis_value = 0;
1307 SDL_Joystick *joy = render_get_joystick(selected_controller);
1308 const char *name = SDL_JoystickName(joy);
1309 size_t namesz = strlen(name);
1310 mapping_string = malloc(512 + namesz);
1311 for (mapping_pos = 0; mapping_pos < namesz; mapping_pos++)
1312 {
1313 char c = name[mapping_pos];
1314 if (c == ',' || c == '\n' || c == '\r') {
1315 c = ' ';
1316 }
1317 mapping_string[mapping_pos] = c;
1318 }
1319
1320 push_view(view_controller_mappings);
1293 } 1321 }
1294 1322
1295 static void view_controller_variant(struct nk_context *context) 1323 static void view_controller_variant(struct nk_context *context)
1296 { 1324 {
1297 uint8_t selected = 0; 1325 uint8_t selected = 0;
1325 nk_end(context); 1353 nk_end(context);
1326 } 1354 }
1327 if (selected) { 1355 if (selected) {
1328 save_controller_info(selected_controller, &selected_controller_info); 1356 save_controller_info(selected_controller, &selected_controller_info);
1329 pop_view(); 1357 pop_view();
1330 SDL_GameController *controller = render_get_controller(selected_controller); 1358 if (initial_controller_config) {
1331 if (controller) { 1359 SDL_GameController *controller = render_get_controller(selected_controller);
1332 push_view(view_controller_bindings); 1360 if (controller) {
1333 controller_binding_changed = 0; 1361 push_view(view_controller_bindings);
1334 SDL_GameControllerClose(controller); 1362 controller_binding_changed = 0;
1335 } else { 1363 SDL_GameControllerClose(controller);
1336 current_button = SDL_CONTROLLER_BUTTON_A; 1364 } else {
1337 button_pressed = -1; 1365 show_mapping_view();
1338 last_button = -1; 1366 }
1339 last_hat = -1;
1340 axis_moved = -1;
1341 last_axis = -1;
1342 last_axis_value = 0;
1343 SDL_Joystick *joy = render_get_joystick(selected_controller);
1344 const char *name = SDL_JoystickName(joy);
1345 size_t namesz = strlen(name);
1346 mapping_string = malloc(512 + namesz);
1347 for (mapping_pos = 0; mapping_pos < namesz; mapping_pos++)
1348 {
1349 char c = name[mapping_pos];
1350 if (c == ',' || c == '\n' || c == '\r') {
1351 c = ' ';
1352 }
1353 mapping_string[mapping_pos] = c;
1354 }
1355
1356 push_view(view_controller_mappings);
1357 } 1367 }
1358 } 1368 }
1359 } 1369 }
1360 1370
1361 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) 1371 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)
1396 } 1406 }
1397 1407
1398 void view_controllers(struct nk_context *context) 1408 void view_controllers(struct nk_context *context)
1399 { 1409 {
1400 if (nk_begin(context, "Controllers", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) { 1410 if (nk_begin(context, "Controllers", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) {
1401 int height = (render_width() - 2*context->style.font->height) / MAX_JOYSTICKS; 1411 int height = (render_height() - 2*context->style.font->height) / 5;
1412 int inner_height = height - context->style.window.spacing.y;
1413 const struct nk_user_font *font = context->style.font;
1414 int bindings_width = font->width(font->userdata, font->height, "Bindings", strlen("Bindings")) + context->style.button.padding.x * 2;
1415 int remap_width = font->width(font->userdata, font->height, "Remap", strlen("Remap")) + context->style.button.padding.x * 2;
1416 int change_type_width = font->width(font->userdata, font->height, "Change Type", strlen("Change Type")) + context->style.button.padding.x * 2;
1417 int total = bindings_width + remap_width + change_type_width;
1418 float bindings_ratio = (float)bindings_width / total;
1419 float remap_ratio = (float)remap_width / total;
1420 float change_type_ratio = (float)change_type_width / total;
1421
1422
1423 uint8_t found_controller = 0;
1402 for (int i = 0; i < MAX_JOYSTICKS; i++) 1424 for (int i = 0; i < MAX_JOYSTICKS; i++)
1403 { 1425 {
1404 SDL_Joystick *joy = render_get_joystick(i); 1426 SDL_Joystick *joy = render_get_joystick(i);
1405 if (joy) { 1427 if (joy) {
1428 found_controller = 1;
1406 controller_info info = get_controller_info(i); 1429 controller_info info = get_controller_info(i);
1407 ui_image *controller_image = select_best_image(&info); 1430 ui_image *controller_image = select_best_image(&info);
1408 int image_width = height * controller_image->width / controller_image->height; 1431 int image_width = inner_height * controller_image->width / controller_image->height;
1409 nk_layout_row_begin(context, NK_STATIC, height, 2); 1432 nk_layout_space_begin(context, NK_STATIC, height, INT_MAX);
1410 nk_layout_row_push(context, image_width); 1433 nk_layout_space_push(context, nk_rect(context->style.font->height / 2, 0, image_width, inner_height));
1411 if (info.type == TYPE_UNKNOWN || info.type == TYPE_GENERIC_MAPPING) { 1434 if (info.type == TYPE_UNKNOWN || info.type == TYPE_GENERIC_MAPPING) {
1412 nk_label(context, "?", NK_TEXT_CENTERED); 1435 nk_label(context, "?", NK_TEXT_CENTERED);
1413 } else { 1436 } else {
1414 nk_image(context, controller_image->ui); 1437 nk_image(context, controller_image->ui);
1415 } 1438 }
1416 nk_layout_row_push(context, render_width() - image_width - 2 * context->style.font->height); 1439 int button_start = image_width + context->style.font->height;
1417 if (nk_button_label(context, info.name)) { 1440 int button_area_width = render_width() - image_width - 2 * context->style.font->height;
1418 selected_controller = i; 1441
1419 selected_controller_info = info; 1442 nk_layout_space_push(context, nk_rect(button_start, 0, button_area_width, inner_height/2));
1420 if (info.type == TYPE_UNKNOWN || info.type == TYPE_GENERIC_MAPPING) { 1443 nk_label(context, info.name, NK_TEXT_CENTERED);
1444 const struct nk_user_font *font = context->style.font;
1445 if (info.type == TYPE_UNKNOWN || info.type == TYPE_GENERIC_MAPPING) {
1446 int button_width = font->width(font->userdata, font->height, "Configure", strlen("Configure"));
1447 nk_layout_space_push(context, nk_rect(button_start, height/2, button_width, inner_height/2));
1448 if (nk_button_label(context, "Configure")) {
1449 selected_controller = i;
1450 selected_controller_info = info;
1451 initial_controller_config = 1;
1421 push_view(view_controller_type); 1452 push_view(view_controller_type);
1422 } else { 1453 }
1454 } else {
1455 button_area_width -= 2 * context->style.window.spacing.x;
1456 bindings_width = bindings_ratio * button_area_width;
1457 nk_layout_space_push(context, nk_rect(button_start, height/2, bindings_width, inner_height/2));
1458 if (nk_button_label(context, "Bindings")) {
1459 selected_controller = i;
1460 selected_controller_info = info;
1423 push_view(view_controller_bindings); 1461 push_view(view_controller_bindings);
1424 controller_binding_changed = 0; 1462 controller_binding_changed = 0;
1425 } 1463 }
1426 1464 button_start += bindings_width + context->style.window.spacing.x;
1465 remap_width = remap_ratio * button_area_width;
1466 nk_layout_space_push(context, nk_rect(button_start, height/2, remap_width, inner_height/2));
1467 if (nk_button_label(context, "Remap")) {
1468 selected_controller = i;
1469 selected_controller_info = info;
1470 initial_controller_config = 0;
1471 show_mapping_view();
1472 }
1473 button_start += remap_width + context->style.window.spacing.x;
1474 change_type_width = change_type_ratio * button_area_width;
1475 nk_layout_space_push(context, nk_rect(button_start, height/2, change_type_width, inner_height/2));
1476 if (nk_button_label(context, "Change Type")) {
1477 selected_controller = i;
1478 selected_controller_info = info;
1479 initial_controller_config = 0;
1480 push_view(view_controller_type);
1481 }
1427 } 1482 }
1428 nk_layout_row_end(context); 1483 //nk_layout_row_end(context);
1429 } 1484 }
1485 }
1486 if (!found_controller) {
1487 nk_layout_row_static(context, context->style.font->height, render_width() - 2 * context->style.font->height, 1);
1488 nk_label(context, "No controllers detected", NK_TEXT_CENTERED);
1430 } 1489 }
1431 nk_layout_row_static(context, context->style.font->height, (render_width() - 2 * context->style.font->height) / 2, 2); 1490 nk_layout_row_static(context, context->style.font->height, (render_width() - 2 * context->style.font->height) / 2, 2);
1432 nk_label(context, "", NK_TEXT_LEFT); 1491 nk_label(context, "", NK_TEXT_LEFT);
1433 if (nk_button_label(context, "Back")) { 1492 if (nk_button_label(context, "Back")) {
1434 pop_view(); 1493 pop_view();