Skip to content
Permalink
Browse files

Scripting: Enable setting of Tile.objectGroup

While it was possible to inspect and modify the object group, there was
no way to replace it or to set it when there was no object group
associated with the tile yet.
  • Loading branch information...
bjorn committed Aug 18, 2019
1 parent 72ba25d commit 658bda071490702c67528571b053e3a5fac6aff4
@@ -994,7 +994,7 @@ Properties
**imageFileName** : string, File name of the tile image (when the tile is part of an image collection tileset).
**terrain** : :ref:`script-tileterrains`, An object specifying the terrain at each corner of the tile.
**probability** : number, Probability that the tile gets chosen relative to other tiles.
**objectGroup** : :ref:`script-objectgroup` |ro|, The :ref:`script-objectgroup` associated with the tile in case collision shapes were defined. Returns ``null`` if no collision shapes were defined for this tile.
**objectGroup** : :ref:`script-objectgroup`, The :ref:`script-objectgroup` associated with the tile in case collision shapes were defined. Returns ``null`` if no collision shapes were defined for this tile.
**frames** : :ref:`[frame] <script-frames>`, This tile's animation as an array of frames.
**animated** : bool |ro|, Indicates whether this tile is animated.
**tileset** : :ref:`script-tileset` |ro|, The tileset of the tile.
@@ -89,16 +89,16 @@ void EditableGroupLayer::insertLayerAt(int index, EditableLayer *editableLayer)
return;
}

if (editableLayer->map()) {
ScriptManager::instance().throwError(tr("Layer already part of a map"));
if (!editableLayer->isOwning()) {
ScriptManager::instance().throwError(tr("Layer is in use"));
return;
}

if (asset()) {
asset()->push(new AddLayer(mapDocument(), index, editableLayer->layer(), groupLayer()));
} else {
groupLayer()->insertLayer(index, editableLayer->layer());
editableLayer->release(); // now owned by the group layer
// ownership moves to the group layer
groupLayer()->insertLayer(index, editableLayer->release());
}
}

@@ -99,11 +99,11 @@ void EditableLayer::hold()
/**
* Release ownership of the referenced layer.
*/
void EditableLayer::release()
Layer *EditableLayer::release()
{
Q_ASSERT(mDetachedLayer.get() == layer());
Q_ASSERT(isOwning());

mDetachedLayer.release();
return mDetachedLayer.release();
}

void EditableLayer::setName(const QString &name)
@@ -72,7 +72,8 @@ class EditableLayer : public EditableObject
void detach();
void attach(EditableAsset *asset);
void hold();
void release();
Layer *release();
bool isOwning() const;

public slots:
void setName(const QString &name);
@@ -140,6 +141,11 @@ inline Layer *EditableLayer::layer() const
return static_cast<Layer*>(object());
}

inline bool EditableLayer::isOwning() const
{
return mDetachedLayer.get() == layer();
}

} // namespace Tiled

Q_DECLARE_METATYPE(Tiled::EditableLayer*)
@@ -52,11 +52,10 @@ class EditableObjectGroup : public EditableLayer
Q_INVOKABLE void addObject(Tiled::EditableMapObject *editableMapObject);
QColor color() const;

ObjectGroup *objectGroup() const;

public slots:
void setColor(const QColor &color);

private:
ObjectGroup *objectGroup() const;
};


@@ -23,6 +23,7 @@
#include "changetile.h"
#include "changetileanimation.h"
#include "changetileimagesource.h"
#include "changetileobjectgroup.h"
#include "changetileprobability.h"
#include "changetileterrain.h"
#include "editablemanager.h"
@@ -211,6 +212,33 @@ void EditableTile::setProbability(qreal probability)
tile()->setProbability(probability);
}

void EditableTile::setObjectGroup(EditableObjectGroup *editableObjectGroup)
{
if (!editableObjectGroup) {
ScriptManager::instance().throwError(tr("Invalid argument"));
return;
}

if (!editableObjectGroup->isOwning()) {
ScriptManager::instance().throwError(tr("ObjectGroup is in use"));
return;
}

std::unique_ptr<ObjectGroup> og(static_cast<ObjectGroup*>(editableObjectGroup->release()));

if (asset()) {
asset()->push(new ChangeTileObjectGroup(tileset()->tilesetDocument(),
tile(),
std::move(og)));
} else {
detachObjectGroup();
tile()->setObjectGroup(std::move(og));
}

Q_ASSERT(editableObjectGroup->objectGroup() == tile()->objectGroup());
Q_ASSERT(!editableObjectGroup->isOwning());
}

void EditableTile::setFrames(QJSValue value)
{
if (!value.isArray()) {
@@ -43,7 +43,7 @@ class EditableTile : public EditableObject
Q_PROPERTY(QString imageFileName READ imageFileName WRITE setImageFileName)
Q_PROPERTY(QJSValue terrain READ terrain WRITE setTerrain)
Q_PROPERTY(qreal probability READ probability WRITE setProbability)
Q_PROPERTY(Tiled::EditableObjectGroup *objectGroup READ objectGroup)
Q_PROPERTY(Tiled::EditableObjectGroup *objectGroup READ objectGroup WRITE setObjectGroup)
Q_PROPERTY(QJSValue frames READ frames WRITE setFrames)
Q_PROPERTY(bool animated READ isAnimated)
Q_PROPERTY(Tiled::EditableTileset *tileset READ tileset)
@@ -91,13 +91,15 @@ class EditableTile : public EditableObject
void detach();
void attach(EditableTileset *tileset);

const ObjectGroup *attachedObjectGroup() const { return mAttachedObjectGroup; }
void detachObjectGroup();

public slots:
void setType(const QString &type);
void setImageFileName(const QString &fileName);
void setTerrain(QJSValue value);
void setProbability(qreal probability);
void setObjectGroup(EditableObjectGroup *editableObjectGroup);
void setFrames(QJSValue value);

private:
@@ -189,7 +189,8 @@ void EditableTileset::tileObjectGroupChanged(Tile *tile)
Q_ASSERT(tile->tileset() == tileset());

if (auto editable = EditableManager::instance().find(tile))
editable->detachObjectGroup();
if (editable->attachedObjectGroup() != tile->objectGroup())
editable->detachObjectGroup();
}

void EditableTileset::terrainAdded(Tileset *tileset, int terrainId)

0 comments on commit 658bda0

Please sign in to comment.
You can’t perform that action at this time.