From 3646850ff65b039f5396fc92278a15fe748e3422 Mon Sep 17 00:00:00 2001 From: James R Date: Fri, 2 Feb 2024 04:28:18 -0800 Subject: [PATCH] Menus: handle caret codes in text fields - All text boxes render caret code colors - On-screen keyboard updates colors in real time --- src/k_menudraw.c | 7 ++++--- src/sanitize.cpp | 10 ++++++++++ src/sanitize.h | 3 +++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/k_menudraw.c b/src/k_menudraw.c index 8a1102665..c147b1985 100644 --- a/src/k_menudraw.c +++ b/src/k_menudraw.c @@ -69,6 +69,7 @@ #include "i_time.h" #include "m_easing.h" +#include "sanitize.h" #ifdef PC_DOS #include // for snprintf @@ -532,12 +533,12 @@ 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); - V_DrawString(x + 8, y + 12, 0, menutyping.cache); + INT32 textwidth = M_DrawCaretString(x + 8, y + 12, menutyping.cache, true); if (skullAnimCounter < 4 && menutyping.menutypingclose == false && menutyping.menutypingfade == (menutyping.keyboardtyping ? 9 : 18)) { - V_DrawCharacter(x + 8 + V_StringWidth(menutyping.cache, 0), y + 12 + 1, '_', false); + V_DrawCharacter(x + 8 + textwidth, y + 12 + 1, '_', false); } const INT32 buttonwidth = ((boxwidth + 1)/NUMVIRTUALKEYSINROW); @@ -4493,7 +4494,7 @@ box_found: V_DrawMenuString(x + (skullAnimCounter/5) + 7, y + 9, highlightflags, "\x1D"); } - V_DrawString(x + xoffs + 8, y + 9, 0, cv->string); + M_DrawCaretString(x + xoffs + 8, y + 9, cv->string, false); y += LINEHEIGHT; } diff --git a/src/sanitize.cpp b/src/sanitize.cpp index 9aa58fff6..fc03a1cfc 100644 --- a/src/sanitize.cpp +++ b/src/sanitize.cpp @@ -15,6 +15,7 @@ #include "doomtype.h" #include "sanitize.h" +#include "v_draw.hpp" using namespace srb2::sanitize; @@ -131,3 +132,12 @@ void D_ParseCarets(char *out, const char *in, size_t out_size) { strlcpy(out, parse_carets(in, ParseMode::kConsume).c_str(), out_size); } + +INT32 M_DrawCaretString(INT32 x, INT32 y, const char *string, boolean preserve) +{ + using srb2::Draw; + Draw::TextElement text(parse_carets(string, preserve ? ParseMode::kPreserve : ParseMode::kConsume)); + text.font(Draw::Font::kConsole); + Draw(x, y).text(text); + return text.width(); +} diff --git a/src/sanitize.h b/src/sanitize.h index f413923b5..f6a56d582 100644 --- a/src/sanitize.h +++ b/src/sanitize.h @@ -45,6 +45,9 @@ extern "C" { void D_SanitizeKeepColors(char *out, const char *in, size_t out_size); // SanitizeMode::kKeepColors void D_ParseCarets(char *out, const char *in, size_t out_size); // ParseMode::kConsume +// returns string width in pixels +INT32 M_DrawCaretString(INT32 x, INT32 y, const char *string, boolean preserve); + #ifdef __cplusplus } // extern "C" #endif