-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscene-tiler-helpers.js
132 lines (113 loc) · 3.64 KB
/
scene-tiler-helpers.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
class SceneTilerHelpers {
/**
* Deletes all placeable objects in the entire scene.
*
* @static
* @memberof SceneTilerHelpers
*/
static async clearScene() {
for (const def of SceneTiler.layers) {
await canvas[def.layer].deleteAll();
}
}
/**
* Calculates the scale ratio between source and target scenes
*
* @static
* @param {SceneData} source - Scene data from which objects are coming
* @param {SceneData} target - Scene data to which objects are going
* @return {Number} - The ratio as a decimal of the grid sizes
* @memberof SceneTilerHelpers
*/
static getScaleFactor(source, target) {
if (source.gridUnits != target.gridUnits)
ui.notifications.warn(game.i18n.localize("SCNTILE.notifications.warn.unmatchedUnits"));
const distScale =
STEntityTranslators.calculateScaleFactor(source.gridDistance, target.gridDistance)
return STEntityTranslators.calculateScaleFactor(source.grid, target.grid) / distScale;
}
/**
* Determin the size and location of the tile.
*
* @static
* @param {SceneData} source - The scene from which the tile is being created
* @param {Number} x - The X coodinate of the location where the scene was dropped
* @param {Number} y - The Y coodinate of the location where the scene was dropped
* @param {Boolean} [centered=true] - If true, the tile position is shifted to be relative to the center of the tile
* @return {{
* width: Number,
* height: Number,
* x: Number,
* y: Number
* }} The width, height, and coordinates of the tile
*
* @memberof SceneTilerHelpers
*/
static getTilePos(source, x, y, centered = true) {
const scale = this.getScaleFactor(source, canvas.scene.data);
const { width, height } =
STEntityTranslators.getScaledTileSize(source, scale);
if (centered) {
x = x - width / 2;
y = y - height / 2;
}
if (!canvas.grid.hitArea.contains(x, y)) x = y = 0;
return { width, height, ...canvas.grid.getSnappedPosition(x, y) };
}
/**
* Calculates the amount of padding in the x and y axis of the source Scene
*
* @static
* @param {SceneData} source - The scene from which the padding is being calcualated
* @return {[Number, Number]} The x, y padding amounts
* @memberof SceneTilerHelpers
*/
static getPadding(source) {
const padding = source.padding, grid = source.grid;
return [ Math.ceil(source.width / grid * padding) * grid,
Math.ceil(source.height / grid * padding) * grid ]
}
}
/* Macro Nonsense, WIP */
class STLayerSwitcher {
static create() {
canvas.scene.setFlag("scene-tiler", "layers",
canvas.tiles.controlled.map(t => {
return {
id: t.id,
z: t.data.z,
active: false
}
}).sort((a, b) => a.z > b.z)
);
}
static async next(forward = true) {
const layers = duplicate(
canvas.scene.getFlag("scene-tiler", "layers")
);
let active = layers.findIndex(l => l.active);
if (active < 0)
active = forward ? layers.length - 1 : 0;
let next = forward ?
active + 1 < layers.length ? active + 1 : 0
: active - 1 > -1 ? active - 1 : layers.length - 1;
let z = layers.reduce((max, l) => Math.max(max, l.z), 0);
await canvas.tiles.get(layers[active].id).update({
z: layers[active].z,
locked: false
});
layers[active].active = false;
await canvas.tiles.get(layers[next].id).update({
z: z + 1,
locked: true
});
layers[next].active = true;
await canvas.scene.setFlag("scene-tiler", "layers", layers);
}
static async up() {
return this.next(true);
}
static async down() {
return this.next(false);
}
}