fix line width calculation in breakLines

This commit is contained in:
gymnast86
2026-08-21 18:30:48 -07:00
parent 3606ec64e3
commit c475d72110
2 changed files with 15 additions and 17 deletions
+11 -13
View File
@@ -60,7 +60,7 @@ namespace randomizer {
}
}
void Text::BreakLines(int maxLineWidth /*= MAX_LINE_WIDTH_ITEM_TEXTBOX*/) {
void Text::BreakLines(float maxLineWidth /*= MAX_LINE_WIDTH_ITEM_TEXTBOX*/) {
for (auto& text : mText) {
breakLines(text, maxLineWidth);
}
@@ -441,21 +441,21 @@ namespace randomizer {
{"<silver>", "\x1A\x06\xFF\x00\x00\x0B"sv},
};
void breakLines(std::string& str, int maxLineWidth) {
void breakLines(std::string& str, float maxStrLength) {
// Randomizer Only shouldn't rely on needing access to the iso
#ifndef RANDOMIZER_ONLY
// Get game's font
auto gameFont = mDoExt_getMesgFont();
#endif
int curLineWidth = 0;
float curLineWidth = 0.f;
size_t i = 0;
size_t previousSpace = 0;
while (i < str.length()) {
// Skip over control codes since they don't get displayed
std::string code{};
for (const auto& [messageCode, replacement] : messageCodes) {
for (const auto& messageCode : messageCodes | std::views::keys) {
if (str.substr(i, messageCode.length()) == messageCode) {
code = messageCode;
break;
@@ -464,9 +464,8 @@ namespace randomizer {
if (!code.empty()) {
// Assume worst case for player name width.
// 8 chars max * max char width
if (code == "<player name>") {
curLineWidth += 8 * 21;
curLineWidth += 8.f;
}
i += code.length();
continue;
@@ -479,27 +478,26 @@ namespace randomizer {
}
// If we encounter an already inserted newline, reset the counter
else if (str[i] == '\n') {
curLineWidth = 0;
curLineWidth = 0.f;
++i;
continue;
}
#ifndef RANDOMIZER_ONLY
JUTFont::TWidth width{};
gameFont->getWidthEntry(str[i], &width);
curLineWidth += /*width.field_0x0 + */width.field_0x1;
auto width = gameFont->getWidth(str[i]);
curLineWidth += width / static_cast<float>(gameFont->getCellWidth());
#else
// Assume worst case with no iso access
curLineWidth += 21;
curLineWidth += 1;
#endif
// If we exceed the maximum line width, replace the
// previous space with a newline and start counting
// from the newline again
if (curLineWidth > maxLineWidth) {
if (curLineWidth > maxStrLength) {
str[previousSpace] = '\n';
i = previousSpace;
curLineWidth = 0;
curLineWidth = 0.f;
}
++i;
+4 -4
View File
@@ -60,8 +60,8 @@ namespace randomizer {
PLURALITY_MAX,
};
static constexpr size_t MAX_LINE_WIDTH_ITEM_TEXTBOX = 441;
static constexpr size_t MAX_LINE_WIDTH_NORMAL_TEXTBOX = 750;
static constexpr float MAX_LINE_WIDTH_ITEM_TEXTBOX = 14.0f;
static constexpr float MAX_LINE_WIDTH_NORMAL_TEXTBOX = 17.0f;
static constexpr size_t MAX_NEWLINES_PER_MESSAGE = 40;
static constexpr size_t LINES_PER_BOX = 4;
@@ -80,7 +80,7 @@ namespace randomizer {
*/
void Replace(const std::string& oldText, const Text& replacementText, uint32_t count = std::numeric_limits<uint32_t>::max());
void Replace(const std::string& oldText, const std::string& replacementText, uint32_t count = std::numeric_limits<uint32_t>::max());
void BreakLines(int maxLineWidth = MAX_LINE_WIDTH_NORMAL_TEXTBOX);
void BreakLines(float maxLineWidth = MAX_LINE_WIDTH_NORMAL_TEXTBOX);
// Inserts newlines to pad the text to the next box
void PadToNextBox();
@@ -126,7 +126,7 @@ namespace randomizer {
Text addColor(const Text& t, Text::Color color, int count = 1);
// Adds newlines in appropriate places to properly break the text string for textboxes
void breakLines(std::string& str, int maxLineWidth);
void breakLines(std::string& str, float maxStrLength);
// Replaces the message codes in the string with the ingame hex equivalents
void applyMessageCodes(std::string&);