Skip to content

Commit

Permalink
Scripting : Made currentWangSet and currentWangColorIndex writeable (#…
Browse files Browse the repository at this point in the history
…4105)

Closes #4101

Co-authored-by: Thorbjørn Lindeijer <bjorn@lindeijer.nl>
  • Loading branch information
dogboydog and bjorn authored Nov 29, 2024
1 parent 24f6ff8 commit 6a994d1
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ default/
*.user*
*.creator.*
*.autosave
build/

# Visual Studio
*.sln
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 12 additions & 12 deletions docs/scripting-doc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2480,13 +2480,13 @@ interface MapEditor {
currentBrushChanged: Signal<void>;

/**
* 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.
Expand All @@ -2496,15 +2496,15 @@ interface MapEditor {
readonly currentWangSetChanged: Signal<void>;

/**
* 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.
Expand Down Expand Up @@ -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.
Expand All @@ -4052,13 +4052,13 @@ interface TilesetEditor {
readonly currentWangSetChanged: Signal<void>;

/**
* 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.
Expand Down
24 changes: 23 additions & 1 deletion src/tiled/mapeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/tiled/mapeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);

Expand Down
23 changes: 23 additions & 0 deletions src/tiled/tileseteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
7 changes: 5 additions & 2 deletions src/tiled/tileseteditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions src/tiled/wangcolormodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/tiled/wangdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/tiled/wangdock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6a994d1

Please sign in to comment.