-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcustom-hotbar.js
456 lines (397 loc) · 14.6 KB
/
custom-hotbar.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import { CHBDebug } from './scripts/custom-hotbar-debug.js';
export class CustomHotbar extends Hotbar {
/**
* @param {CustomHotbarPopulator} populator
* @param {*} options
*/
constructor(populator, options) {
super(Hotbar);
//super(options);
if( !game.macros.apps.find( (app) => app.constructor.name=="CustomHotbar" ) ) game.macros.apps.push(this);
/**
* The currently viewed macro page
* @type {number}
*/
this.page = 1;
/**
* The currently displayed set of macros
* @type {Array}
*/
this.macros = [];
/**
* Track collapsed state
* @type {boolean}
*/
this._collapsed = false;
/**
* Track which hotbar slot is the current hover target, if any
* @type {number|null}
*/
this._hover = null;
/**
*
*/
this.customMacros = [];
/**
*
*/
this.populator = populator;
}
/** @override */
static get defaultOptions() {
return mergeObject(super.defaultOptions, {
id: "custom-hotbar",
template: "modules/custom-hotbar/templates/customHotbar.html",
popOut: false,
dragDrop: [{ dragSelector: ".macro", dropSelector: "#custom-macro-list" }]
});
}
/* -------------------------------------------- */
/** @override */
getData(options) {
this.macros = this._getCustomMacrosByPage(this.page);
return {
page: this.page,
macros: this.macros,
barClass: this._collapsed ? "collapsed" : ""
};
}
/* -------------------------------------------- */
/**
* Get the Array of Macro (or null) values that should be displayed on a numbered page of the custom hotbar
* @param {number} page
* @returns {Array}
* @private
*/
_getCustomMacrosByPage(page) {
const macros = this.getCustomHotbarMacros(page);
for ( let [i, m] of macros.entries() ) {
m.customSlot = i<9 ? i+1 : 0;
m.cssClass = m.macro ? "active" : "inactive";
m.icon = m.macro ? m.macro.img : null;
//TODO: Apply better logic to match keybinding options
m.key = `Sh-${m.customSlot}`;
m.tooltip = m.macro ? m.macro.name : "";
}
return macros;
}
_getMacrosByPage(page) {
return this._getCustomMacrosByPage(page);
}
/* -------------------------------------------- */
/**
* Get an Array of Macro Entities on this User's Hotbar by page
* @param {number} page The hotbar page number
* @return {Array.<Object>}
*/
getCustomHotbarMacros(page=1) {
const macros = Array.fromRange(50).map(m => null);
for ( let [k, v] of Object.entries(this.populator.chbGetMacros())) {
macros[parseInt(k)-1] = v
}
const start = (page-1) * 10;
return macros.slice(start, start+10).map((m, i) => {
return {
slot: start + i + 1,
macro: m ? game.macros.get(m) : null
};
});
}
/* -------------------------------------------- */
/**
* Assign a Macro to a numbered custom hotbar slot between 1 and 10
* eventually expand this to a full 50 later maybe
* @param {Macro|null} macro The Macro entity to assign
* @param {number} slot The integer Hotbar slot to fill
* @param {number} [fromSlot] An optional origin slot from which the Macro is being shifted
* @return {Promise} A Promise which resolves once the User update is complete
*/
async assignCustomHotbarMacro(macro, slot, {fromSlot=null}={}) {
CHBDebug("Custom Hotbar | assignCustomHotbarMacro: macro");
CHBDebug(macro);
CHBDebug("Custom Hotbar | assignCustomHotbarMacro: slot");
CHBDebug(slot);
CHBDebug("Custom Hotbar | assignCustomHotbarMacro: fromSlot");
CHBDebug(fromSlot);
if ( !(macro instanceof Macro) && (macro !== null) ) throw new Error("Invalid Macro provided");
// const chbMacros = this.populator.chbGetMacros();
// If a slot was not provided, get the first available slot
slot = slot ? parseInt(slot) : Array.fromRange(10).find(i => !(i in ui.customHotbar));
if ( !slot ) throw new Error("No available Hotbar slot exists");
if ( slot < 1 || slot > 10 ) throw new Error("Invalid Hotbar slot requested");
// Update the hotbar data
const update = foundry.utils.duplicate(ui.customHotbar);
if ( macro ) {
CHBDebug("Custom Hotbar | Setting macro with following ID and slot:");
CHBDebug(macro.id);
CHBDebug(slot);
await this.populator.chbSetMacro(macro.id,slot);
}
else {
CHBDebug('Custom Hotbar | Unsetting!');
await this.populator.chbUnsetMacro(slot);
}
//functional but needs cleanup
CHBDebug("Custom Hotbar | Finding move origin");
if ( fromSlot ) {
CHBDebug("Custom Hotbar |", ui.customHotbar.macros);
CHBDebug("Custom Hotbar |", ui.customHotbar.macros[fromSlot-1]?.macro, ui.customHotbar.macros[fromSlot-1]?.macro === macro);
if (ui.customHotbar.macros[fromSlot-1]?.macro === macro) {
CHBDebug("Custom Hotbar | internal move detected!");
if ( fromSlot != slot ) {
CHBDebug(`Custom Hotbar | trying to delete slot ${fromSlot} in CustomHotbar`);
await this.populator.chbUnsetMacro(fromSlot);
}
} else {
CHBDebug("Custom Hotbar | drop from core macro hotbar detected!");
}
} else {
CHBDebug("Custom Hotbar | non-hotbar drop detected!");
}
//ui.customHotbar.render();
//code suggested by tposney. creates hook to allow reassignment of monkey hotpatch
Hooks.callAll("customHotbarAssignComplete");
return update;
};
/* -------------------------------------------- */
/**
* Collapse the ui.customHotbar, minimizing its display.
* @return {Promise} A promise which resolves once the collapse animation completes
*/
async collapse() {
if ( this._collapsed ) return true;
const toggle = this.element.find("#custom-bar-toggle");
const icon = toggle.children("i");
const bar = this.element.find("#custom-action-bar");
return new Promise(resolve => {
bar.slideUp(200, () => {
bar.addClass("collapsed");
icon.removeClass("fa-caret-down").addClass("fa-caret-up");
this._collapsed = true;
resolve(true);
});
});
}
/* -------------------------------------------- */
/**
* Expand the CustomHotbar, displaying it normally.
* @return {Promise} A promise which resolves once the expand animation completes
*/
expand() {
if ( !this._collapsed ) return true;
const toggle = this.element.find("#custom-bar-toggle");
const icon = toggle.children("i");
const bar = this.element.find("#custom-action-bar");
return new Promise(resolve => {
bar.slideDown(200, () => {
bar.css("display", "");
bar.removeClass("collapsed");
icon.removeClass("fa-caret-up").addClass("fa-caret-down");
this._collapsed = false;
resolve(true);
});
});
}
//const macro = game.macros.get(this.populator.macroMap[li.data("slot")]);
/* -------------------------------------------- */
/** @inheritdoc */
_contextMenu(html) {
ContextMenu.create(this, html, ".macro", this._getEntryContextOptions());
}
/* -------------------------------------------- */
/**
* Get the Macro entry context options
* @returns {object[]} The Macro entry context options
* @private
*/
_getEntryContextOptions() {
return [
{
name: "MACRO.Edit",
icon: '<i class="fas fa-edit"></i>',
condition: li => {
const macro = game.macros.get(this.populator.macroMap[li.data("slot")]);
return macro ? macro.isOwner : false;
},
callback: li => {
const macro = game.macros.get(this.populator.macroMap[li.data("slot")]);
macro.sheet.render(true);
}
},
{
name: "MACRO.Remove",
icon: '<i class="fas fa-times"></i>',
condition: li => !!this.populator.macroMap[li.data("slot")],
callback: async li => {
await this.populator.chbUnsetMacro( li.data("slot") )
this.render();
}
},
{
name: "MACRO.Delete",
icon: '<i class="fas fa-trash"></i>',
condition: li => {
const macro = game.macros.get(this.populator.macroMap[li.data("slot")]);
return macro ? macro.isOwner : false;
},
callback: li => {
const macro = game.macros.get(this.populator.macroMap[li.data("slot")]);
return Dialog.confirm({
title: `${game.i18n.localize("MACRO.Delete")} ${macro.name}`,
content: `<h4>${game.i18n.localize("AreYouSure")}</h4><p>${game.i18n.localize("MACRO.DeleteWarning")}</p>`,
yes: macro.delete.bind(macro)
});
}
},
];
}
/* -------------------------------------------- */
/* Event Listeners and Handlers
/* -------------------------------------------- */
/** @override */
activateListeners(html) {
//event.preventDefault();
super.activateListeners(html);
html.find('#custom-bar-toggle').click(this._onToggleBar.bind(this));
// Disable pages for now, will just work with first page.
// html.find(".page-control").click(this._onClickPageControl.bind(this));
}
/** @override */
async _onDrop(event) {
event.preventDefault();
CHBDebug("Custom Hotbar | custom-hotbar drop detected!");
// Try to extract the data
const data = TextEditor.getDragEventData(event);
// Get the drop target
const li = event.target.closest(".macro");
// Allow for a Hook function to handle the event
let customSlot = Number(li.dataset.slot);
// Get the dropped document
const cls = getDocumentClass(data.type);
const doc = await cls?.fromDropData(data);
if ( !doc ) return;
//If needed, temporarily hijack assignHotbarMacro to trick core/modules to auto-create macros for CustomHotbar instead
//only needs to be done when dropping an item onto the Custom Hotbar.
//revert once assign custom macro complete
CHBDebug("Custom Hotbar | Dropped type:", data.type);
if (data.type == "Item" || data.type =="RollTable") {
CHBDebug("Custom Hotbar | Attempting monkey hotpatch!");
let coreAssignHotbarMacro = game.user.assignHotbarMacro;
game.user.assignHotbarMacro = this.assignCustomHotbarMacro.bind(this);
Hooks.once("customHotbarAssignComplete", () => game.user.assignHotbarMacro = coreAssignHotbarMacro);
//does this need to be set to false when done?
if ( await Hooks.call("hotbarDrop", this, data, customSlot) === undefined ) {
CHBDebug("Custom Hotbar | hotbarDrop not found, reverting monkey hotpatch!")
game.user.assignHotbarMacro = coreAssignHotbarMacro;
return;
} else {
CHBDebug("Custom Hotbar | hotbarDrop true");
}
}
// Only handles Macro drops
// Get the Macro to add to the bar
let macro;
if ( data.type === "Macro" ) {
macro = game.macros.has(doc.id) ? doc : await cls.create(doc.toObject());
}
else macro = await this._createDocumentSheetToggle(doc);
if ( macro ) {
CHBDebug("Custom Hotbar | macro provided:", macro, "fromSlot", data.customSlot);
CHBDebug("Custom Hotbar | monkey hotpatch?", game.user.assignHotbarMacro === this.assignCustomHotbarMacro);
await this.assignCustomHotbarMacro(macro, customSlot, {fromSlot: data.customSlot});
}
game.tooltip.deactivate();
await ui.customHotbar.render();
}
/* -------------------------------------------- */
/**
* Handle left-click events too
* @param event
* @private
*/
async _onClickMacro(event) {
CHBDebug("custom macro click detected!");
event.preventDefault();
const li = event.currentTarget;
// Case 1 - create a new Macro
if ( li.classList.contains("inactive") ) {
const macro = await Macro.create({name: "New Macro", type: "chat", scope: "global"});
await ui.customHotbar.assignCustomHotbarMacro(macro, li.dataset.slot);
macro.sheet.render(true);
}
// Case 2 - trigger a Macro
else {
const macro = game.macros.get(this.populator.macroMap[li.dataset.slot]);
return macro.execute();
}
}
/* -------------------------------------------- */
/** @override */
_onDragStart(event) {
//hide tooltip so it doesn't get in the way
CHBDebug("Custom Hotbar | Attempting to hide tooltip.");
game.tooltip.deactivate();
const li = event.currentTarget.closest(".macro");
const macro = game.macros.get(this.populator.macroMap[li.dataset.slot]);
if ( !macro ) return false;
const dragData = foundry.utils.mergeObject(macro.toDragData(), {customSlot: li.dataset.slot});
event.dataTransfer.setData("text/plain", JSON.stringify(dragData));
}
/**
* Get the Macro entity being dropped in the customHotbar. If the data comes from a non-World source, create the Macro
* @param {Object} data The data transfer attached to the DragEvent
* @return {Promise<Macro|null>} A Promise which returns the dropped Macro, or null
* @private
*/
async _getDropMacro(data) {
CHBDebug("Custom Hotbar | in _getDropMacro", data);
if ( data.type !== "Macro" ) return null;
// Case 1 - Data explicitly provided (but no ID)
if ( data.data && !data.id ) {
return await Macro.create(data.data);
}
// Case 2 - Imported from a Compendium pack
else if ( data.pack ) {
const createData = await game.packs.get(data.pack).getEntry(data.id);
return Macro.create(createData);
}
// Case 3 - Imported from a World ID
else {
return game.macros.get(data.id);
}
}
/* -------------------------------------------- */
/**
* Handle hover events on a macro button to track which slot is the hover target
* @param {Event} event The originating mouseover or mouseleave event
* @private
*/
/** @override */
_onHoverMacro(event) {
event.preventDefault();
const li = event.currentTarget;
const hasAction = !li.classList.contains("inactive");
// Remove any existing tooltip
//const tooltip = li.querySelector(".tooltip");
//if ( tooltip ) li.removeChild(tooltip);
// Handle hover-in
if ( event.type === "mouseenter" ) {
CHBDebug("Custom Hotbar | Macro tooltip override fired!");
this._hover = li.dataset.slot;
if ( hasAction ) {
CHBDebug("Custom Hotbar | hasAction true.");
const macro = game.macros.get(this.populator.macroMap[li.dataset.slot]);
CHBDebug( li.dataset );
const tooltip = document.createElement("SPAN");
tooltip.classList.add("tooltip");
tooltip.textContent = macro.name;
li.appendChild(tooltip);
}
}
// Handle hover-out
else {
CHBDebug("Custom Hotbar | Mouse out!");
this._hover = null;
}
}
}