-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathfind-layer-by-id.js
56 lines (46 loc) · 1.14 KB
/
find-layer-by-id.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/// <reference types="@mapeditor/tiled-api" />
/*
* find-layer-by-id.js
*
* This extension adds a 'Select Layer by ID' (Ctrl+Shift+L) action to the
* Layer menu, which can be used to quickly jump to and select a layer when
* you know its ID.
*/
function findLayerById(thing, id) {
for (let i = thing.layerCount - 1; i >= 0; i--) {
const layer = thing.layerAt(i);
if (layer.id == id) {
return layer;
}
if (layer.isGroupLayer) {
const l = findLayerById(layer, id);
if (l) {
return l;
}
}
}
return null;
}
let selectLayerById = tiled.registerAction("SelectLayerById", function(/* action */) {
const map = tiled.activeAsset;
if (!map.isTileMap) {
tiled.alert("Not a tile map!");
return;
}
let id = tiled.prompt("Please enter a layer ID:");
if (id == "") {
return;
}
id = Number(id);
const layer = findLayerById(map, id);
if (!layer) {
tiled.alert("Failed to find a layer with ID " + id);
return;
}
map.currentLayer = layer;
});
selectLayerById.text = "Select Layer by ID";
selectLayerById.shortcut = "Ctrl+Alt+L";
tiled.extendMenu("Layer", [
{ action: "SelectLayerById", before: "SelectPreviousLayer" },
]);