1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +00:00

Allow to set the displayed item count and its alignment via meta (#8448)

* Allow to set the displayed item count and its offset via meta

* fix rect constr call

* devtest: add dump_item chatcommand

* fix rect2 constr call (sdim is a position (typedef for v2s32), not a dimension) and remove background because it would work now

* add missing utf8 to wide conversion

* rename to count_meta
This commit is contained in:
DS 2022-02-10 12:17:52 +01:00 committed by GitHub
parent ad1da994b2
commit a8707158a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 111 additions and 12 deletions

View file

@ -1013,6 +1013,10 @@ void drawItemStack(
bool has_mesh = false;
ItemMesh *imesh;
core::rect<s32> viewrect = rect;
if (clip != nullptr)
viewrect.clipAgainst(*clip);
// Render as mesh if animated or no inventory image
if ((enable_animations && rotation_kind < IT_ROT_NONE) || def.inventory_image.empty()) {
imesh = client->idef()->getWieldMesh(def.name, client);
@ -1034,9 +1038,6 @@ void drawItemStack(
core::rect<s32> oldViewPort = driver->getViewPort();
core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
core::rect<s32> viewrect = rect;
if (clip)
viewrect.clipAgainst(*clip);
core::matrix4 ProjMatrix;
ProjMatrix.buildProjectionMatrixOrthoLH(2.0f, 2.0f, -1.0f, 100.0f);
@ -1180,24 +1181,68 @@ void drawItemStack(
driver->draw2DRectangle(color, progressrect2, clip);
}
if (font != NULL && item.count >= 2) {
const std::string &count_text = item.metadata.getString("count_meta");
if (font != nullptr && (item.count >= 2 || !count_text.empty())) {
// Get the item count as a string
std::string text = itos(item.count);
v2u32 dim = font->getDimension(utf8_to_wide(text).c_str());
std::string text = count_text.empty() ? itos(item.count) : count_text;
v2u32 dim = font->getDimension(utf8_to_wide(unescape_enriched(text)).c_str());
v2s32 sdim(dim.X, dim.Y);
core::rect<s32> rect2(
/*rect.UpperLeftCorner,
core::dimension2d<u32>(rect.getWidth(), 15)*/
rect.LowerRightCorner - sdim,
sdim
rect.LowerRightCorner
);
video::SColor bgcolor(128, 0, 0, 0);
driver->draw2DRectangle(bgcolor, rect2, clip);
// get the count alignment
s32 count_alignment = stoi(item.metadata.getString("count_alignment"));
if (count_alignment != 0) {
s32 a_x = count_alignment & 3;
s32 a_y = (count_alignment >> 2) & 3;
s32 x1, x2, y1, y2;
switch (a_x) {
case 1: // left
x1 = rect.UpperLeftCorner.X;
x2 = x1 + sdim.X;
break;
case 2: // middle
x1 = (rect.UpperLeftCorner.X + rect.LowerRightCorner.X - sdim.X) / 2;
x2 = x1 + sdim.X;
break;
case 3: // right
x2 = rect.LowerRightCorner.X;
x1 = x2 - sdim.X;
break;
default: // 0 = default
x1 = rect2.UpperLeftCorner.X;
x2 = rect2.LowerRightCorner.X;
break;
}
switch (a_y) {
case 1: // up
y1 = rect.UpperLeftCorner.Y;
y2 = y1 + sdim.Y;
break;
case 2: // middle
y1 = (rect.UpperLeftCorner.Y + rect.LowerRightCorner.Y - sdim.Y) / 2;
y2 = y1 + sdim.Y;
break;
case 3: // down
y2 = rect.LowerRightCorner.Y;
y1 = y2 - sdim.Y;
break;
default: // 0 = default
y1 = rect2.UpperLeftCorner.Y;
y2 = rect2.LowerRightCorner.Y;
break;
}
rect2 = core::rect<s32>(x1, y1, x2, y2);
}
video::SColor color(255, 255, 255, 255);
font->draw(text.c_str(), rect2, color, false, false, clip);
font->draw(utf8_to_wide(text).c_str(), rect2, color, false, false, &viewrect);
}
}