draw recipes wherever the UI states what something makes
Implements the requirements of 0bf24a7. Recipes were explained in prose:
the recipe dialog was a grid of icon-only buttons with a text tooltip
listing name, inputs, time and output, the unlock-choice dialog listed
recipe names with the same tooltip, an item chip said nothing about where
its item came from, and a module button never showed its cost.
RecipeLineRow is the one widget that draws a recipe -- optional building
chip and name, inputs, arrow, outputs, time -- and every place that states
what something makes now goes through it: the panel's recipe summary (which
it replaces RecipeSummaryRow for), the selection dialog's option buttons,
the item production tooltip, the unlock-choice dialog's recipe lines, the
module buttons' cost lines, and the layout dialog's build cost. Its arrow
is drawn only where there are outputs, so a shipyard summary no longer
points at nothing.
ItemProducers answers where an item comes from, filtering by unlock state
per building type, and ItemTooltip draws it as a window of its own -- Qt's
tooltips are text, this one has item squares and building chips in it.
ItemChip pops it after a hover delay.
The selection dialog becomes one scrolling vertical column of buttons, each
showing its name over its recipe line, and no dialog carries a tooltip any
more. RecipeTooltip and recipes.toml's unused "icon" field go with the
behavior they served.
Two requirement amendments the implementation forced, both in this commit:
the option column scrolls when taller than the window allows, and a Smelter
or Reprocessing recipe is listed once its building is unlocked, those
recipes carrying no unlock of their own. The layout dialog's build cost
also moved out of the shared ShipStatsPanel, which the balancing tool
compiles directly and is deliberately kept free of the icon caches.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ne3mejABZoLWKLh8fgpM3x
This commit is contained in:
140
src/ui/RecipeLineRow.cpp
Normal file
140
src/ui/RecipeLineRow.cpp
Normal file
@@ -0,0 +1,140 @@
|
||||
#include "RecipeLineRow.h"
|
||||
|
||||
#include <QHBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QLayoutItem>
|
||||
#include <QPixmap>
|
||||
|
||||
#include "BuildingIconCache.h"
|
||||
#include "ItemIconCache.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
// Size the items' colored squares and the building's chip are drawn at on a line, in
|
||||
// device-independent pixels. Larger than the artwork they carry, which the square
|
||||
// insets (REQ-UI-ITEM-ICON).
|
||||
const int kIconSizePx = 18;
|
||||
|
||||
// Adds a freshly built label to the line and shows it.
|
||||
//
|
||||
// The show is what makes it count: a widget created under an already-visible parent
|
||||
// starts hidden, and a layout treats a hidden item as empty, so an unshown label would
|
||||
// add nothing to the size hint the surrounding widget measures itself against as soon
|
||||
// as this returns.
|
||||
void addAndShow(QHBoxLayout* layout, QLabel* label)
|
||||
{
|
||||
layout->addWidget(label);
|
||||
label->show();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
RecipeLineRow::RecipeLineRow(ItemIconCache* itemIcons, BuildingIconCache* buildingIcons,
|
||||
QWidget* parent)
|
||||
: QWidget(parent)
|
||||
, m_itemIcons(itemIcons)
|
||||
, m_buildingIcons(buildingIcons)
|
||||
{
|
||||
m_layout = new QHBoxLayout(this);
|
||||
m_layout->setContentsMargins(0, 0, 0, 0);
|
||||
m_layout->setSpacing(4);
|
||||
|
||||
hide();
|
||||
}
|
||||
|
||||
void RecipeLineRow::setLine(const Spec& spec)
|
||||
{
|
||||
if (spec.isEmpty())
|
||||
{
|
||||
// Forgotten as well as hidden, so re-selecting the same recipe later is seen as
|
||||
// a change and shows the row again.
|
||||
m_spec = Spec();
|
||||
hide();
|
||||
return;
|
||||
}
|
||||
if (spec == m_spec)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_spec = spec;
|
||||
rebuild(spec);
|
||||
show();
|
||||
}
|
||||
|
||||
void RecipeLineRow::rebuild(const Spec& spec)
|
||||
{
|
||||
while (QLayoutItem* item = m_layout->takeAt(0))
|
||||
{
|
||||
if (item->widget())
|
||||
{
|
||||
item->widget()->deleteLater();
|
||||
}
|
||||
delete item;
|
||||
}
|
||||
|
||||
// The building the recipe runs in, where the line has to distinguish one producer
|
||||
// from another (REQ-UI-ITEM-TOOLTIP). A building with no chip file leaves the icon
|
||||
// off, as everywhere else (REQ-UI-BUILD-ICON).
|
||||
if (spec.building.has_value() && m_buildingIcons != nullptr)
|
||||
{
|
||||
const QPixmap chip =
|
||||
m_buildingIcons->getChip(buildingTypeId(*spec.building), kIconSizePx);
|
||||
if (!chip.isNull())
|
||||
{
|
||||
QLabel* chipLabel = new QLabel(this);
|
||||
chipLabel->setPixmap(chip);
|
||||
addAndShow(m_layout, chipLabel);
|
||||
}
|
||||
}
|
||||
if (!spec.name.isEmpty())
|
||||
{
|
||||
addAndShow(m_layout, new QLabel(spec.name, this));
|
||||
}
|
||||
|
||||
addAmounts(spec.inputs);
|
||||
if (!spec.inputs.empty() && !spec.outputs.empty())
|
||||
{
|
||||
const QChar rightArrow(0x2192); // U+2192 RIGHTWARDS ARROW
|
||||
addAndShow(m_layout, new QLabel(QString(rightArrow), this));
|
||||
}
|
||||
addAmounts(spec.outputs);
|
||||
|
||||
if (spec.durationSeconds.has_value() && *spec.durationSeconds > 0.0)
|
||||
{
|
||||
const QChar middleDot(0x00B7); // U+00B7 MIDDLE DOT
|
||||
const QString time = spec.durationIsAddition
|
||||
? tr("+%1 s").arg(*spec.durationSeconds, 0, 'f', 1)
|
||||
: tr("%1 s").arg(*spec.durationSeconds, 0, 'f', 1);
|
||||
addAndShow(m_layout, new QLabel(
|
||||
QStringLiteral("%1 %2").arg(middleDot).arg(time), this));
|
||||
}
|
||||
m_layout->addStretch(1);
|
||||
}
|
||||
|
||||
void RecipeLineRow::addAmounts(const std::vector<Amount>& amounts)
|
||||
{
|
||||
for (const Amount& entry : amounts)
|
||||
{
|
||||
// The item's icon on its colored square (REQ-UI-ITEM-ICON). A missing icon file
|
||||
// is not an error: the square stands alone then, and only an item with no square
|
||||
// either falls back to its id in text.
|
||||
const QPixmap icon = (m_itemIcons != nullptr)
|
||||
? m_itemIcons->getSquarePixmap(entry.itemId, kIconSizePx)
|
||||
: QPixmap();
|
||||
if (!icon.isNull())
|
||||
{
|
||||
QLabel* iconLabel = new QLabel(this);
|
||||
iconLabel->setPixmap(icon);
|
||||
addAndShow(m_layout, iconLabel);
|
||||
}
|
||||
else
|
||||
{
|
||||
addAndShow(m_layout,
|
||||
new QLabel(QString::fromStdString(entry.itemId), this));
|
||||
}
|
||||
addAndShow(m_layout, new QLabel(QString::number(entry.amount), this));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user