Skip to content

Commit

Permalink
Delay loading of tile stamps until after initializing the session
Browse files Browse the repository at this point in the history
The tilesets referenced by stored tile stamps may rely on custom
types defined in the project and possibly even on custom tileset formats
added by extensions. Hence, it is better to wait with loading them until
after the project has been loaded and script manager is initialized.

The user will still run into issues when the project isn't loaded on
startup, but this at least makes running into issues less likely. It could
help to force reloading of the stamps when switching projects.
  • Loading branch information
bjorn committed Nov 18, 2024
1 parent 9d70216 commit 2e9e9ee
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
}
Expand Down
5 changes: 5 additions & 0 deletions src/tiled/projectmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ ProjectManager::ProjectManager(QObject *parent)
ourInstance = this;
}

ProjectManager::~ProjectManager()
{
ourInstance = nullptr;
}

/**
* Replaces the current project with the given \a project.
*/
Expand Down
1 change: 1 addition & 0 deletions src/tiled/projectmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class TILED_EDITOR_EXPORT ProjectManager : public QObject

public:
explicit ProjectManager(QObject *parent = nullptr);
~ProjectManager() override;

static ProjectManager *instance();

Expand Down
13 changes: 7 additions & 6 deletions src/tiled/tilestampmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -46,6 +42,8 @@

using namespace Tiled;

TileStampManager *TileStampManager::ourInstance;

TileStampManager::TileStampManager(const ToolManager &toolManager,
QObject *parent)
: QObject(parent)
Expand All @@ -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,
Expand All @@ -64,15 +65,15 @@ TileStampManager::TileStampManager(const ToolManager &toolManager,
this, &TileStampManager::saveStamp);
connect(mTileStampModel, &TileStampModel::stampRemoved,
this, &TileStampManager::deleteStamp);

loadStamps();
}

TileStampManager::~TileStampManager()
{
// needs to be over here where the TileStamp type is complete

stampsDirectory.unregister(mRegisteredCb);

ourInstance = nullptr;
}

static TileStamp stampFromContext(AbstractTool *selectedTool)
Expand Down
13 changes: 11 additions & 2 deletions src/tiled/tilestampmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ class TileStampManager : public QObject
TileStampManager(const ToolManager &toolManager, QObject *parent = nullptr);
~TileStampManager() override;

static TileStampManager *instance();

static QList<Qt::Key> quickStampKeys();

TileStampModel *tileStampModel() const;

SessionOption<QString> stampsDirectory;

void loadStamps();

public slots:
TileStamp createStamp();
void addVariation(const TileStamp &targetStamp);
Expand All @@ -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);
Expand All @@ -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 <Ctrl> is added. The given keys will work
Expand Down

0 comments on commit 2e9e9ee

Please sign in to comment.