Skip to content

Commit

Permalink
Derive World from Object
Browse files Browse the repository at this point in the history
This way we don't need the ScriptWorld class.

Now a worlds can in theory get custom properties assigned to it. In
practice, you can only do this through the scripting API for now, and
these properties currently do not get saved (though that should be easy
to add).
  • Loading branch information
bjorn committed Jan 16, 2024
1 parent b043528 commit 08d39f9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 33 deletions.
6 changes: 4 additions & 2 deletions src/libtiled/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#pragma once

#include "tiled_global.h"
#include "object.h"

#include <QCoreApplication>
#include <QPoint>
Expand All @@ -41,11 +41,13 @@

namespace Tiled {

class TILEDSHARED_EXPORT World
class TILEDSHARED_EXPORT World : public Object
{
Q_DECLARE_TR_FUNCTIONS(Tiled::WorldManager);

public:
World() : Object(WorldType) {}

struct Pattern
{
QRegularExpression regexp;
Expand Down
15 changes: 7 additions & 8 deletions src/tiled/editableworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ namespace Tiled {

EditableWorld::EditableWorld(WorldDocument *worldDocument, QObject *parent)
: EditableAsset(worldDocument, nullptr, parent)
, mWorldObject(WorldManager::instance().worlds().value(worldDocument->fileName()))
{
setObject(&mWorldObject);
setObject(WorldManager::instance().worlds().value(worldDocument->fileName()));
}

QString EditableWorld::displayName() const
Expand Down Expand Up @@ -69,17 +68,17 @@ bool EditableWorld::isReadOnly() const

int EditableWorld::mapIndex(const QString &fileName) const
{
return mWorldObject.world->mapIndex(fileName);
return world()->mapIndex(fileName);
}

void EditableWorld::setMapRect(int mapIndex, const QRect &rect)
{
if (mapIndex < 0 || mapIndex >= mWorldObject.world->maps.size()) {
if (mapIndex < 0 || mapIndex >= world()->maps.size()) {
ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "Index out of range"));
return;
}

const QString &fileName = mWorldObject.world->maps.at(mapIndex).fileName;
const QString &fileName = world()->maps.at(mapIndex).fileName;
document()->undoStack()->push(new SetMapRectCommand(fileName, rect));
}

Expand All @@ -100,18 +99,18 @@ void EditableWorld::addMap(const QString &mapFileName, const QRect &rect)

void EditableWorld::removeMap(int mapIndex)
{
if (mapIndex < 0 || mapIndex >= mWorldObject.world->maps.size()) {
if (mapIndex < 0 || mapIndex >= world()->maps.size()) {
ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "Index out of range"));
return;
}

const QString &fileName = mWorldObject.world->maps.at(mapIndex).fileName;
const QString &fileName = world()->maps.at(mapIndex).fileName;
document()->undoStack()->push(new RemoveMapCommand(fileName));
}

bool EditableWorld::save()
{
return WorldManager::instance().saveWorld(mWorldObject.world->fileName);
return WorldManager::instance().saveWorld(world()->fileName);
}

QSharedPointer<Document> EditableWorld::createDocument()
Expand Down
20 changes: 1 addition & 19 deletions src/tiled/editableworld.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,6 @@ class ScriptWorldMapEntry : public QObject
World::MapEntry mMapEntry;
};

/**
* Wrapper which allows world structs to be used with the EditableAsset
* class.
*/
class ScriptWorld : public Object
{
public:
ScriptWorld(World *world)
: Object(WorldType)
, world(world)
{}

World *world;
};

/**
* @brief The EditableWorld class provides access to Worlds via scripting.
*/
Expand Down Expand Up @@ -93,14 +78,11 @@ class EditableWorld final : public EditableAsset
Q_INVOKABLE bool save();

QSharedPointer<Document> createDocument() override;

private:
ScriptWorld mWorldObject;
};

inline World *EditableWorld::world() const
{
return static_cast<ScriptWorld*>(object())->world;
return static_cast<World*>(object());
}

} // namespace Tiled
Expand Down
4 changes: 2 additions & 2 deletions src/tiled/scriptmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,9 @@ void ScriptModule::currentDocumentChanged(Document *document)
emit activeAssetChanged(document ? document->editable() : nullptr);
}

QList<Tiled::EditableAsset*> ScriptModule::worlds() const
QList<QObject *> ScriptModule::worlds() const
{
QList<Tiled::EditableAsset*> worlds;
QList<QObject*> worlds;

auto documentManager = DocumentManager::maybeInstance();
if (!documentManager)
Expand Down
4 changes: 2 additions & 2 deletions src/tiled/scriptmodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ScriptModule : public QObject

Q_PROPERTY(Tiled::MapEditor *mapEditor READ mapEditor)
Q_PROPERTY(Tiled::TilesetEditor *tilesetEditor READ tilesetEditor)
Q_PROPERTY(QList<Tiled::EditableAsset*> worlds READ worlds)
Q_PROPERTY(QList<QObject*> worlds READ worlds)

public:
ScriptModule(QObject *parent = nullptr);
Expand Down Expand Up @@ -142,7 +142,7 @@ class ScriptModule : public QObject
Q_INVOKABLE QString promptOpenFile(const QString &defaultDir = QString(), const QString &filters = QString(), const QString &title = QString()) const;
Q_INVOKABLE QString promptSaveFile(const QString &defaultDir = QString(), const QString &filters = QString(), const QString &title = QString()) const;

QList<Tiled::EditableAsset*> worlds() const;
QList<QObject*> worlds() const;
Q_INVOKABLE void loadWorld(const QString &fileName) const;
Q_INVOKABLE void unloadWorld(const QString &fileName) const;
Q_INVOKABLE void unloadAllWorlds() const;
Expand Down

0 comments on commit 08d39f9

Please sign in to comment.