-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpoi-teleport.js
executable file
·357 lines (335 loc) · 9.39 KB
/
poi-teleport.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
* @class PointOfInterestTeleporter
*/
class PointOfInterestTeleporter {
/**
* Handles on the canvasReady Hook.
*
* Checks all notes, and adds event listeners for
* closing the note context menu.
*
* @static
* @memberof PointOfInterestTeleporter
*/
static onReady() {
canvas.notes.placeables.forEach(n => this.checkNote(n));
canvas.mouseInteractionManager.target.on("rightdown", () => canvas.hud.poiTp.clear());
canvas.mouseInteractionManager.target.on("mousedown", () => canvas.hud.poiTp.clear());
console.log(game.i18n.localize("poitp.name"), "| Ready.");
}
/**
* Handles renderHeadsUpDisplay Hook.
*
* Creates a new HUD for map notes,
* and adds it to the document.
*
* @static
* @param {HeadsUpDisplay} hud - The heads up display container class
* @param {jquery} html - The html of the HUD
* @memberof PointOfInterestTeleporter
*/
static renderHeadsUpDisplay(hud, html) {
hud.poiTp = new PoiTpHUD();
const hudTemp = document.createElement("template");
hudTemp.id = "poi-tp-ctx-menu";
html.append(hudTemp);
}
/**
* Handles the createNote Hook.
*
* @static
* @param {NoteDocument} noteDocument - The document associated with the new note
* @memberof PointOfInterestTeleporter
*/
static createNote(noteDocument) {
if (noteDocument.object) return this.checkNote(noteDocument.object);
}
/**
* Handles updateNote Hook.
*
* @static
* @param {NoteDocument} noteDocument - The document associated with the new note
* @memberof PointOfInterestTeleporter
*/
static updateNote(noteDocument) {
if (noteDocument.object) return this.checkNote(noteDocument.object);
}
/**
* Handles the getSceneDirectoryEntryContext Hook.
*
* Adds a new item to the scene directory context
* menu. The new item allows for a new scene note
* to be created in one click.
*
* The new option appears in place of the "Scene Notes"
* option in the context menu if the scene doesn't have notes.
*
* @static
* @param {jquery} html - The HTML of the directory tab
* @param {object[]} options - An array of objects defining options in the context menu
* @memberof PointOfInterestTeleporter
*/
static getSceneDirEnCtx(html, options) {
// Add this option at the third index, so that it appears in the same place that
// the scene notes option would appear
options.splice(2, 0, {
name: "poitp.createNote",
icon: '<i class="fas fa-scroll"></i>',
/**
* Checks whether or not this option should be shown
*
* @param {jquery} li - The list item of this option
* @return {boolean} True if the scene doesn't have a journal entry defined
*/
condition: li => {
const scene = game.scenes.get(li.data("documentId"));
return !scene.journal;
},
/**
* Creates a new Journal Entry for the scene,
* with the same name as the scene. Then sets
* the new note as the scene notes for that scene.
*
* @param {jquery} li - The list item of this option
*/
callback: li => {
const scene = game.scenes.get(li.data("documentId"));
JournalEntry.create({
name: scene.name,
type: "base",
types: "base"
}, { renderSheet: true })
.then(entry => scene.update({ "journal": entry.id }));
}
});
}
/**
* Returns a promise that resolves on the next animation frame.
*
* @static
* @return {Promise} A promise that resolves on the next animation frame
* @memberof PointOfInterestTeleporter
*/
static nextFrame() {
return new Promise(resolve => window.requestAnimationFrame(resolve));
}
/**
* Waits for the existence of a property on an object, or some limited number of loops.
*
* @static
* @param {object} object
* @param {string} property
* @param {number} limit
* @memberof PointOfInterestTeleporter
* @return {Promise<boolean>} A promise that resolves when the property exists, or the limit is reached
*/
static async waitFor(object, property, limit) {
for (; limit > 0 && !object[property]; limit--) await this.nextFrame();
console.log(object, object[property]);
return Boolean(object[property]);
}
/**
* Checks if the supplied note is associated with a scene,
* if so creates a new PointOfInterestTeleporter for that note.
*
* @static
* @param {Note} note - A map note to check
* @memberof PointOfInterestTeleporter
*/
static async checkNote(note) {
const scene = game.scenes.find(s => s.journal?.id == note?.entry?.id);
if (!scene) return;
if (!await this.waitFor(note, "mouseInteractionManager", 60)) return;
new PointOfInterestTeleporter(note, scene);
}
/**
* Creates an instance of PointOfInterestTeleporter.
*
* @param {Note} note - A map note
* @param {Scene} scene - A target scene
* @memberof PointOfInterestTeleporter
*/
constructor(note, scene) {
this.note = note;
this.scene = scene;
this.activateListeners();
}
/**
* Activate any event handlers
*
* @memberof PointOfInterestTeleporter
*/
activateListeners() {
console.log(this.note);
this.note.mouseInteractionManager.target.on("rightdown", this._contextMenu.bind(this));
}
/**
* Handle the right click event
*
* Binds this note to the context menu HUD
* and prevents the event from bubbling
*
* @param {Event} event - The event that triggered this callback
* @memberof PointOfInterestTeleporter
*/
_contextMenu(event) {
console.log(event);
event.stopPropagation();
canvas.hud.poiTp.bind(this);
}
/**
* Convenience alias for the note x coordniate
*
* @readonly
* @memberof PointOfInterestTeleporter
*/
get x() { return this.note.x; }
/**
* Convenience alias for the note y coordniate
*
* @readonly
* @memberof PointOfInterestTeleporter
*/
get y() { return this.note.y; }
/**
* @typedef ContextMenuOption
* @property {string} icon - A string of HTML representing a Font Awesome icon
* @property {string} title - The text, or i18n reference, for the text to display on the option
* @property {string} trigger - The name of a method of PointOfInterestTeleporter to call in response to clicking this option
*//**
* Returns an array of menu option for the context menu.
*
* @return {ContextMenuOption[]}
* @memberof PointOfInterestTeleporter
*/
getOptions() {
const options = [
{
icon: `<i class="fas fa-eye fa-fw"></i>`,
title: "poitp.view",
trigger: "viewScene"
}
];
const gmOptions = game.user.isGM ? [
{
icon: `<i class="fas fa-bullseye fa-fw"></i>`,
title: "poitp.activate",
trigger: "activateScene"
},
{
icon: `<i class="fas fa-scroll fa-fw"></i>`,
title: "poitp.toggleNav",
trigger: "toggleNav"
}
] : [];
return options.concat(gmOptions);
}
/**
* Activates the scene.
*
* @memberof PointOfInterestTeleporter
*/
activateScene() {
this.scene.activate();
}
/**
* Shows the scene, but doens't activate it.
*
* @memberof PointOfInterestTeleporter
*/
viewScene() {
this.scene.view();
}
/**
* Toggles whether or not the scene is shown in the navigation bar.
*
* @memberof PointOfInterestTeleporter
*/
toggleNav() {
this.scene.update({ navigation: !this.scene.navigation });
}
}
/**
* The HUD used as a context menu for map notes.
*
* @class PoiTpHUD
* @extends {BasePlaceableHUD}
*/
class PoiTpHUD extends BasePlaceableHUD {
/**
* Assign the default options which are supported by the entity edit sheet
* @type {Object}
*/
static get defaultOptions() {
return mergeObject(super.defaultOptions, {
id: "poi-tp-ctx-menu",
template: "modules/poi-teleport/poi-hud.html"
});
}
/**
* Binds an entity to the HUD
*
* The PointOfInterestTeleporter is stored,
* and the note associated with it is bound.
*
* @override
* @param {PointOfInterestTeleporter} poitp
* @memberof PoiTpHUD
*/
bind(poitp) {
this.poitp = poitp;
super.bind(poitp.note);
}
/**
* @typedef PoiTpHudData - Data to be sent to the POI TP HUD template
* @property {ContextMenuOption[]} options - The set of options
*//**
* Creates a data object to be passed to this HUD's Handlesbars template
*
* @override
* @return {PoiTpHudData}
* @memberof PoiTpHUD - Data to be sent to the POI TP HUD template
*/
getData() {
/** @type PoiTpHudData */
const data = {};
data.options = this.poitp.getOptions();
return data;
}
/**
* Activate any event listenders on the HUD
*
* Activates a click listener to prevent propagation,
* as activates click listeners for all menu options.
*
* Each option has its own handler, stored in its data-trigger.
*
* @override
* @param {jquery} html - The html of the HUD
* @memberof PoiTpHUD
*/
activateListeners(html) {
super.activateListeners(html);
html.click(e => e.stopPropagation());
html.find("[data-trigger]")
.click((event) => this.poitp[event.currentTarget.dataset.trigger](event));
}
/**
* Set's the position of the HUD to match the position of the map note.
*
* @override
* @memberof PoiTpHUD
*/
setPosition() {
const position = {
left: this.object.x,
top: this.object.y
};
this.element.css(position);
}
}
Hooks.on("getSceneDirectoryEntryContext", (...args) => PointOfInterestTeleporter.getSceneDirEnCtx(...args));
Hooks.on("renderHeadsUpDisplay", (...args) => PointOfInterestTeleporter.renderHeadsUpDisplay(...args));
Hooks.on("canvasReady", () => PointOfInterestTeleporter.onReady());
Hooks.on("createNote", (...args) => PointOfInterestTeleporter.createNote(...args));
Hooks.on("updateNote", (...args) => PointOfInterestTeleporter.updateNote(...args));