comparison nuklear_ui/blastem_nuklear.c @ 1598:5e2af89c3467

Made controller binding page more resolution independent. Added binding boxes for all buttons/axes
author Michael Pavone <pavone@retrodev.com>
date Wed, 25 Jul 2018 09:38:40 -0700
parents 437e80a700aa
children 1fc61c844ec5
comparison
equal deleted inserted replaced
1597:75aa418d0227 1598:5e2af89c3467
506 static controller_info selected_controller_info; 506 static controller_info selected_controller_info;
507 static struct nk_image controller_360_image; 507 static struct nk_image controller_360_image;
508 static uint32_t controller_360_width, controller_360_height; 508 static uint32_t controller_360_width, controller_360_height;
509 //#define MIN_BIND_BOX_WIDTH 140 509 //#define MIN_BIND_BOX_WIDTH 140
510 #define MAX_BIND_BOX_WIDTH 350 510 #define MAX_BIND_BOX_WIDTH 350
511
512 #define AXIS 0x40000000
513 #define STICKDIR 0x30000000
514 #define LEFTSTICK 0x10000000
515 #define RIGHTSTICK 0x20000000
516 enum {
517 UP,DOWN,LEFT,RIGHT
518 };
519 void binding_box(struct nk_context *context, char *name, float x, float y, float width, int num_binds, int *binds)
520 {
521 const struct nk_user_font *font = context->style.font;
522 float row_height = font->height * 2;
523
524 nk_layout_space_push(context, nk_rect(x, y, width, num_binds * (row_height + 4) + 4));
525 nk_group_begin(context, name, NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR);
526
527 char const **labels = calloc(sizeof(char *), num_binds);
528 float max_width = 0.0f;
529 for (int i = 0; i < num_binds; i++)
530 {
531 if (binds[i] & AXIS) {
532 labels[i] = get_axis_label(&selected_controller_info, binds[i] & ~AXIS);
533 } else if (binds[i] & STICKDIR) {
534 static char const * dirs[] = {"Up", "Down", "Left", "Right"};
535 labels[i] = dirs[binds[i] & 3];
536 } else {
537 labels[i] = get_button_label(&selected_controller_info, binds[i]);
538 }
539 float lb_width = font->width(font->userdata, font->height, labels[i], strlen(labels[i]));
540 max_width = max_width < lb_width ? lb_width : max_width;
541 }
542 float widths[] = {max_width + 3, width - (max_width + 6)};
543 nk_layout_row(context, NK_STATIC, row_height, 2, widths);
544 for (int i = 0; i < num_binds; i++)
545 {
546 nk_label(context, labels[i], NK_TEXT_LEFT);
547 nk_button_label(context, i & 1 ? "Internal Screenshot" : "A");
548 }
549 nk_group_end(context);
550 }
551
511 void view_controller_bindings(struct nk_context *context) 552 void view_controller_bindings(struct nk_context *context)
512 { 553 {
513 if (nk_begin(context, "Controller Bindings", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) { 554 if (nk_begin(context, "Controller Bindings", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) {
514 float orig_height = def_font->handle.height; 555 float orig_height = def_font->handle.height;
515 def_font->handle.height *= 0.5f; 556 def_font->handle.height *= 0.5f;
532 desired_height = desired_width / controller_ratio; 573 desired_height = desired_width / controller_ratio;
533 } 574 }
534 float img_left = render_width() / 2.0f - desired_width / 2.0f; 575 float img_left = render_width() / 2.0f - desired_width / 2.0f;
535 float img_top = avail_height / 2.0f - desired_height / 2.0f; 576 float img_top = avail_height / 2.0f - desired_height / 2.0f;
536 float img_right = img_left + desired_width; 577 float img_right = img_left + desired_width;
578 float img_bot = img_top + desired_height;
537 nk_layout_space_begin(context, NK_STATIC, avail_height, INT_MAX); 579 nk_layout_space_begin(context, NK_STATIC, avail_height, INT_MAX);
538 nk_layout_space_push(context, nk_rect(img_left, img_top, desired_width, desired_height)); 580 nk_layout_space_push(context, nk_rect(img_left, img_top, desired_width, desired_height));
539 nk_image(context, controller_360_image); 581 nk_image(context, controller_360_image);
540 582
541 float bind_box_width = (render_width() - img_right) * 0.8f; 583 float bind_box_width = (render_width() - img_right) * 0.8f;
552 bind_box_left = img_right; 594 bind_box_left = img_right;
553 } else { 595 } else {
554 bind_box_left = img_right + (render_width() - img_right) / 2.0f - bind_box_width / 2.0f; 596 bind_box_left = img_right + (render_width() - img_right) / 2.0f - bind_box_width / 2.0f;
555 } 597 }
556 598
557 nk_layout_space_push(context, nk_rect(bind_box_left, img_top, bind_box_width, 165)); 599 binding_box(context, "Action Buttons", bind_box_left, img_top, bind_box_width, 4, (int[]){
558 nk_group_begin(context, "Action Buttons", NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR); 600 SDL_CONTROLLER_BUTTON_A,
559 float widths[] = {34, bind_box_width - 60}; 601 SDL_CONTROLLER_BUTTON_B,
560 nk_layout_row(context, NK_STATIC, 34, 2, widths); 602 SDL_CONTROLLER_BUTTON_X,
561 nk_label(context, get_button_label(&selected_controller_info, SDL_CONTROLLER_BUTTON_A), NK_TEXT_LEFT); 603 SDL_CONTROLLER_BUTTON_Y
562 nk_button_label(context, "A"); 604 });
563 nk_label(context, get_button_label(&selected_controller_info, SDL_CONTROLLER_BUTTON_B), NK_TEXT_LEFT); 605
564 nk_button_label(context, "B"); 606 binding_box(context, "Right Shoulder", bind_box_left, font->height/2, bind_box_width, 2, (int[]){
565 nk_label(context, get_button_label(&selected_controller_info, SDL_CONTROLLER_BUTTON_X), NK_TEXT_LEFT); 607 SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
566 nk_button_label(context, "X"); 608 AXIS | SDL_CONTROLLER_AXIS_TRIGGERRIGHT
567 nk_label(context, get_button_label(&selected_controller_info, SDL_CONTROLLER_BUTTON_Y), NK_TEXT_LEFT); 609 });
568 nk_button_label(context, "Internal Screenshot"); 610
569 nk_group_end(context); 611 binding_box(context, "Misc Buttons", (render_width() - bind_box_width) / 2, font->height/2, bind_box_width, 3, (int[]){
612 SDL_CONTROLLER_BUTTON_BACK,
613 SDL_CONTROLLER_BUTTON_GUIDE,
614 SDL_CONTROLLER_BUTTON_START
615 });
616
617 binding_box(context, "Right Stick", img_right - desired_width/3, img_bot, bind_box_width, 5, (int[]){
618 RIGHTSTICK | UP,
619 RIGHTSTICK | DOWN,
620 RIGHTSTICK | LEFT,
621 RIGHTSTICK | RIGHT,
622 SDL_CONTROLLER_BUTTON_RIGHTSTICK
623 });
624
570 625
571 bind_box_left -= img_right; 626 bind_box_left -= img_right;
572 widths[0] += 44; 627 binding_box(context, "Left Stick", bind_box_left, img_top, bind_box_width, 5, (int[]){
573 widths[1] -= 44; 628 LEFTSTICK | UP,
574 nk_layout_space_push(context, nk_rect(bind_box_left, img_top, bind_box_width, 205)); 629 LEFTSTICK | DOWN,
575 nk_group_begin(context, "Left Stick", NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR); 630 LEFTSTICK | LEFT,
576 nk_layout_row(context, NK_STATIC, 34, 2, widths); 631 LEFTSTICK | RIGHT,
577 nk_label(context, "Up", NK_TEXT_LEFT); 632 SDL_CONTROLLER_BUTTON_LEFTSTICK
578 nk_button_label(context, "Up"); 633 });
579 nk_label(context, "Down", NK_TEXT_LEFT); 634
580 nk_button_label(context, "Down"); 635 binding_box(context, "Left Shoulder", bind_box_left, font->height/2, bind_box_width, 2, (int[]){
581 nk_label(context, "Left", NK_TEXT_LEFT); 636 SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
582 nk_button_label(context, "Left"); 637 AXIS | SDL_CONTROLLER_AXIS_TRIGGERLEFT
583 nk_label(context, "Right", NK_TEXT_LEFT); 638 });
584 nk_button_label(context, "Right"); 639
585 nk_label(context, get_button_label(&selected_controller_info, SDL_CONTROLLER_BUTTON_LEFTSTICK), NK_TEXT_LEFT); 640 binding_box(context, "D-pad", img_left - desired_width/6, img_bot + font->height * 1.5, bind_box_width, 4, (int[]){
586 nk_button_label(context, "None"); 641 SDL_CONTROLLER_BUTTON_DPAD_UP,
587 642 SDL_CONTROLLER_BUTTON_DPAD_DOWN,
588 nk_group_end(context); 643 SDL_CONTROLLER_BUTTON_DPAD_LEFT,
644 SDL_CONTROLLER_BUTTON_DPAD_RIGHT
645 });
589 646
590 nk_layout_space_end(context); 647 nk_layout_space_end(context);
591 648
592 def_font->handle.height = orig_height; 649 def_font->handle.height = orig_height;
593 nk_layout_row_static(context, orig_height + 4, (render_width() - 80) / 2, 1); 650 nk_layout_row_static(context, orig_height + 4, (render_width() - 2*orig_height) / 4, 1);
594 if (nk_button_label(context, "Back")) { 651 if (nk_button_label(context, "Back")) {
595 pop_view(); 652 pop_view();
596 } 653 }
597 nk_end(context); 654 nk_end(context);
598 } 655 }