diff options
| author | Garrett Cox <garrettjcox@gmail.com> | 2026-01-20 19:26:50 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-01-20 19:26:50 -0600 |
| commit | d7bd6f9c7df615cd1b41a5b482e2589a6a03cca0 (patch) | |
| tree | e7ef22b8eb223bd5cd58310f1c85ae6837e5b2dd /mm/2s2h/CustomMessage | |
| parent | f237dd2930e60a2128a0a80d4783318025dd8f1d (diff) | |
Implement sarias song hint item (#1161)
Diffstat (limited to 'mm/2s2h/CustomMessage')
| -rw-r--r-- | mm/2s2h/CustomMessage/CustomMessage.cpp | 55 |
1 files changed, 44 insertions, 11 deletions
diff --git a/mm/2s2h/CustomMessage/CustomMessage.cpp b/mm/2s2h/CustomMessage/CustomMessage.cpp index ff3a16152..3bc6c5f1f 100644 --- a/mm/2s2h/CustomMessage/CustomMessage.cpp +++ b/mm/2s2h/CustomMessage/CustomMessage.cpp @@ -65,37 +65,70 @@ void CustomMessage::AddLineBreaks(std::string* msg) { for (size_t i = 0; i < msg->size(); ++i) { char currentChar = (*msg)[i]; - if ((uint8_t)currentChar >= 0x20 && (uint8_t)currentChar < 0x20 + ARRAY_COUNTU(sNESFontWidths)) { - currentLineWidth += sNESFontWidths[(uint8_t)currentChar - 0x20]; - } - - // Increment for existing new liens + // Handle existing newlines if (currentChar == 0x11) { currentLineWidth = 0.0f; lastSpaceIndex = std::string::npos; ++currentLineCount; + continue; + } + + // Handle existing box breaks + if (currentChar == 0x10) { + currentLineWidth = 0.0f; + lastSpaceIndex = std::string::npos; + currentLineCount = 0; + continue; } + // Track spaces for word wrapping if (currentChar == ' ') { lastSpaceIndex = i; } - if (currentLineWidth > MAX_TEXTBOX_WIDTH) { + + // Calculate width for printable characters + float charWidth = 0.0f; + if ((uint8_t)currentChar >= 0x20 && (uint8_t)currentChar < 0x20 + ARRAY_COUNTU(sNESFontWidths)) { + charWidth = sNESFontWidths[(uint8_t)currentChar - 0x20]; + } + + // Check if adding this character would exceed width + if (currentLineWidth + charWidth > MAX_TEXTBOX_WIDTH) { if (lastSpaceIndex != std::string::npos) { + // Break at the last space (*msg)[lastSpaceIndex] = 0x11; - i = lastSpaceIndex; + + // Recalculate width from after the space to current position + currentLineWidth = 0.0f; + for (size_t j = lastSpaceIndex + 1; j < i; ++j) { + char c = (*msg)[j]; + if ((uint8_t)c >= 0x20 && (uint8_t)c < 0x20 + ARRAY_COUNTU(sNESFontWidths)) { + currentLineWidth += sNESFontWidths[(uint8_t)c - 0x20]; + } + } + // Add current character's width + currentLineWidth += charWidth; + + lastSpaceIndex = std::string::npos; } else { + // No space found, force break before current character msg->insert(i, 1, 0x11); + currentLineWidth = charWidth; // Current char is now first on new line + ++i; // Skip the inserted newline character } - currentLineWidth = 0.0f; - lastSpaceIndex = std::string::npos; ++currentLineCount; if (currentLineCount >= MAX_LINES_PER_PAGE) { - // Replace the added new line for a box break instead - (*msg)[i] = 0x10; + // Find the newline we just added and replace it with box break + size_t newlinePos = (lastSpaceIndex != std::string::npos) ? lastSpaceIndex : i - 1; + (*msg)[newlinePos] = 0x10; currentLineCount = 0; + lastSpaceIndex = std::string::npos; } + } else { + // Character fits on current line + currentLineWidth += charWidth; } } } |
