diff --git a/.gitignore b/.gitignore index cb0ed15503..d85e383626 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ default/ *.user* *.creator.* *.autosave +build/ # Visual Studio *.sln diff --git a/NEWS.md b/NEWS.md index 5ed9e8ad13..d56d93678a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,7 @@ * Scripting: Added `MapEditor.currentBrushChanged` signal * Scripting: Added `tiled.cursor` to create mouse cursor values * Scripting: Added `Tileset.transformationFlags` (#3753) +* Scripting: Made `currentWangSet` and `currentWangColorIndex` properties writeable (#4105) * Fixed saving/loading of custom properties set on worlds (#4025) * Fixed issue with placing tile objects after switching maps (#3497) * Fixed crash when accessing a world through a symlink (#4042) diff --git a/docs/scripting-doc/index.d.ts b/docs/scripting-doc/index.d.ts index 26140ed269..acc2b85c85 100644 --- a/docs/scripting-doc/index.d.ts +++ b/docs/scripting-doc/index.d.ts @@ -2480,13 +2480,13 @@ interface MapEditor { currentBrushChanged: Signal; /** - * Gets the currently selected {@link WangSet} in the "Terrain Sets" view. + * The currently selected {@link WangSet} in the "Terrain Sets" view. * * See also {@link TileLayerWangEdit}. * - * @since 1.8 + * @since 1.8 (writable since 1.11.1) */ - readonly currentWangSet: WangSet; + currentWangSet: WangSet; /** * The signal emitted when {@link currentWangSet} changes. @@ -2496,15 +2496,15 @@ interface MapEditor { readonly currentWangSetChanged: Signal; /** - * Gets the currently selected Wang color index in the "Terrain Sets" view. + * 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. * * See also {@link TileLayerWangEdit}. * - * @since 1.8 + * @since 1.8 (writable since 1.11.1) */ - readonly currentWangColorIndex: number; + currentWangColorIndex: number; /** * The signal emitted when {@link currentWangColorIndex} changes. @@ -4038,11 +4038,11 @@ interface TilesetEditor { readonly collisionEditor: TileCollisionEditor; /** - * Gets the currently selected {@link WangSet} in the "Terrain Sets" view. + * The currently selected {@link WangSet} in the "Terrain Sets" view. * - * @since 1.9 + * @since 1.9 (writable since 1.11.1) */ - readonly currentWangSet: WangSet; + currentWangSet: WangSet; /** * The signal emitted when {@link currentWangSet} changes. @@ -4052,13 +4052,13 @@ interface TilesetEditor { readonly currentWangSetChanged: Signal; /** - * Gets the currently selected Wang color index in the "Terrain Sets" view. + * 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 1.9 + * @since 1.9 (writable since 1.11.1) */ - readonly currentWangColorIndex: number; + currentWangColorIndex: number; /** * The signal emitted when {@link currentWangColorIndex} changes. diff --git a/src/tiled/mapeditor.cpp b/src/tiled/mapeditor.cpp index 37926b812b..c99388bace 100644 --- a/src/tiled/mapeditor.cpp +++ b/src/tiled/mapeditor.cpp @@ -281,7 +281,7 @@ MapEditor::MapEditor(QObject *parent) connect(mWangDock, &WangDock::wangColorChanged, mWangBrush, &WangBrush::setColor); connect(mWangBrush, &WangBrush::colorCaptured, - mWangDock, &WangDock::onColorCaptured); + mWangDock, &WangDock::setCurrentWangColor); connect(mTileStampsDock, &TileStampsDock::setStamp, this, &MapEditor::setStamp); @@ -1067,11 +1067,33 @@ EditableWangSet *MapEditor::currentWangSet() const return EditableWangSet::get(mWangDock->currentWangSet()); } +void MapEditor::setCurrentWangSet(EditableWangSet *wangSet) +{ + if (!wangSet) { + ScriptManager::instance().throwNullArgError(0); + return; + } + mWangDock->setCurrentWangSet(wangSet->wangSet()); +} + int MapEditor::currentWangColorIndex() const { return mWangDock->currentWangColor(); } +void MapEditor::setCurrentWangColorIndex(int newIndex) +{ + if (!mWangDock->currentWangSet()) { + ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "No current Wang set")); + return; + } + if (newIndex < 0 || newIndex > mWangDock->currentWangSet()->colorCount()) { + ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "An invalid index was provided")); + return; + } + mWangDock->setCurrentWangColor(newIndex); +} + AbstractTool *MapEditor::selectedTool() const { return mSelectedTool; diff --git a/src/tiled/mapeditor.h b/src/tiled/mapeditor.h index 7d8d9b81db..b794460815 100644 --- a/src/tiled/mapeditor.h +++ b/src/tiled/mapeditor.h @@ -74,8 +74,8 @@ class MapEditor final : public Editor Q_PROPERTY(Tiled::TilesetDock *tilesetsView READ tilesetDock CONSTANT) Q_PROPERTY(Tiled::EditableMap *currentBrush READ currentBrush WRITE setCurrentBrush NOTIFY currentBrushChanged) - Q_PROPERTY(Tiled::EditableWangSet *currentWangSet READ currentWangSet NOTIFY currentWangSetChanged) - Q_PROPERTY(int currentWangColorIndex READ currentWangColorIndex NOTIFY currentWangColorIndexChanged) + Q_PROPERTY(Tiled::EditableWangSet *currentWangSet READ currentWangSet WRITE setCurrentWangSet NOTIFY currentWangSetChanged) + Q_PROPERTY(int currentWangColorIndex READ currentWangColorIndex WRITE setCurrentWangColorIndex NOTIFY currentWangColorIndexChanged) Q_PROPERTY(Tiled::MapView *currentMapView READ currentMapView CONSTANT) public: @@ -120,7 +120,10 @@ class MapEditor final : public Editor void setCurrentBrush(EditableMap *editableMap); EditableWangSet *currentWangSet() const; + void setCurrentWangSet(EditableWangSet *wangSet); + int currentWangColorIndex() const; + void setCurrentWangColorIndex(int newIndex); void addExternalTilesets(const QStringList &fileNames); diff --git a/src/tiled/tileseteditor.cpp b/src/tiled/tileseteditor.cpp index 07e091c536..9580d9d9ae 100644 --- a/src/tiled/tileseteditor.cpp +++ b/src/tiled/tileseteditor.cpp @@ -37,6 +37,7 @@ #include "objecttemplate.h" #include "preferences.h" #include "propertiesdock.h" +#include "scriptmanager.h" #include "session.h" #include "templatesdock.h" #include "tile.h" @@ -510,11 +511,33 @@ EditableWangSet *TilesetEditor::currentWangSet() const return EditableWangSet::get(mWangDock->currentWangSet()); } +void TilesetEditor::setCurrentWangSet(EditableWangSet *wangSet) +{ + if (!wangSet) { + ScriptManager::instance().throwNullArgError(0); + return; + } + mWangDock->setCurrentWangSet(wangSet->wangSet()); +} + int TilesetEditor::currentWangColorIndex() const { return mWangDock->currentWangColor(); } +void TilesetEditor::setCurrentWangColorIndex(int newIndex) +{ + if (!mWangDock->currentWangSet()) { + ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "No current Wang set")); + return; + } + if (newIndex < 0 || newIndex > mWangDock->currentWangSet()->colorCount()) { + ScriptManager::instance().throwError(QCoreApplication::translate("Script Errors", "An invalid index was provided")); + return; + } + mWangDock->setCurrentWangColor(newIndex); +} + void TilesetEditor::currentWidgetChanged() { if (!mWidgetStack->currentWidget()) diff --git a/src/tiled/tileseteditor.h b/src/tiled/tileseteditor.h index 1080dd5e34..5f86bb2f33 100644 --- a/src/tiled/tileseteditor.h +++ b/src/tiled/tileseteditor.h @@ -57,8 +57,8 @@ class TilesetEditor final : public Editor Q_OBJECT Q_PROPERTY(Tiled::TileCollisionDock *collisionEditor READ collisionEditor CONSTANT) - Q_PROPERTY(Tiled::EditableWangSet *currentWangSet READ currentWangSet NOTIFY currentWangSetChanged) - Q_PROPERTY(int currentWangColorIndex READ currentWangColorIndex NOTIFY currentWangColorIndexChanged) + Q_PROPERTY(Tiled::EditableWangSet *currentWangSet READ currentWangSet WRITE setCurrentWangSet NOTIFY currentWangSetChanged) + Q_PROPERTY(int currentWangColorIndex READ currentWangColorIndex WRITE setCurrentWangColorIndex NOTIFY currentWangColorIndexChanged) public: explicit TilesetEditor(QObject *parent = nullptr); @@ -101,7 +101,10 @@ class TilesetEditor final : public Editor TileCollisionDock *collisionEditor() const; EditableWangSet *currentWangSet() const; + void setCurrentWangSet(EditableWangSet *wangSet); + int currentWangColorIndex() const; + void setCurrentWangColorIndex(int newIndex); signals: void currentTileChanged(Tile *tile); diff --git a/src/tiled/wangcolormodel.cpp b/src/tiled/wangcolormodel.cpp index 28c177df27..866d7d6021 100644 --- a/src/tiled/wangcolormodel.cpp +++ b/src/tiled/wangcolormodel.cpp @@ -42,9 +42,7 @@ WangColorModel::WangColorModel(TilesetDocument *tilesetDocument, QModelIndex WangColorModel::colorIndex(int color) const { - if (mWangSet) - Q_ASSERT(color <= mWangSet->colorCount()); - else + if (!mWangSet || color > mWangSet->colorCount()) return QModelIndex(); return createIndex(color - 1, 0); diff --git a/src/tiled/wangdock.cpp b/src/tiled/wangdock.cpp index 5ab91b6533..a1b0786ffb 100644 --- a/src/tiled/wangdock.cpp +++ b/src/tiled/wangdock.cpp @@ -678,7 +678,7 @@ void WangDock::onWangIdUsedChanged(WangId wangId) mWangTemplateView->update(index); } -void WangDock::onColorCaptured(int color) +void WangDock::setCurrentWangColor(int color) { const QModelIndex index = mWangColorModel->colorIndex(color); diff --git a/src/tiled/wangdock.h b/src/tiled/wangdock.h index 9f7eb8cb53..42c510bf53 100644 --- a/src/tiled/wangdock.h +++ b/src/tiled/wangdock.h @@ -88,7 +88,7 @@ public slots: void setCurrentWangSet(WangSet *wangSet); void onCurrentWangIdChanged(WangId wangId); void onWangIdUsedChanged(WangId wangId); - void onColorCaptured(int color); + void setCurrentWangColor(int color); protected: void changeEvent(QEvent *event) override;