add item 'uses-display' property

This commit is contained in:
MihailRis 2025-02-18 03:24:52 +03:00
parent 449b0ebca4
commit df2cff3e4c
4 changed files with 56 additions and 13 deletions

View File

@ -438,6 +438,18 @@ void ContentLoader::loadItem(
root.at("stack-size").get(def.stackSize);
root.at("uses").get(def.uses);
std::string usesDisplayStr = "";
root.at("uses-display").get(usesDisplayStr);
if (usesDisplayStr == "none") {
def.usesDisplay = ItemUsesDisplay::NONE;
} else if (usesDisplayStr == "relation") {
def.usesDisplay = ItemUsesDisplay::RELATION;
} else if (usesDisplayStr == "vbar") {
def.usesDisplay = ItemUsesDisplay::VBAR;
} else if (usesDisplayStr.length()) {
logger.error() << name << ": unknown uses display mode " << usesDisplayStr;
}
if (auto found = root.at("emission")) {
const auto& emissionarr = *found;
def.emission[0] = emissionarr[0].asNumber();

View File

@ -234,19 +234,38 @@ void SlotView::drawItemInfo(
if (!ptr->isInteger()) {
return;
}
{
std::wstring text = std::to_wstring(ptr->asInteger());
batch.setColor({0, 0, 0, 1.0f});
font.draw(batch, text, pos.x - 2, pos.y - 2, nullptr, 0);
batch.resetColor();
font.draw(batch, text, pos.x - 3, pos.y - 3, nullptr, 0);
}
{
std::wstring text = std::to_wstring(item.uses);
batch.setColor({0, 0, 0, 1.0f});
font.draw(batch, text, pos.x - 2, pos.y - 2 + 12, nullptr, 0);
batch.resetColor();
font.draw(batch, text, pos.x - 3, pos.y - 3 + 12, nullptr, 0);
int16_t uses = ptr->asInteger();
switch (item.usesDisplay) {
case ItemUsesDisplay::NONE:
break;
case ItemUsesDisplay::RELATION:
{
std::wstring text = std::to_wstring(uses);
batch.setColor({0, 0, 0, 1.0f});
font.draw(batch, text, pos.x - 2, pos.y - 2, nullptr, 0);
batch.resetColor();
font.draw(batch, text, pos.x - 3, pos.y - 3, nullptr, 0);
}
{
std::wstring text = std::to_wstring(item.uses);
batch.setColor({0, 0, 0, 1.0f});
font.draw(batch, text, pos.x - 2, pos.y - 2 + 12, nullptr, 0);
batch.resetColor();
font.draw(batch, text, pos.x - 3, pos.y - 3 + 12, nullptr, 0);
}
break;
case ItemUsesDisplay::VBAR: {
batch.untexture();
batch.setColor({0, 0, 0, 0.75f});
batch.rect(pos.x - 2, pos.y - 2, 6, SLOT_SIZE + 4);
float t = static_cast<float>(uses) / item.uses;
batch.setColor({(1.0f - t * 0.8f), 0.4f, t * 0.8f + 0.2f, 1.0f});
int height = SLOT_SIZE * t;
batch.rect(pos.x, pos.y + SLOT_SIZE - height, 2, height);
break;
}
}
}
}

View File

@ -15,4 +15,6 @@ void ItemDef::cloneTo(ItemDef& dst) {
dst.placingBlock = placingBlock;
dst.scriptName = scriptName;
dst.modelName = modelName;
dst.uses = uses;
dst.usesDisplay = usesDisplay;
}

View File

@ -19,6 +19,13 @@ enum class ItemIconType {
BLOCK, // block preview: icon is string block id
};
enum class ItemUsesDisplay {
NONE, // uses count is not displayed
RELATION, // uses count is displayed as `remain/default` relation
VBAR, // uses count is displayed as vertical bar without counter
DEFAULT = VBAR,
};
struct ItemDef {
/// @brief Item string id (with prefix included)
std::string const name;
@ -40,6 +47,9 @@ struct ItemDef {
/// @brief Default item uses count
int16_t uses = -1;
/// @brief Item uses count display mode
ItemUsesDisplay usesDisplay = ItemUsesDisplay::DEFAULT;
ItemIconType iconType = ItemIconType::SPRITE;
std::string icon = "blocks:notfound";