diff --git a/src/tiled/mainwindow.cpp b/src/tiled/mainwindow.cpp index c8ec75f53c..8857fc0fbe 100644 --- a/src/tiled/mainwindow.cpp +++ b/src/tiled/mainwindow.cpp @@ -68,6 +68,7 @@ #include "tilesetdocument.h" #include "tileseteditor.h" #include "tilesetmanager.h" +#include "tilestampmanager.h" #include "tmxmapformat.h" #include "utils.h" #include "world.h" @@ -1017,6 +1018,11 @@ void MainWindow::initializeSession() // adding the project's extension path. ScriptManager::instance().ensureInitialized(); + // Load tile stamps (delayed so that potential custom types and file + // formats can be supported by stamps - which isn't perfect since there + // will still be issues when the project isn't open on startup) + TileStampManager::instance()->loadStamps(); + if (projectLoaded || Preferences::instance()->restoreSessionOnStartup()) restoreSession(); } diff --git a/src/tiled/projectmanager.cpp b/src/tiled/projectmanager.cpp index d7ff950be0..536e39faf3 100644 --- a/src/tiled/projectmanager.cpp +++ b/src/tiled/projectmanager.cpp @@ -37,6 +37,11 @@ ProjectManager::ProjectManager(QObject *parent) ourInstance = this; } +ProjectManager::~ProjectManager() +{ + ourInstance = nullptr; +} + /** * Replaces the current project with the given \a project. */ diff --git a/src/tiled/projectmanager.h b/src/tiled/projectmanager.h index 22d93d1239..19fef2d501 100644 --- a/src/tiled/projectmanager.h +++ b/src/tiled/projectmanager.h @@ -40,6 +40,7 @@ class TILED_EDITOR_EXPORT ProjectManager : public QObject public: explicit ProjectManager(QObject *parent = nullptr); + ~ProjectManager() override; static ProjectManager *instance(); diff --git a/src/tiled/tilestampmanager.cpp b/src/tiled/tilestampmanager.cpp index 3fc63e25d7..764c52ff30 100644 --- a/src/tiled/tilestampmanager.cpp +++ b/src/tiled/tilestampmanager.cpp @@ -29,10 +29,6 @@ #include "preferences.h" #include "savefile.h" #include "stampbrush.h" -#include "tilelayer.h" -#include "tileselectiontool.h" -#include "tileset.h" -#include "tilesetmanager.h" #include "tilestampmodel.h" #include "toolmanager.h" #include "utils.h" @@ -46,6 +42,8 @@ using namespace Tiled; +TileStampManager *TileStampManager::ourInstance; + TileStampManager::TileStampManager(const ToolManager &toolManager, QObject *parent) : QObject(parent) @@ -54,6 +52,9 @@ TileStampManager::TileStampManager(const ToolManager &toolManager, , mTileStampModel(new TileStampModel(this)) , mToolManager(toolManager) { + Q_ASSERT(!ourInstance); + ourInstance = this; + mRegisteredCb = stampsDirectory.onChange([this] { stampsDirectoryChanged(); }); connect(mTileStampModel, &TileStampModel::stampAdded, @@ -64,8 +65,6 @@ TileStampManager::TileStampManager(const ToolManager &toolManager, this, &TileStampManager::saveStamp); connect(mTileStampModel, &TileStampModel::stampRemoved, this, &TileStampManager::deleteStamp); - - loadStamps(); } TileStampManager::~TileStampManager() @@ -73,6 +72,8 @@ TileStampManager::~TileStampManager() // needs to be over here where the TileStamp type is complete stampsDirectory.unregister(mRegisteredCb); + + ourInstance = nullptr; } static TileStamp stampFromContext(AbstractTool *selectedTool) diff --git a/src/tiled/tilestampmanager.h b/src/tiled/tilestampmanager.h index dc90f6d899..9f8a41c351 100644 --- a/src/tiled/tilestampmanager.h +++ b/src/tiled/tilestampmanager.h @@ -51,12 +51,16 @@ class TileStampManager : public QObject TileStampManager(const ToolManager &toolManager, QObject *parent = nullptr); ~TileStampManager() override; + static TileStampManager *instance(); + static QList quickStampKeys(); TileStampModel *tileStampModel() const; SessionOption stampsDirectory; + void loadStamps(); + public slots: TileStamp createStamp(); void addVariation(const TileStamp &targetStamp); @@ -76,8 +80,6 @@ public slots: void eraseQuickStamp(int index); void setQuickStamp(int index, TileStamp stamp); - void loadStamps(); - private: void stampAdded(TileStamp stamp); void stampRenamed(TileStamp stamp); @@ -93,9 +95,16 @@ public slots: Session::CallbackIterator mRegisteredCb; const ToolManager &mToolManager; + + static TileStampManager *ourInstance; }; +inline TileStampManager *TileStampManager::instance() +{ + return ourInstance; +} + /** * Returns the keys used for quickly accessible tile stamps. * Note: To store a tile layer is added. The given keys will work