Options Menu for custom console variables

https://git.do.srb2.org/STJr/SRB2/-/merge_requests/2380#9cc73f4b99ff925448389969b0ade6ba3947fc31
This commit is contained in:
Alug committed 2024-03-04 16:57:00 +01:00
1 parent d6dcbb8ba8
commit 8093b16f28
3 files changed
+136 -11

No files matched your search

+52 -9
View File
@@ -17,6 +17,11 @@
#include "byteptr.h"
#include "z_zone.h"
#include "d_netfil.h"
#include "m_menu.h"
#include "w_wad.h"
#include "lua_script.h"
#include "lua_libs.h"
#include "lua_hud.h" // hud_running errors
@@ -186,8 +191,8 @@ static int lib_comAddCommand(lua_State *L)
const char *luaname = luaL_checkstring(L, 1);
// must store in all lowercase
char *name = Z_StrDup(luaname);
strlwr(name);
char *comname = Z_StrDup(luaname);
strlwr(comname);
luaL_checktype(L, 2, LUA_TFUNCTION);
NOHUD
@@ -220,25 +225,26 @@ static int lib_comAddCommand(lua_State *L)
lua_pushvalue(L, 3);
lua_rawseti(L, -2, 2);
lua_setfield(L, -2, name);
lua_setfield(L, -2, comname);
// Try to add the Lua command
com_return = COM_AddLuaCommand(name);
com_return = COM_AddLuaCommand(comname);
if (com_return < 0)
{ // failed to add -- free the lowercased name and return error
Z_Free(name);
Z_Free(comname);
return luaL_error(L, "Couldn't add a new console command \"%s\"", luaname);
}
else if (com_return == 1)
{ // command existed already -- free our name as the old string will continue to be used
CONS_Printf("Replaced command \"%s\"\n", name);
Z_Free(name);
CONS_Printf("Replaced command \"%s\"\n", comname);
Z_Free(comname);
}
else
{ // new command was added -- do NOT free the string as it will forever be used by the console
CONS_Printf("Added command \"%s\"\n", name);
CONS_Printf("Added command \"%s\"\n", comname);
}
return 0;
}
@@ -314,12 +320,17 @@ static int lib_cvRegisterVar(lua_State *L)
const char *k;
lua_Integer i;
consvar_t *cvar;
const char* category = NULL;
const char* menu_name = NULL;
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 1); // Clear out all other possible arguments, leaving only the first one.
NOHUD
cvar = lua_newuserdata(L, sizeof(consvar_t));
luaL_getmetatable(L, META_CVAR);
lua_setmetatable(L, -2);
boolean hasminmax = false;
#define FIELDERROR(f, e) luaL_error(L, "bad value for " LUA_QL(f) " in table passed to " LUA_QL("CV_RegisterVar") " (%s)", e);
#define TYPEERROR(f, t) FIELDERROR(f, va("%s expected, got %s", lua_typename(L, t), luaL_typename(L, -1)))
@@ -385,13 +396,17 @@ static int lib_cvRegisterVar(lua_State *L)
if (lua_type(L, 5) != LUA_TSTRING
|| lua_type(L, 6) != LUA_TNUMBER)
FIELDERROR("PossibleValue", "custom PossibleValue table requires a format of string=integer, i.e. {MIN=0, MAX=9999}");
cvpv[i].strvalue = Z_StrDup(lua_tostring(L, 5));
char *strvalue = Z_StrDup(lua_tostring(L, 5));
if (fasticmp(strvalue, "MIN"))
hasminmax = true;
cvpv[i].strvalue = strvalue;
cvpv[i].value = (INT32)lua_tonumber(L, 6);
i++;
lua_pop(L, 1);
}
cvpv[i].value = 0;
cvpv[i].strvalue = NULL;
hasminmax = false;
cvar->PossibleValue = cvpv;
} else
FIELDERROR("PossibleValue", va("%s or CV_PossibleValue_t expected, got %s", lua_typename(L, LUA_TTABLE), luaL_typename(L, -1)))
@@ -405,6 +420,18 @@ static int lib_cvRegisterVar(lua_State *L)
lua_pop(L, 1);
cvar->func = Lua_OnChange;
}
else if (((i == 5 && !(cvar->flags & CV_CALL))
|| (cvar->flags & CV_CALL && i == 7))
|| (k && fasticmp(k, "category")))
{
category = lua_isnoneornil(L, 4) ? NULL : lua_tostring(L, 4);
}
else if (((i == 6 && !(cvar->flags & CV_CALL))
|| (cvar->flags & CV_CALL && i == 8))
|| (k && fasticmp(k, "menuname")))
{
menu_name = lua_isnoneornil(L, 4) ? NULL : lua_tostring(L, 4);
}
lua_pop(L, 1);
}
#undef FIELDERROR
@@ -429,6 +456,22 @@ static int lib_cvRegisterVar(lua_State *L)
if (cvar->flags & CV_MODIFIED)
return luaL_error(L, "failed to register cvar (probable conflict with internal variable/command names)");
if (!(((cvar->flags & CV_HIDEN)) || (cvar->flags & CV_NOSHOWHELP))
{
if (!category)
{
char *temp = strdup(wadfiles[numwadfiles - 1]->filename);
nameonly(temp);
category = temp;
}
if (menu_name && menu_name[0] != '\0')
M_SlotCvarIntoModMenu(cvar, category, menu_name, hasminmax);
else
M_SlotCvarIntoModMenu(cvar, category, cvar->name, hasminmax);
}
// return cvar userdata
return 1;
}
+78 -1
View File
@@ -50,6 +50,10 @@
#include "p_setup.h"
#include "f_finale.h"
#include "lua_libs.h"
#include "fastcmp.h"
#ifdef HWRENDER
#include "hardware/hw_main.h"
#endif
@@ -302,6 +306,7 @@ static void M_SetupMultiHandler(INT32 choice);
menu_t OP_ControlsDef, OP_AllControlsDef;
menu_t OP_MouseOptionsDef, OP_Mouse2OptionsDef;
menu_t OP_Joystick1Def, OP_Joystick2Def, OP_Joystick3Def, OP_Joystick4Def;
menu_t OP_CustomCvarMenuDef;
static void M_VideoModeMenu(INT32 choice);
static void M_Setup1PControlsMenu(INT32 choice);
static void M_Setup2PControlsMenu(INT32 choice);
@@ -346,6 +351,8 @@ static void M_AddonsInternal();
static void M_Addons(INT32 choice);
static void M_LocalSkins(INT32 choice);
static void M_AddonsOptions(INT32 choice);
static void M_CustomCvarMenu(INT32 choice);
static patch_t *addonsp[NUM_EXT+5];
static void M_DeleteProtocol(void);
@@ -1086,7 +1093,8 @@ static menuitem_t OP_MainMenu[] =
{IT_SUBMENU|IT_STRING, NULL, "Gameplay Options...", &OP_GameOptionsDef, 60},
{IT_SUBMENU|IT_STRING, NULL, "Server Options...", &OP_ServerOptionsDef, 70},
{IT_SUBMENU|IT_STRING, NULL, "Data Options...", &OP_DataOptionsDef, 90},
{IT_SUBMENU|IT_STRING, NULL, "Data Options...", &OP_DataOptionsDef, 90},
{IT_CALL|IT_STRING, NULL, "Custom Options...", M_CustomCvarMenu, 100},
{IT_CALL|IT_STRING, NULL, "Tricks & Secrets (F1)", M_Manual, 110},
{IT_CALL|IT_STRING, NULL, "Play Credits", M_Credits, 120},
@@ -2273,6 +2281,8 @@ enum
syncspecialonly,
};
menuitem_t OP_CustomCvarMenu[MAXMENUCCVARS];
// ==========================================================================
// ALL MENU DEFINITIONS GO HERE
// ==========================================================================
@@ -2768,6 +2778,23 @@ menu_t OP_BirdDef = DEFAULTMENUSTYLE(NULL, OP_BirdMenu, &OP_MainDef, 30, 30);
menu_t OP_TiltDef = DEFAULTMENUSTYLE(NULL, OP_TiltMenu, &OP_BirdDef, 30, 60);
menu_t OP_AdvancedBirdDef = DEFAULTMENUSTYLE(NULL, OP_AdvancedBirdMenu, &OP_BirdDef, 30, 60);
INT16 ccvarposition = 0;
static void M_CustomCvarMenu(INT32 choice)
{
(void)choice;
if (ccvarposition)
M_SetupNextMenu(&OP_CustomCvarMenuDef);
else
M_StartMessage(M_GetText("No custom options were found\n"), NULL, MM_NOTHING);
}
/*menu_t OP_CustomCvarMenuDef = DEFAULTSCROLLMENUSTYLE(
MTREE3(MN_OP_MAIN, MN_OP_DATA, MN_OP_ADDONS),
"M_ADDONS", OP_CustomCvarMenu, &OP_MainDef, 30, 30);*/
menu_t OP_CustomCvarMenuDef = DEFAULTSCROLLSTYLE("M_ADDONS", OP_CustomCvarMenu, &OP_MainDef, 30, 30);
menu_t OP_ForkedBirdDef = {
NULL,
sizeof(OP_ForkedBirdMenu)/sizeof(menuitem_t),
@@ -7648,6 +7675,56 @@ void M_PopupMasterServerRules(void)
#endif
}
#define CCVHEIGHT 5
#define CCVHEIGHTHEADER 1
#define CCVHEIGHTHEADERAFTER 6
UINT16 ccvaralphakey = 4;
INT16 ccvarlaststheader = 0;
INT32 CVARSETUP;
void M_SlotCvarIntoModMenu(consvar_t* cvar, const char* category, const char* name, boolean minmax)
{
if (ccvarposition == INT16_MAX)
return;
if (ccvarposition >= MAXMENUCCVARS - 2)
{
CONS_Printf("failed to register cvar into custom settings menu as menu reached limit\n");
ccvarposition = INT16_MAX;
return;
}
if (!CVARSETUP)
{
CONS_Printf("custom settings menu initiation\n");
for (CVARSETUP = 0; CVARSETUP < MAXMENUCCVARS; ++CVARSETUP)
OP_CustomCvarMenu[CVARSETUP] = (menuitem_t){IT_DISABLED, NULL, "", 0, INT16_MAX};
}
if (category && ((ccvarposition == 0 && category[0] != '\0') || !fasticmp(category, OP_CustomCvarMenu[ccvarlaststheader].text)))
{
ccvarlaststheader = ccvarposition;
ccvaralphakey += CCVHEIGHTHEADER;
OP_CustomCvarMenu[ccvarposition] = (menuitem_t){IT_HEADER, NULL, Z_StrDup(category), NULL, ccvaralphakey};
ccvaralphakey += CCVHEIGHTHEADERAFTER;
++ccvarposition;
}
if (minmax)
OP_CustomCvarMenu[ccvarposition] = (menuitem_t){ IT_STRING | IT_CVAR | IT_CV_SLIDER, NULL, Z_StrDup(name), cvar, ccvaralphakey };
else if (cvar->flags & CV_FLOAT)
OP_CustomCvarMenu[ccvarposition] = (menuitem_t){ IT_STRING | IT_CVAR , NULL, Z_StrDup(name), cvar, ccvaralphakey };
else
OP_CustomCvarMenu[ccvarposition] = (menuitem_t){ IT_STRING | IT_CVAR, NULL, Z_StrDup(name), cvar, ccvaralphakey };
ccvaralphakey += CCVHEIGHT;
++ccvarposition;
}
// ======
// CHEATS
// ======
+6 -1
View File
@@ -147,6 +147,8 @@ boolean M_CanShowLevelInList(INT32 mapnum, INT32 gt);
#define MAXSTRINGLENGTH 32
#define MAXMENUCCVARS 999
#define MAXTOOLTIPS 255
typedef union
@@ -171,7 +173,7 @@ typedef struct menuitem_s
void *itemaction;
// hotkey in menu or y of the item
UINT8 alphaKey;
UINT16 alphaKey;
} menuitem_t;
extern menuitem_t PlayerMenu[MAXSKINS];
@@ -289,6 +291,8 @@ void M_PopupMasterServerRules(void);
void M_PopupMasterServerConnectError(void);
#endif
void M_SlotCvarIntoModMenu(consvar_t* cvar, const char* category, const char* name, boolean minmax);
// These defines make it a little easier to make menus
#define DEFAULTMENUSTYLE(header, source, prev, x, y)\
{\
@@ -316,6 +320,7 @@ void M_PopupMasterServerConnectError(void);
NULL\
}
#define PAUSEMENUSTYLE(source, x, y)\
{\
NULL,\