Skip to content

Commit

Permalink
Enable opening of .world files
Browse files Browse the repository at this point in the history
If a .world file is being opened, load the world and try to open its
first map. Also make sure .world files are shown in the Project view.

Thanks to SpiderDave for this suggestion!
  • Loading branch information
bjorn committed Jun 24, 2020
1 parent 7de5882 commit 8c47043
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/libtiled/worldmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,27 @@ QVector<World::MapEntry> World::contextMaps(const QString &fileName) const
return allMaps();
}

QString World::firstMap() const
{
if (!maps.isEmpty())
return maps.first().fileName;

if (!patterns.isEmpty()) {
const QDir dir(QFileInfo(fileName).dir());
const QStringList entries = dir.entryList(QDir::Files | QDir::Readable);

for (const World::Pattern &pattern : patterns) {
for (const QString &fileName : entries) {
QRegularExpressionMatch match = pattern.regexp.match(fileName);
if (match.hasMatch())
return dir.filePath(fileName);
}
}
}

return QString();
}

void World::error(const QString &message) const
{
ERROR(message, [fileName = this->fileName] { QDesktopServices::openUrl(QUrl::fromLocalFile(fileName)); }, this);
Expand Down
1 change: 1 addition & 0 deletions src/libtiled/worldmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ struct TILEDSHARED_EXPORT World
QVector<MapEntry> allMaps() const;
QVector<MapEntry> mapsInRect(const QRect &rect) const;
QVector<MapEntry> contextMaps(const QString &fileName) const;
QString firstMap() const;

void error(const QString &message) const;
void warning(const QString &message) const;
Expand Down
14 changes: 14 additions & 0 deletions src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,20 @@ bool MainWindow::openFile(const QString &fileName, FileFormat *fileFormat)
if (mDocumentManager->switchToDocument(fileName))
return true;

// HACK: World files can't open as document, but we can instead open the
// first map in the world.
if (fileName.endsWith(QLatin1String(".world"))) {
QString errorString;
World *world = WorldManager::instance().loadWorld(fileName, &errorString);
if (!world) {
QMessageBox::critical(this, tr("Error Loading World"), errorString);
return false;
} else {
mLoadedWorlds = WorldManager::instance().worlds().keys();
return openFile(world->firstMap());
}
}

QString error;
DocumentPtr document = mDocumentManager->loadDocument(fileName, fileFormat, &error);

Expand Down
4 changes: 4 additions & 0 deletions src/tiled/projectmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ void ProjectModel::updateNameFilters()
nameFilters.append(Utils::cleanFilterList(filter));
}

// HACK: Needed to display world files in the project, since they do not
// have a registered FileFormat.
nameFilters.append(QLatin1String("*.world"));

nameFilters.removeDuplicates();

if (mNameFilters != nameFilters) {
Expand Down

0 comments on commit 8c47043

Please sign in to comment.