Skip to content

Commit

Permalink
[Refactor]: Add submenu.c for refactor the menu system.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolf3s committed Jan 1, 2025
1 parent a626d98 commit 9334294
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 214 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ ifneq ($(GIT_TAG),latest)
endif
endif

FRONTEND_OBJS = pad.o xparam.o fntsys.o renderman.o menusys.o OSDHistory.o system.o lang.o lang_internal.o config.o hdd.o dialogs.o \
FRONTEND_OBJS = pad.o xparam.o fntsys.o renderman.o submenu.o menu.o OSDHistory.o system.o lang.o lang_internal.o config.o hdd.o dialogs.o \
dia.o ioman.o texcache.o themes.o supportbase.o bdmsupport.o ethsupport.o hddsupport.o zso.o lz4.o \
appsupport.o gui.o guigame.o textures.o opl.o atlas.o nbns.o httpclient.o gsm.o cheatman.o sound.o ps2cnf.o

Expand Down
2 changes: 1 addition & 1 deletion include/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "include/opl.h"
#include "include/texcache.h"
#include "include/dialogs.h"
#include "include/menusys.h"
#include "include/menu.h"

typedef enum {
// Informs gui that init is over and main gui can be rendered
Expand Down
33 changes: 0 additions & 33 deletions include/menusys.h → include/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,6 @@
#include "include/config.h"
#include "include/dia.h"

/// a single submenu item
typedef struct submenu_item
{
/// Icon used for rendering of this item
int icon_id;

/// item description
char *text;

/// item description in localized form (used if value is not negative)
int text_id;

/// item id (MUST BE VALID, we assert it is != -1 to optimize rendering)
int id;

int *cache_id;
int *cache_uid;
} submenu_item_t;

typedef struct submenu_list
{
struct submenu_item item;

struct submenu_list *prev, *next;
} submenu_list_t;

typedef struct menu_hint_item
{
int icon_id;
Expand Down Expand Up @@ -89,13 +63,6 @@ void menuInitAppMenu(void);

void menuAppendItem(menu_item_t *item);

void submenuRebuildCache(submenu_list_t *submenu);
submenu_list_t *submenuAppendItem(submenu_list_t **submenu, int icon_id, char *text, int id, int text_id);
void submenuRemoveItem(submenu_list_t **submenu, int id);
void submenuDestroy(submenu_list_t **submenu);
void submenuSort(submenu_list_t **submenu);

char *submenuItemGetText(submenu_item_t *it);
char *menuItemGetText(menu_item_t *it);
config_set_t *menuLoadConfig();
config_set_t *gameMenuLoadConfig(struct UIItem *ui);
Expand Down
3 changes: 2 additions & 1 deletion include/opl.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ void setDefaultColors(void);

#define MENU_ITEM_HEIGHT 19

#include "include/menusys.h"
#include "include/submenu.h"
#include "include/menu.h"

typedef struct
{
Expand Down
42 changes: 42 additions & 0 deletions include/submenu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef __SUBMENU_H
#define __SUBMENU_H

/// a single submenu item
typedef struct submenu_item
{
/// Icon used for rendering of this item
int icon_id;

/// item description
char *text;

/// item description in localized form (used if value is not negative)
int text_id;

/// item id (MUST BE VALID, we assert it is != -1 to optimize rendering)
int id;

int *cache_id;
int *cache_uid;
} submenu_item_t;

typedef struct submenu_list
{
struct submenu_item item;

struct submenu_list *prev, *next;
} submenu_list_t;

void submenuRebuildCache(submenu_list_t *submenu);
submenu_list_t *submenuAppendItem(submenu_list_t **submenu, int icon_id, char *text, int id, int text_id);
void submenuRemoveItem(submenu_list_t **submenu, int id);
void submenuDestroy(submenu_list_t **submenu);
void submenuSort(submenu_list_t **submenu);

char *submenuItemGetText(submenu_item_t *it);

void submenuDestroyItem(submenu_list_t *submenu);

submenu_list_t *submenuAllocItem(int icon_id, char *text, int id, int text_id);

#endif
2 changes: 1 addition & 1 deletion include/themes.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "include/textures.h"
#include "include/texcache.h"
#include "include/menusys.h"
#include "include/menu.h"

#define THM_MAX_FILES 64
#define THM_MAX_FONTS 16
Expand Down
2 changes: 1 addition & 1 deletion src/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "include/opl.h"
#include "include/gui.h"
#include "include/renderman.h"
#include "include/menusys.h"
#include "include/menu.h"
#include "include/fntsys.h"
#include "include/ioman.h"
#include "include/lang.h"
Expand Down
176 changes: 1 addition & 175 deletions src/menusys.c → src/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

#include "include/opl.h"
#include "include/menusys.h"
#include "include/menu.h"
#include "include/iosupport.h"
#include "include/renderman.h"
#include "include/fntsys.h"
Expand Down Expand Up @@ -386,112 +386,6 @@ static void refreshMenuPosition(void)
selected_item = cur;
}

void submenuRebuildCache(submenu_list_t *submenu)
{
while (submenu) {
if (submenu->item.cache_id)
free(submenu->item.cache_id);
if (submenu->item.cache_uid)
free(submenu->item.cache_uid);

int size = gTheme->gameCacheCount * sizeof(int);
submenu->item.cache_id = malloc(size);
memset(submenu->item.cache_id, -1, size);
submenu->item.cache_uid = malloc(size);
memset(submenu->item.cache_uid, -1, size);

submenu = submenu->next;
}
}

static submenu_list_t *submenuAllocItem(int icon_id, char *text, int id, int text_id)
{
submenu_list_t *it = (submenu_list_t *)malloc(sizeof(submenu_list_t));

it->prev = NULL;
it->next = NULL;
it->item.icon_id = icon_id;
it->item.text = text;
it->item.text_id = text_id;
it->item.id = id;
it->item.cache_id = NULL;
it->item.cache_uid = NULL;
submenuRebuildCache(it);

return it;
}

submenu_list_t *submenuAppendItem(submenu_list_t **submenu, int icon_id, char *text, int id, int text_id)
{
if (*submenu == NULL) {
*submenu = submenuAllocItem(icon_id, text, id, text_id);
return *submenu;
}

submenu_list_t *cur = *submenu;

// traverse till the end
while (cur->next)
cur = cur->next;

// create new item
submenu_list_t *newitem = submenuAllocItem(icon_id, text, id, text_id);

// link
cur->next = newitem;
newitem->prev = cur;

return newitem;
}

static void submenuDestroyItem(submenu_list_t *submenu)
{
free(submenu->item.cache_id);
free(submenu->item.cache_uid);

free(submenu);
}

void submenuRemoveItem(submenu_list_t **submenu, int id)
{
submenu_list_t *cur = *submenu;
submenu_list_t *prev = NULL;

while (cur) {
if (cur->item.id == id) {
submenu_list_t *next = cur->next;

if (prev)
prev->next = cur->next;

if (*submenu == cur)
*submenu = next;

submenuDestroyItem(cur);

cur = next;
} else {
prev = cur;
cur = cur->next;
}
}
}

void submenuDestroy(submenu_list_t **submenu)
{
// destroy sub menu
submenu_list_t *cur = *submenu;

while (cur) {
submenu_list_t *td = cur;
cur = cur->next;

submenuDestroyItem(td);
}

*submenu = NULL;
}

void menuAddHint(menu_item_t *menu, int text_id, int icon_id)
{
// allocate a new hint item
Expand Down Expand Up @@ -531,74 +425,6 @@ char *menuItemGetText(menu_item_t *it)
return it->text;
}

char *submenuItemGetText(submenu_item_t *it)
{
if (it->text_id >= 0)
return _l(it->text_id);
else
return it->text;
}

static void swap(submenu_list_t *a, submenu_list_t *b)
{
submenu_list_t *pa, *nb;
pa = a->prev;
nb = b->next;

a->next = nb;
b->prev = pa;
b->next = a;
a->prev = b;

if (pa)
pa->next = b;

if (nb)
nb->prev = a;
}

// Sorts the given submenu by comparing the on-screen titles
void submenuSort(submenu_list_t **submenu)
{
// a simple bubblesort
// *submenu = mergeSort(*submenu);
submenu_list_t *head;
int sorted = 0;

if ((submenu == NULL) || (*submenu == NULL) || ((*submenu)->next == NULL))
return;

head = *submenu;

while (!sorted) {
sorted = 1;

submenu_list_t *tip = head;

while (tip->next) {
submenu_list_t *nxt = tip->next;

char *txt1 = submenuItemGetText(&tip->item);
char *txt2 = submenuItemGetText(&nxt->item);

int cmp = strcasecmp(txt1, txt2);

if (cmp > 0) {
swap(tip, nxt);

if (tip == head)
head = nxt;

sorted = 0;
} else {
tip = tip->next;
}
}
}

*submenu = head;
}

static void menuNextH()
{
struct menu_list *next = selected_item->next;
Expand Down
2 changes: 1 addition & 1 deletion src/opl.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "include/texcache.h"
#include "include/dia.h"
#include "include/dialogs.h"
#include "include/menusys.h"
#include "include/menu.h"
#include "include/system.h"
#include "include/debug.h"
#include "include/config.h"
Expand Down
Loading

0 comments on commit 9334294

Please sign in to comment.