Skip to content

Commit

Permalink
add overload for addMap using an EditableMap and position QPoint, and…
Browse files Browse the repository at this point in the history
… removeMap using an EditableMap
  • Loading branch information
dogboydog committed Jan 18, 2024
1 parent 8bf70ef commit b532a3f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
52 changes: 52 additions & 0 deletions docs/scripting-doc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/tiled/editableworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/tiled/editableworld.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#pragma once

#include "editableasset.h"
#include "editablemap.h"
#include "world.h"
#include "worlddocument.h"

Expand Down Expand Up @@ -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<Document> createDocument() override;
Expand Down

0 comments on commit b532a3f

Please sign in to comment.