Cache event pixmaps

This commit is contained in:
GriffinR 2025-02-21 12:40:06 -05:00
parent bf5ead848d
commit eef9a37d16
4 changed files with 42 additions and 51 deletions

View File

@ -107,8 +107,6 @@ public:
static Event* create(Event::Type type);
static QMap<Event::Group, const QPixmap*> icons;
// standard public methods
public:
@ -171,8 +169,6 @@ public:
static QString groupToString(Event::Group group);
static QString typeToString(Event::Type type);
static Event::Type typeFromString(QString type);
static void clearIcons();
static void setIcons();
// protected attributes
protected:

View File

@ -4,8 +4,6 @@
#include "project.h"
#include "config.h"
QMap<Event::Group, const QPixmap*> Event::icons;
Event* Event::create(Event::Type type) {
switch (type) {
case Event::Type::Object: return new ObjectEvent();
@ -108,46 +106,10 @@ Event::Type Event::typeFromString(QString type) {
}
void Event::loadPixmap(Project *project) {
const QPixmap * pixmap = Event::icons.value(this->getEventGroup());
this->pixmap = pixmap ? *pixmap : QPixmap();
this->pixmap = project->getEventPixmap(this->getEventGroup());
this->usesDefaultPixmap = true;
}
void Event::clearIcons() {
qDeleteAll(icons);
icons.clear();
}
void Event::setIcons() {
clearIcons();
const int w = 16;
const int h = 16;
static const QPixmap defaultIcons = QPixmap(":/images/Entities_16x16.png");
// Custom event icons may be provided by the user.
const int numIcons = qMin(defaultIcons.width() / w, static_cast<int>(Event::Group::None));
for (int i = 0; i < numIcons; i++) {
Event::Group group = static_cast<Event::Group>(i);
QString customIconPath = projectConfig.getEventIconPath(group);
if (customIconPath.isEmpty()) {
// No custom icon specified, use the default icon.
icons[group] = new QPixmap(defaultIcons.copy(i * w, 0, w, h));
continue;
}
// Try to load custom icon
QString validPath = Project::getExistingFilepath(customIconPath);
if (!validPath.isEmpty()) customIconPath = validPath; // Otherwise allow it to fail with the original path
const QPixmap customIcon = QPixmap(customIconPath);
if (customIcon.isNull()) {
// Custom icon failed to load, use the default icon.
icons[group] = new QPixmap(defaultIcons.copy(i * w, 0, w, h));
logWarn(QString("Failed to load custom event icon '%1', using default icon.").arg(customIconPath));
} else {
icons[group] = new QPixmap(customIcon.scaled(w, h));
}
}
}
Event *ObjectEvent::duplicate() const {

View File

@ -1103,7 +1103,6 @@ bool MainWindow::setProjectUI() {
ui->newEventToolButton->newSecretBaseAction->setVisible(projectConfig.eventSecretBaseEnabled);
ui->newEventToolButton->newCloneObjectAction->setVisible(projectConfig.eventCloneObjectEnabled);
Event::setIcons();
editor->setCollisionGraphics();
ui->spinBox_SelectedElevation->setMaximum(Block::getMaxElevation());
ui->spinBox_SelectedCollision->setMaximum(Block::getMaxCollision());
@ -1161,8 +1160,6 @@ void MainWindow::clearProjectUI() {
delete this->layoutTreeModel;
delete this->layoutListProxyModel;
resetMapListFilters();
Event::clearIcons();
}
void MainWindow::scrollMapList(MapTree *list, const QString &itemName) {

View File

@ -44,6 +44,7 @@ Project::~Project()
clearMapLayouts();
clearEventGraphics();
clearHealLocations();
QPixmapCache::clear();
}
void Project::set_root(QString dir) {
@ -2768,6 +2769,12 @@ QPixmap Project::getEventPixmap(const QString &gfxName, const QString &movementN
}
QPixmap Project::getEventPixmap(const QString &gfxName, int frame, bool hFlip) {
QPixmap pixmap;
const QString cacheKey = QString("EVENT#%1#%2#%3").arg(gfxName).arg(frame).arg(hFlip ? "1" : "0");
if (QPixmapCache::find(cacheKey, &pixmap)) {
return pixmap;
}
EventGraphics* gfx = this->eventGraphicsMap.value(gfxName, nullptr);
if (!gfx) {
// Invalid gfx constant. If this is a number, try to use that instead.
@ -2823,15 +2830,44 @@ QPixmap Project::getEventPixmap(const QString &gfxName, int frame, bool hFlip) {
}
// Set first palette color fully transparent.
img.setColor(0, qRgba(0, 0, 0, 0));
QPixmap pixmap = QPixmap::fromImage(img);
// TODO: Cache?
pixmap = QPixmap::fromImage(img);
QPixmapCache::insert(cacheKey, pixmap);
return pixmap;
}
QPixmap Project::getEventPixmap(Event::Group) {
// TODO
return QPixmap();
QPixmap Project::getEventPixmap(Event::Group group) {
if (group == Event::Group::None)
return QPixmap();
QPixmap pixmap;
const QString cacheKey = QString("EVENT#%1").arg(Event::groupToString(group));
if (QPixmapCache::find(cacheKey, &pixmap)) {
return pixmap;
}
const int defaultWidth = 16;
const int defaultHeight = 16;
static const QPixmap defaultIcons = QPixmap(":/images/Entities_16x16.png");
QPixmap defaultIcon = QPixmap(defaultIcons.copy(static_cast<int>(group) * defaultWidth, 0, defaultWidth, defaultHeight));
// Custom event icons may be provided by the user.
QString customIconPath = projectConfig.getEventIconPath(group);
if (customIconPath.isEmpty()) {
// No custom icon specified, use the default icon.
pixmap = defaultIcon;
} else {
// Try to load custom icon
QString validPath = Project::getExistingFilepath(customIconPath);
if (!validPath.isEmpty()) customIconPath = validPath; // Otherwise allow it to fail with the original path
pixmap = QPixmap(customIconPath);
if (pixmap.isNull()) {
pixmap = defaultIcon;
logWarn(QString("Failed to load custom event icon '%1', using default icon.").arg(customIconPath));
}
}
QPixmapCache::insert(cacheKey, pixmap);
return pixmap;
}
bool Project::readSpeciesIconPaths() {