diff --git a/include/core/events.h b/include/core/events.h index 73e61141..6ecad0c1 100644 --- a/include/core/events.h +++ b/include/core/events.h @@ -107,8 +107,6 @@ public: static Event* create(Event::Type type); - static QMap 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: diff --git a/src/core/events.cpp b/src/core/events.cpp index 37e5ec0a..c124e58d 100644 --- a/src/core/events.cpp +++ b/src/core/events.cpp @@ -4,8 +4,6 @@ #include "project.h" #include "config.h" -QMap 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(Event::Group::None)); - for (int i = 0; i < numIcons; i++) { - Event::Group group = static_cast(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 { diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 2124bf65..a38505e2 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -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) { diff --git a/src/project.cpp b/src/project.cpp index 7f9a1ed5..bd4e9bf9 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -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(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() {