Skip to content

Commit

Permalink
Scripting: Added Object.setProperty overload for setting nested values
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn committed Jun 4, 2024
1 parent f907c3c commit bcb6615
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/scripting-doc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,28 @@ declare class TiledObject {
*/
setProperty(name: string, value: TiledObjectPropertyValue): void;

/**
* Sets the value of an object's property identified the given \a path
* to \a value.
*
* The \a path is a list of property names, where each name identifies
* a member of the previous member's value. The last name in the list
* identifies the property to set.
*
* Supported types are `bool`, `number`, `string`, {@link FilePath},
* {@link ObjectRef}, {@link MapObject} and {@link PropertyValue}.
*
* @note When setting a `number`, the property type will be set to either
* `int` or `float`, depending on whether it is a whole number. To force
* the property to be `float`, use {@link setFloatProperty}.
*
* @note This function does not support setting `color` properties. Use
* {@link setColorProperty} instead.
*
* @since 1.11
*/
setProperty(path: string[], value: TiledObjectPropertyValue): void;

/**
* Sets the value of the custom property with the given name to the given
* color value.
Expand Down
5 changes: 5 additions & 0 deletions src/libtiled/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ QVariantMap Object::resolvedProperties() const
return allProperties;
}

void Object::setProperty(const QStringList &path, const QVariant &value)
{
setPropertyMemberValue(mProperties, path, value);
}

void Object::setPropertyTypes(const SharedPropertyTypes &propertyTypes)
{
mPropertyTypes = propertyTypes;
Expand Down
10 changes: 10 additions & 0 deletions src/libtiled/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ class TILEDSHARED_EXPORT Object
void setProperty(const QString &name, const QVariant &value)
{ mProperties.insert(name, value); }

/**
* Sets the value of an object's property identified the given \a path
* to \a value.
*
* The \a path is a list of property names, where each name identifies
* a member of the previous member's value. The last name in the list
* identifies the property to set.
*/
void setProperty(const QStringList &path, const QVariant &value);

/**
* Removes the property with the given \a name.
*/
Expand Down
13 changes: 13 additions & 0 deletions src/tiled/editableobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ void EditableObject::setPropertyImpl(const QString &name, const QVariant &value)
mObject->setProperty(name, fromScript(value));
}

void EditableObject::setPropertyImpl(const QStringList &path, const QVariant &value)
{
if (path.isEmpty()) {
ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "Invalid argument"));
return;
}

if (Document *doc = document())
asset()->push(new SetProperty(doc, { mObject }, path, fromScript(value)));
else
mObject->setProperty(path, fromScript(value));
}

void EditableObject::setProperties(const QVariantMap &properties)
{
if (Document *doc = document())
Expand Down
7 changes: 7 additions & 0 deletions src/tiled/editableobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class EditableObject : public QObject

Q_INVOKABLE QVariant property(const QString &name) const;
Q_INVOKABLE void setProperty(const QString &name, const QJSValue &value);
Q_INVOKABLE void setProperty(const QStringList &path, const QJSValue &value);
Q_INVOKABLE void setColorProperty(const QString &name, const QColor &value);
Q_INVOKABLE void setColorProperty(const QString &name, int r, int g, int b, int a = 255);
Q_INVOKABLE void setFloatProperty(const QString &name, qreal value);
Expand Down Expand Up @@ -89,6 +90,7 @@ public slots:

private:
void setPropertyImpl(const QString &name, const QVariant &value);
void setPropertyImpl(const QStringList &path, const QVariant &value);

QVariant toScript(const QVariant &value) const;
QVariant fromScript(const QVariant &value) const;
Expand Down Expand Up @@ -120,6 +122,11 @@ inline void EditableObject::setProperty(const QString &name, const QJSValue &val
setPropertyImpl(name, value.toVariant());
}

inline void EditableObject::setProperty(const QStringList &path, const QJSValue &value)
{
setPropertyImpl(path, value.toVariant());
}

inline void EditableObject::setColorProperty(const QString &name, const QColor &value)
{
setPropertyImpl(name, value);
Expand Down

0 comments on commit bcb6615

Please sign in to comment.