From b532a3f4c13f11e9ebc35a5f2b639fd509989811 Mon Sep 17 00:00:00 2001 From: dogboydog Date: Wed, 17 Jan 2024 19:32:27 -0500 Subject: [PATCH] add overload for addMap using an EditableMap and position QPoint, and removeMap using an EditableMap --- docs/scripting-doc/index.d.ts | 52 +++++++++++++++++++++++++++++++++++ src/tiled/editableworld.cpp | 19 +++++++++++++ src/tiled/editableworld.h | 3 ++ 3 files changed, 74 insertions(+) diff --git a/docs/scripting-doc/index.d.ts b/docs/scripting-doc/index.d.ts index 7d1c8ef775..1d0c70e41e 100644 --- a/docs/scripting-doc/index.d.ts +++ b/docs/scripting-doc/index.d.ts @@ -1231,6 +1231,58 @@ declare class World extends TiledObject { * Returns true if this world contains the map specified in fileName. */ containsMap(fileName : string) : boolean; + + /** + * Get the index for a given map filename. + * @param fileName The file name of the map. + */ + mapIndex(fileName : string): number; + + /** + * Add a map to this world. + * @param fileName The file name of the map to add to this world + * @param worldRect A Qt.rect specifying the position and size of the map to add + */ + addMap(fileName: string, worldRect: rect): void; + + /** + * Add a map to this world. + * @param map The TileMap instance to add to the world. + * @param worldRect A Qt.point specifying the position to add the map at. The map + * size will automatically be used to specify the map's size in the + * world. + */ + addMap(map: TileMap, worldRect: point): void; + + /** + * Remove a map from this world. + * @param mapIndex - the index of the map to remove. + */ + removeMap(mapIndex: number): void; + + /** + * Remove a map from this world. + * @param map The TileMap instance to remove from this world. + */ + removeMap(map: TileMap): void; + + /** + * Returns true if the map at the given fileName is contained in this world. + * @param fileName The file name of the map to check for. + */ + containsMap(fileName : string): boolean; + + /** + * Change the position and size of a TileMap within this world. + * @param mapIndex The index of the map to change the position and size for. + * @param rect The new rect describing the position and size of the map. + */ + setMapRect(mapIndex : number, rect : rect): void; + + /** + * Save this world to disk. Returns true if the world was saved successfully. + */ + save(): boolean; } /** diff --git a/src/tiled/editableworld.cpp b/src/tiled/editableworld.cpp index 90796872bb..5c1d83d4c5 100644 --- a/src/tiled/editableworld.cpp +++ b/src/tiled/editableworld.cpp @@ -96,6 +96,15 @@ void EditableWorld::addMap(const QString &mapFileName, const QRect &rect) document()->undoStack()->push(new AddMapCommand(fileName(), mapFileName, rect)); } +void EditableWorld::addMap(EditableMap *map, const QPoint &position) +{ + if (map == nullptr) { + ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "Invalid argument")); + return; + } + addMap(map->fileName(), QRect(position.x(), position.y(), map->size().width(), map->size().height())); +} + void EditableWorld::removeMap(int mapIndex) { if (mapIndex < 0 || mapIndex >= world()->maps.size()) { @@ -107,6 +116,16 @@ void EditableWorld::removeMap(int mapIndex) document()->undoStack()->push(new RemoveMapCommand(fileName)); } +void EditableWorld::removeMap(EditableMap *map) +{ + if (map == nullptr) { + ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "Invalid argument")); + return; + } + int removeMapIndex = mapIndex(map->fileName()); + removeMap(removeMapIndex); +} + bool EditableWorld::save() { return WorldManager::instance().saveWorld(world()->fileName); diff --git a/src/tiled/editableworld.h b/src/tiled/editableworld.h index ecffc6f3db..74f542727d 100644 --- a/src/tiled/editableworld.h +++ b/src/tiled/editableworld.h @@ -22,6 +22,7 @@ #pragma once #include "editableasset.h" +#include "editablemap.h" #include "world.h" #include "worlddocument.h" @@ -53,7 +54,9 @@ class EditableWorld final : public EditableAsset Q_INVOKABLE int mapIndex(const QString &fileName) const; Q_INVOKABLE void setMapRect(int mapIndex, const QRect &rect); Q_INVOKABLE void addMap(const QString &mapFileName, const QRect &rect); + Q_INVOKABLE void addMap(EditableMap *map, const QPoint &position); Q_INVOKABLE void removeMap(int mapIndex); + Q_INVOKABLE void removeMap(EditableMap *map); Q_INVOKABLE bool save(); QSharedPointer createDocument() override;