Skip to content

Commit

Permalink
Misc changes and added Asset.assetType
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn committed Jan 12, 2024
1 parent 6816b58 commit a95d295
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 27 deletions.
18 changes: 18 additions & 0 deletions docs/scripting-doc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,19 @@ declare class MapObject extends TiledObject {
constructor(shape: MapObjectShape, name? : string)
}

/**
* The top-level assets supported by Tiled. Not all of these assets have
* associated editors.
*
* @since 1.10.x TODO
*/
declare enum AssetType {
TileMap = 1,
Tileset,
Project,
World,
}

/**
* Represents any top-level data type that can be saved to a file.
*
Expand Down Expand Up @@ -1407,6 +1420,11 @@ declare class Asset extends TiledObject {
*/
readonly isTileset: boolean;

/**
* The type of this asset.
*/
readonly assetType: AssetType;

/**
* Creates a single undo command that wraps all changes applied to this
* asset by the given callback. Recommended to avoid spamming the undo
Expand Down
12 changes: 0 additions & 12 deletions src/tiled/editableasset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#include "editableasset.h"

#include "document.h"
#include "editablemap.h"
#include "editabletileset.h"
#include "scriptmanager.h"

#include <QCoreApplication>
Expand All @@ -47,16 +45,6 @@ QString EditableAsset::fileName() const
return QString();
}

bool EditableAsset::isMap() const
{
return qobject_cast<const EditableMap*>(this) != nullptr;
}

bool EditableAsset::isTileset() const
{
return qobject_cast<const EditableTileset*>(this) != nullptr;
}

QUndoStack *EditableAsset::undoStack() const
{
return document() ? document()->undoStack() : nullptr;
Expand Down
18 changes: 16 additions & 2 deletions src/tiled/editableasset.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ namespace Tiled {

class Document;

namespace AssetType {
Q_NAMESPACE

enum Value {
TileMap = 1,
Tileset,
Project,
World,
};
Q_ENUM_NS(Value)
} // namespace AssetType

class EditableAsset : public EditableObject
{
Q_OBJECT
Expand All @@ -42,14 +54,16 @@ class EditableAsset : public EditableObject
Q_PROPERTY(bool modified READ isModified NOTIFY modifiedChanged)
Q_PROPERTY(bool isTileMap READ isMap CONSTANT)
Q_PROPERTY(bool isTileset READ isTileset CONSTANT)
Q_PROPERTY(AssetType::Value assetType READ assetType CONSTANT)

public:
EditableAsset(Document *document, Object *object, QObject *parent = nullptr);

QString fileName() const;
bool isReadOnly() const override = 0;
bool isMap() const;
bool isTileset() const;
bool isMap() const { return assetType() == AssetType::TileMap; }
bool isTileset() const { return assetType() == AssetType::Tileset; }
virtual AssetType::Value assetType() const = 0;

QUndoStack *undoStack() const;
bool isModified() const;
Expand Down
5 changes: 3 additions & 2 deletions src/tiled/editablemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class EditableSelectedArea;
class EditableTileLayer;
class EditableTileset;

class EditableMap : public EditableAsset
class EditableMap final : public EditableAsset
{
Q_OBJECT

Expand Down Expand Up @@ -113,7 +113,8 @@ class EditableMap : public EditableAsset
explicit EditableMap(std::unique_ptr<Map> map, QObject *parent = nullptr);
~EditableMap() override;

bool isReadOnly() const final;
bool isReadOnly() const override;
AssetType::Value assetType() const override { return AssetType::TileMap; }

int width() const;
int height() const;
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/editableproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class EditableProject final : public EditableAsset
EditableProject(ProjectDocument *projectDocument, QObject *parent = nullptr);

bool isReadOnly() const override;
AssetType::Value assetType() const override { return AssetType::Project; }

QString extensionsPath() const;
QString automappingRulesFile() const;
QString fileName() const;
Expand Down
1 change: 1 addition & 0 deletions src/tiled/editabletileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class EditableTileset : public EditableAsset
~EditableTileset() override;

bool isReadOnly() const final;
AssetType::Value assetType() const override { return AssetType::Tileset; }

const QString &name() const;
QString image() const;
Expand Down
9 changes: 7 additions & 2 deletions src/tiled/editableworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ namespace Tiled {

ScriptWorld::ScriptWorld(World *world)
: Object(*this)
, world(world)
{
this->world = world;
}

EditableWorld::EditableWorld(WorldDocument *worldDocument, QObject *parent)
Expand All @@ -48,10 +48,12 @@ QString ScriptWorldMapEntry::fileName() const
{
return mMapEntry->fileName;
}

QRect ScriptWorldMapEntry::rect() const
{
return mMapEntry->rect;
}

QString EditableWorld::displayName() const
{
return world()->displayName();
Expand All @@ -69,13 +71,15 @@ QVector<ScriptWorldMapEntry*> EditableWorld::allMaps() const
maps.append(new ScriptWorldMapEntry(&entry));
return maps;
}

QVector<ScriptWorldMapEntry*> EditableWorld::mapsInRect(const QRect &rect) const
{
QVector<ScriptWorldMapEntry*> maps;
for (auto &entry : world()->mapsInRect(rect))
maps.append(new ScriptWorldMapEntry(&entry));
return maps;
}

bool EditableWorld::isReadOnly() const
{
return !world()->canBeModified();
Expand All @@ -87,4 +91,5 @@ QSharedPointer<Document> EditableWorld::createDocument()
// function is meant for.
return nullptr;
}
}

} // namespace Tiled
17 changes: 11 additions & 6 deletions src/tiled/editableworld.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,26 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "editableasset.h"
#include "worlddocument.h"
#include "worldmanager.h"

#include <QObject>

namespace Tiled {
/*

/**
* Wrapper which allows world structs to be used with the EditableAsset
* class
* class.
*/
class ScriptWorld : public Object
{

public:
ScriptWorld(World *world);

World *world;
};

Expand All @@ -57,18 +60,20 @@ class ScriptWorldMapEntry : public QObject
};

/**
* @brief The EditableWorld class provides access to Worlds via scripting
* @brief The EditableWorld class provides access to Worlds via scripting.
*/
class EditableWorld final : public EditableAsset
{

Q_OBJECT
Q_PROPERTY(QString displayName READ displayName)
Q_PROPERTY(QVector<ScriptWorldMapEntry*> allMaps READ allMaps)

public:
EditableWorld(WorldDocument *worldDocument, QObject *parent = nullptr);

bool isReadOnly() const override;
AssetType::Value assetType() const override { return AssetType::World; }

World *world() const;
QString displayName() const;
Q_INVOKABLE QVector<ScriptWorldMapEntry*> allMaps() const;
Expand All @@ -86,7 +91,7 @@ inline World *EditableWorld::world() const
return static_cast<ScriptWorld*>(object())->world;
}

}
} // namespace Tiled

Q_DECLARE_METATYPE(Tiled::EditableWorld*)
Q_DECLARE_METATYPE(Tiled::ScriptWorldMapEntry*)
4 changes: 3 additions & 1 deletion src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,9 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
});
}
});
connect(mUi->actionUnloadAllWorlds, &QAction::triggered, this, []{WorldManager::instance().unloadAllWorlds();});
connect(mUi->actionUnloadAllWorlds, &QAction::triggered, this, [] {
WorldManager::instance().unloadAllWorlds();
});
connect(mUi->menuWorld, &QMenu::aboutToShow, this, [this] {
mUi->menuUnloadWorld->setEnabled(!WorldManager::instance().worlds().isEmpty());
mUi->actionUnloadAllWorlds->setEnabled(!WorldManager::instance().worlds().isEmpty());
Expand Down
5 changes: 4 additions & 1 deletion src/tiled/scriptmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ ScriptManager::ScriptManager(QObject *parent)
mResetTimer.setSingleShot(true);
connect(&mResetTimer, &QTimer::timeout, this, &ScriptManager::reset);

qRegisterMetaType<AssetType::Value>("AssetType");
qRegisterMetaType<Cell>();
qRegisterMetaType<EditableAsset*>();
qRegisterMetaType<EditableGroupLayer*>();
Expand Down Expand Up @@ -133,6 +134,7 @@ ScriptManager::ScriptManager(QObject *parent)
qRegisterMetaType<ScriptTilesetFormatWrapper*>();
qRegisterMetaType<ScriptImage*>();
qRegisterMetaType<WangIndex::Value>("WangIndex");

connect(&mWatcher, &FileSystemWatcher::pathsChanged,
this, &ScriptManager::scriptFilesChanged);

Expand Down Expand Up @@ -392,6 +394,7 @@ void ScriptManager::initialize()

globalObject.setProperty(QStringLiteral("tiled"), engine->newQObject(mModule));
globalObject.setProperty(QStringLiteral("Tiled"), engine->newQMetaObject<ScriptModule>());
globalObject.setProperty(QStringLiteral("AssetType"), engine->newQMetaObject(&AssetType::staticMetaObject));
globalObject.setProperty(QStringLiteral("GroupLayer"), engine->newQMetaObject<EditableGroupLayer>());
globalObject.setProperty(QStringLiteral("Image"), engine->newQMetaObject<ScriptImage>());
globalObject.setProperty(QStringLiteral("ImageLayer"), engine->newQMetaObject<EditableImageLayer>());
Expand All @@ -402,8 +405,8 @@ void ScriptManager::initialize()
globalObject.setProperty(QStringLiteral("TileLayer"), engine->newQMetaObject<EditableTileLayer>());
globalObject.setProperty(QStringLiteral("TileMap"), engine->newQMetaObject<EditableMap>());
globalObject.setProperty(QStringLiteral("Tileset"), engine->newQMetaObject<EditableTileset>());
globalObject.setProperty(QStringLiteral("WangSet"), engine->newQMetaObject<EditableWangSet>());
globalObject.setProperty(QStringLiteral("WangIndex"), engine->newQMetaObject(&WangIndex::staticMetaObject));
globalObject.setProperty(QStringLiteral("WangSet"), engine->newQMetaObject<EditableWangSet>());

registerBase64(engine);
registerDialog(engine);
Expand Down
2 changes: 1 addition & 1 deletion src/tiled/worlddocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "editableworld.h"
#include "worlddocument.h"

#include "editableworld.h"
#include "worldmanager.h"

#include <QFileInfo>
Expand Down

0 comments on commit a95d295

Please sign in to comment.