Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripting: Added tiled.load to load assets without opening an editor #3879

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/libtiled/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ class TILEDSHARED_EXPORT Object
static void setPropertyTypes(const SharedPropertyTypes &propertyTypes);
static const PropertyTypes &propertyTypes();

QObject *editable() const { return mEditable; }

private:
const TypeId mTypeId;
QString mClassName;
Expand Down
13 changes: 0 additions & 13 deletions src/tiled/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,6 @@ Document::~Document()
}
}

EditableAsset *Document::editable()
{
if (!mEditable)
mEditable = createEditable();
return mEditable.get();
}

void Document::setEditable(std::unique_ptr<EditableAsset> editable)
{
mEditable = std::move(editable);
mEditable->setDocument(this);
}

void Document::setFileName(const QString &fileName)
{
if (mFileName == fileName)
Expand Down
8 changes: 1 addition & 7 deletions src/tiled/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include <QString>
#include <QVariant>

#include <memory>

class QUndoStack;

namespace Tiled {
Expand Down Expand Up @@ -95,8 +93,7 @@ class Document : public QObject,
QUndoStack *undoStack() const;
bool isModified() const;

EditableAsset *editable();
void setEditable(std::unique_ptr<EditableAsset> editable);
virtual EditableAsset *editable() = 0;

Object *currentObject() const { return mCurrentObject; }
void setCurrentObject(Object *object);
Expand Down Expand Up @@ -149,7 +146,6 @@ class Document : public QObject,
void ignoreBrokenLinksChanged(bool ignoreBrokenLinks);

protected:
virtual std::unique_ptr<EditableAsset> createEditable() = 0;
virtual bool isModifiedImpl() const;

void updateIsModified();
Expand All @@ -163,8 +159,6 @@ class Document : public QObject,
Object *mCurrentObject = nullptr; /**< Current properties object. */
Document *mCurrentObjectDocument = nullptr;

std::unique_ptr<EditableAsset> mEditable;

private:
void currentObjectDocumentChanged(const ChangeEvent &change);
void currentObjectDocumentDestroyed();
Expand Down
23 changes: 17 additions & 6 deletions src/tiled/editableasset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@

namespace Tiled {

EditableAsset::EditableAsset(Document *document, Object *object, QObject *parent)
EditableAsset::EditableAsset(Object *object, QObject *parent)
: EditableObject(this, object, parent)
, mDocument(document)
{
if (document) {
connect(document, &Document::modifiedChanged,
this, &EditableAsset::modifiedChanged);
}
}

EditableAsset::~EditableAsset()
{
// Prevent owned object from trying to delete us again
if (mDocument)
setObject(nullptr);
}

QString EditableAsset::fileName() const
Expand Down Expand Up @@ -126,6 +128,15 @@ void EditableAsset::redo()
ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "Undo system not available for this asset"));
}

void EditableAsset::setDocument(Document *document)
{
Q_ASSERT(!mDocument && document);

mDocument = document->sharedFromThis();
connect(document, &Document::modifiedChanged,
this, &EditableAsset::modifiedChanged);
}

} // namespace Tiled

#include "moc_editableasset.cpp"
17 changes: 7 additions & 10 deletions src/tiled/editableasset.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#pragma once

#include "editableobject.h"
#include "document.h"

#include <QJSValue>
#include <QSharedPointer>
Expand Down Expand Up @@ -57,7 +58,8 @@ class EditableAsset : public EditableObject
Q_PROPERTY(AssetType::Value assetType READ assetType CONSTANT)

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

QString fileName() const;
bool isReadOnly() const override = 0;
Expand Down Expand Up @@ -88,22 +90,17 @@ public slots:
void modifiedChanged();
void fileNameChanged(const QString &fileName, const QString &oldFileName);

private:
friend class Document;
protected:
void setDocument(Document *document);

Document *mDocument;
private:
DocumentPtr mDocument;
};


inline Document *EditableAsset::document() const
{
return mDocument;
}

inline void EditableAsset::setDocument(Document *document)
{
mDocument = document;
return mDocument.get();
}

} // namespace Tiled
Expand Down
59 changes: 38 additions & 21 deletions src/tiled/editablemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,14 @@
namespace Tiled {

EditableMap::EditableMap(QObject *parent)
: EditableAsset(nullptr, new Map(), parent)
: EditableMap(std::make_unique<Map>(), parent)
{
mDetachedMap.reset(map());
}

EditableMap::EditableMap(MapDocument *mapDocument, QObject *parent)
: EditableAsset(mapDocument, mapDocument->map(), parent)
, mSelectedArea(new EditableSelectedArea(mapDocument, this))
: EditableAsset(mapDocument->map(), parent)
{
connect(mapDocument, &Document::fileNameChanged, this, &EditableAsset::fileNameChanged);
connect(mapDocument, &Document::changed, this, &EditableMap::documentChanged);
connect(mapDocument, &MapDocument::layerAdded, this, &EditableMap::attachLayer);
connect(mapDocument, &MapDocument::layerRemoved, this, &EditableMap::detachLayer);

connect(mapDocument, &MapDocument::currentLayerChanged, this, &EditableMap::currentLayerChanged);
connect(mapDocument, &MapDocument::selectedLayersChanged, this, &EditableMap::selectedLayersChanged);
connect(mapDocument, &MapDocument::selectedObjectsChanged, this, &EditableMap::selectedObjectsChanged);

connect(mapDocument, &MapDocument::regionEdited, this, &EditableMap::onRegionEdited);
setDocument(mapDocument);
}

/**
Expand All @@ -76,14 +65,13 @@ EditableMap::EditableMap(MapDocument *mapDocument, QObject *parent)
* The map's lifetime must exceed that of the EditableMap instance.
*/
EditableMap::EditableMap(const Map *map, QObject *parent)
: EditableAsset(nullptr, const_cast<Map*>(map), parent)
: EditableAsset(const_cast<Map*>(map), parent)
, mReadOnly(true)
, mSelectedArea(nullptr)
{
}

EditableMap::EditableMap(std::unique_ptr<Map> map, QObject *parent)
: EditableAsset(nullptr, map.get(), parent)
: EditableAsset(map.get(), parent)
, mDetachedMap(std::move(map))
{
}
Expand Down Expand Up @@ -671,15 +659,26 @@ void EditableMap::setSelectedObjects(const QList<QObject *> &objects)
QSharedPointer<Document> EditableMap::createDocument()
{
Q_ASSERT(mDetachedMap);
Q_ASSERT(!document());

auto document = MapDocumentPtr::create(std::move(mDetachedMap));
document->setEditable(std::unique_ptr<EditableAsset>(this));
setDocument(document.data());

return document;
}

mSelectedArea = new EditableSelectedArea(document.data(), this);
EditableMap *EditableMap::get(MapDocument *mapDocument)
{
if (!mapDocument)
return nullptr;

QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
auto editable = EditableMap::find(mapDocument->map());
if (editable)
return editable;

return document;
editable = new EditableMap(mapDocument);
// editable->moveOwnershipToCpp();
return editable;
}

void EditableMap::documentChanged(const ChangeEvent &change)
Expand Down Expand Up @@ -759,6 +758,24 @@ MapRenderer *EditableMap::renderer() const
return mRenderer.get();
}

void EditableMap::setDocument(MapDocument *mapDocument)
{
EditableAsset::setDocument(mapDocument);

mSelectedArea = new EditableSelectedArea(mapDocument, this);

connect(mapDocument, &Document::fileNameChanged, this, &EditableAsset::fileNameChanged);
connect(mapDocument, &Document::changed, this, &EditableMap::documentChanged);
connect(mapDocument, &MapDocument::layerAdded, this, &EditableMap::attachLayer);
connect(mapDocument, &MapDocument::layerRemoved, this, &EditableMap::detachLayer);

connect(mapDocument, &MapDocument::currentLayerChanged, this, &EditableMap::currentLayerChanged);
connect(mapDocument, &MapDocument::selectedLayersChanged, this, &EditableMap::selectedLayersChanged);
connect(mapDocument, &MapDocument::selectedObjectsChanged, this, &EditableMap::selectedObjectsChanged);

connect(mapDocument, &MapDocument::regionEdited, this, &EditableMap::onRegionEdited);
}

} // namespace Tiled

#include "moc_editablemap.cpp"
10 changes: 10 additions & 0 deletions src/tiled/editablemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ class EditableMap final : public EditableAsset

QSharedPointer<Document> createDocument() override;

static EditableMap *find(Map *map);
static EditableMap *get(MapDocument *mapDocument);

signals:
void currentLayerChanged();
void selectedLayersChanged();
Expand All @@ -221,6 +224,8 @@ class EditableMap final : public EditableAsset

MapRenderer *renderer() const;

void setDocument(MapDocument *mapDocument);

std::unique_ptr<Map> mDetachedMap;
mutable std::unique_ptr<MapRenderer> mRenderer;
bool mReadOnly = false;
Expand Down Expand Up @@ -380,6 +385,11 @@ inline MapDocument *EditableMap::mapDocument() const
return static_cast<MapDocument*>(document());
}

inline EditableMap *EditableMap::find(Map *map)
{
return static_cast<EditableMap*>(EditableObject::find(map));
}

} // namespace Tiled

Q_DECLARE_METATYPE(Tiled::EditableMap*)
2 changes: 1 addition & 1 deletion src/tiled/editableobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ inline void EditableObject::setAsset(EditableAsset *asset)

inline EditableObject *EditableObject::find(Object *object)
{
return object ? static_cast<EditableObject*>(object->mEditable.data())
return object ? static_cast<EditableObject*>(object->editable())
: nullptr;
}

Expand Down
15 changes: 14 additions & 1 deletion src/tiled/editableproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
namespace Tiled {

EditableProject::EditableProject(ProjectDocument *projectDocument, QObject *parent)
: EditableAsset(projectDocument, &projectDocument->project(), parent)
: EditableAsset(&projectDocument->project(), parent)
{
setDocument(projectDocument);
}

QString EditableProject::extensionsPath() const
Expand Down Expand Up @@ -62,6 +63,18 @@ QSharedPointer<Document> EditableProject::createDocument()
return nullptr;
}

EditableProject *EditableProject::get(ProjectDocument *projectDocument)
{
if (!projectDocument)
return nullptr;

auto editable = find(&projectDocument->project());
if (editable)
return editable;

return new EditableProject(projectDocument);
}

} // namespace Tiled

#include "moc_editableproject.cpp"
8 changes: 8 additions & 0 deletions src/tiled/editableproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,21 @@ class EditableProject final : public EditableAsset
Project *project() const;

QSharedPointer<Document> createDocument() override;

static EditableProject *find(Project *project);
static EditableProject *get(ProjectDocument *projectDocument);
};

inline Project *EditableProject::project() const
{
return static_cast<Project*>(object());
}

inline EditableProject *EditableProject::find(Project *project)
{
return static_cast<EditableProject*>(EditableObject::find(project));
}

} // namespace Tiled

Q_DECLARE_METATYPE(Tiled::EditableProject*)
Loading