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

docs: Provide snippet to get a tile's image #3969

Merged
merged 2 commits into from
Jun 17, 2024
Merged
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Layer names are now trimmed when edited in the UI, to avoid accidental whitespace
* Scripting: Added API for working with worlds (#3539)
* Scripting: Added Tile.image for accessing a tile's image data
* Scripting: Added Image.copy overload that takes a rectangle
* Scripting: Added Tileset.imageFileName and ImageLayer.imageFileName
* Scripting: Added FilePath.localFile and FileEdit.fileName (string alternatives to Qt.QUrl properties)
* Scripting: Made Tileset.margin and Tileset.tileSpacing writable
Expand Down
19 changes: 19 additions & 0 deletions docs/scripting-doc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2203,6 +2203,15 @@ declare class Image {
*/
setColorTable(colors: number[] | string[]): void;

/**
* Copies the given rectangle to a new image object.
*
* When no rectangle is given, the entire image is copied.
*
* @since 1.11
*/
copy(rect?: rect) : Image;

/**
* Copies the given rectangle to a new image object.
*/
Expand Down Expand Up @@ -2466,6 +2475,16 @@ declare class Tile extends TiledObject {
* Returns the image of this tile, or the image of its tileset if it doesn't
* have an individual one.
*
* Note that a tile represents a sub-rectangle of its image (or its tileset's
* image), even if is part of an image collection tileset. The {@link
* imageRect} property provides access to this sub-rectangle. If you need a
* copy of the tile's image that is already cropped to this sub-rectangle,
* you can use the following snippet:
*
* ```js
* let image = tile.image.copy(tile.imageRect);
* ```
*
* You can assign an {@link Image} to this property to change the tile's
* image. See {@link setImage} for more information.
*
Expand Down
5 changes: 5 additions & 0 deletions src/tiled/scriptimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ void ScriptImage::setColorTable(QJSValue colors)
mImage.setColorTable(std::move(colorTable));
}

ScriptImage *ScriptImage::copy(QRect rect) const
{
return new ScriptImage(mImage.copy(rect));
}

ScriptImage *ScriptImage::copy(int x, int y, int w, int h) const
{
return new ScriptImage(mImage.copy(x, y, w, h));
Expand Down
1 change: 1 addition & 0 deletions src/tiled/scriptimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class ScriptImage : public QObject

Q_INVOKABLE void setColorTable(QJSValue colors);

Q_INVOKABLE Tiled::ScriptImage *copy(QRect rect = {}) const;
Q_INVOKABLE Tiled::ScriptImage *copy(int x, int y, int w, int h) const;
Q_INVOKABLE Tiled::ScriptImage *scaled(int w, int h,
AspectRatioMode aspectMode = IgnoreAspectRatio,
Expand Down
Loading