Skip to content

Commit

Permalink
Scripting: Added fileName argument to Tile.setImage
Browse files Browse the repository at this point in the history
Now it also clears an existing file name, when no file name is
specified. This way the function should behave more like expected, and
more consistent with ImageLayer.setImage (which takes a URL however...)
and Tileset.loadFromImage.

Also fixed updating of the map view, when using Tileset.loadFromImage
and Tile.setImage.

Issue mapeditor#3630
  • Loading branch information
bjorn committed Feb 2, 2024
1 parent a407da0 commit 7a6c42b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
23 changes: 14 additions & 9 deletions docs/scripting-doc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2260,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 @@ -2494,19 +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.
*
* You should prefer to set the {@link imageFileName} when possible. This
* function is mostly useful when the image data is loaded from a custom
* 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, instead of setting the {@link
* imageFileName}, when saving the tileset the image data will be embedded
* for formats that support this (currently only TMX/TSX).
* 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
10 changes: 8 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 Down Expand Up @@ -88,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
3 changes: 2 additions & 1 deletion src/tiled/editabletile.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class EditableTile : public EditableObject
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
7 changes: 6 additions & 1 deletion 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

0 comments on commit 7a6c42b

Please sign in to comment.