-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename-object-by-type.js
64 lines (51 loc) · 1.41 KB
/
rename-object-by-type.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
/// <reference types="@mapeditor/tiled-api" />
/*
* rename-object-by-type.js
*
* This extension adds a 'Rename Object by Type' (Ctrl+Shift+R) action to the Map
* menu, which can be used to bulk rename custom objects which have a Type, very
* usefull if exporting the map to Godot.
* I have based this script on https://github.com/mapeditor/tiled-extensions/blob/master/find-object-by-id.js
*
*/
/* global tiled */
function findObjectByType(thing, type) {
for (let i = thing.layerCount - 1; i >= 0; i--) {
const layer = thing.layerAt(i);
if (layer.isGroupLayer) {
const obj = findObjectByType(layer, type);
if (obj) {
return obj;
}
} else if (layer.isObjectLayer) {
for (const obj of layer.objects) {
if (obj.type == type) {
obj.name = obj.type + '_' + obj.id
}
}
}
}
return null;
}
let renameObjects = tiled.registerAction("RenameObjects", function(/* action */) {
const map = tiled.activeAsset;
if (!map.isTileMap) {
tiled.alert("Not a tile map!");
return;
}
let type = tiled.prompt("Please enter an object type:");
if (type == "") {
return;
}
const object = findObjectByType(map, type);
if (!object) {
tiled.alert("Failed to find object of type " + type);
return;
}
});
renameObjects.text = "Rename objects of type";
renameObjects.shortcut = "Ctrl+Shift+R";
tiled.extendMenu("Map", [
{ separator: true },
{ action: "RenameObjects" },
]);