Does the Script API give access to the toolbar tools?

I can get all the way to creating a selection with Javascript but I cannot find a way to get a tool.
I would like to select the Fill tool and fill that selection.
Do I have to create my own fill algorithm and setTile 1 at a time?

Unfortunately yes, the fill tool is implemented in C++ and its algorithm (primarily in fillRegion in tilepainter.cpp) is not currently exposed in the scripting API. The API can only select the tool through tiled.trigger("BucketFillTool").

I could imagine adding basic filling support to the TileLayerEdit. Since you have a need for filling, maybe you have a suggestion as to what this API could look like?

Since it isn’t already implemented I would say this is a feature request for future consideration.
In my case I will be selecting a bunch of regions to fill but for the time being they are all rectangle so I can just use for loops with setTile().
That said, this is what I would like to see (and bear in mind I am not fully versed in any area of tiled or the Scripting API):
There are a number of toolbar tools I could see benefiting from being added to TileLayerEdit.

  1. Stamp Tool - This one is already there as setTile()
  2. Erase Tool - This one is also already there as setTile(null)
  3. Fill Tool - This is the one we are talking about. Yes I would put it in TileLayerEdit
    I see this as setFillSelection(TileMap.selectedArea) that fills a selected region and additionally setFillRect(Rect).
  4. Shape Fill Tool - I am not familiar with this tool but I imagine it could set up to work the same as the fill tool
  5. Rectangle Select - Its already doable via TileMap.selectedArea.set().
  6. Magic Wand Select - This is another excellent candidate to be added in. wandSelect(x, y) where x,y is a map location to begin the search. This might be better off placed with the other selection function under TileMap
  7. Select Same Tile - Another candidate for a TileMap method. selectSameTile(x,y) or possibly selectSameTile(Tile) (or both)

The scripting API is an excellent system and thanks for looking at this.

Regarding the Stamp Tool, it could be worth noting TileMap.merge, which can apply a multi-layer stamp in similar fashion to the Stamp Tool. It is mostly useful when working with the user’s existing brush (MapEditor.currentBrush) or for applying a tool preview.

Regarding the Shape Fill Tool, it has an ellipse filling mode, for which we have Geometry.ellipseRegion.

I’m thinking about adding TileLayerEdit.fillRegion(region | rect, Tile) to fill a region or rectangle with a single tile. But I’m unsure if this really help you? It would mostly be a convenience thing and help with performance for this specific case.

I think the “Select Same Tile” is trivial enough to code in JS when needed. The “Magic Wand” is a bit more effort since it entails the flood fill algorithm, so I’m wondering how to expose that. Do you need a flood fill, though? It’s not as easy as wandSelect(x, y), at least not anymore because for Tiled 1.12 I’ve implemented click+drag to expand the range of tiles considered to be filled.

1 Like

It isn’t really necessary for the current project I am working on.