Virtual Keyboard: dynamically allocate cache; allow full size of replay title buffer

This commit is contained in:
James R 2024-02-29 22:51:22 -08:00
parent 38e1ae1c53
commit 8c76dee523
5 changed files with 31 additions and 9 deletions

View file

@ -4113,6 +4113,7 @@ boolean G_CheckDemoTitleEntry(void)
demo.willsave = true;
M_OpenVirtualKeyboard(
false,
sizeof demo.titlename,
[](const char* replace) -> const char*
{
if (replace)

View file

@ -557,7 +557,8 @@ extern struct menutyping_s
vkb_query_fn_t queryfn; // callback on open and close
menu_t *dummymenu;
char cache[MAXSTRINGLENGTH]; // cached string
size_t cachelen;
char *cache; // cached string
} menutyping;
// While typing, we'll have a fade strongly darken the screen to overlay the typing menu instead
@ -685,7 +686,7 @@ void M_PlayMenuJam(void);
boolean M_ConsiderSealedSwapAlert(void);
void M_OpenVirtualKeyboard(boolean gamepad, vkb_query_fn_t queryfn, menu_t *dummymenu);
void M_OpenVirtualKeyboard(boolean gamepad, size_t cachelen, vkb_query_fn_t queryfn, menu_t *dummymenu);
void M_AbortVirtualKeyboard(void);
void M_MenuTypingInput(INT32 key);

View file

@ -495,6 +495,21 @@ static void M_DrawMenuTooltips(void)
}
}
static const char *M_MenuTypingCroppedString(void)
{
static char buf[36];
const char *p = menutyping.cache;
size_t n = strlen(p);
if (n > sizeof buf)
{
p += n - sizeof buf;
n = sizeof buf;
}
memcpy(buf, p, n);
buf[n] = '\0';
return buf;
}
// Draws the typing submenu
static void M_DrawMenuTyping(void)
{
@ -534,7 +549,7 @@ static void M_DrawMenuTyping(void)
V_DrawFill(x + 4, y + 4 + 5, 1, 8+6, 121);
V_DrawFill(x + 5 + boxwidth - 8, y + 4 + 5, 1, 8+6, 121);
INT32 textwidth = M_DrawCaretString(x + 8, y + 12, menutyping.cache, true);
INT32 textwidth = M_DrawCaretString(x + 8, y + 12, M_MenuTypingCroppedString(), true);
if (skullAnimCounter < 4
&& menutyping.menutypingclose == false
&& menutyping.menutypingfade == (menutyping.keyboardtyping ? 9 : 18))

View file

@ -1136,7 +1136,7 @@ static void M_HandleMenuInput(void)
if (M_MenuConfirmPressed(pid))
{
// If we entered this menu by pressing a menu Key, default to keyboard typing, otherwise use controller.
M_OpenVirtualKeyboard(thisMenuKey == -1, M_QueryCvarAction, NULL);
M_OpenVirtualKeyboard(thisMenuKey == -1, MAXSTRINGLENGTH, M_QueryCvarAction, NULL);
return;
}
else if (M_MenuExtraPressed(pid))

View file

@ -5,6 +5,7 @@
#include "../../s_sound.h"
#include "../../console.h" // CON_ShiftChar
#include "../../i_system.h" // I_Clipboard funcs
#include "../../z_zone.h"
// Typing "sub"-menu
struct menutyping_s menutyping;
@ -91,9 +92,9 @@ boolean M_ChangeStringCvar(INT32 choice)
const char *paste = I_ClipboardPaste();
if (paste == NULL || paste[0] == '\0')
;
else if (len < MAXSTRINGLENGTH - 1)
else if (len < menutyping.cachelen)
{
strlcat(menutyping.cache, paste, MAXSTRINGLENGTH);
strlcat(menutyping.cache, paste, menutyping.cachelen + 1);
S_StartSound(NULL, sfx_tmxbdn); // Tails
}
@ -146,7 +147,7 @@ boolean M_ChangeStringCvar(INT32 choice)
if (choice >= 32 && choice <= 127)
{
len = strlen(menutyping.cache);
if (len < MAXSTRINGLENGTH - 1)
if (len < menutyping.cachelen)
{
menutyping.cache[len++] = (char)choice;
menutyping.cache[len] = 0;
@ -189,6 +190,8 @@ void M_AbortVirtualKeyboard(void)
return;
menutyping.active = false;
Z_Free(menutyping.cache);
if (currentMenu == menutyping.dummymenu)
M_GoBack(0);
}
@ -415,7 +418,7 @@ void M_MenuTypingInput(INT32 key)
}
}
void M_OpenVirtualKeyboard(boolean gamepad, vkb_query_fn_t queryfn, menu_t *dummymenu)
void M_OpenVirtualKeyboard(boolean gamepad, size_t cachelen, vkb_query_fn_t queryfn, menu_t *dummymenu)
{
menutyping.keyboardtyping = !gamepad;
menutyping.active = true;
@ -423,7 +426,9 @@ void M_OpenVirtualKeyboard(boolean gamepad, vkb_query_fn_t queryfn, menu_t *dumm
menutyping.queryfn = queryfn;
menutyping.dummymenu = dummymenu;
strlcpy(menutyping.cache, queryfn(NULL), MAXSTRINGLENGTH);
menutyping.cachelen = cachelen;
Z_Malloc(cachelen + 1, PU_STATIC, &menutyping.cache);
strlcpy(menutyping.cache, queryfn(NULL), cachelen + 1);
if (dummymenu)
{