From 05b17e842dade465537b4c0beb621f24f0e9ab6d Mon Sep 17 00:00:00 2001 From: Ross Gouldthorpe Date: Sun, 8 Dec 2024 23:10:26 +0000 Subject: [PATCH] Added favorite removing Users can remove favorites via the menu --- src/menu/rom_history.c | 45 ++++++++++++++++-- src/menu/rom_history.h | 8 ++-- src/menu/ui_components.h | 2 +- src/menu/views/browser.c | 4 +- src/menu/views/favorite.c | 95 ++++++++++++++++++++++++-------------- src/menu/views/load_disk.c | 2 +- src/menu/views/load_rom.c | 6 +-- 7 files changed, 113 insertions(+), 49 deletions(-) diff --git a/src/menu/rom_history.c b/src/menu/rom_history.c index a621d4e2..ac8c04f1 100644 --- a/src/menu/rom_history.c +++ b/src/menu/rom_history.c @@ -33,7 +33,7 @@ void bookkeeping_ini_load_list(bookkeeping_item_t* list, int count, mini_t* ini, list[i].secondary_path = path_create(mini_get_string(ini, group, buf, "")); sprintf(buf,"%d_type", i); - list[i].bookkeeping_type = mini_get_int(ini, group, buf, HISTORY_TYPE_EMPTY); + list[i].bookkeeping_type = mini_get_int(ini, group, buf, BOOKKEEPING_TYPE_EMPTY); } } @@ -88,18 +88,30 @@ static bool bookkeeping_item_match(bookkeeping_item_t* left, bookkeeping_item_t* return false; } -static void bookkeeping_clear_item(bookkeeping_item_t* item) { +static void bookkeeping_clear_item(bookkeeping_item_t* item, bool leave_null) { if(item->primary_path != NULL){ path_free(item->primary_path); + + if(leave_null) { + item->primary_path = NULL; + } else { + item->primary_path = path_create(""); + } } if(item->secondary_path != NULL){ path_free(item->secondary_path); + + if(leave_null) { + item->secondary_path = NULL; + } else { + item->secondary_path = path_create(""); + } } - item->bookkeeping_type = HISTORY_TYPE_EMPTY; + item->bookkeeping_type = BOOKKEEPING_TYPE_EMPTY; } static void bookkeeping_copy_item(bookkeeping_item_t* source, bookkeeping_item_t* destination){ - bookkeeping_clear_item(destination); + bookkeeping_clear_item(destination, true); destination->primary_path = path_clone(source->primary_path); destination->secondary_path = source->secondary_path != NULL ? path_clone(source->secondary_path) : path_create(""); @@ -120,6 +132,19 @@ static void bookkeeping_move_items_down(bookkeeping_item_t* list, int start, int } +static void bookkeeping_move_items_up(bookkeeping_item_t* list, int start, int end) { + int current = start; + + do { + if(current > end) { + break; + } + + bookkeeping_copy_item(&list[current + 1], &list[current]); + current++; + } while(true); +} + static void bookkeeping_insert_top(bookkeeping_item_t* list, int count, bookkeeping_item_t* new_item) { // if it matches the top of the list already then nothing to do @@ -128,7 +153,7 @@ static void bookkeeping_insert_top(bookkeeping_item_t* list, int count, bookkeep } // if the top isn't empty then we need to move things around - if(list[0].bookkeeping_type != HISTORY_TYPE_EMPTY) { + if(list[0].bookkeeping_type != BOOKKEEPING_TYPE_EMPTY) { int found_at = -1; for(int i=1; i < count; i++) { if(bookkeeping_item_match(&list[i], new_item)){ @@ -168,4 +193,14 @@ void bookkeeping_favorite_add(bookkeeping_t *bookkeeping, path_t* primary_path, bookkeeping_insert_top(bookkeeping->favorite_items, FAVORITES_COUNT, &new_item); bookkeeping_save(bookkeeping); +} + +void bookkeeping_favorite_remove(bookkeeping_t *bookkeeping, int selection) { + if(bookkeeping->favorite_items[selection].bookkeeping_type != BOOKKEEPING_TYPE_EMPTY) { + + bookkeeping_move_items_up(bookkeeping->favorite_items, selection, FAVORITES_COUNT -1); + bookkeeping_clear_item(&bookkeeping->favorite_items[FAVORITES_COUNT -1], false); + + bookkeeping_save(bookkeeping); + } } \ No newline at end of file diff --git a/src/menu/rom_history.h b/src/menu/rom_history.h index 0860bfe1..326f70c1 100644 --- a/src/menu/rom_history.h +++ b/src/menu/rom_history.h @@ -14,9 +14,9 @@ #define HISTORY_COUNT 5 typedef enum { - HISTORY_TYPE_EMPTY, - HISTORY_TYPE_ROM, - HISTORY_TYPE_DISK, + BOOKKEEPING_TYPE_EMPTY, + BOOKKEEPING_TYPE_ROM, + BOOKKEEPING_TYPE_DISK, } bookkeeping_item_types_t; typedef struct { @@ -51,6 +51,8 @@ void bookkeeping_save (bookkeeping_t *history); void bookkeeping_history_add(bookkeeping_t *bookkeeping, path_t* primary_path, path_t* secondary_path, bookkeeping_item_types_t type ); + void bookkeeping_favorite_add(bookkeeping_t *bookkeeping, path_t* primary_path, path_t* secondary_path, bookkeeping_item_types_t type ); +void bookkeeping_favorite_remove(bookkeeping_t *bookkeeping, int selection); #endif \ No newline at end of file diff --git a/src/menu/ui_components.h b/src/menu/ui_components.h index ed88e42c..7274b409 100644 --- a/src/menu/ui_components.h +++ b/src/menu/ui_components.h @@ -261,7 +261,7 @@ void ui_components_main_text_draw_location (float x, float y, char *fmt, ...); /** * */ -void ui_components_tabs_draw(const char** text, int count, int selected ); +void ui_components_tabs_draw(const char** text, int count, int selected, int width ); /** * diff --git a/src/menu/views/browser.c b/src/menu/views/browser.c index ae694dbe..49f2d887 100644 --- a/src/menu/views/browser.c +++ b/src/menu/views/browser.c @@ -409,14 +409,14 @@ static void draw (menu_t *menu, surface_t *d) { ui_components_actions_bar_text_draw( ALIGN_CENTER, VALIGN_TOP, "%s" //ctime includes a newline - "", + "", ctime(&menu->current_time) ); } else { ui_components_actions_bar_text_draw( ALIGN_CENTER, VALIGN_TOP, "\n" - "" + "" ); } diff --git a/src/menu/views/favorite.c b/src/menu/views/favorite.c index 8d848b5c..3f23eda0 100644 --- a/src/menu/views/favorite.c +++ b/src/menu/views/favorite.c @@ -16,12 +16,14 @@ static int selected_item = -1; static bookkeeping_item_t* item_list; static int item_max; +#define IS_FAVORITE (screen_mode == BOOKKEEPING_SCREEN_MODE_FAVORITE) +#define IS_HISTORY (screen_mode == BOOKKEEPING_SCREEN_MODE_HISTORY) static void reset_selected(menu_t* menu) { selected_item = -1; for(int i=0;i= item_max) { selected_item = last; break; - } else if(item_list[selected_item].bookkeeping_type != HISTORY_TYPE_EMPTY) { + } else if(item_list[selected_item].bookkeeping_type != BOOKKEEPING_TYPE_EMPTY) { sound_play_effect(SFX_CURSOR); break; } @@ -56,13 +58,15 @@ static void move_back() { if(selected_item < 0) { selected_item = last; break; - } else if(item_list[selected_item].bookkeeping_type != HISTORY_TYPE_EMPTY) { + } else if(item_list[selected_item].bookkeeping_type != BOOKKEEPING_TYPE_EMPTY) { sound_play_effect(SFX_CURSOR); break; } } while (true); } + + static void process(menu_t* menu) { if(menu->actions.go_down) { move_next(); @@ -70,74 +74,85 @@ static void process(menu_t* menu) { move_back(); } else if(menu->actions.enter && selected_item != -1) { - if(screen_mode == BOOKKEEPING_SCREEN_MODE_FAVORITE) { + if(IS_FAVORITE) { menu->load.load_favorite = selected_item; - } else if(screen_mode == BOOKKEEPING_SCREEN_MODE_HISTORY) { + } else if(IS_HISTORY) { menu->load.load_history = selected_item; } - if(item_list[selected_item].bookkeeping_type == HISTORY_TYPE_DISK) { + if(item_list[selected_item].bookkeeping_type == BOOKKEEPING_TYPE_DISK) { menu->next_mode = MENU_MODE_LOAD_DISK; sound_play_effect(SFX_ENTER); - } else if(item_list[selected_item].bookkeeping_type == HISTORY_TYPE_ROM) { + } else if(item_list[selected_item].bookkeeping_type == BOOKKEEPING_TYPE_ROM) { menu->next_mode = MENU_MODE_LOAD_ROM; sound_play_effect(SFX_ENTER); } } else if (menu->actions.previous_tab) { - if(screen_mode == BOOKKEEPING_SCREEN_MODE_FAVORITE) { + if(IS_FAVORITE) { menu->next_mode = MENU_MODE_HISTORY; - } else if(screen_mode == BOOKKEEPING_SCREEN_MODE_HISTORY) { + } else if(IS_HISTORY) { menu->next_mode = MENU_MODE_BROWSER; } } else if (menu->actions.next_tab) { - if(screen_mode == BOOKKEEPING_SCREEN_MODE_FAVORITE) { + if(IS_FAVORITE) { menu->next_mode = MENU_MODE_BROWSER; - } else if(screen_mode == BOOKKEEPING_SCREEN_MODE_HISTORY) { + } else if(IS_HISTORY) { menu->next_mode = MENU_MODE_FAVORITE; } - }else if(screen_mode == BOOKKEEPING_SCREEN_MODE_FAVORITE && menu->actions.options && selected_item != -1) { - //history_favorite_remove(&menu->history, selected_item); + }else if(IS_FAVORITE && menu->actions.options && selected_item != -1) { + bookkeeping_favorite_remove(&menu->history, selected_item); reset_selected(menu); sound_play_effect(SFX_SETTING); } } -static void draw_favorites(menu_t *menu, surface_t *display) { - - float y = VISIBLE_AREA_Y0; - float x = 10 + VISIBLE_AREA_X0; - +static void draw_list(menu_t *menu, surface_t *display) { if(selected_item != -1) { - float highlight_y = y + (selected_item * 18 * 2); + float highlight_y = VISIBLE_AREA_Y0 + TEXT_MARGIN_VERTICAL + TEXT_OFFSET_VERTICAL + (selected_item * 20 * 2); ui_components_box_draw( VISIBLE_AREA_X0, highlight_y, VISIBLE_AREA_X0 + FILE_LIST_HIGHLIGHT_WIDTH, - highlight_y + 36, + highlight_y + 40, FILE_LIST_HIGHLIGHT_COLOR ); } - for(int i=0;i < FAVORITES_COUNT; i++) { + + char buffer[1024]; + buffer[0] = 0; + + for(int i=0;i < item_max; i++) { if(path_has_value(item_list[i].primary_path)) { - ui_components_main_text_draw_location(x, y, "%d : %s",(i+1), path_last_get(item_list[i].primary_path)); + sprintf(buffer, "%s%d : %s\n",buffer ,(i+1), path_last_get(item_list[i].primary_path)); } else { - ui_components_main_text_draw_location(x, y, "%d :", (i+1)); + sprintf(buffer, "%s%d : \n",buffer ,(i+1)); } - y += 16; - + if(path_has_value(item_list[i].secondary_path)) { - ui_components_main_text_draw_location(x, y," %s", path_last_get(item_list[i].secondary_path)); + sprintf(buffer, "%s %s\n", buffer, path_last_get(item_list[i].secondary_path)); + } else { + sprintf(buffer, "%s\n", buffer); } - y += 20; } - if(screen_mode == BOOKKEEPING_SCREEN_MODE_FAVORITE) { - ui_compontents_tabs_common_draw(2); - } else if(screen_mode == BOOKKEEPING_SCREEN_MODE_HISTORY) { - ui_compontents_tabs_common_draw(1); - } + int nbytes = strlen(buffer); + rdpq_text_printn( + &(rdpq_textparms_t) { + .width = VISIBLE_AREA_WIDTH - (TEXT_MARGIN_HORIZONTAL * 2), + .height = LAYOUT_ACTIONS_SEPARATOR_Y - OVERSCAN_HEIGHT - (TEXT_MARGIN_VERTICAL * 2), + .align = ALIGN_LEFT, + .valign = VALIGN_TOP, + .wrap = WRAP_ELLIPSES, + .line_spacing = TEXT_OFFSET_VERTICAL, + }, + FNT_DEFAULT, + VISIBLE_AREA_X0 + TEXT_MARGIN_HORIZONTAL, + VISIBLE_AREA_Y0 + TEXT_MARGIN_VERTICAL + TEXT_OFFSET_VERTICAL, + buffer, + nbytes + ); } static void draw(menu_t *menu, surface_t *display) { @@ -147,7 +162,13 @@ static void draw(menu_t *menu, surface_t *display) { ui_components_layout_draw(); - draw_favorites(menu, display); + if(IS_FAVORITE) { + ui_compontents_tabs_common_draw(2); + } else if(IS_HISTORY) { + ui_compontents_tabs_common_draw(1); + } + + draw_list(menu, display); if(selected_item != -1) { ui_components_actions_bar_text_draw( @@ -155,7 +176,7 @@ static void draw(menu_t *menu, surface_t *display) { "A: Load Game" ); - if(screen_mode == BOOKKEEPING_SCREEN_MODE_FAVORITE) { + if(IS_FAVORITE && selected_item != -1) { ui_components_actions_bar_text_draw( ALIGN_RIGHT, VALIGN_TOP, "R: Remove Favorite" @@ -163,6 +184,12 @@ static void draw(menu_t *menu, surface_t *display) { } } + ui_components_actions_bar_text_draw( + ALIGN_CENTER, VALIGN_TOP, + "\n" + "" + ); + rdpq_detach_show(); } diff --git a/src/menu/views/load_disk.c b/src/menu/views/load_disk.c index 20d89477..b69f3dcd 100644 --- a/src/menu/views/load_disk.c +++ b/src/menu/views/load_disk.c @@ -139,7 +139,7 @@ static void load (menu_t *menu) { return; } - bookkeeping_history_add(&menu->history, menu->load.disk_path, menu->load.rom_path, HISTORY_TYPE_DISK); + bookkeeping_history_add(&menu->history, menu->load.disk_path, menu->load.rom_path, BOOKKEEPING_TYPE_DISK); menu->next_mode = MENU_MODE_BOOT; if (load_disk_with_rom) { diff --git a/src/menu/views/load_rom.c b/src/menu/views/load_rom.c index 2239dd9d..a502d19f 100644 --- a/src/menu/views/load_rom.c +++ b/src/menu/views/load_rom.c @@ -159,7 +159,7 @@ static void set_autoload_type (menu_t *menu, void *arg) { } static void add_favorite (menu_t *menu, void *arg) { - bookkeeping_favorite_add(&menu->history, menu->load.rom_path, NULL, HISTORY_TYPE_ROM); + bookkeeping_favorite_add(&menu->history, menu->load.rom_path, NULL, BOOKKEEPING_TYPE_ROM); } static component_context_menu_t set_cic_type_context_menu = { .list = { @@ -256,7 +256,7 @@ static void draw (menu_t *menu, surface_t *d) { "\n" "\n" "\n" - "Description:\n None.\n\n\n\n\n\n\n\n" + "Description:\n None.\n\n\n\n\n\n\n" "Expansion PAK: %s\n" "TV type: %s\n" "CIC: %s\n" @@ -341,7 +341,7 @@ static void load (menu_t *menu) { return; } - bookkeeping_history_add(&menu->history, menu->load.rom_path, NULL, HISTORY_TYPE_ROM); + bookkeeping_history_add(&menu->history, menu->load.rom_path, NULL, BOOKKEEPING_TYPE_ROM); menu->next_mode = MENU_MODE_BOOT;