Skip to content

Commit

Permalink
DrawUniqueInfo: Word-wrap long lines
Browse files Browse the repository at this point in the history
Pre-wraps the string at spaces, otherwise `DrawString` would hard wrap in the middle of words.
  • Loading branch information
glebm committed Jan 29, 2025
1 parent 04896c1 commit f7be00c
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Source/items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@
#include "qol/stash.h"
#include "spells.h"
#include "stores.h"
#include "utils/algorithm/container.hpp"
#include "utils/format_int.hpp"
#include "utils/is_of.hpp"
#include "utils/language.h"
#include "utils/log.hpp"
#include "utils/math.h"
#include "utils/str_case.hpp"
#include "utils/str_cat.hpp"
#include "utils/str_split.hpp"
#include "utils/utf8.hpp"

namespace devilution {
Expand Down Expand Up @@ -4098,12 +4100,19 @@ void DrawUniqueInfo(const Surface &out)

rect.position.y += (10 - uitem.UINumPL) * 12;
assert(uitem.UINumPL <= sizeof(uitem.powers) / sizeof(*uitem.powers));
const TextRenderOptions textRenderOptions { .flags = UiFlags::ColorWhite | UiFlags::AlignCenter };
const GameFontTables fontSize = GetFontSizeFromUiFlags(textRenderOptions.flags);
for (const auto &power : uitem.powers) {
if (power.type == IPL_INVALID)
break;
rect.position.y += 2 * 12;
DrawString(out, PrintItemPower(power.type, curruitem), rect,
{ .flags = UiFlags::ColorWhite | UiFlags::AlignCenter });
// Pre-wrap the string at spaces, otherwise DrawString would hard wrap in the middle of words.
const std::string wrapped = WordWrapString(PrintItemPower(power.type, curruitem), rect.size.width);
DrawString(out, wrapped, rect, textRenderOptions);
for (const std::string_view line : SplitByChar(wrapped, '\n')) {
if (line.data() + line.size() == wrapped.data() + wrapped.size()) break;
rect.position.y += GetLineHeight(line, fontSize);
}
}
}

Expand Down

0 comments on commit f7be00c

Please sign in to comment.