Merge pull request #1125 from apartfromtime/menu-stack

Menu: minor code refactor
This commit is contained in:
Yamagi 2024-07-20 09:31:54 +02:00 committed by GitHub
commit 85ca97192d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 147 additions and 61 deletions

View file

@ -63,6 +63,8 @@ typedef struct _tag_menuframework
const char *statusbar; const char *statusbar;
void (*draw)(void);
const char *(*key)(int k);
void (*cursordraw)(struct _tag_menuframework *m); void (*cursordraw)(struct _tag_menuframework *m);
} menuframework_s; } menuframework_s;
@ -135,6 +137,8 @@ typedef struct
menucommon_s generic; menucommon_s generic;
} menuseparator_s; } menuseparator_s;
void M_PushMenu(menuframework_s* menu);
qboolean Field_Key(menufield_s *field, int key); qboolean Field_Key(menufield_s *field, int key);
void Menu_AddItem(menuframework_s *menu, void *item); void Menu_AddItem(menuframework_s *menu, void *item);

View file

@ -59,7 +59,7 @@ static void M_Menu_JoinServer_f(void);
static void M_Menu_AddressBook_f(void); static void M_Menu_AddressBook_f(void);
static void M_Menu_StartServer_f(void); static void M_Menu_StartServer_f(void);
static void M_Menu_DMOptions_f(void); static void M_Menu_DMOptions_f(void);
static void M_Menu_Video_f(void); void M_Menu_Video_f(void);
static void M_Menu_Options_f(void); static void M_Menu_Options_f(void);
static void M_Menu_Keys_f(void); static void M_Menu_Keys_f(void);
static void M_Menu_Joy_f(void); static void M_Menu_Joy_f(void);
@ -71,9 +71,6 @@ void M_Menu_Credits(void);
qboolean m_entersound; /* play after drawing a frame, so caching won't disrupt the sound */ qboolean m_entersound; /* play after drawing a frame, so caching won't disrupt the sound */
void (*m_drawfunc)(void);
const char *(*m_keyfunc)(int key);
/* Maximal number of submenus */ /* Maximal number of submenus */
#define MAX_MENU_DEPTH 8 #define MAX_MENU_DEPTH 8
@ -84,6 +81,7 @@ typedef struct
} menulayer_t; } menulayer_t;
menulayer_t m_layers[MAX_MENU_DEPTH]; menulayer_t m_layers[MAX_MENU_DEPTH];
menulayer_t m_active; /* active menu layer */
int m_menudepth; int m_menudepth;
static qboolean static qboolean
@ -113,11 +111,11 @@ M_Banner(char *name)
void void
M_ForceMenuOff(void) M_ForceMenuOff(void)
{ {
m_drawfunc = NULL;
m_keyfunc = NULL;
cls.key_dest = key_game; cls.key_dest = key_game;
m_active.draw = NULL;
m_active.key = NULL;
m_menudepth = 0; m_menudepth = 0;
Key_MarkAllUp(); Key_MarkAllUp();
Cvar_Set("paused", "0"); Cvar_Set("paused", "0");
} }
@ -133,8 +131,8 @@ M_PopMenu(void)
m_menudepth--; m_menudepth--;
m_drawfunc = m_layers[m_menudepth].draw; m_active.draw = m_layers[m_menudepth].draw;
m_keyfunc = m_layers[m_menudepth].key; m_active.key = m_layers[m_menudepth].key;
if (!m_menudepth) if (!m_menudepth)
{ {
@ -166,12 +164,17 @@ M_PopMenu(void)
* to the stack and make the requested menu the menu in * to the stack and make the requested menu the menu in
* flight. * flight.
*/ */
static void void
M_PushMenu(void (*draw)(void), const char *(*key)(int)) M_PushMenu(menuframework_s* menu)
{ {
int i; int i;
int alreadyPresent = 0; int alreadyPresent = 0;
if (menu == NULL)
{
return;
}
if ((Cvar_VariableValue("maxclients") == 1) && if ((Cvar_VariableValue("maxclients") == 1) &&
Com_ServerState()) Com_ServerState())
{ {
@ -194,22 +197,23 @@ M_PushMenu(void (*draw)(void), const char *(*key)(int))
/* if this menu is already open (and on top), /* if this menu is already open (and on top),
close it => toggling behaviour */ close it => toggling behaviour */
if ((m_drawfunc == draw) && (m_keyfunc == key)) if ((m_active.draw == menu->draw) &&
(m_active.key == menu->key))
{ {
M_PopMenu(); M_PopMenu();
return; return;
} }
/* if this menu is already present, drop back to /* if this menu is already present, drop back to
that level to avoid stacking menus by hotkeys */ that level to avoid stacking menus by hotkeys */
for (i = 0; i < m_menudepth; i++) for (i = 0; i < m_menudepth; i++)
{ {
if ((m_layers[i].draw == draw) && if ((m_layers[i].draw == menu->draw) &&
(m_layers[i].key == key)) (m_layers[i].key == menu->key))
{ {
alreadyPresent = 1; alreadyPresent = 1;
break; break;
} }
} }
/* menu was already opened further down the stack */ /* menu was already opened further down the stack */
@ -224,12 +228,12 @@ M_PushMenu(void (*draw)(void), const char *(*key)(int))
return; return;
} }
m_layers[m_menudepth].draw = m_drawfunc; m_layers[m_menudepth].draw = m_active.draw;
m_layers[m_menudepth].key = m_keyfunc; m_layers[m_menudepth].key = m_active.key;
m_menudepth++; m_menudepth++;
m_drawfunc = draw; m_active.draw = menu->draw;
m_keyfunc = key; m_active.key = menu->key;
m_entersound = true; m_entersound = true;
@ -771,7 +775,10 @@ M_Menu_Main_f(void)
} }
} }
M_PushMenu(M_Main_Draw, M_Main_Key); s_main.draw = M_Main_Draw;
s_main.key = M_Main_Key;
M_PushMenu(&s_main);
} }
/* /*
@ -873,7 +880,10 @@ static void
M_Menu_Multiplayer_f(void) M_Menu_Multiplayer_f(void)
{ {
Multiplayer_MenuInit(); Multiplayer_MenuInit();
M_PushMenu(Multiplayer_MenuDraw, Multiplayer_MenuKey); s_multiplayer_menu.draw = Multiplayer_MenuDraw;
s_multiplayer_menu.key = Multiplayer_MenuKey;
M_PushMenu(&s_multiplayer_menu);
} }
/* /*
@ -1140,7 +1150,10 @@ static void
M_Menu_Keys_f(void) M_Menu_Keys_f(void)
{ {
Keys_MenuInit(); Keys_MenuInit();
M_PushMenu(Keys_MenuDraw, Keys_MenuKey); s_keys_menu.draw = Keys_MenuDraw;
s_keys_menu.key = Keys_MenuKey;
M_PushMenu(&s_keys_menu);
} }
/* /*
@ -1290,7 +1303,10 @@ static void
M_Menu_Multiplayer_Keys_f(void) M_Menu_Multiplayer_Keys_f(void)
{ {
MultiplayerKeys_MenuInit(); MultiplayerKeys_MenuInit();
M_PushMenu(MultiplayerKeys_MenuDraw, MultiplayerKeys_MenuKey); s_multiplayer_keys_menu.draw = MultiplayerKeys_MenuDraw;
s_multiplayer_keys_menu.key = MultiplayerKeys_MenuKey;
M_PushMenu(&s_multiplayer_keys_menu);
} }
/* /*
@ -1453,7 +1469,10 @@ static void
M_Menu_ControllerButtons_f(void) M_Menu_ControllerButtons_f(void)
{ {
ControllerButtons_MenuInit(); ControllerButtons_MenuInit();
M_PushMenu(ControllerButtons_MenuDraw, ControllerButtons_MenuKey); s_controller_buttons_menu.draw = ControllerButtons_MenuDraw;
s_controller_buttons_menu.key = ControllerButtons_MenuKey;
M_PushMenu(&s_controller_buttons_menu);
} }
/* /*
@ -1617,7 +1636,10 @@ static void
M_Menu_ControllerAltButtons_f(void) M_Menu_ControllerAltButtons_f(void)
{ {
ControllerAltButtons_MenuInit(); ControllerAltButtons_MenuInit();
M_PushMenu(ControllerAltButtons_MenuDraw, ControllerAltButtons_MenuKey); s_controller_alt_buttons_menu.draw = ControllerAltButtons_MenuDraw;
s_controller_alt_buttons_menu.key = ControllerAltButtons_MenuKey;
M_PushMenu(&s_controller_alt_buttons_menu);
} }
/* /*
@ -1765,7 +1787,10 @@ static void
M_Menu_Stick_f(void) M_Menu_Stick_f(void)
{ {
Stick_MenuInit(); Stick_MenuInit();
M_PushMenu(Stick_MenuDraw, Stick_MenuKey); s_sticks_config_menu.draw = Stick_MenuDraw;
s_sticks_config_menu.key = Stick_MenuKey;
M_PushMenu(&s_sticks_config_menu);
} }
/* /*
@ -1980,7 +2005,10 @@ static void
M_Menu_Gyro_f(void) M_Menu_Gyro_f(void)
{ {
Gyro_MenuInit(); Gyro_MenuInit();
M_PushMenu(Gyro_MenuDraw, Gyro_MenuKey); s_gyro_menu.draw = Gyro_MenuDraw;
s_gyro_menu.key = Gyro_MenuKey;
M_PushMenu(&s_gyro_menu);
} }
/* /*
@ -2151,7 +2179,10 @@ static void
M_Menu_Joy_f(void) M_Menu_Joy_f(void)
{ {
Joy_MenuInit(); Joy_MenuInit();
M_PushMenu(Joy_MenuDraw, Joy_MenuKey); s_joy_menu.draw = Joy_MenuDraw;
s_joy_menu.key = Joy_MenuKey;
M_PushMenu(&s_joy_menu);
} }
/* /*
@ -2553,24 +2584,18 @@ static void
M_Menu_Options_f(void) M_Menu_Options_f(void)
{ {
Options_MenuInit(); Options_MenuInit();
M_PushMenu(Options_MenuDraw, Options_MenuKey); s_options_menu.draw = Options_MenuDraw;
} s_options_menu.key = Options_MenuKey;
/* M_PushMenu(&s_options_menu);
* VIDEO MENU
*/
static void
M_Menu_Video_f(void)
{
VID_MenuInit();
M_PushMenu(VID_MenuDraw, VID_MenuKey);
} }
/* /*
* END GAME MENU * END GAME MENU
*/ */
menuframework_s s_credits;
#define CREDITS_SIZE 256 #define CREDITS_SIZE 256
static int credits_start_time; static int credits_start_time;
static const char **credits; static const char **credits;
@ -2933,7 +2958,7 @@ static const char *roguecredits[] =
}; };
static void static void
M_Credits_MenuDraw(void) M_Credits_Draw(void)
{ {
int i, y; int i, y;
float scale = SCR_GetMenuScale(); float scale = SCR_GetMenuScale();
@ -3076,7 +3101,11 @@ M_Menu_Credits_f(void)
} }
credits_start_time = cls.realtime; credits_start_time = cls.realtime;
M_PushMenu(M_Credits_MenuDraw, M_Credits_Key);
s_credits.draw = M_Credits_Draw;
s_credits.key = M_Credits_Key;
M_PushMenu(&s_credits);
} }
/* /*
@ -3241,7 +3270,10 @@ static void
M_Menu_Mods_f(void) M_Menu_Mods_f(void)
{ {
Mods_MenuInit(); Mods_MenuInit();
M_PushMenu(Mods_MenuDraw, Mods_MenuKey); s_mods_menu.draw = Mods_MenuDraw;
s_mods_menu.key = Mods_MenuKey;
M_PushMenu(&s_mods_menu);
} }
/* /*
@ -3433,7 +3465,10 @@ static void
M_Menu_Game_f(void) M_Menu_Game_f(void)
{ {
Game_MenuInit(); Game_MenuInit();
M_PushMenu(Game_MenuDraw, Game_MenuKey); s_game_menu.draw = Game_MenuDraw;
s_game_menu.key = Game_MenuKey;
M_PushMenu(&s_game_menu);
m_game_cursor = 1; m_game_cursor = 1;
} }
@ -3756,7 +3791,10 @@ M_Menu_LoadGame_f(void)
{ {
LoadSave_AdjustPage(0); LoadSave_AdjustPage(0);
LoadGame_MenuInit(); LoadGame_MenuInit();
M_PushMenu(LoadGame_MenuDraw, LoadGame_MenuKey); s_loadgame_menu.draw = LoadGame_MenuDraw;
s_loadgame_menu.key = LoadGame_MenuKey;
M_PushMenu(&s_loadgame_menu);
} }
/* /*
@ -3911,7 +3949,10 @@ M_Menu_SaveGame_f(void)
LoadSave_AdjustPage(0); LoadSave_AdjustPage(0);
SaveGame_MenuInit(); SaveGame_MenuInit();
M_PushMenu(SaveGame_MenuDraw, SaveGame_MenuKey); s_savegame_menu.draw = SaveGame_MenuDraw;
s_savegame_menu.key = SaveGame_MenuKey;
M_PushMenu(&s_savegame_menu);
} }
/* /*
@ -4112,7 +4153,10 @@ static void
M_Menu_JoinServer_f(void) M_Menu_JoinServer_f(void)
{ {
JoinServer_MenuInit(); JoinServer_MenuInit();
M_PushMenu(JoinServer_MenuDraw, JoinServer_MenuKey); s_joinserver_menu.draw = JoinServer_MenuDraw;
s_joinserver_menu.key = JoinServer_MenuKey;
M_PushMenu(&s_joinserver_menu);
} }
/* /*
@ -4511,7 +4555,10 @@ static void
M_Menu_StartServer_f(void) M_Menu_StartServer_f(void)
{ {
StartServer_MenuInit(); StartServer_MenuInit();
M_PushMenu(StartServer_MenuDraw, StartServer_MenuKey); s_startserver_menu.draw = StartServer_MenuDraw;
s_startserver_menu.key = StartServer_MenuKey;
M_PushMenu(&s_startserver_menu);
} }
/* /*
@ -4992,7 +5039,10 @@ static void
M_Menu_DMOptions_f(void) M_Menu_DMOptions_f(void)
{ {
DMOptions_MenuInit(); DMOptions_MenuInit();
M_PushMenu(DMOptions_MenuDraw, DMOptions_MenuKey); s_dmoptions_menu.draw = DMOptions_MenuDraw;
s_dmoptions_menu.key = DMOptions_MenuKey;
M_PushMenu(&s_dmoptions_menu);
} }
/* /*
@ -5157,7 +5207,10 @@ static void
M_Menu_DownloadOptions_f(void) M_Menu_DownloadOptions_f(void)
{ {
DownloadOptions_MenuInit(); DownloadOptions_MenuInit();
M_PushMenu(DownloadOptions_MenuDraw, DownloadOptions_MenuKey); s_downloadoptions_menu.draw = DownloadOptions_MenuDraw;
s_downloadoptions_menu.key = DownloadOptions_MenuKey;
M_PushMenu(&s_downloadoptions_menu);
} }
/* /*
@ -5233,7 +5286,10 @@ static void
M_Menu_AddressBook_f(void) M_Menu_AddressBook_f(void)
{ {
AddressBook_MenuInit(); AddressBook_MenuInit();
M_PushMenu(AddressBook_MenuDraw, AddressBook_MenuKey); s_addressbook_menu.draw = AddressBook_MenuDraw;
s_addressbook_menu.key = AddressBook_MenuKey;
M_PushMenu(&s_addressbook_menu);
} }
/* /*
@ -6069,13 +6125,18 @@ M_Menu_PlayerConfig_f(void)
} }
Menu_SetStatusBar(&s_multiplayer_menu, NULL); Menu_SetStatusBar(&s_multiplayer_menu, NULL);
M_PushMenu(PlayerConfig_MenuDraw, PlayerConfig_MenuKey); s_player_config_menu.draw = PlayerConfig_MenuDraw;
s_player_config_menu.key = PlayerConfig_MenuKey;
M_PushMenu(&s_player_config_menu);
} }
/* /*
* QUIT MENU * QUIT MENU
*/ */
menuframework_s s_quit_menu;
static const char * static const char *
M_Quit_Key(int key) M_Quit_Key(int key)
{ {
@ -6115,7 +6176,10 @@ M_Quit_Draw(void)
static void static void
M_Menu_Quit_f(void) M_Menu_Quit_f(void)
{ {
M_PushMenu(M_Quit_Draw, M_Quit_Key); s_quit_menu.draw = M_Quit_Draw;
s_quit_menu.key = M_Quit_Key;
M_PushMenu(&s_quit_menu);
} }
void void
@ -6195,7 +6259,10 @@ M_Draw(void)
Draw_FadeScreen(); Draw_FadeScreen();
} }
m_drawfunc(); if (m_active.draw)
{
m_active.draw();
}
/* delay playing the enter sound until after the /* delay playing the enter sound until after the
menu has been drawn, to avoid delay while menu has been drawn, to avoid delay while
@ -6210,10 +6277,10 @@ M_Draw(void)
void void
M_Keydown(int key) M_Keydown(int key)
{ {
if (m_keyfunc) if (m_active.key)
{ {
const char *s; const char *s;
if ((s = m_keyfunc(key)) != 0) if ((s = m_active.key(key)) != 0)
{ {
S_StartLocalSound((char *)s); S_StartLocalSound((char *)s);
} }

View file

@ -882,3 +882,18 @@ VID_MenuKey(int key)
return sound; return sound;
} }
/*
* VIDEO MENU
*/
void
M_Menu_Video_f(void)
{
VID_MenuInit();
s_opengl_menu.draw = VID_MenuDraw;
s_opengl_menu.key = VID_MenuKey;
M_PushMenu(&s_opengl_menu);
}