-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scripting: Tool function updateEnabledState() no longer usable #3756
Comments
Hmm, how does this work? Maybe this means we can't simply reuse the I guess the proper solution would be to have a separate tiled/src/tiled/scriptedtool.cpp Lines 77 to 80 in cb4c33c
|
@TehCupcakes Regarding the actual bug report, it seems what broke this is the initial call The issue can be worked around as follows: tiled.registerTool('test', {
name: "test",
updateEnabledState() {
if (this.toString() === '[object Object]')
return; // unintended and too early call from ScriptedTool constructor
this.enabled = true;
}
}); This avoids overriding the |
Yes, it errors on read only properties. I think the argument should have its own type, while the return type is still export type RegisterToolParams = Partial<Omit<Tool, 'map' | 'selectedTile' | 'tilePosition'>> & Required<Pick<Tool, 'name'>>; Furthermore, since you pointed out the handler functions operate on a mouseEntered(this: Tool): void;
mouseLeft(this: Tool): void;
mouseMoved(this: Tool, x: number, y: number, modifiers: number): void; etc. Thanks for the workaround! Glad you were able to track down the issue. :) |
@TehCupcakes I have so much still to learn about TS, thanks for explaining these solutions! Would you consider opening a pull request to make these changes to |
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 mapeditor#3756
The argument passed to registerTool is not entirely the same as the Tool instance it returns. Most of its properties are optional and some properties are only available on the Tool instance. The functions in ToolDefinition now define the type of 'this' as Tool. See mapeditor#3756
#3800 addresses the issue with the initial I went with the inheritance rather than using @TehCupcakes Would you have time to give #3800 a quick review? |
The argument passed to registerTool is not entirely the same as the Tool instance it returns. Most of its properties are optional and some properties are only available on the Tool instance. The functions in ToolDefinition now define the type of 'this' as Tool. See #3756
Describe the bug
As of 1.10.0, one can no longer use
updateEnabledState()
to override when a tool should be enabled. This is likely a regression introduced with the changes to accommodatetargetLayerType
. While this covers the most common method of enabling/disabling a tool, there are still more complex requirements which call for a way to hook into layer changes to determine enabled status.To Reproduce
Steps to reproduce the behavior:
tiled.registerTool()
and hasupdateEnabledState()
defined in the Tool definition.updateEnableState()
, setthis.enabled = true;
Expected behavior
Tool extensions should be able to dynamically set enabled status inside
updateEnabledState()
when either:targetLayerType
is definedtargetLayerType
Specifications:
NOTES
AbstractTileTool:updateEnabledState()
instead ofAbstractTool:updateEnabledState()
? (diff)mousePressed()
event if an incorrect layer is selected, since this tool happens to be related to tile placement. But it would be better to disable it sooner when the layer is selected.tiled.registerTool
include the fullTool
interface, which has no optional properties. I think the types could be improved to indicate most fields are optional. As it is, I had to ignore a Typescript error in order to avoid defining dozens of properties/functions on my tool.The text was updated successfully, but these errors were encountered: