Mercurial > repos > blastem
comparison nuklear_ui/blastem_nuklear.c @ 1499:756f8616d1bf nuklear_ui
Refactor basic settings dropdowns
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 08 Dec 2017 23:24:21 -0800 |
parents | 050a5b032bc5 |
children | 39a199dca772 |
comparison
equal
deleted
inserted
replaced
1498:050a5b032bc5 | 1499:756f8616d1bf |
---|---|
412 pop_view(); | 412 pop_view(); |
413 } | 413 } |
414 nk_end(context); | 414 nk_end(context); |
415 } | 415 } |
416 } | 416 } |
417 | |
418 int32_t find_match(const char **options, uint32_t num_options, char *path, char *def) | |
419 { | |
420 char *setting = tern_find_path_default(config, path, (tern_val){.ptrval = def}, TVAL_PTR).ptrval; | |
421 int32_t selected = -1; | |
422 for (uint32_t i = 0; i < num_options; i++) | |
423 { | |
424 if (!strcmp(setting, options[i])) { | |
425 selected = i; | |
426 break; | |
427 } | |
428 } | |
429 if (selected == -1) { | |
430 for (uint32_t i = 0; i < num_options; i++) | |
431 { | |
432 if (!strcmp(def, options[i])) { | |
433 selected = i; | |
434 break; | |
435 } | |
436 } | |
437 } | |
438 return selected; | |
439 } | |
440 | |
441 int32_t settings_dropdown(struct nk_context *context, char *label, const char **options, uint32_t num_options, int32_t current, char *path) | |
442 { | |
443 nk_label(context, label, NK_TEXT_LEFT); | |
444 int32_t next = nk_combo(context, options, num_options, current, 30, nk_vec2(300, 300)); | |
445 if (next != current) { | |
446 config = tern_insert_path(config, path, (tern_val){.ptrval = strdup(options[next])}, TVAL_PTR); | |
447 } | |
448 return next; | |
449 } | |
450 | |
417 void view_audio_settings(struct nk_context *context) | 451 void view_audio_settings(struct nk_context *context) |
418 { | 452 { |
419 const char *rates[] = { | 453 const char *rates[] = { |
420 "192000", | 454 "192000", |
421 "96000", | 455 "96000", |
433 const uint32_t num_rates = sizeof(rates)/sizeof(*rates); | 467 const uint32_t num_rates = sizeof(rates)/sizeof(*rates); |
434 const uint32_t num_sizes = sizeof(sizes)/sizeof(*sizes); | 468 const uint32_t num_sizes = sizeof(sizes)/sizeof(*sizes); |
435 static int32_t selected_rate = -1; | 469 static int32_t selected_rate = -1; |
436 static int32_t selected_size = -1; | 470 static int32_t selected_size = -1; |
437 if (selected_rate < 0 || selected_size < 0) { | 471 if (selected_rate < 0 || selected_size < 0) { |
438 char *rate = tern_find_path_default(config, "audio\0rate\0", (tern_val){.ptrval = "48000"}, TVAL_PTR).ptrval; | 472 selected_rate = find_match(rates, num_rates, "autio\0rate\0", "48000"); |
439 for (uint32_t i = 0; i < num_rates; i++) | 473 selected_size = find_match(sizes, num_sizes, "audio\0buffer\0", "512"); |
440 { | |
441 if (!strcmp(rate, rates[i])) { | |
442 selected_rate = i; | |
443 break; | |
444 } | |
445 } | |
446 if (selected_rate < 0) { | |
447 //better solution would be to add the custom rate to the rate list, but this is probably sufficient for now | |
448 selected_rate = 2; | |
449 } | |
450 char *size = tern_find_path_default(config, "audio\0buffer\0", (tern_val){.ptrval = "512"}, TVAL_PTR).ptrval; | |
451 for (uint32_t i = 0; i < num_sizes; i++) | |
452 { | |
453 if (!strcmp(size, sizes[i])) { | |
454 selected_size = i; | |
455 break; | |
456 } | |
457 } | |
458 if (selected_size < 0) { | |
459 //better solution would be to add the custom rate to the rate list, but this is probably sufficient for now | |
460 selected_size = 2; | |
461 } | |
462 } | 474 } |
463 uint32_t width = render_width(); | 475 uint32_t width = render_width(); |
464 uint32_t height = render_height(); | 476 uint32_t height = render_height(); |
465 if (nk_begin(context, "Audio Settings", nk_rect(0, 0, width, height), 0)) { | 477 if (nk_begin(context, "Audio Settings", nk_rect(0, 0, width, height), 0)) { |
466 nk_layout_row_static(context, 30, width > 300 ? 300 : width, 2); | 478 nk_layout_row_static(context, 30, width > 300 ? 300 : width, 2); |
467 nk_label(context, "Rate in Hz", NK_TEXT_LEFT); | 479 selected_rate = settings_dropdown(context, "Rate in Hz", rates, num_rates, selected_rate, "audio\0rate\0"); |
468 int32_t next_selected = nk_combo(context, rates, num_rates, selected_rate, 30, nk_vec2(300, 300)); | 480 selected_size = settings_dropdown(context, "Buffer Samples", sizes, num_sizes, selected_size, "audio\0buffer\0"); |
469 if (next_selected != selected_rate) { | |
470 config = tern_insert_path(config, "audio\0rate\0", (tern_val){.ptrval = strdup(rates[next_selected])}, TVAL_PTR); | |
471 selected_rate = next_selected; | |
472 } | |
473 nk_label(context, "Buffer Samples", NK_TEXT_LEFT); | |
474 next_selected = nk_combo(context, sizes, num_sizes, selected_size, 30, nk_vec2(300, 300)); | |
475 if (next_selected != selected_size) { | |
476 config = tern_insert_path(config, "audio\0buffer\0", (tern_val){.ptrval = strdup(sizes[next_selected])}, TVAL_PTR); | |
477 selected_size = next_selected; | |
478 } | |
479 settings_int_input(context, "Lowpass Cutoff Hz", "audio\0lowpass_cutoff\0", "3390"); | 481 settings_int_input(context, "Lowpass Cutoff Hz", "audio\0lowpass_cutoff\0", "3390"); |
480 if (nk_button_label(context, "Back")) { | 482 if (nk_button_label(context, "Back")) { |
481 pop_view(); | 483 pop_view(); |
482 } | 484 } |
483 nk_end(context); | 485 nk_end(context); |