change for chat input handling on color

has a bug where the text after the cursor gets stripped of its color, need to make a function to backstep on a string and find the last used color codes, or get all color codes used before the string is split, and apply them to the start of the next string
This commit is contained in:
DrPerkyLegit 2026-04-10 00:56:39 -04:00
parent af3df4c636
commit 2321135a1e
2 changed files with 30 additions and 6 deletions

View file

@ -17,6 +17,29 @@ bool ChatScreen::isAllowedChatChar(wchar_t c)
return c >= 0x20 && (c == L'\u00A7' || allowedChars.empty() || allowedChars.find(c) != wstring::npos);
}
int VisibleCharLength(wstring message) {
static std::unordered_set<wchar_t> validStyleCharacters = {
L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7', L'8', L'9',
L'a', L'b', L'c', L'd', L'e', L'f',
};
int results = 0;
for (int i = 0; i < message.size(); i++) {
wchar_t _char = message[i];
if (_char == L'§' && (i + 1) < message.size()) {
wchar_t colorChar = message[i + 1];
if (validStyleCharacters.find(colorChar) != validStyleCharacters.end()) {
i++; //do this so it increments 2 instead of 1
continue;
}
}
results++;
}
return results;
}
ChatScreen::ChatScreen()
{
frame = 0;
@ -131,7 +154,7 @@ void ChatScreen::keyPressed(wchar_t ch, int eventKey)
cursorIndex--;
return;
}
if (isAllowedChatChar(ch) && static_cast<int>(message.length()) < SharedConstants::maxChatLength)
if (isAllowedChatChar(ch) && VisibleCharLength(message) < SharedConstants::maxVisibleLength)
{
message.insert(cursorIndex, 1, ch);
cursorIndex++;
@ -147,12 +170,12 @@ void ChatScreen::render(int xm, int ym, float a)
x += font->width(prefix);
wstring beforeCursor = message.substr(0, cursorIndex);
wstring afterCursor = message.substr(cursorIndex);
drawStringLiteral(font, beforeCursor, x, height - 12, 0xe0e0e0);
x += font->widthLiteral(beforeCursor);
drawString(font, beforeCursor, x, height - 12, 0xe0e0e0);
x += font->width(beforeCursor);
if (frame / 6 % 2 == 0)
drawString(font, L"_", x, height - 12, 0xe0e0e0);
drawString(font, L"_", x, height - 12, 0xe0e0e0); //todo: fix this breaking the chat color in afterString
x += font->width(L"_");
drawStringLiteral(font, afterCursor, x, height - 12, 0xe0e0e0);
drawString(font, afterCursor, x, height - 12, 0xe0e0e0);
Screen::render(xm, ym, a);
}

View file

@ -20,7 +20,8 @@ class SharedConstants
static wstring readAcceptableChars();
public:
static const int maxChatLength = 1024;
static const int maxChatLength = 255;
static const int maxVisibleLength = 100; //to be changed
static wstring acceptableLetters;
static const int ILLEGAL_FILE_CHARACTERS_LENGTH = 15;