comparison nuklear_ui/blastem_nuklear.c @ 1646:60b199cbb3f7

Added PS4 6-button fighting pad image and cleaned up controller image handling code
author Michael Pavone <pavone@retrodev.com>
date Fri, 14 Dec 2018 19:24:51 -0800
parents 84ef1eb2c96a
children 5a662692c215
comparison
equal deleted inserted replaced
1645:84ef1eb2c96a 1646:60b199cbb3f7
16 #include "../png.h" 16 #include "../png.h"
17 #include "../controller_info.h" 17 #include "../controller_info.h"
18 #include "../bindings.h" 18 #include "../bindings.h"
19 19
20 static struct nk_context *context; 20 static struct nk_context *context;
21
22 typedef struct
23 {
24 uint32_t *image_data;
25 uint32_t width, height;
26 struct nk_image ui;
27 } ui_image;
28
29 static ui_image **ui_images, *controller_360, *controller_ps4, *controller_ps4_6b;
30 static uint32_t num_ui_images, ui_image_storage;
21 31
22 typedef void (*view_fun)(struct nk_context *); 32 typedef void (*view_fun)(struct nk_context *);
23 static view_fun current_view; 33 static view_fun current_view;
24 static view_fun *previous_views; 34 static view_fun *previous_views;
25 static uint32_t view_storage; 35 static uint32_t view_storage;
506 } 516 }
507 } 517 }
508 518
509 static int selected_controller; 519 static int selected_controller;
510 static controller_info selected_controller_info; 520 static controller_info selected_controller_info;
511 static struct nk_image controller_360_image, controller_ps4_image;
512 static uint32_t controller_360_width, controller_360_height, controller_ps4_width, controller_ps4_height;
513 //#define MIN_BIND_BOX_WIDTH 140 521 //#define MIN_BIND_BOX_WIDTH 140
514 #define MAX_BIND_BOX_WIDTH 350 522 #define MAX_BIND_BOX_WIDTH 350
515 523
516 #define AXIS 0x40000000 524 #define AXIS 0x40000000
517 #define STICKDIR 0x30000000 525 #define STICKDIR 0x30000000
944 nk_end(context); 952 nk_end(context);
945 } 953 }
946 free((char *)options[SIMILAR_CONTROLLERS].title); 954 free((char *)options[SIMILAR_CONTROLLERS].title);
947 } 955 }
948 956
957 static ui_image *select_best_image(controller_info *info)
958 {
959 if (info->variant != VARIANT_NORMAL) {
960 return controller_ps4_6b;
961 } else if (info->type == TYPE_PSX) {
962 return controller_ps4;
963 } else {
964 return controller_360;
965 }
966 }
967
949 void view_controller_bindings(struct nk_context *context) 968 void view_controller_bindings(struct nk_context *context)
950 { 969 {
951 if (nk_begin(context, "Controller Bindings", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) { 970 if (nk_begin(context, "Controller Bindings", nk_rect(0, 0, render_width(), render_height()), NK_WINDOW_NO_SCROLLBAR)) {
952 if (!bindings) { 971 if (!bindings) {
953 bindings = calloc(1, sizeof(*bindings)); 972 bindings = calloc(1, sizeof(*bindings));
968 float orig_height = def_font->handle.height; 987 float orig_height = def_font->handle.height;
969 def_font->handle.height *= 0.5f; 988 def_font->handle.height *= 0.5f;
970 989
971 uint32_t avail_height = render_height() - 2 * orig_height; 990 uint32_t avail_height = render_height() - 2 * orig_height;
972 float desired_width = render_width() * 0.5f, desired_height = avail_height * 0.5f; 991 float desired_width = render_width() * 0.5f, desired_height = avail_height * 0.5f;
973 uint32_t controller_width, controller_height; 992 ui_image *controller_image = select_best_image(&selected_controller_info);
974 if (selected_controller_info.type == TYPE_PSX) { 993
975 controller_width = controller_ps4_width; 994 float controller_ratio = (float)controller_image->width / (float)controller_image->height;
976 controller_height = controller_ps4_height;
977 } else {
978 controller_width = controller_360_width;
979 controller_height = controller_360_height;
980 }
981
982 float controller_ratio = (float)controller_width / (float)controller_height;
983 995
984 const struct nk_user_font *font = context->style.font; 996 const struct nk_user_font *font = context->style.font;
985 int MIN_BIND_BOX_WIDTH = font->width(font->userdata, font->height, "Right", strlen("Right")) 997 int MIN_BIND_BOX_WIDTH = font->width(font->userdata, font->height, "Right", strlen("Right"))
986 + def_font->handle.width(font->userdata, font->height, "Internal Screenshot", strlen("Internal Screenshot")); 998 + def_font->handle.width(font->userdata, font->height, "Internal Screenshot", strlen("Internal Screenshot"));
987 999
998 float img_top = avail_height / 2.0f - desired_height / 2.0f; 1010 float img_top = avail_height / 2.0f - desired_height / 2.0f;
999 float img_right = img_left + desired_width; 1011 float img_right = img_left + desired_width;
1000 float img_bot = img_top + desired_height; 1012 float img_bot = img_top + desired_height;
1001 nk_layout_space_begin(context, NK_STATIC, avail_height, INT_MAX); 1013 nk_layout_space_begin(context, NK_STATIC, avail_height, INT_MAX);
1002 nk_layout_space_push(context, nk_rect(img_left, img_top, desired_width, desired_height)); 1014 nk_layout_space_push(context, nk_rect(img_left, img_top, desired_width, desired_height));
1003 nk_image(context, selected_controller_info.type == TYPE_PSX ? controller_ps4_image : controller_360_image); 1015 nk_image(context, controller_image->ui);
1004 1016
1005 float bind_box_width = (render_width() - img_right) * 0.8f; 1017 float bind_box_width = (render_width() - img_right) * 0.8f;
1006 if (bind_box_width < MIN_BIND_BOX_WIDTH) { 1018 if (bind_box_width < MIN_BIND_BOX_WIDTH) {
1007 bind_box_width = render_width() - img_right; 1019 bind_box_width = render_width() - img_right;
1008 if (bind_box_width > MIN_BIND_BOX_WIDTH) { 1020 if (bind_box_width > MIN_BIND_BOX_WIDTH) {
1286 for (int i = 0; i < MAX_JOYSTICKS; i++) 1298 for (int i = 0; i < MAX_JOYSTICKS; i++)
1287 { 1299 {
1288 SDL_Joystick *joy = render_get_joystick(i); 1300 SDL_Joystick *joy = render_get_joystick(i);
1289 if (joy) { 1301 if (joy) {
1290 controller_info info = get_controller_info(i); 1302 controller_info info = get_controller_info(i);
1291 uint32_t controller_width, controller_height; 1303 ui_image *controller_image = select_best_image(&info);
1292 if (info.type == TYPE_PSX) { 1304 int image_width = height * controller_image->width / controller_image->height;
1293 controller_width = controller_ps4_width;
1294 controller_height = controller_ps4_height;
1295 } else {
1296 controller_width = controller_360_width;
1297 controller_height = controller_360_height;
1298 }
1299 int image_width = height * controller_width / controller_height;
1300 nk_layout_row_begin(context, NK_STATIC, height, 2); 1305 nk_layout_row_begin(context, NK_STATIC, height, 2);
1301 nk_layout_row_push(context, image_width); 1306 nk_layout_row_push(context, image_width);
1302 if (info.type == TYPE_UNKNOWN || info.type == TYPE_GENERIC_MAPPING) { 1307 if (info.type == TYPE_UNKNOWN || info.type == TYPE_GENERIC_MAPPING) {
1303 nk_label(context, "?", NK_TEXT_CENTERED); 1308 nk_label(context, "?", NK_TEXT_CENTERED);
1304 } else { 1309 } else {
1305 nk_image(context, info.type == TYPE_PSX ? controller_ps4_image : controller_360_image); 1310 nk_image(context, controller_image->ui);
1306 } 1311 }
1307 nk_layout_row_push(context, render_width() - image_width - 2 * context->style.font->height); 1312 nk_layout_row_push(context, render_width() - image_width - 2 * context->style.font->height);
1308 if (nk_button_label(context, info.name)) { 1313 if (nk_button_label(context, info.name)) {
1309 selected_controller = i; 1314 selected_controller = i;
1310 selected_controller_info = info; 1315 selected_controller_info = info;
1807 static void context_destroyed(void) 1812 static void context_destroyed(void)
1808 { 1813 {
1809 nk_sdl_shutdown(); 1814 nk_sdl_shutdown();
1810 } 1815 }
1811 1816
1812 static uint32_t *controller_360_buf, *controller_ps4_buf;
1813
1814 static struct nk_image load_image_texture(uint32_t *buf, uint32_t width, uint32_t height) 1817 static struct nk_image load_image_texture(uint32_t *buf, uint32_t width, uint32_t height)
1815 { 1818 {
1816 GLuint tex; 1819 GLuint tex;
1817 glGenTextures(1, &tex); 1820 glGenTextures(1, &tex);
1818 glBindTexture(GL_TEXTURE_2D, tex); 1821 glBindTexture(GL_TEXTURE_2D, tex);
1835 } 1838 }
1836 def_font = nk_font_atlas_add_from_memory(atlas, font, font_size, render_height() / 16, NULL); 1839 def_font = nk_font_atlas_add_from_memory(atlas, font, font_size, render_height() / 16, NULL);
1837 free(font); 1840 free(font);
1838 nk_sdl_font_stash_end(); 1841 nk_sdl_font_stash_end();
1839 nk_style_set_font(context, &def_font->handle); 1842 nk_style_set_font(context, &def_font->handle);
1840 if (controller_360_buf) { 1843 for (uint32_t i = 0; i < num_ui_images; i++)
1841 controller_360_image = load_image_texture(controller_360_buf, controller_360_width, controller_360_height); 1844 {
1842 } 1845 ui_images[i]->ui = load_image_texture(ui_images[i]->image_data, ui_images[i]->width, ui_images[i]->height);
1843 if (controller_ps4_buf) {
1844 controller_ps4_image = load_image_texture(controller_ps4_buf, controller_ps4_width, controller_ps4_height);
1845 } 1846 }
1846 } 1847 }
1847 1848
1848 static void context_created(void) 1849 static void context_created(void)
1849 { 1850 {
1888 if (config_dirty) { 1889 if (config_dirty) {
1889 persist_config(config); 1890 persist_config(config);
1890 } 1891 }
1891 } 1892 }
1892 1893
1894 ui_image *load_ui_image(char *name)
1895 {
1896 uint32_t buf_size;
1897 uint8_t *buf = (uint8_t *)read_bundled_file(name, &buf_size);
1898 if (buf) {
1899 num_ui_images++;
1900 if (num_ui_images > ui_image_storage) {
1901 ui_image_storage = (ui_image_storage + 1) * 2;
1902 ui_images = realloc(ui_images, ui_image_storage * sizeof(*ui_images));
1903 }
1904 ui_image *this_image = ui_images[num_ui_images-1] = calloc(1, sizeof(ui_image));
1905 this_image->image_data = load_png(buf, buf_size, &this_image->width, &this_image->height);
1906 free(buf);
1907 if (!this_image->image_data) {
1908 num_ui_images--;
1909 free(this_image);
1910 return NULL;
1911 }
1912 return this_image;
1913 } else {
1914 return NULL;
1915 }
1916 }
1917
1893 void blastem_nuklear_init(uint8_t file_loaded) 1918 void blastem_nuklear_init(uint8_t file_loaded)
1894 { 1919 {
1895 context = nk_sdl_init(render_get_window()); 1920 context = nk_sdl_init(render_get_window());
1896 1921
1897 uint32_t buf_size; 1922 controller_360 = load_ui_image("images/360.png");
1898 uint8_t *buf = (uint8_t *)read_bundled_file("images/360.png", &buf_size); 1923 controller_ps4 = load_ui_image("images/ps4.png");
1899 if (buf) { 1924 controller_ps4_6b = load_ui_image("images/ps4_6b.png");
1900 controller_360_buf = load_png(buf, buf_size, &controller_360_width, &controller_360_height); 1925
1901 free(buf);
1902 }
1903 buf = (uint8_t *)read_bundled_file("images/ps4.png", &buf_size);
1904 if (buf) {
1905 controller_ps4_buf = load_png(buf, buf_size, &controller_ps4_width, &controller_ps4_height);
1906 free(buf);
1907 }
1908 texture_init(); 1926 texture_init();
1909 1927
1910 current_view = file_loaded ? view_play : view_menu; 1928 current_view = file_loaded ? view_play : view_menu;
1911 render_set_ui_render_fun(blastem_nuklear_render); 1929 render_set_ui_render_fun(blastem_nuklear_render);
1912 render_set_event_handler(handle_event); 1930 render_set_event_handler(handle_event);