Skip to content

Commit

Permalink
Scripting: Extended the terrain related API
Browse files Browse the repository at this point in the history
* Added MapEditor.currentWangSet (and currentWangSetChanged)
* Added MapEditor.currentWangColorIndex (and currentWangColorIndexChanged)
* Added WangSet.colorName(colorIndex: number) : string
* Added WangSet.setColorName(colorIndex: number, name: string)

This resolves part of issue #2663.
  • Loading branch information
bjorn committed Jan 18, 2022
1 parent 02a7cdd commit 33b186b
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 16 deletions.
44 changes: 44 additions & 0 deletions docs/scripting-doc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,36 @@ interface MapEditor {
*/
currentBrush : TileMap

/**
* Gets the currently selected {@link WangSet} in the "Terrain Sets" view.
*
* @since Tiled 1.8
*/
readonly currentWangSet: WangSet

/**
* The signal emitted when {@link currentWangSet} changes.
*
* @since Tiled 1.8
*/
readonly currentWangSetChanged: Signal<null>;

/**
* Gets the currently selected Wang color index in the "Terrain Sets" view.
* The value 0 is used to represent the eraser mode, and the first Wang color
* has index 1.
*
* @since Tiled 1.8
*/
readonly currentWangColorIndex: number

/**
* The signal emitted when {@link currentWangColorIndex} changes.
*
* @since Tiled 1.8
*/
readonly currentWangColorIndexChanged: Signal<number>;

/**
* Access the current map view.
*/
Expand Down Expand Up @@ -2234,6 +2264,20 @@ declare class WangSet {
* Make sure the Wang set color count is set before calling this function, because it will raise an error when the Wang ID refers to non-existing colors.
*/
public setWangId(tile : Tile, wangId : number[]) : void

/**
* Returns the name of the Wang color at the given index.
*
* @since Tiled 1.8
*/
public colorName(colorIndex: number) : string

/**
* Sets the name of the Wang color at the given index.
*
* @since Tiled 1.8
*/
public setColorName(colorIndex: number, name: string) : void
}

interface color {}
Expand Down
4 changes: 4 additions & 0 deletions src/tiled/editablemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "editablemanager.h"

#include "documentmanager.h"
#include "editablegrouplayer.h"
#include "editableimagelayer.h"
#include "editablemap.h"
Expand Down Expand Up @@ -151,6 +152,9 @@ EditableTileset *EditableManager::editableTileset(Tileset *tileset)
if (!tileset)
return nullptr;

if (auto document = TilesetDocument::findDocumentForTileset(tileset->sharedPointer()))
return document->editable();

EditableTileset* &editableTileset = mEditableTilesets[tileset];
if (becomesNullValue(editableTileset)) {
editableTileset = new EditableTileset(tileset);
Expand Down
9 changes: 3 additions & 6 deletions src/tiled/editablemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,9 @@ QList<QObject *> EditableMap::tilesets() const
QList<QObject *> editableTilesets;
auto &editableManager = EditableManager::instance();

for (const SharedTileset &tileset : map()->tilesets()) {
if (auto document = TilesetDocument::findDocumentForTileset(tileset))
editableTilesets.append(document->editable());
else
editableTilesets.append(editableManager.editableTileset(tileset.data()));
}
for (const SharedTileset &tileset : map()->tilesets())
editableTilesets.append(editableManager.editableTileset(tileset.data()));

return editableTilesets;
}

Expand Down
23 changes: 23 additions & 0 deletions src/tiled/editablewangset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "editablewangset.h"

#include "changetilewangid.h"
#include "changewangcolordata.h"
#include "changewangsetdata.h"
#include "editablemanager.h"
#include "editabletile.h"
Expand Down Expand Up @@ -103,6 +104,28 @@ void EditableWangSet::setWangId(EditableTile *editableTile, QJSValue value)
wangSet()->setWangId(editableTile->id(), wangId);
}

QString EditableWangSet::colorName(int colorIndex) const
{
if (colorIndex <= 0 || colorIndex > colorCount()) {
ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "Index out of range"));
return QString();
}
return wangSet()->colorAt(colorIndex)->name();
}

void EditableWangSet::setColorName(int colorIndex, const QString &name)
{
if (colorIndex <= 0 || colorIndex > colorCount()) {
ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "Index out of range"));
return;
}

if (auto doc = tilesetDocument())
asset()->push(new ChangeWangColorName(doc, wangSet()->colorAt(colorIndex).data(), name));
else if (!checkReadOnly())
wangSet()->colorAt(colorIndex)->setName(name);
}

void EditableWangSet::setName(const QString &name)
{
if (auto document = tilesetDocument())
Expand Down
3 changes: 3 additions & 0 deletions src/tiled/editablewangset.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class EditableWangSet : public EditableObject
Q_INVOKABLE QJSValue wangId(Tiled::EditableTile *tile);
Q_INVOKABLE void setWangId(Tiled::EditableTile *tile, QJSValue value);

Q_INVOKABLE QString colorName(int colorIndex) const;
Q_INVOKABLE void setColorName(int colorIndex, const QString &name);

void setName(const QString &name);
void setType(Type type);
void setImageTile(Tiled::EditableTile *imageTile);
Expand Down
26 changes: 25 additions & 1 deletion src/tiled/mapeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
#include "createtextobjecttool.h"
#include "createtileobjecttool.h"
#include "documentmanager.h"
#include "editablemanager.h"
#include "editablemap.h"
#include "editablewangset.h"
#include "editpolygontool.h"
#include "eraser.h"
#include "filechangedwarning.h"
Expand Down Expand Up @@ -277,6 +279,10 @@ MapEditor::MapEditor(QObject *parent)
mStampBrush, &StampBrush::setWangSet);
connect(mWangDock, &WangDock::currentWangSetChanged,
mWangBrush, &WangBrush::wangSetChanged);
connect(mWangDock, &WangDock::currentWangSetChanged,
this, &MapEditor::currentWangSetChanged);
connect(mWangDock, &WangDock::wangColorChanged,
this, &MapEditor::currentWangColorIndexChanged);
connect(mWangDock, &WangDock::selectWangBrush,
this, &MapEditor::selectWangBrush);
connect(mWangDock, &WangDock::wangColorChanged,
Expand Down Expand Up @@ -1019,7 +1025,25 @@ void MapEditor::setCurrentBrush(EditableMap *editableMap)
setStamp(TileStamp(editableMap->map()->clone()));
}

AbstractTool *MapEditor::selectedTool() const {
EditableWangSet *MapEditor::currentWangSet() const
{
auto currentWangSet = mWangDock->currentWangSet();
auto tileset = currentWangSet->tileset();

auto &editableManager = EditableManager::instance();
if (auto editableTileset = editableManager.editableTileset(tileset))
return EditableManager::instance().editableWangSet(editableTileset, currentWangSet);

return nullptr;
}

int MapEditor::currentWangColorIndex() const
{
return mWangDock->currentWangColor();
}

AbstractTool *MapEditor::selectedTool() const
{
return mSelectedTool;
}

Expand Down
12 changes: 11 additions & 1 deletion src/tiled/mapeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ namespace Tiled {
class AbstractTool;
class BucketFillTool;
class ComboBoxProxyModel;
class EditableMap;
class EditPolygonTool;
class EditableMap;
class EditableWangSet;
class LayerDock;
class MapDocument;
class MapView;
Expand Down Expand Up @@ -73,6 +74,8 @@ class MapEditor final : public Editor

Q_PROPERTY(Tiled::TilesetDock *tilesetsView READ tilesetDock)
Q_PROPERTY(Tiled::EditableMap *currentBrush READ currentBrush WRITE setCurrentBrush)
Q_PROPERTY(Tiled::EditableWangSet *currentWangSet READ currentWangSet NOTIFY currentWangSetChanged)
Q_PROPERTY(int currentWangColorIndex READ currentWangColorIndex NOTIFY currentWangColorIndexChanged)
Q_PROPERTY(Tiled::MapView *currentMapView READ currentMapView)

public:
Expand Down Expand Up @@ -116,13 +119,20 @@ class MapEditor final : public Editor
EditableMap *currentBrush() const;
void setCurrentBrush(EditableMap *editableMap);

EditableWangSet *currentWangSet() const;
int currentWangColorIndex() const;

void addExternalTilesets(const QStringList &fileNames);

QAction *actionSelectNextTileset() const;
QAction *actionSelectPreviousTileset() const;

AbstractTool *selectedTool() const;

signals:
void currentWangSetChanged();
void currentWangColorIndexChanged(int colorIndex);

private:
void setSelectedTool(AbstractTool *tool);
void currentDocumentChanged(Document *document);
Expand Down
23 changes: 15 additions & 8 deletions src/tiled/wangdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,20 @@ void WangDock::setDocument(Document *document)
}
}

int WangDock::currentWangColor() const
{
QItemSelectionModel *selectionModel = mWangColorView->selectionModel();
const auto currentIndex = selectionModel->currentIndex();
int color = 0;

if (currentIndex.isValid()) {
QModelIndex index = static_cast<QAbstractProxyModel*>(mWangColorView->model())->mapToSource(currentIndex);
color = mWangColorModel->colorAt(index);
}

return color;
}

void WangDock::editWangSetName(WangSet *wangSet)
{
const QModelIndex index = wangSetIndex(wangSet);
Expand Down Expand Up @@ -434,14 +448,7 @@ void WangDock::refreshCurrentWangId()

void WangDock::refreshCurrentWangColor()
{
QItemSelectionModel *selectionModel = mWangColorView->selectionModel();
const auto currentIndex = selectionModel->currentIndex();
int color = 0;

if (currentIndex.isValid()) {
QModelIndex index = static_cast<QAbstractProxyModel*>(mWangColorView->model())->mapToSource(currentIndex);
color = mWangColorModel->colorAt(index);
}
const int color = currentWangColor();

mEraseWangIdsButton->setChecked(color == 0);
mRemoveColor->setEnabled(color != 0);
Expand Down
1 change: 1 addition & 0 deletions src/tiled/wangdock.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class WangDock : public QDockWidget

WangSet *currentWangSet() const { return mCurrentWangSet; }
WangId currentWangId() const { return mCurrentWangId; }
int currentWangColor() const;

void editWangSetName(WangSet *wangSet);
void editWangColorName(int colorIndex);
Expand Down

0 comments on commit 33b186b

Please sign in to comment.