-
Notifications
You must be signed in to change notification settings - Fork 24
/
find-object-by-id.js
66 lines (55 loc) · 1.47 KB
/
find-object-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
57
58
59
60
61
62
63
64
65
66
/// <reference types="@mapeditor/tiled-api" />
/*
* find-object-by-id.js
*
* This extension adds a 'Find Object by ID' (Ctrl+Shift+F) action to the Map
* menu, which can be used to quickly jump to and select an object when you
* know its ID.
*
* The script relies on the recently added TileMap.pixelToScreen conversion
* function to work properly for isometric maps.
*/
function findObjectById(thing, id) {
for (let i = thing.layerCount - 1; i >= 0; i--) {
const layer = thing.layerAt(i);
if (layer.isGroupLayer) {
const obj = findObjectById(layer, id);
if (obj) {
return obj;
}
} else if (layer.isObjectLayer) {
for (const obj of layer.objects) {
if (obj.id == id) {
return obj;
}
}
}
}
return null;
}
let jumpToObject = tiled.registerAction("JumpToObject", function(/* action */) {
const map = tiled.activeAsset;
if (!map.isTileMap) {
tiled.alert("Not a tile map!");
return;
}
let id = tiled.prompt("Please enter an object ID:");
if (id == "") {
return;
}
id = Number(id);
const object = findObjectById(map, id);
if (!object) {
tiled.alert("Failed to find object with ID " + id);
return;
}
const pos = map.pixelToScreen ? map.pixelToScreen(object.pos) : object.pos;
tiled.mapEditor.currentMapView.centerOn(pos.x, pos.y);
map.selectedObjects = [object];
});
jumpToObject.text = "Find Object by ID";
jumpToObject.shortcut = "Ctrl+Shift+F";
tiled.extendMenu("Map", [
{ separator: true },
{ action: "JumpToObject" },
]);