From 0ea33b17753547b82c61c3bf883fde57f8ac57bc Mon Sep 17 00:00:00 2001 From: terminx Date: Sun, 18 Nov 2018 18:05:32 +0000 Subject: [PATCH] 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 --- source/build/src/osd.cpp | 36 +++----- source/duke3d/src/osdcmds.cpp | 157 ++++++++++++++++------------------ 2 files changed, 87 insertions(+), 106 deletions(-) diff --git a/source/build/src/osd.cpp b/source/build/src/osd.cpp index 807517e39..166b5cf4b 100644 --- a/source/build/src/osd.cpp +++ b/source/build/src/osd.cpp @@ -500,26 +500,24 @@ static int32_t osdfunc_listsymbols(osdfuncparm_t const * const parm) else 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) { if (i->func == OSD_UNALIASED || (parm->numparms == 1 && Bstrncmp(parm->parms[0], i->name, parmlen))) continue; + int32_t j = hash_find(&h_cvars, i->name); + + if (j != -1 && OSD_CvarModified(&osd->cvars[j])) { - int32_t j = hash_find(&h_cvars, i->name); - - if (j != -1 && OSD_CvarModified(&osd->cvars[j])) - { - OSD_Printf("%s*", osd->draw.highlight); - OSD_Printf("%-*s",maxwidth-1,i->name); - } - else OSD_Printf("%-*s",maxwidth,i->name); - - x += maxwidth; - count++; + OSD_Printf("%s*", osd->draw.highlight); + OSD_Printf("%-*s",maxwidth-1,i->name); } + else OSD_Printf("%-*s",maxwidth,i->name); + + x += maxwidth; + count++; 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*)) { - osdsymbol_t *symb; - if (!osd) 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->func = func; - return 0; + symb = osd_addsymbol(pszName); + symb->name = pszName; } - symb = osd_addsymbol(pszName); - - symb->name = pszName; symb->help = pszDesc; symb->func = func; diff --git a/source/duke3d/src/osdcmds.cpp b/source/duke3d/src/osdcmds.cpp index d02bdc2f3..1b7c34dcc 100644 --- a/source/duke3d/src/osdcmds.cpp +++ b/source/duke3d/src/osdcmds.cpp @@ -1001,24 +1001,22 @@ const char *const ConsoleButtons[] = static int32_t osdcmd_bind(osdfuncparm_t const * const parm) { - int32_t i, j, repeat; - 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); - for (i=0; inumparms==0) { - int32_t j=0; + int j=0; OSD_Printf("Current key bindings:\n"); - for (i=0; iparms[0],ConsoleKeys[i].name)) break; @@ -1104,35 +1104,35 @@ static int32_t osdcmd_bind(osdfuncparm_t const * const parm) CONTROL_BindKey(ConsoleKeys[i].id, tempbuf, repeat, ConsoleKeys[i].name); + char *cp = tempbuf; + + // Populate the keyboard config menu based on the bind. + // Take care of processing one-to-many bindings properly, too. + static char const s_gamefunc_[] = "gamefunc_"; + size_t constexpr strlen_gamefunc_ = ARRAY_SIZE(s_gamefunc_) - 1; + + while ((cp = Bstrstr(cp, s_gamefunc_))) { - char *cp = tempbuf; + cp += strlen_gamefunc_; - // Populate the keyboard config menu based on the bind. - // Take care of processing one-to-many bindings properly, too. - while ((cp = Bstrstr(cp, "gamefunc_"))) + char *semi = Bstrchr(cp, ';'); + + if (semi) + *semi = 0; + + j = CONFIG_FunctionNameToNum(cp); + + if (semi) + cp = semi+1; + + if (j != -1) { - char *semi; - - cp += 9; // skip the "gamefunc_" - - semi = Bstrchr(cp, ';'); - if (semi) - *semi = 0; - - j = CONFIG_FunctionNameToNum(cp); - - if (semi) - cp = semi+1; - - if (j != -1) - { - ud.config.KeyboardKeys[j][1] = ud.config.KeyboardKeys[j][0]; - ud.config.KeyboardKeys[j][0] = ConsoleKeys[i].id; + ud.config.KeyboardKeys[j][1] = ud.config.KeyboardKeys[j][0]; + ud.config.KeyboardKeys[j][0] = ConsoleKeys[i].id; // CONTROL_MapKey(j, ConsoleKeys[i].id, ud.config.KeyboardKeys[j][0]); - if (j == gamefunc_Show_Console) - OSD_CaptureKey(ConsoleKeys[i].id); - } + if (j == gamefunc_Show_Console) + OSD_CaptureKey(ConsoleKeys[i].id); } } @@ -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)) { - int32_t i; - UNREFERENCED_CONST_PARAMETER(parm); - for (i=0; inumparms != 1) + return OSDCMD_SHOWHELP; - 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 (auto ConsoleKey : ConsoleKeys) { - for (i=0; iparms[0],ConsoleButtons[i])) - break; - - if (i >= MAXMOUSEBUTTONS) - return OSDCMD_SHOWHELP; - - CONTROL_FreeMouseBind(i); - - OSD_Printf("unbound %s\n",ConsoleButtons[i]); - - return OSDCMD_OK; + if (ConsoleKey.name && !Bstrcasecmp(parm->parms[0], ConsoleKey.name)) + { + CONTROL_FreeKeyBind(ConsoleKey.id); + OSD_Printf("unbound key %s\n", ConsoleKey.name); + return OSDCMD_OK; + } } - CONTROL_FreeKeyBind(ConsoleKeys[i].id); + for (int i = 0; i < MAXMOUSEBUTTONS; i++) + { + if (!Bstrcasecmp(parm->parms[0], ConsoleButtons[i])) + { + CONTROL_FreeMouseBind(i); + OSD_Printf("unbound %s\n", ConsoleButtons[i]); + return OSDCMD_OK; + } + } - OSD_Printf("unbound key %s\n",ConsoleKeys[i].name); - - return OSDCMD_OK; + return OSDCMD_SHOWHELP; } 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) { - uint32_t i; - static osdcvardata_t cvars_game[] = { { "crosshair", "enable/disable crosshair", (void *)&ud.crosshair, CVAR_BOOL, 0, 1 }, @@ -1756,17 +1744,17 @@ int32_t registerosdcommands(void) osdcmd_cheatsinfo_stat.cheatnum = -1; - for (i=0; i=0; j--) + Bsprintf(tempbuf, "gamefunc_%s", func); + + char *const t = Xstrdup(tempbuf); + int const len = Bstrlen(t); + + for (int j=0; j <= len; j++) t[j] = Btolower(t[j]); - Bstrcat(tempbuf,": game button"); + + Bstrcat(tempbuf, ": game button"); OSD_RegisterFunction(t, Xstrdup(tempbuf), osdcmd_button); }