Skip to content

Commit

Permalink
Scripting: Added Object.setColorProperty and Object.setFloatProperty
Browse files Browse the repository at this point in the history
These functions provide a reliable way of setting custom properties of
type "color" and "float".

Closes #3423
  • Loading branch information
bjorn committed Nov 4, 2022
1 parent 8b335b5 commit c5a0af7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
### Unreleased

* Fixed new layer names to be always unique (by Logan Higinbotham, #3452)
* Scripting: Fixed painting issues after changing TileLayer size (#3481)
* Scripting: Added Object.setColorProperty and Object.setFloatProperty (#3423)
* Scripting: Allow assigning null to Tile.objectGroup (by Logan Higinbotham, #3495)
* Scripting: Allow changing the items in a combo box added to a dialog
* Scripting: Fixed painting issues after changing TileLayer size (#3481)
* Defold plugin: Allow overriding z value also when exporting to .collection (#3214)
* Qt 6: Fixed invisible tileset tabs when only a single tileset is open
* Fixed positioning of point object name labels (by Logan Higinbotham, #3400)
* Fixed compile against Qt 6.4
* AppImage: Updated to Sentry 0.5.2

### Tiled 1.9.2 (16 September 2022)

Expand Down
43 changes: 40 additions & 3 deletions docs/scripting-doc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ declare namespace Qt {

/**
* Adds the given items to the combo box.
*
* @since 1.9.3
*/
addItems(texts : string[]) : void;
}
Expand Down Expand Up @@ -933,13 +935,48 @@ declare class TiledObject {
* types are `bool`, `number`, `string`, {@link FilePath},
* {@link ObjectRef} and {@link MapObject}.
*
* When setting a `number`, the property type will be set to either
* `int` or `float`, depending on whether it is a whole number.
* @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 Support for setting `color` properties is currently missing.
* @note This function does not support setting `color` properties. Use
* {@link setColorProperty} instead.
*/
setProperty(name: string, value: TiledObjectPropertyValue): void;

/**
* Sets the value of the custom property with the given name to the given
* color value.
*
* The color is specified as a string "#RGB", "#RRGGBB" or "#AARRGGBB".
*
* @since 1.9.3
*/
setColorProperty(name: string, value: color): void;

/**
* Sets the value of the custom property with the given name to the given
* color value.
*
* The color is specified by its red, green, blue and alpha components.
* Each component takes a value from 0 to 255. When not provided, the alpha
* defaults to 255.
*
* @since 1.9.3
*/
setColorProperty(name: string, red: number, green: number, blue: number, alpha?: number): void;

/**
* Sets the value of the custom property with the given name to the given
* float value.
*
* This function is provided as alternative to {@link setProperty}, since
* that function will set whole numbers as `int` properties.
*
* @since 1.9.3
*/
setFloatProperty(name: string, value: number): void;

/**
* Returns all custom properties set on this object.
*
Expand Down
18 changes: 18 additions & 0 deletions src/tiled/editableobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class EditableObject : public QObject

Q_INVOKABLE QVariant property(const QString &name) const;
Q_INVOKABLE void setProperty(const QString &name, const QVariant &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);

Q_INVOKABLE QVariantMap properties() const;
Q_INVOKABLE void setProperties(const QVariantMap &properties);
Expand Down Expand Up @@ -103,6 +106,21 @@ inline QVariant EditableObject::property(const QString &name) const
return toScript(mObject->property(name));
}

inline void EditableObject::setColorProperty(const QString &name, const QColor &value)
{
setProperty(name, value);
}

inline void EditableObject::setColorProperty(const QString &name, int r, int g, int b, int a)
{
setProperty(name, QColor(r, g, b, a));
}

inline void EditableObject::setFloatProperty(const QString &name, qreal value)
{
setProperty(name, value);
}

inline QVariantMap EditableObject::properties() const
{
return toScript(mObject->properties());
Expand Down

0 comments on commit c5a0af7

Please sign in to comment.