forked from ironmonk88/TerrainLayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terrain-main.js
155 lines (134 loc) · 5.02 KB
/
terrain-main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { TerrainLayer } from './classes/terrainlayer.js';
import { TerrainHUD } from './classes/terrainhud.js';
import { registerSettings } from "./js/settings.js";
export let debug = (...args) => {
if (debugEnabled > 1) console.log("DEBUG: terrainlayer-v2 | ", ...args);
};
export let log = (...args) => console.log("terrainlayer-v2 | ", ...args);
export let warn = (...args) => {
if (debugEnabled > 0) console.warn("terrainlayer-v2 | ", ...args);
};
export let error = (...args) => console.error("terrainlayer-v2 | ", ...args);
export let i18n = key => {
return game.i18n.localize(key);
};
export let setting = key => {
return game.settings.get("terrainlayer-v2", key);
};
function registerLayer() {
const layers = mergeObject(Canvas.layers, {
terrain: TerrainLayer
});
Object.defineProperty(Canvas, 'layers', {
get: function () {
return layers;
}
});
}
async function checkUpgrade() {
let hasInformed = false;
let inform = function () {
if (!hasInformed) {
ui.notifications.info('Converting old TerrainLayer data, please wait');
hasInformed = true;
}
}
for (let scene of game.scenes.entries) {
if (scene.data.flags?.TerrainLayer) {
let gW = scene.data.grid;
let gH = scene.data.grid;
let data = duplicate(scene.data.flags?.TerrainLayer);
for (let [k, v] of Object.entries(data)) {
if (k.startsWith('terrain')) {
if (k == 'terrainundefined' || v == undefined || v.x == undefined || v.y == undefined)
await scene.unsetFlag('TerrainLayer', k);
else if (v.points == undefined) {
inform();
let data = duplicate(v);
data.x = data.x * gW;
data.y = data.y * gH;
data.points = [[0, 0], [gW, 0], [gW, gH], [0, gH], [0, 0]];
data.width = gW;
data.height = gH;
await scene.setFlag('TerrainLayer', k, data);
}
} else if (k == 'costGrid') {
let grid = scene.getFlag('TerrainLayer', 'costGrid');
for (let y in grid) {
for (let x in grid[y]) {
if (Object.values(data).find(t => { return t.x == (parseInt(x) * gW) && t.y == (parseInt(y) * gH); }) == undefined) {
inform();
let id = makeid();
let data = { _id: id, x: parseInt(x) * gW, y: parseInt(y) * gH, points: [[0, 0], [gW, 0], [gW, gH], [0, gH], [0, 0]], width: gW, height: gH, multiple: grid[y][x].multiple };
await scene.setFlag('TerrainLayer', 'terrain' + id, data);
}
}
}
await scene.unsetFlag('TerrainLayer', 'costGrid');
}
};
}
}
if (hasInformed)
ui.notifications.info('TerrainLayer conversion complete.');
}
export function makeid() {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < 16; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
Hooks.on('canvasInit', () => {
canvas.hud.terrain = new TerrainHUD();
//Scene.constructor.config.embeddedEntities.Terrain = "terrain";
});
Hooks.on('ready', () => {
checkUpgrade();
})
Hooks.on('init', () => {
game.socket.on('module.TerrainLayer', async (data) => {
console.log(data);
canvas.terrain[data.action].apply(canvas.terrain, data.arguments);
});
registerSettings();
registerLayer();
let oldOnDragLeftStart = Token.prototype._onDragLeftStart;
Token.prototype._onDragLeftStart = function (event) {
oldOnDragLeftStart.apply(this, [event])
if (canvas != null)
canvas.terrain.visible = (canvas.grid.type != 0);
}
let oldOnDragLeftDrop = Token.prototype._onDragLeftDrop;
Token.prototype._onDragLeftDrop = function (event) {
if (canvas != null)
canvas.terrain.visible = (canvas.grid.type != 0 && (canvas.terrain.showterrain || ui.controls.activeControl == 'terrain'));
oldOnDragLeftDrop.apply(this, [event]);
}
let oldOnDragLeftCancel = Token.prototype._onDragLeftCancel;
Token.prototype._onDragLeftCancel = function (event) {
//event.stopPropagation();
const ruler = canvas.controls.ruler;
if (canvas != null && !(ruler.isDragRuler || ruler._state === Ruler.STATES.MEASURING))
canvas.terrain.visible = (canvas.grid.type != 0 && (canvas.terrain.showterrain || ui.controls.activeControl == 'terrain'));
oldOnDragLeftCancel.apply(this, [event])
}
//let handleDragCancel = MouseInteractionManager.prototype._handleDragCancel;
/*
MouseInteractionManager.prototype._handleDragCancel = function (event) {
if (canvas != null)
canvas.terrain.highlight.children[0].visible = (canvas.terrain.showterrain || ui.controls.activeControl == 'terrain');
handleDragCancel.apply(this, [event])
}*/
})
Hooks.on('renderMeasuredTemplateConfig', (config, html, data) => {
let widthRow = $('input[name="width"]', html).parent();
let tlrow = $('<div>').addClass('form-group')
.append($('<label>').html('Movement Cost'))
.append($('<input>').attr('type', 'number').attr('name', 'flags.TerrainLayer.multiple').attr('data-type', 'Number').val(config.object.getFlag('TerrainLayer', 'multiple')))
.insertAfter(widthRow);
let height = $(html).height();
$(html).css({height: height + 30});
})