From 9fb6f2badf2dce3bd06bcd6d9dac27e7a69cf71b Mon Sep 17 00:00:00 2001 From: Adrien Floriant Date: Mon, 11 Apr 2022 14:11:09 +0200 Subject: [PATCH 1/2] binding layer to sets extension draft --- bindLayerSets.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 bindLayerSets.js diff --git a/bindLayerSets.js b/bindLayerSets.js new file mode 100644 index 0000000..027cd19 --- /dev/null +++ b/bindLayerSets.js @@ -0,0 +1,79 @@ +// This script binds layers and tilesets with the same name : +// On layer change, if the current layer is a tile layer, it selects the corresponding tileset (if it exists) +// On tileset change, it selects the corresponding layer (if it exists) on the active TileMap + +// recursive function that parses TileMaps and GroupLayers to search for a layer named targetName +// code taken from user eishiya (https://github.com/eishiya) in this issue https://github.com/mapeditor/tiled/issues/3256 +function findLayer(curLayer, targetName) { + if(curLayer.isGroupLayer || curLayer.isTileMap) { + let numLayers = curLayer.layerCount; + for(let layerID = 0; layerID < numLayers; layerID++) { + let found = findLayer(curLayer.layerAt(layerID), targetName); + if(found) + return found; + } + } else if(curLayer.name == targetName) + return curLayer; + + return null; +} + +// change active TileMap layer based on the current TileSet +function onTilesetChange() { + let map = tiled.activeAsset; + // only consier TileMap + if (!map.isTileMap) { + return; + } + + // find layer corresponding to current Tileset + let targetName = tiled.mapEditor.tilesetsView.currentTileset.name; + let layer = findLayer(map, targetName); + + // apply + if (layer) { + map.currentLayer = layer; + } +} + +// change TileSet based on current layer +function onLayerChanged(asset) { + let layer = asset.currentLayer; + + // only consider tile layers + if (!layer.isTileLayer) { + return; + } + + // find correspongind Tileset + let tileSet = undefined; + for (ts of asset.tilesets) { + if (ts.name == layer.name) { + tileSet = ts; + break; + } + } + + // apply + if (tileSet) { + tiled.mapEditor.tilesetsView.currentTileset = tileSet; + } +} + +// do the binding for a TileMap +function bindTileSetToLayer(asset) { + // only work on TileMap + if (!asset || !asset.isTileMap) { + return; + } + + asset.currentLayerChanged.connect(() => { + onLayerChanged(asset); + }); +} + +// call layer update on each tile change +tiled.mapEditor.tilesetsView.stampCaptured.connect(onTilesetChange); + +// call the binding for each opened asset +tiled.assetOpened.connect(bindTileSetToLayer); \ No newline at end of file From 8859c273064c1acf4bd107b972fb8d835afe32ae Mon Sep 17 00:00:00 2001 From: Adrien Floriant Date: Mon, 11 Apr 2022 14:23:49 +0200 Subject: [PATCH 2/2] bind-layer-tileset extension rename + fix Better description Fix linter errors Add comment global tiled Replace CRLF by LF Replace some spaces by tabs Add missing let in for loop --- bind-layer-tileset.js | 85 +++++++++++++++++++++++++++++++++++++++++++ bindLayerSets.js | 79 ---------------------------------------- 2 files changed, 85 insertions(+), 79 deletions(-) create mode 100644 bind-layer-tileset.js delete mode 100644 bindLayerSets.js diff --git a/bind-layer-tileset.js b/bind-layer-tileset.js new file mode 100644 index 0000000..fdb310f --- /dev/null +++ b/bind-layer-tileset.js @@ -0,0 +1,85 @@ +/* + * bind-layer-tileset.js + * + * This extension connects to signals emitted when selecting a layer of a tilemap, and a tile in a tileset + * It finds a corresponding tileset (resp layer of the active Tilemap) and selects it + * The match is done between tile layers and tilesets with exact same name + */ + +/* global tiled */ + +// recursive function that parses TileMaps and GroupLayers to search for a layer named targetName +// code taken from user eishiya (https://github.com/eishiya) in this issue https://github.com/mapeditor/tiled/issues/3256 +function findLayer(curLayer, targetName) { + if(curLayer.isGroupLayer || curLayer.isTileMap) { + let numLayers = curLayer.layerCount; + for(let layerID = 0; layerID < numLayers; layerID++) { + let found = findLayer(curLayer.layerAt(layerID), targetName); + if(found) + return found; + } + } else if(curLayer.name == targetName) + return curLayer; + + return null; +} + +// change active TileMap layer based on the current TileSet +function onTilesetChange() { + let map = tiled.activeAsset; + // only consier TileMap + if (!map.isTileMap) { + return; + } + + // find layer corresponding to current Tileset + let targetName = tiled.mapEditor.tilesetsView.currentTileset.name; + let layer = findLayer(map, targetName); + + // apply + if (layer) { + map.currentLayer = layer; + } +} + +// change TileSet based on current layer +function onLayerChanged(asset) { + let layer = asset.currentLayer; + + // only consider tile layers + if (!layer.isTileLayer) { + return; + } + + // find correspongind Tileset + let tileSet = undefined; + for (let ts of asset.tilesets) { + if (ts.name == layer.name) { + tileSet = ts; + break; + } + } + + // apply + if (tileSet) { + tiled.mapEditor.tilesetsView.currentTileset = tileSet; + } +} + +// do the binding for a TileMap +function bindTileSetToLayer(asset) { + // only work on TileMap + if (!asset || !asset.isTileMap) { + return; + } + + asset.currentLayerChanged.connect(() => { + onLayerChanged(asset); + }); +} + +// call layer update on each tile change +tiled.mapEditor.tilesetsView.stampCaptured.connect(onTilesetChange); + +// call the binding for each opened asset +tiled.assetOpened.connect(bindTileSetToLayer); \ No newline at end of file diff --git a/bindLayerSets.js b/bindLayerSets.js deleted file mode 100644 index 027cd19..0000000 --- a/bindLayerSets.js +++ /dev/null @@ -1,79 +0,0 @@ -// This script binds layers and tilesets with the same name : -// On layer change, if the current layer is a tile layer, it selects the corresponding tileset (if it exists) -// On tileset change, it selects the corresponding layer (if it exists) on the active TileMap - -// recursive function that parses TileMaps and GroupLayers to search for a layer named targetName -// code taken from user eishiya (https://github.com/eishiya) in this issue https://github.com/mapeditor/tiled/issues/3256 -function findLayer(curLayer, targetName) { - if(curLayer.isGroupLayer || curLayer.isTileMap) { - let numLayers = curLayer.layerCount; - for(let layerID = 0; layerID < numLayers; layerID++) { - let found = findLayer(curLayer.layerAt(layerID), targetName); - if(found) - return found; - } - } else if(curLayer.name == targetName) - return curLayer; - - return null; -} - -// change active TileMap layer based on the current TileSet -function onTilesetChange() { - let map = tiled.activeAsset; - // only consier TileMap - if (!map.isTileMap) { - return; - } - - // find layer corresponding to current Tileset - let targetName = tiled.mapEditor.tilesetsView.currentTileset.name; - let layer = findLayer(map, targetName); - - // apply - if (layer) { - map.currentLayer = layer; - } -} - -// change TileSet based on current layer -function onLayerChanged(asset) { - let layer = asset.currentLayer; - - // only consider tile layers - if (!layer.isTileLayer) { - return; - } - - // find correspongind Tileset - let tileSet = undefined; - for (ts of asset.tilesets) { - if (ts.name == layer.name) { - tileSet = ts; - break; - } - } - - // apply - if (tileSet) { - tiled.mapEditor.tilesetsView.currentTileset = tileSet; - } -} - -// do the binding for a TileMap -function bindTileSetToLayer(asset) { - // only work on TileMap - if (!asset || !asset.isTileMap) { - return; - } - - asset.currentLayerChanged.connect(() => { - onLayerChanged(asset); - }); -} - -// call layer update on each tile change -tiled.mapEditor.tilesetsView.stampCaptured.connect(onTilesetChange); - -// call the binding for each opened asset -tiled.assetOpened.connect(bindTileSetToLayer); \ No newline at end of file