Small cleanups to OSD and game keybinding code before larger changes. Everything still works at this point.

git-svn-id: https://svn.eduke32.com/eduke32@7147 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2018-11-18 18:05:32 +00:00
parent a6e60e9723
commit 0ea33b1775
2 changed files with 87 additions and 106 deletions

View file

@ -500,14 +500,13 @@ static int32_t osdfunc_listsymbols(osdfuncparm_t const * const parm)
else else
OSD_Printf("%sSymbol listing:\n", osd->draw.highlight); OSD_Printf("%sSymbol listing:\n", osd->draw.highlight);
int const parmlen = Bstrlen(parm->parms[0]); int const parmlen = parm->numparms ? Bstrlen(parm->parms[0]) : 0;
for (i=symbols; i!=NULL; i=i->next) for (i=symbols; i!=NULL; i=i->next)
{ {
if (i->func == OSD_UNALIASED || (parm->numparms == 1 && Bstrncmp(parm->parms[0], i->name, parmlen))) if (i->func == OSD_UNALIASED || (parm->numparms == 1 && Bstrncmp(parm->parms[0], i->name, parmlen)))
continue; continue;
{
int32_t j = hash_find(&h_cvars, i->name); int32_t j = hash_find(&h_cvars, i->name);
if (j != -1 && OSD_CvarModified(&osd->cvars[j])) if (j != -1 && OSD_CvarModified(&osd->cvars[j]))
@ -519,7 +518,6 @@ static int32_t osdfunc_listsymbols(osdfuncparm_t const * const parm)
x += maxwidth; x += maxwidth;
count++; count++;
}
if (x > osd->draw.cols - maxwidth) if (x > osd->draw.cols - maxwidth)
{ {
@ -1882,23 +1880,17 @@ void OSD_Dispatch(const char *cmd)
// //
int32_t OSD_RegisterFunction(const char *pszName, const char *pszDesc, int32_t (*func)(const osdfuncparm_t*)) int32_t OSD_RegisterFunction(const char *pszName, const char *pszDesc, int32_t (*func)(const osdfuncparm_t*))
{ {
osdsymbol_t *symb;
if (!osd) if (!osd)
OSD_Init(); OSD_Init();
symb = osd_findexactsymbol(pszName); auto symb = osd_findexactsymbol(pszName);
if (symb) // allow this now for reusing an alias name if (!symb) // allow reusing an alias name
{ {
symb->help = pszDesc; symb = osd_addsymbol(pszName);
symb->func = func; symb->name = pszName;
return 0;
} }
symb = osd_addsymbol(pszName);
symb->name = pszName;
symb->help = pszDesc; symb->help = pszDesc;
symb->func = func; symb->func = func;

View file

@ -1001,24 +1001,22 @@ const char *const ConsoleButtons[] =
static int32_t osdcmd_bind(osdfuncparm_t const * const parm) static int32_t osdcmd_bind(osdfuncparm_t const * const parm)
{ {
int32_t i, j, repeat;
if (parm->numparms==1 && !Bstrcasecmp(parm->parms[0],"showkeys")) if (parm->numparms==1 && !Bstrcasecmp(parm->parms[0],"showkeys"))
{ {
for (i=0; ConsoleKeys[i].name; i++) for (int i=0; ConsoleKeys[i].name; i++)
OSD_Printf("%s\n",ConsoleKeys[i].name); OSD_Printf("%s\n",ConsoleKeys[i].name);
for (i=0; i<MAXMOUSEBUTTONS; i++) for (auto ConsoleButton : ConsoleButtons)
OSD_Printf("%s\n",ConsoleButtons[i]); OSD_Printf("%s\n",ConsoleButton);
return OSDCMD_OK; return OSDCMD_OK;
} }
if (parm->numparms==0) if (parm->numparms==0)
{ {
int32_t j=0; int j=0;
OSD_Printf("Current key bindings:\n"); OSD_Printf("Current key bindings:\n");
for (i=0; i<MAXBOUNDKEYS+MAXMOUSEBUTTONS; i++) for (int i=0; i<MAXBOUNDKEYS+MAXMOUSEBUTTONS; i++)
if (CONTROL_KeyIsBound(i)) if (CONTROL_KeyIsBound(i))
{ {
j++; j++;
@ -1032,6 +1030,8 @@ static int32_t osdcmd_bind(osdfuncparm_t const * const parm)
return OSDCMD_OK; return OSDCMD_OK;
} }
int i, j, repeat;
for (i=0; ConsoleKeys[i].name; i++) for (i=0; ConsoleKeys[i].name; i++)
if (!Bstrcasecmp(parm->parms[0],ConsoleKeys[i].name)) if (!Bstrcasecmp(parm->parms[0],ConsoleKeys[i].name))
break; break;
@ -1104,18 +1104,19 @@ static int32_t osdcmd_bind(osdfuncparm_t const * const parm)
CONTROL_BindKey(ConsoleKeys[i].id, tempbuf, repeat, ConsoleKeys[i].name); CONTROL_BindKey(ConsoleKeys[i].id, tempbuf, repeat, ConsoleKeys[i].name);
{
char *cp = tempbuf; char *cp = tempbuf;
// Populate the keyboard config menu based on the bind. // Populate the keyboard config menu based on the bind.
// Take care of processing one-to-many bindings properly, too. // Take care of processing one-to-many bindings properly, too.
while ((cp = Bstrstr(cp, "gamefunc_"))) static char const s_gamefunc_[] = "gamefunc_";
size_t constexpr strlen_gamefunc_ = ARRAY_SIZE(s_gamefunc_) - 1;
while ((cp = Bstrstr(cp, s_gamefunc_)))
{ {
char *semi; cp += strlen_gamefunc_;
cp += 9; // skip the "gamefunc_" char *semi = Bstrchr(cp, ';');
semi = Bstrchr(cp, ';');
if (semi) if (semi)
*semi = 0; *semi = 0;
@ -1134,7 +1135,6 @@ static int32_t osdcmd_bind(osdfuncparm_t const * const parm)
OSD_CaptureKey(ConsoleKeys[i].id); OSD_CaptureKey(ConsoleKeys[i].id);
} }
} }
}
if (!OSD_ParsingScript()) if (!OSD_ParsingScript())
OSD_Printf("%s\n",parm->raw); OSD_Printf("%s\n",parm->raw);
@ -1144,21 +1144,16 @@ static int32_t osdcmd_bind(osdfuncparm_t const * const parm)
static int32_t osdcmd_unbindall(osdfuncparm_t const * const UNUSED(parm)) static int32_t osdcmd_unbindall(osdfuncparm_t const * const UNUSED(parm))
{ {
int32_t i;
UNREFERENCED_CONST_PARAMETER(parm); UNREFERENCED_CONST_PARAMETER(parm);
for (i=0; i<MAXBOUNDKEYS; i++) for (int i = 0; i < MAXBOUNDKEYS; ++i)
CONTROL_FreeKeyBind(i); CONTROL_FreeKeyBind(i);
for (i=0; i<MAXMOUSEBUTTONS; i++) for (int i = 0; i < MAXMOUSEBUTTONS; ++i)
CONTROL_FreeMouseBind(i); CONTROL_FreeMouseBind(i);
for (i=0; i<NUMGAMEFUNCTIONS; i++) for (auto &KeyboardKey : ud.config.KeyboardKeys)
{ KeyboardKey[0] = KeyboardKey[1] = 0xff;
ud.config.KeyboardKeys[i][0] = ud.config.KeyboardKeys[i][1] = 0xff;
// CONTROL_MapKey(i, ud.config.KeyboardKeys[i][0], ud.config.KeyboardKeys[i][1]);
}
if (!OSD_ParsingScript()) if (!OSD_ParsingScript())
OSD_Printf("unbound all controls\n"); OSD_Printf("unbound all controls\n");
@ -1168,35 +1163,30 @@ static int32_t osdcmd_unbindall(osdfuncparm_t const * const UNUSED(parm))
static int32_t osdcmd_unbind(osdfuncparm_t const * const parm) static int32_t osdcmd_unbind(osdfuncparm_t const * const parm)
{ {
int32_t i; if (parm->numparms != 1)
if (parm->numparms < 1) return OSDCMD_SHOWHELP;
for (i=0; ConsoleKeys[i].name; i++)
if (!Bstrcasecmp(parm->parms[0],ConsoleKeys[i].name))
break;
if (!ConsoleKeys[i].name)
{
for (i=0; i<MAXMOUSEBUTTONS; i++)
if (!Bstrcasecmp(parm->parms[0],ConsoleButtons[i]))
break;
if (i >= MAXMOUSEBUTTONS)
return OSDCMD_SHOWHELP; return OSDCMD_SHOWHELP;
CONTROL_FreeMouseBind(i); for (auto ConsoleKey : ConsoleKeys)
{
OSD_Printf("unbound %s\n",ConsoleButtons[i]); if (ConsoleKey.name && !Bstrcasecmp(parm->parms[0], ConsoleKey.name))
{
CONTROL_FreeKeyBind(ConsoleKey.id);
OSD_Printf("unbound key %s\n", ConsoleKey.name);
return OSDCMD_OK; return OSDCMD_OK;
} }
}
CONTROL_FreeKeyBind(ConsoleKeys[i].id); for (int i = 0; i < MAXMOUSEBUTTONS; i++)
{
OSD_Printf("unbound key %s\n",ConsoleKeys[i].name); if (!Bstrcasecmp(parm->parms[0], ConsoleButtons[i]))
{
CONTROL_FreeMouseBind(i);
OSD_Printf("unbound %s\n", ConsoleButtons[i]);
return OSDCMD_OK; return OSDCMD_OK;
}
}
return OSDCMD_SHOWHELP;
} }
static int32_t osdcmd_quicksave(osdfuncparm_t const * const UNUSED(parm)) static int32_t osdcmd_quicksave(osdfuncparm_t const * const UNUSED(parm))
@ -1619,8 +1609,6 @@ static int32_t osdcmd_cvar_set_multi(osdfuncparm_t const * const parm)
int32_t registerosdcommands(void) int32_t registerosdcommands(void)
{ {
uint32_t i;
static osdcvardata_t cvars_game[] = static osdcvardata_t cvars_game[] =
{ {
{ "crosshair", "enable/disable crosshair", (void *)&ud.crosshair, CVAR_BOOL, 0, 1 }, { "crosshair", "enable/disable crosshair", (void *)&ud.crosshair, CVAR_BOOL, 0, 1 },
@ -1756,17 +1744,17 @@ int32_t registerosdcommands(void)
osdcmd_cheatsinfo_stat.cheatnum = -1; osdcmd_cheatsinfo_stat.cheatnum = -1;
for (i=0; i<ARRAY_SIZE(cvars_game); i++) for (auto & cv : cvars_game)
{ {
switch (cvars_game[i].flags & (CVAR_FUNCPTR|CVAR_MULTI)) switch (cv.flags & (CVAR_FUNCPTR|CVAR_MULTI))
{ {
case CVAR_FUNCPTR: case CVAR_FUNCPTR:
OSD_RegisterCvar(&cvars_game[i], osdcmd_cvar_set_game); break; OSD_RegisterCvar(&cv, osdcmd_cvar_set_game); break;
case CVAR_MULTI: case CVAR_MULTI:
case CVAR_FUNCPTR|CVAR_MULTI: case CVAR_FUNCPTR|CVAR_MULTI:
OSD_RegisterCvar(&cvars_game[i], osdcmd_cvar_set_multi); break; OSD_RegisterCvar(&cv, osdcmd_cvar_set_multi); break;
default: default:
OSD_RegisterCvar(&cvars_game[i], osdcmd_cvar_set); break; OSD_RegisterCvar(&cv, osdcmd_cvar_set); break;
} }
} }
@ -1789,21 +1777,22 @@ int32_t registerosdcommands(void)
OSD_RegisterFunction("disconnect","disconnect: disconnects from the local multiplayer game", osdcmd_disconnect); OSD_RegisterFunction("disconnect","disconnect: disconnects from the local multiplayer game", osdcmd_disconnect);
#endif #endif
for (i=0; i<NUMGAMEFUNCTIONS; i++) for (auto & func : gamefunctions)
{ {
char *t; if (func[0] == '\0')
int32_t j;
if (gamefunctions[i][0] == '\0')
continue; continue;
// if (!Bstrcmp(gamefunctions[i],"Show_Console")) continue; // if (!Bstrcmp(gamefunctions[i],"Show_Console")) continue;
Bsprintf(tempbuf,"gamefunc_%s",gamefunctions[i]); Bsprintf(tempbuf, "gamefunc_%s", func);
t = Xstrdup(tempbuf);
for (j=Bstrlen(t); j>=0; j--) char *const t = Xstrdup(tempbuf);
int const len = Bstrlen(t);
for (int j=0; j <= len; j++)
t[j] = Btolower(t[j]); t[j] = Btolower(t[j]);
Bstrcat(tempbuf,": game button");
Bstrcat(tempbuf, ": game button");
OSD_RegisterFunction(t, Xstrdup(tempbuf), osdcmd_button); OSD_RegisterFunction(t, Xstrdup(tempbuf), osdcmd_button);
} }