Skip to content

Commit

Permalink
Merge pull request #3882 from bjorn/scripting-tile-image
Browse files Browse the repository at this point in the history
Closes #3630
  • Loading branch information
bjorn committed Feb 2, 2024
2 parents 4733a6a + 7a6c42b commit f5452a3
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 19 deletions.
6 changes: 4 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

* Added --project command-line parameter for use when exporting (#3797)
* Scripting: Added API for working with worlds (#3539)
* Scripting: Added Tile.image for accessing a tile's image data
* Scripting: Added Tileset.imageFileName and ImageLayer.imageFileName
* Scripting: Made Tileset.margin and Tileset.tileSpacing writable
* JSON format: Fixed tile order when loading a tileset using the old format
* Godot export: Fixed positioning of tile collision shapes (by Ryan Petrie, #3862)
* tmxrasterizer: Added --hide-object and --show-object arguments (by Lars Luz, #3819)
* Scripting: Made Tileset.margin and Tileset.tileSpacing writable
* tmxrasterizer: Added --frames and --frame-duration arguments to export animated maps as multiple images (#3868)
* tmxviewer: Added support for viewing JSON maps (#3866)
* Windows: Fixed the support for WebP images (updated to Qt 6.5.3, #3661)
* Windows: Fixed the support for WebP images (updated to Qt 6.6.1, #3661)
* Fixed mouse handling issue when zooming while painting (#3863)
* Fixed possible crash after a scripted tool disappears while active
* TMX format: Embedded images are now also supported on tilesets and image layers
Expand Down
59 changes: 51 additions & 8 deletions docs/scripting-doc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2214,14 +2214,25 @@ declare class ImageLayer extends Layer {

/**
* Reference to the image rendered by this layer.
*
* If you need a plain string, you'll want to use {@link imageFileName}
* instead.
*/
imageSource: Qt.QUrl;

/**
* Reference to the image rendered by this layer.
*
* @since 1.10.3
*/
imageFileName: string;

/**
* Returns a copy of this layer's image.
*
* When assigning an image to this property, the imageSource property is
* cleared. Use {@link setImage} when you want to also set the imageSource.
* When assigning an image to this property, the {@link imageFileName}
* property is cleared. Use {@link setImage} when you want to also set the
* imageSource.
*
* @warning This property is writable but has no undo!
*
Expand Down Expand Up @@ -2249,8 +2260,8 @@ declare class ImageLayer extends Layer {
constructor(name? : string);

/**
* Sets the image for this layer to the given image, optionally also
* setting the source of the image.
* Sets the image for this layer to the given image, optionally also setting
* its file name. The existing image file name is cleared.
*
* @warning This function has no undo!
*/
Expand Down Expand Up @@ -2431,6 +2442,17 @@ declare class Tile extends TiledObject {
*/
imageFileName : string

/**
* Returns the image of this tile, or the image of its tileset if it doesn't
* have an individual one.
*
* You can assign an {@link Image} to this property to change the tile's
* image. See {@link setImage} for more information.
*
* @since 1.10.3
*/
image: Image;

/**
* The source rectangle (in pixels) for this tile.
*
Expand Down Expand Up @@ -2472,11 +2494,24 @@ declare class Tile extends TiledObject {
readonly tileset : Tileset

/**
* Sets the image of this tile.
* Sets the image of this tile, optionally also setting its file name. The
* existing image file name is cleared.
*
* @warning This function has no undo and does not affect the saved tileset!
* You should prefer to just set the {@link imageFileName} when possible.
* This function is mostly useful when the image data is loaded from a custom
* format.
*
* If an image is set directly on a tile, without specifying its file name,
* when saving the tileset the image data will be embedded for formats that
* support this (currently only TMX/TSX).
*
* @note Before Tiled 1.10.3, this function did not change the image file
* name. For compatibility, set {@link imageFileName} before calling this
* function, if necessary.
*
* @warning This function has no undo!
*/
setImage(image : Image) : void
setImage(image : Image, source?: string) : void
}

/**
Expand Down Expand Up @@ -3454,6 +3489,13 @@ declare class Tileset extends Asset {
* repeatedly setting up the tiles in response to changing parameters.
*
* @note Map files are supported tileset image source as well.
*
* @since 1.10.3
*/
imageFileName : string

/**
* @deprecated Use {@link imageFileName} instead.
*/
image : string

Expand Down Expand Up @@ -4716,6 +4758,7 @@ declare class ColorButton extends Qt.QWidget {
*/
colorChanged: Signal<color>;
}

/**
* Widget with a button which opens a file picker dialog
* and displays the path in the dialog.
Expand All @@ -4740,11 +4783,11 @@ declare class FileEdit extends Qt.QWidget {
*/
filter: FileFilter;
}

/**
* A widget that displays an {@link Image} on your dialog.
*/
declare class ImageWidget extends Qt.QWidget {

/**
* The image to be displayed in the widget
*/
Expand Down
9 changes: 9 additions & 0 deletions src/tiled/editableimagelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "editableimagelayer.h"

#include "changeevents.h"
#include "changeimagelayerproperty.h"
#include "editablemap.h"
#include "scriptimage.h"
Expand Down Expand Up @@ -68,13 +69,21 @@ void EditableImageLayer::setImageSource(const QUrl &imageSource)
}
}

void EditableImageLayer::setImageFileName(const QString &fileName)
{
setImageSource(QUrl::fromLocalFile(fileName));
}

void EditableImageLayer::setImage(ScriptImage *image, const QUrl &source)
{
if (checkReadOnly())
return;

// WARNING: This function has no undo!
imageLayer()->loadFromImage(QPixmap::fromImage(image->image()), source);

if (auto doc = document())
emit doc->changed(ImageLayerChangeEvent(imageLayer(), ImageLayerChangeEvent::ImageSourceProperty));
}

void EditableImageLayer::setRepeatX(bool repeatX)
Expand Down
8 changes: 8 additions & 0 deletions src/tiled/editableimagelayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class EditableImageLayer : public EditableLayer

Q_PROPERTY(QColor transparentColor READ transparentColor WRITE setTransparentColor)
Q_PROPERTY(QUrl imageSource READ imageSource WRITE setImageSource)
Q_PROPERTY(QString imageFileName READ imageFileName WRITE setImageFileName)
Q_PROPERTY(Tiled::ScriptImage *image READ image WRITE setImage)
Q_PROPERTY(bool repeatX READ repeatX WRITE setRepeatX)
Q_PROPERTY(bool repeatY READ repeatY WRITE setRepeatY)
Expand All @@ -47,12 +48,14 @@ class EditableImageLayer : public EditableLayer

const QColor &transparentColor() const;
const QUrl &imageSource() const;
QString imageFileName() const;
ScriptImage *image() const;
bool repeatX() const;
bool repeatY() const;

void setTransparentColor(const QColor &transparentColor);
void setImageSource(const QUrl &imageSource);
void setImageFileName(const QString &fileName);
void setRepeatX(bool repeatX);
void setRepeatY(bool repeatY);

Expand All @@ -72,6 +75,11 @@ inline const QUrl &EditableImageLayer::imageSource() const
return imageLayer()->imageSource();
}

inline QString EditableImageLayer::imageFileName() const
{
return imageLayer()->imageSource().toString(QUrl::PreferLocalFile);
}

inline bool EditableImageLayer::repeatX() const
{
return imageLayer()->repeatX();
Expand Down
15 changes: 13 additions & 2 deletions src/tiled/editabletile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "objectgroup.h"
#include "scriptimage.h"
#include "scriptmanager.h"
#include "tilesetdocument.h"

#include <QCoreApplication>
#include <QJSEngine>
Expand All @@ -48,6 +49,11 @@ EditableTile::~EditableTile()
setObject(nullptr);
}

ScriptImage *EditableTile::image() const
{
return new ScriptImage(tile()->image().toImage());
}

EditableObjectGroup *EditableTile::objectGroup() const
{
if (!mAttachedObjectGroup) {
Expand Down Expand Up @@ -83,15 +89,20 @@ EditableTileset *EditableTile::tileset() const
return static_cast<EditableTileset*>(asset());
}

void EditableTile::setImage(ScriptImage *image)
void EditableTile::setImage(ScriptImage *image, const QString &fileName)
{
if (!image) {
ScriptManager::instance().throwNullArgError(0);
return;
}

const auto pixmap = QPixmap::fromImage(image->image());

// WARNING: This function has no undo!
tile()->setImage(QPixmap::fromImage(image->image()));
if (auto doc = tilesetDocument())
doc->setTileImage(tile(), pixmap, QUrl::fromLocalFile(fileName));
else
tile()->setImage(pixmap);
}

void EditableTile::detach()
Expand Down
5 changes: 4 additions & 1 deletion src/tiled/editabletile.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class EditableTile : public EditableObject
Q_PROPERTY(QSize size READ size)
Q_PROPERTY(QString type READ className WRITE setClassName) // compatibility with Tiled < 1.9
Q_PROPERTY(QString imageFileName READ imageFileName WRITE setImageFileName)
Q_PROPERTY(Tiled::ScriptImage *image READ image WRITE setImage)
Q_PROPERTY(QRect imageRect READ imageRect WRITE setImageRect)
Q_PROPERTY(qreal probability READ probability WRITE setProbability)
Q_PROPERTY(Tiled::EditableObjectGroup *objectGroup READ objectGroup WRITE setObjectGroup)
Expand Down Expand Up @@ -76,14 +77,16 @@ class EditableTile : public EditableObject
int height() const;
QSize size() const;
QString imageFileName() const;
ScriptImage *image() const;
QRect imageRect() const;
qreal probability() const;
EditableObjectGroup *objectGroup() const;
QJSValue frames() const;
bool isAnimated() const;
EditableTileset *tileset() const;

Q_INVOKABLE void setImage(Tiled::ScriptImage *image);
Q_INVOKABLE void setImage(Tiled::ScriptImage *image,
const QString &fileName = QString());

Tile *tile() const;

Expand Down
9 changes: 7 additions & 2 deletions src/tiled/editabletileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "scriptmanager.h"
#include "tilesetchanges.h"
#include "tilesetdocument.h"
#include "tilesetmanager.h"
#include "tilesetwangsetmodel.h"

#include <QCoreApplication>
Expand Down Expand Up @@ -78,7 +79,11 @@ void EditableTileset::loadFromImage(ScriptImage *image, const QString &source)
}

// WARNING: This function has no undo!
tileset()->loadFromImage(image->image(), source);
if (tileset()->loadFromImage(image->image(), source))
emit TilesetManager::instance()->tilesetImagesChanged(tileset());

if (auto doc = tilesetDocument())
emit doc->tilesetChanged(tileset());
}

EditableTile *EditableTileset::tile(int id)
Expand Down Expand Up @@ -245,7 +250,7 @@ void EditableTileset::setName(const QString &name)
tileset()->setName(name);
}

void EditableTileset::setImage(const QString &imageFilePath)
void EditableTileset::setImageFileName(const QString &imageFilePath)
{
if (isCollection() && tileCount() > 0) {
ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "Can't set the image of an image collection tileset"));
Expand Down
9 changes: 5 additions & 4 deletions src/tiled/editabletileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class EditableTileset : public EditableAsset
Q_OBJECT

Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(QString image READ image WRITE setImage)
Q_PROPERTY(QString image READ imageFileName WRITE setImageFileName) // deprecated
Q_PROPERTY(QString imageFileName READ imageFileName WRITE setImageFileName)
Q_PROPERTY(QList<QObject*> tiles READ tiles)
Q_PROPERTY(QList<QObject*> wangSets READ wangSets)
Q_PROPERTY(int tileCount READ tileCount)
Expand Down Expand Up @@ -108,7 +109,7 @@ class EditableTileset : public EditableAsset
AssetType::Value assetType() const override { return AssetType::Tileset; }

const QString &name() const;
QString image() const;
QString imageFileName() const;
int tileCount() const;
int columnCount() const;
int nextTileId() const;
Expand Down Expand Up @@ -156,7 +157,7 @@ class EditableTileset : public EditableAsset

public slots:
void setName(const QString &name);
void setImage(const QString &imageFilePath);
void setImageFileName(const QString &imageFilePath);
void setTileWidth(int width);
void setTileHeight(int height);
void setTileSize(QSize size);
Expand Down Expand Up @@ -199,7 +200,7 @@ inline const QString &EditableTileset::name() const
return tileset()->name();
}

inline QString EditableTileset::image() const
inline QString EditableTileset::imageFileName() const
{
return tileset()->imageSource().toString(QUrl::PreferLocalFile);
}
Expand Down

0 comments on commit f5452a3

Please sign in to comment.