From e46715757f9e0663dd63b0789154f84959b1770e Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Sun, 4 Jun 2023 23:13:39 +0100 Subject: [PATCH] Improve sub menus --- src/main.c | 2 + src/menu/menu_fileinfo.c | 93 ++++++++++++++++++++++++++++++++++++++- src/menu/menu_fileinfo.h | 4 +- src/menu/menu_info.c | 51 ++++++++++++++------- src/menu/menu_main.c | 16 +++---- src/menu/menu_res_setup.h | 15 +++++++ src/utils/str_utils.h | 15 +++++++ 7 files changed, 168 insertions(+), 28 deletions(-) create mode 100644 src/menu/menu_res_setup.h create mode 100644 src/utils/str_utils.h diff --git a/src/main.c b/src/main.c index c4e5e96b..dbec27da 100644 --- a/src/main.c +++ b/src/main.c @@ -17,6 +17,8 @@ static void init (void) { assertf(error != FLASHCART_ERROR_OUTDATED, "Outdated flashcart firmware"); assertf(error != FLASHCART_ERROR_UNSUPPORTED, "Unsupported flashcart"); assertf(error == FLASHCART_OK, "Unknown error while initializing flashcart"); + + display_init(RESOLUTION_512x240, DEPTH_16_BPP, 3, GAMMA_NONE, ANTIALIAS_RESAMPLE); } static void deinit (void) { diff --git a/src/menu/menu_fileinfo.c b/src/menu/menu_fileinfo.c index 458feed5..12d67f9c 100644 --- a/src/menu/menu_fileinfo.c +++ b/src/menu/menu_fileinfo.c @@ -1,7 +1,98 @@ #include +#include "menu_res_setup.h" #include "menu_fileinfo.h" +#include "../utils/str_utils.h" -void menu_fileinfo(void) { +static char* get_file_type(FILINFO current_fileinfo) { + // TODO: should be at least a switch statement! + if (str_endswith(current_fileinfo.fname, ".z64") || str_endswith(current_fileinfo.fname, ".n64") || str_endswith(current_fileinfo.fname, ".v64") || str_endswith(current_fileinfo.fname, ".rom")) { + // TODO: check the necessary bytes in the header to ensure! + return "N64 ROM"; + } + else if (str_endswith(current_fileinfo.fname, ".txt")) { + return "Text File"; + } + else if (str_endswith(current_fileinfo.fname, ".ini")) { + return "INI File"; + } + else if (str_endswith(current_fileinfo.fname, ".yml") || str_endswith(current_fileinfo.fname, ".yaml")) { + return "YAML File"; + } + else if (str_endswith(current_fileinfo.fname, ".toml")) { + return "TOML File"; + } + else if (str_endswith(current_fileinfo.fname, ".sav")) { + return "Save File"; + } + else if (str_endswith(current_fileinfo.fname, ".emu")) { + return "Emulator File"; + } + else { + return "Unknown File"; + } +} +void menu_fileinfo_draw_header(display_context_t disp) { + + graphics_draw_text(disp, (disp->width / 2) - 64, vertical_start_position, "FILE INFORMATION"); // centre = numchars * font_horizontal_pixels / 2 + graphics_draw_line(disp,0,30,disp->width,30, 0xff); + +} + +void menu_fileinfo_draw_footer(display_context_t disp) { + + graphics_draw_line(disp,0,disp->height - overscan_vertical_pixels - font_vertical_pixels,disp->width,disp->height - overscan_vertical_pixels - font_vertical_pixels, 0xff); + graphics_draw_text(disp, (disp->width / 2) - 80,disp->height - overscan_vertical_pixels, "Press (B) to return!"); // centre = numchars * font_horizontal_pixels / 2 + +} + +void menu_fileinfo(FILINFO current_fileinfo) { + + char str_buffer[1024]; + + display_context_t disp = display_try_get(); + graphics_fill_screen(disp, 0x00); + menu_fileinfo_draw_header(disp); + + int16_t vertical_position = 40; + + graphics_draw_text(disp, horizontal_start_position, vertical_position, "Name:"); + graphics_draw_text(disp, horizontal_indent, vertical_position += font_vertical_pixels, current_fileinfo.fname); + vertical_position += (font_vertical_pixels * 2); + + graphics_draw_text(disp, horizontal_start_position, vertical_position, "Size:"); + sprintf(str_buffer, "%10d %s", (int)current_fileinfo.fsize, "Bytes"); + graphics_draw_text(disp, horizontal_indent, vertical_position += font_vertical_pixels, str_buffer); + vertical_position += (font_vertical_pixels * 2); + + graphics_draw_text(disp, horizontal_start_position, vertical_position, "Attributes:"); + sprintf(str_buffer, "%s%s%s", + ((current_fileinfo.fattrib & AM_DIR) ? "Directory\n" : "File\n"), + ((current_fileinfo.fattrib & AM_RDO) ? "Readonly\n" : " "), + ((current_fileinfo.fattrib & AM_HID) ? "Hidden\n" : " ")); + + graphics_draw_text(disp, horizontal_indent, vertical_position += font_vertical_pixels, str_buffer); + vertical_position += (font_vertical_pixels * 2); + + graphics_draw_text(disp, horizontal_start_position, vertical_position, "Modified Date:"); + sprintf(str_buffer, "%d T %d", current_fileinfo.fdate, current_fileinfo.ftime); + graphics_draw_text(disp, horizontal_indent, vertical_position += font_vertical_pixels, str_buffer); + vertical_position += (font_vertical_pixels * 2); + + graphics_draw_text(disp, horizontal_start_position, vertical_position, "Type:"); + graphics_draw_text(disp, horizontal_indent, vertical_position += font_vertical_pixels, get_file_type(current_fileinfo)); + + menu_fileinfo_draw_footer(disp); + + display_show(disp); + + for (;;) { + controller_scan(); + struct controller_data ckeys = get_keys_down(); + + if (ckeys.c[0].B) { + break; + } + } } \ No newline at end of file diff --git a/src/menu/menu_fileinfo.h b/src/menu/menu_fileinfo.h index e92f3561..06b4fe0e 100644 --- a/src/menu/menu_fileinfo.h +++ b/src/menu/menu_fileinfo.h @@ -1,6 +1,8 @@ #ifndef MENU_FILEINFO_H__ #define MENU_FILEINFO_H__ -void menu_fileinfo(void); +#include + +void menu_fileinfo(FILINFO current_fileinfo); #endif diff --git a/src/menu/menu_info.c b/src/menu/menu_info.c index 012018ae..e994b74a 100644 --- a/src/menu/menu_info.c +++ b/src/menu/menu_info.c @@ -1,29 +1,46 @@ #include +#include "menu_info.h" +#include "menu_res_setup.h" -static void draw_header(display_context_t disp) { - graphics_draw_text(disp, 200-70, 10, "Menu Information"); +void menu_info_draw_header(display_context_t disp) { + + graphics_draw_text(disp, (disp->width / 2) - 64, vertical_start_position, "MENU INFORMATION"); // centre = numchars * font_horizontal_pixels / 2 + graphics_draw_line(disp,0,30,disp->width,30, 0xff); + } +void menu_info_draw_footer(display_context_t disp) { + + graphics_draw_line(disp,0,disp->height - overscan_vertical_pixels - font_vertical_pixels,disp->width,disp->height - overscan_vertical_pixels - font_vertical_pixels, 0xff); + graphics_draw_text(disp, (disp->width / 2) - 80,disp->height - overscan_vertical_pixels, "Press (B) to return!"); // centre = numchars * font_horizontal_pixels / 2 + +} void menu_info(void) { - display_init(RESOLUTION_512x240, DEPTH_16_BPP, 3, GAMMA_NONE, ANTIALIAS_RESAMPLE); - display_context_t disp = display_get(); - graphics_fill_screen(disp, 0); - draw_header(disp); + + display_context_t disp = display_try_get(); + graphics_fill_screen(disp, 0x00); + menu_info_draw_header(disp); - graphics_draw_text(disp, 30, 50, "Authors:"); - graphics_draw_text(disp, 30, 58, "JonesAlmighty / NetworkFusion"); - graphics_draw_text(disp, 30, 66, "korgeaux / Polprzewodnikowy"); + int16_t vertical_position = 40; - graphics_draw_text(disp, 30, 80, "Github:"); - graphics_draw_text(disp, 30, 88, "https://github.com/Polprzewodnikowy/SummerCart64"); - graphics_draw_text(disp, 30, 96, "https://github.com/Polprzewodnikowy/N64FlashcartMenu"); + graphics_draw_text(disp, horizontal_start_position, vertical_position, "Menu Version:"); + graphics_draw_text(disp, horizontal_indent,vertical_position += font_vertical_pixels, "Vx.x.x.x"); // FIXME: use global setting. + vertical_position += (font_vertical_pixels * 2); + graphics_draw_text(disp, horizontal_start_position, vertical_position, "Authors:"); + graphics_draw_text(disp, horizontal_indent, vertical_position += font_vertical_pixels, "JonesAlmighty / NetworkFusion"); + graphics_draw_text(disp, horizontal_indent, vertical_position += font_vertical_pixels, "korgeaux / Polprzewodnikowy"); + vertical_position += (font_vertical_pixels * 2); + graphics_draw_text(disp, horizontal_start_position, vertical_position += font_vertical_pixels, "Github:"); + graphics_draw_text(disp, horizontal_indent, vertical_position += font_vertical_pixels, "https://github.com/Polprzewodnikowy/SummerCart64"); + graphics_draw_text(disp, horizontal_indent, vertical_position += font_vertical_pixels, "https://github.com/Polprzewodnikowy/N64FlashcartMenu"); + graphics_draw_text(disp, horizontal_indent, vertical_position += font_vertical_pixels, "https://github.com/networkfusion/N64FlashcartMenu"); + vertical_position += (font_vertical_pixels * 2); + graphics_draw_text(disp, horizontal_start_position, vertical_position, "OSS licenses used:"); + graphics_draw_text(disp, horizontal_indent,vertical_position += font_vertical_pixels, "GPL"); + graphics_draw_text(disp, horizontal_indent,vertical_position += font_vertical_pixels, "MIT"); - graphics_draw_text(disp, 30,112, "OSS licenses used:"); - graphics_draw_text(disp, 30,120, "GPL"); - graphics_draw_text(disp, 30,128, "MIT"); - - graphics_draw_text(disp, 30,144, "Press B to return!"); + menu_info_draw_footer(disp); display_show(disp); diff --git a/src/menu/menu_main.c b/src/menu/menu_main.c index 73f78db8..1ac43c0b 100644 --- a/src/menu/menu_main.c +++ b/src/menu/menu_main.c @@ -12,18 +12,15 @@ #include "menu_info.h" #include "menu_fileinfo.h" #include "rom_database.h" +#include "menu_res_setup.h" + +#include "../utils/str_utils.h" static int scroll_menu_position = 1; static int items_in_dir = 1; static FILINFO current_fileinfo; -// e.g. if (str_endswith(cur_rom, ".z64") || str_endswith(cur_rom, ".n64")) -static bool str_endswith(const char *str, const char *suffix) { - char *p = strstr(str, suffix); - return p && p[strlen(suffix)] == '\0'; -} - // FIXME: use newlib rather than fatfs to do this! FRESULT scan_file_path (char* path) { @@ -72,6 +69,9 @@ FRESULT scan_file_path (char* path) { } void menu_main_refresh (char *dir_path) { + // display_context_t disp = display_try_get(); + // graphics_fill_screen(disp, 0); + // graphics_draw_text(disp, 200-70, 10, "SC64 Flashcart Menu Rev: 0.0.2\n\n"); console_clear(); printf("SC64 Flashcart Menu Rev: 0.0.2\n\n"); printf("SD Card Directory list: %s\n\n", dir_path); @@ -169,13 +169,11 @@ void menu_main_init (settings_t *settings) { current_dir = last_dir; } if (joypad.c[0].start) { // FIXME: the START button on any controller! - //console_clear(); menu_info(); menu_main_refresh(current_dir); } if (joypad.c[0].Z) { - //console_clear(); - menu_fileinfo(); + menu_fileinfo(current_fileinfo); menu_main_refresh(current_dir); } } diff --git a/src/menu/menu_res_setup.h b/src/menu/menu_res_setup.h new file mode 100644 index 00000000..76cd684c --- /dev/null +++ b/src/menu/menu_res_setup.h @@ -0,0 +1,15 @@ +#ifndef MENU_RES_SETUP_H__ +#define MENU_RES_SETUP_H__ + +#include + +static const int16_t font_vertical_pixels = 8; +static const int16_t overscan_vertical_pixels = 24; +static const int16_t vertical_start_position = overscan_vertical_pixels / 2; + +static const int16_t font_horizontal_pixels = 8; +static const int16_t overscan_horizontal_pixels = 32; +static const int16_t horizontal_start_position = overscan_horizontal_pixels + font_horizontal_pixels; +static const int16_t horizontal_indent = horizontal_start_position + (font_horizontal_pixels * 2); + +#endif diff --git a/src/utils/str_utils.h b/src/utils/str_utils.h new file mode 100644 index 00000000..c91fa8aa --- /dev/null +++ b/src/utils/str_utils.h @@ -0,0 +1,15 @@ +#ifndef STR_UTILS_H__ +#define STR_UTILS_H__ + + +#include +//#include // TODO: does this work... will unlock qsort! +#include + +// e.g. if (str_endswith(cur_rom, ".z64") || str_endswith(cur_rom, ".n64")) +static bool str_endswith(const char *str, const char *suffix) { + char *p = strstr(str, suffix); + return p && p[strlen(suffix)] == '\0'; +} + +#endif \ No newline at end of file