Skip to content

Commit

Permalink
Scripting: Fixed Tool.updateEnabledState behavior
Browse files Browse the repository at this point in the history
The setTargetLayerType(0) call in the ScriptedTool constructor happened
before the ScriptedTool instance was set as the prototype of the
provided script object. If the provided updateEnabledState would then
set the "this.enabled" property, it would override the Tool.enabled
property instead of changing it and cause all further attempts to change
the "this.enabled" property to not affect the Tool.enabled property.

Fixed by making sure the prototype is set before a possible call to
updateEnabledState happens.

Closes #3756
  • Loading branch information
bjorn committed Aug 3, 2023
1 parent da84438 commit 9fb9510
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
3 changes: 0 additions & 3 deletions src/tiled/abstractworldtool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@
#include "mainwindow.h"
#include "map.h"
#include "mapdocument.h"
#include "mapeditor.h"
#include "maprenderer.h"
#include "mapscene.h"
#include "mapview.h"
#include "selectionrectangle.h"
#include "tile.h"
#include "utils.h"
#include "worlddocument.h"
#include "worldmanager.h"

Expand Down
26 changes: 14 additions & 12 deletions src/tiled/scriptedtool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,25 @@ ScriptedTool::ScriptedTool(Id id, QJSValue object, QObject *parent)
: AbstractTileTool(id, QStringLiteral("<unnamed tool>"), QIcon(), QKeySequence(), nullptr, parent)
, mScriptObject(std::move(object))
{
setTargetLayerType(0); // default behavior is not to disable based on current layer

// Read out the properties from the script object before setting its prototype
const QJSValue nameProperty = mScriptObject.property(QStringLiteral("name"));
const QJSValue iconProperty = mScriptObject.property(QStringLiteral("icon"));
const QJSValue toolBarActionsProperty = mScriptObject.property(QStringLiteral("toolBarActions"));
const QJSValue usesSelectedTilesProperty = mScriptObject.property(QStringLiteral("usesSelectedTiles"));
const QJSValue usesWangSetsProperty = mScriptObject.property(QStringLiteral("usesWangSets"));
const QJSValue targetLayerTypeProperty = mScriptObject.property(QStringLiteral("targetLayerType"));

// Make members of ScriptedTool available through the original object
auto &scriptManager = ScriptManager::instance();
auto self = scriptManager.engine()->newQObject(this);
mScriptObject.setPrototype(self);

if (nameProperty.isString())
setName(nameProperty.toString());

const QJSValue iconProperty = mScriptObject.property(QStringLiteral("icon"));
if (iconProperty.isString())
setIconFileName(iconProperty.toString());

const QJSValue toolBarActionsProperty = mScriptObject.property(QStringLiteral("toolBarActions"));
if (toolBarActionsProperty.isArray()) {
QStringList actionNames;
const int length = toolBarActionsProperty.property(QStringLiteral("length")).toInt();
Expand All @@ -62,22 +70,16 @@ ScriptedTool::ScriptedTool(Id id, QJSValue object, QObject *parent)
setToolBarActions(actionNames);
}

const QJSValue usesSelectedTilesProperty = mScriptObject.property(QStringLiteral("usesSelectedTiles"));
if (usesSelectedTilesProperty.isBool())
setUsesSelectedTiles(usesSelectedTilesProperty.toBool());

const QJSValue usesWangSetsProperty = mScriptObject.property(QStringLiteral("usesWangSets"));
if (usesWangSetsProperty.isBool())
setUsesWangSets(usesWangSetsProperty.toBool());

const QJSValue targetLayerTypeProperty = mScriptObject.property(QStringLiteral("targetLayerType"));
if (targetLayerTypeProperty.isNumber())
setTargetLayerType(targetLayerTypeProperty.toInt());

// Make members of ScriptedTool available through the original object
auto &scriptManager = ScriptManager::instance();
auto self = scriptManager.engine()->newQObject(this);
mScriptObject.setPrototype(self);
else
setTargetLayerType(0); // default behavior is not to disable based on current layer

PluginManager::addObject(this);
}
Expand Down

0 comments on commit 9fb9510

Please sign in to comment.