56 lines
2.5 KiB
C++
56 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include <map>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#include <QByteArray>
|
|
#include <QPixmap>
|
|
#include <QString>
|
|
|
|
// Rasterizes and caches the per-building chip SVGs (REQ-UI-BUILD-ICON). A chip is a
|
|
// rounded colored background bearing a white line glyph, loaded from a directory with
|
|
// one file per building named after the building's id (e.g. "belt.svg"). Shared by the
|
|
// build button bar and the selection panel's card header (REQ-UI-SELECTION-CARD) so the
|
|
// rasterization is not duplicated.
|
|
//
|
|
// A missing icon file is not an error: hasIcon() returns false for it and the caller
|
|
// falls back (a name caption on a build button, a bare title in the panel header).
|
|
class BuildingIconCache
|
|
{
|
|
public:
|
|
// iconDir is the directory holding the "<id>.svg" chip files (typically
|
|
// "<configDir>/../icons/buildings").
|
|
explicit BuildingIconCache(const QString& iconDir);
|
|
|
|
// True if a chip SVG file exists for the given icon name. Loads the file's bytes on
|
|
// first query and remembers the result (including absence) so repeated calls are
|
|
// cheap. The name is a building id for the building buttons, but need not be one --
|
|
// the Deconstruct button's "deconstruct" chip goes through the same path.
|
|
bool hasIcon(const std::string& iconName);
|
|
|
|
// The chip rasterized to a transparent sizePx*sizePx pixmap at the device pixel
|
|
// ratio, cached per (name, size). Returns a null pixmap when the file is missing.
|
|
QPixmap getChip(const std::string& iconName, int sizePx);
|
|
|
|
// As getChip(), but with the chip background recolored grey and the glyph left
|
|
// alone, for a build button the player cannot currently afford
|
|
// (REQ-UI-BUILD-DISABLED).
|
|
QPixmap getGreyChip(const std::string& iconName, int sizePx);
|
|
|
|
private:
|
|
// Raw SVG bytes for an icon name, loading and caching them on first access. An
|
|
// absent file caches an empty QByteArray so it is not retried.
|
|
const QByteArray& getSvg(const std::string& iconName);
|
|
// The same SVG with its background fill replaced by grey, cached alongside.
|
|
const QByteArray& getGreySvg(const std::string& iconName);
|
|
// Shared rasterize-and-cache step. cacheKey distinguishes the normal and grey
|
|
// variants of one icon name within the single pixmap cache.
|
|
QPixmap getPixmap(const std::string& cacheKey, const QByteArray& svg, int sizePx);
|
|
|
|
QString m_iconDir;
|
|
std::map<std::string, QByteArray> m_svgByName;
|
|
std::map<std::string, QByteArray> m_greySvgByName;
|
|
std::map<std::pair<std::string, int>, QPixmap> m_pixmapCache;
|
|
};
|