Skip to content

Commit

Permalink
Fix item label height for CJK
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Jan 11, 2024
1 parent 607737e commit dbf14dd
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions Source/qol/itemlabels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ bool highlightKeyPressed = false;
bool isLabelHighlighted = false;
std::array<std::optional<int>, ITEMTYPES> labelCenterOffsets;

const int BorderX = 4; // minimal horizontal space between labels
const int BorderY = 2; // minimal vertical space between labels
const int MarginX = 2; // horizontal margins between text and edges of the label
const int MarginY = 1; // vertical margins between text and edges of the label
const int Height = 11 + MarginY * 2; // going above 13 scatters labels of items that are next to each other
const int BorderX = 4; // minimal horizontal space between labels
const int BorderY = 2; // minimal vertical space between labels
const int MarginX = 2; // horizontal margins between text and edges of the label

// Vertical space between the text and the edges of the label.
int TextMarginTop() { return IsSmallFontTall() ? 1 : -1; }
int TextMarginBottom() { return IsSmallFontTall() ? 1 : 3; }

// The total height of the label box.
int LabelHeight() { return (IsSmallFontTall() ? 16 : 11) + TextMarginBottom() + TextMarginTop(); }

/**
* @brief The set of used X coordinates for a certain Y coordinate.
Expand Down Expand Up @@ -123,7 +128,7 @@ void AddItemToLabelQueue(int id, Point position)
position *= 2;
}
position.x -= nameWidth / 2;
position.y -= Height;
position.y -= LabelHeight();
labelQueue.push_back(ItemLabel { id, nameWidth, position, std::move(textOnGround) });
}

Expand All @@ -146,6 +151,8 @@ void DrawItemNameLabels(const Surface &out)
if (labelQueue.empty())
return;
UsedX usedX;
const int labelHeight = LabelHeight();
const int labelMarginTop = TextMarginTop();

for (unsigned i = 0; i < labelQueue.size(); ++i) {
usedX.clear();
Expand All @@ -156,7 +163,7 @@ void DrawItemNameLabels(const Surface &out)
for (unsigned j = 0; j < i; ++j) {
ItemLabel &a = labelQueue[i];
ItemLabel &b = labelQueue[j];
if (std::abs(b.pos.y - a.pos.y) < Height + BorderY) {
if (std::abs(b.pos.y - a.pos.y) < labelHeight + BorderY) {
const int widthA = a.width + BorderX + MarginX * 2;
const int widthB = b.width + BorderX + MarginX * 2;
int newpos = b.pos.x;
Expand All @@ -181,7 +188,8 @@ void DrawItemNameLabels(const Surface &out)
for (const ItemLabel &label : labelQueue) {
Item &item = Items[label.id];

if (MousePosition.x >= label.pos.x && MousePosition.x < label.pos.x + label.width && MousePosition.y >= label.pos.y + MarginY && MousePosition.y < label.pos.y + MarginY + Height) {
if (MousePosition.x >= label.pos.x && MousePosition.x < label.pos.x + label.width
&& MousePosition.y >= label.pos.y && MousePosition.y < label.pos.y + labelHeight) {
if (!gmenu_is_active()
&& PauseMode == 0
&& !MyPlayerIsDead
Expand All @@ -194,10 +202,10 @@ void DrawItemNameLabels(const Surface &out)
}
}
if (pcursitem == label.id && stextflag == TalkID::None)
FillRect(clippedOut, label.pos.x, label.pos.y + MarginY, label.width, Height, PAL8_BLUE + 6);
FillRect(clippedOut, label.pos.x, label.pos.y, label.width, labelHeight, PAL8_BLUE + 6);
else
DrawHalfTransparentRectTo(clippedOut, label.pos.x, label.pos.y + MarginY, label.width, Height);
DrawString(clippedOut, label.text, { { label.pos.x + MarginX, label.pos.y }, { label.width, Height } },
DrawHalfTransparentRectTo(clippedOut, label.pos.x, label.pos.y, label.width, labelHeight);
DrawString(clippedOut, label.text, { { label.pos.x + MarginX, label.pos.y + labelMarginTop }, { label.width, labelHeight } },
{ .flags = item.getTextColor() });
}
labelQueue.clear();
Expand Down

0 comments on commit dbf14dd

Please sign in to comment.