-
Notifications
You must be signed in to change notification settings - Fork 25
/
shortcuts.mjs
110 lines (96 loc) · 3.12 KB
/
shortcuts.mjs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/// <reference types="@mapeditor/tiled-api" />
/*
* shortcuts.js
*
* Example script that enables associating shortcuts with tilesets and layers.
*
* To use this script, add a custom property named "shortcut" to a tileset or
* layer, and set its value to the desired shortcut. For example, to assign F1
* to a tileset, add a custom property named "shortcut" with the value "F1" to
* the tileset. The property name is case-sensitive.
*
* Since the script is not aware of changes made to custom properties, a change
* of shortcut requires a restart of Tiled or reopening the relevant assets.
*
* If a shortcut doesn't work, it's likely because it's already assigned to
* another action. You can find the list of all actions and their shortcuts in
* the Preferences dialog.
*/
/**
* @param {Tileset} tileset
*/
function checkTilesetShortcut(tileset) {
let shortcut = tileset.property("shortcut");
if (typeof shortcut != "string") {
return;
}
let actionId = "tileset_" + shortcut;
tiled.log(`Registering ${shortcut} for selecting tileset "${tileset.name}"`);
let action = tiled.registerAction(actionId, () => {
if (tileset && tileset.name) {
tiled.log(`Selecting tileset "${tileset.name}"`);
tiled.mapEditor.tilesetsView.currentTileset = tileset;
}
});
action.shortcut = shortcut;
action.text = `Select ${tileset.name} tileset`;
tiled.extendMenu("Map", [
{ action: actionId, before: "AddExternalTileset" }
]);
}
let registeredLayerShortcuts = [];
/**
* @param {Layer} layer
*/
function checkLayerShortcut(layer) {
let shortcut = layer.property("shortcut");
if (typeof shortcut != "string") {
return;
}
let actionId = "layer_" + shortcut;
tiled.log(`Registering ${shortcut} for selecting layer "${layer.name}"`);
let action = tiled.registerAction(actionId, () => {
if (layer && layer.asset) {
tiled.log(`Selecting layer "${layer.name}"`);
layer.asset.currentLayer = layer;
}
});
action.shortcut = shortcut;
action.text = `Select ${layer.name} layer`;
registeredLayerShortcuts.push(action);
tiled.extendMenu("Layer", [
{ action: actionId, before: "SelectPreviousLayer" }
]);
}
/**
* @param {TileMap | GroupLayer} mapOrGroupLayer
*/
function checkMapOrGroupLayer(mapOrGroupLayer) {
mapOrGroupLayer.layers.forEach(layer => {
checkLayerShortcut(layer);
if (layer.isGroupLayer) {
checkMapOrGroupLayer(layer);
}
});
}
function checkAsset(asset) {
if (asset.isTileMap) {
/** @type TileMap */
let map = asset;
map.tilesets.forEach(checkTilesetShortcut);
} else if (asset.isTileset) {
checkTilesetShortcut(asset);
}
}
tiled.assetOpened.connect(checkAsset);
tiled.openAssets.forEach(checkAsset);
tiled.activeAssetChanged.connect(asset => {
// Hide any already created actions
registeredLayerShortcuts.forEach(action => {
action.visible = false;
});
registeredLayerShortcuts = [];
if (asset && asset.isTileMap) {
checkMapOrGroupLayer(asset);
}
});