forked from syl3r86/BetterNPCSheet5e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
betternpcsheet.js
558 lines (489 loc) · 20.9 KB
/
betternpcsheet.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/**
* @author Felix Müller aka syl3r86
* @version 0.7.0
*/
import { ActorSheet5eNPC } from "../../systems/dnd5e/module/actor/sheets/npc.js";
//let Actor5eSheet = CONFIG.Actor.sheetClass;
export class BetterNPCActor5eSheet extends ActorSheet5eNPC {
// ActorSheet5eNPC
get template() {
// adding the #equals and #unequals handlebars helper
Handlebars.registerHelper('equals', function (arg1, arg2, options) {
return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
});
Handlebars.registerHelper('unequals', function (arg1, arg2, options) {
return (arg1 != arg2) ? options.fn(this) : options.inverse(this);
});
const path = "systems/dnd5e/templates/actors/";
if (!game.user.isGM && this.actor.limited) return path + "limited-sheet.html";
return "modules/betternpcsheet5e/template/npc-sheet.html";
}
static get defaultOptions() {
const options = super.defaultOptions;
mergeObject(options, {
classes: ["sheet","better-npc-sheet-container"],
width: 600,
height: 300,
blockFavTab: true
});
return options;
}
getData() {
const data = super.getData();
// setting to display either the Icon of the item (true) or a generic d20 icon (false)
this.useFeatIcons = false;
this.useWeaponIcons = false;
this.useSpellIcons = false;
data['useFeatIcons'] = this.useFeatIcons;
data['useWeaponIcons'] = this.useWeaponIcons;
data['useSpellIcons'] = this.useSpellIcons;
return data;
}
render(...args) {
if (this.supressRender) {
return;
} else {
return super.render(...args);
}
}
activateListeners(html) {
super.activateListeners(html);
// only do stuff if its for npcs
if (this.actor.data.type === "character") {
return;
}
// rebind roll function
html.find('.item .rollable').click(event => this._onItemRoll(event));
// register settings
game.settings.register("BetterNPCSheet", this.object.data._id, {
name: "Settings specific to NPC display",
hint: "Settings to exclude packs from loading",
default: "",
type: String,
scope: 'user',
onChange: settings => {
this.settings = JSON.parse(settings);
}
});
// load settings from container
let settings = game.settings.get("BetterNPCSheet", this.object.data._id);
if (settings == '') { // if settings are empty create the settings data
console.log("NPC Settings | Creating settings");
settings = {};
for (let item of this.object.data.items) {
if (item.type == 'spell') {
let target = `.item[data-item-id=${item.id}] .item-description`;
settings[target] = false;
}
}
settings.editMode = false;
game.settings.set('BetterNPCSheet', this.object.data._id, JSON.stringify(settings));
} else {
settings = JSON.parse(settings);
}
// apply them
console.log("NPC Settings | Loading settings");
for (let key in settings) {
let target = html.find(key);
if (settings[key] == true) {
target.show();
} else {
target.hide();
}
}
this.settings = settings;
// hide elements that are part of the edit mode or empty
this._applySettingsMode(this.settings.editMode, html);
// toggle edit mode button event
html.find('.editBtn').click(e => {
this.settings.editMode = !this.settings.editMode;
game.settings.set('BetterNPCSheet', this.object.data._id, JSON.stringify(this.settings));
this._applySettingsMode(this.settings.editMode, html);
});
// set dynamic input width
let inputs = html.find('.npc-textinput,.npc-textinput-small');
inputs.keyup(e => {
let input = e.target;
let inputText = e.target.value || e.target.placeholder;
let prop = ["font-style", "font-variant", "font-weight", "font-size", "font-family"];
let font = "";
for (let x in prop)
font += window.getComputedStyle(input, null).getPropertyValue(prop[x]) + " ";
let element = document.createElement("canvas").getContext("2d");
element.font = font;
let txtWidth = element.measureText(inputText).width * 1.1 + 1;
e.target.style.width = txtWidth + "px";
});
inputs.change(e => {
inputs.trigger('keyup');
});
inputs.trigger('keyup');
// adding toggle for item detail
html.find('.npc-item-name').click(event => this._onItemSummary(event));
this.saveState = false;
for (let element of html.find('.npc-item-name')) {
let item = this.actor.getOwnedItem($(element).parents('.item').data("item-id"));
if (hasProperty(item, 'data.flags.betternpcsheet5e.showItemSummary') && item.data.flags.betternpcsheet5e.showItemSummary) {
$(element).trigger('click');
}
}
this.saveState = true;
html.find('.body-tile-name').click(e => {
let target = e.target.getAttribute('data-tile');
let collapsTarget = e.target.getAttribute('data-target') || 'item';
let targetTile = `.body-tile[data-tile=${target}] .${collapsTarget}`;
if (this.settings[targetTile] === undefined || this.settings[targetTile] === true) {
html.find(targetTile).hide(100);
this.settings[targetTile] = false;
} else {
html.find(targetTile).show(100);
this.settings[targetTile] = true;
}
game.settings.set('BetterNPCSheet', this.object.data._id, JSON.stringify(this.settings));
});
// remove window padding
$('.better-npc-sheet').parent().css('padding', '0');
// setting npcsheet width & height
setTimeout(() => {
let style = html.parent().parent().attr('style');
// change width
let columnCount = 2;
if (this.object.data.items.length > 10) {
columnCount = 3;
}
let newWdith = columnCount * 300;
style = style.replace('width: 600px;', `width: ${newWdith}px;`);
this.position.width = newWdith;
//change height
let newHeight = $(html).outerHeight(true) + $(html.parent().parent().find('.window-header')).outerHeight(true);
style = style.replace('height: 300px;', `height: ${newHeight}px;`);
this.position.height = newHeight;
$(html.parent().parent()).attr('style', style);
}, 10);
// spellslot control buttons
html.find('.spellslot-mod').click(ev => {
let mod = event.target.getAttribute("data-mod");
let level = event.target.getAttribute("data-level");
let slotElement = $(html.find(`input[name="data.spells.spell${level}.value"]`));
let newValue = mod == '+' ? Number(slotElement.val()) + 1 : Number(slotElement.val()) - 1;
slotElement.val(newValue >= 0 ? newValue : 0);
slotElement.trigger('submit');
});
// list changing logic:
html.find('.item-change-list').click(ev => {
let target = $(ev.target).parents('.item').find('.type-list');
target.toggle(200);
});
html.find('.npc-item-name').contextmenu(ev => {
let target = $(ev.target).parents('.item').find('.type-list');
target.toggle(200);
});
html.find('.type-list a').click(ev => {
let targetList = ev.target.dataset.value
let itemId = $(ev.target).parents('.item').attr('data-item-id');
let item = this.actor.getOwnedItem(itemId);
item.update({ "flags.adnd5e.itemInfo.type": targetList });
});
// Rollable Health Formula
html.find(".npc-roll-hp").click(this._onRollHealthFormula.bind(this));
}
async _onItemSummary(event) {
super._onItemSummary(event);
if (this.saveState !== false) {
let li = $(event.currentTarget).parents(".item");
let item = this.actor.getOwnedItem(li.data("item-id"));
let showItemSummary = true;
if (hasProperty(item, 'data.flags.betternpcsheet5e.showItemSummary')) {
showItemSummary = !item.data.flags.betternpcsheet5e.showItemSummary;
}
this.supressRender = true;
await item.setFlag('betternpcsheet5e', 'showItemSummary', showItemSummary);
this.supressRender = false;
}
}
_applySettingsMode(editMode, html) {
let hidable = html.find('.hidable');
for (let obj of hidable) {
let data = obj.getAttribute('data-hidable-attr');
if (data == '' || data == 0) {
if (editMode == false) {
obj.style.display = 'none';
} else {
obj.style.display = '';
}
}
//let hidableAttr = obj.getElementsByClassName('.hidable-attr');
}
if (editMode) {
html.find('.show-on-edit').show();
html.find('.hide-on-edit').hide();
html.find('input').addClass('white-input');//.css('background', 'white');
html.find('.saves-div').show();
html.find('.skills-div').show();
} else {
html.find('.show-on-edit:not(.hidable)').hide();
html.find('.hide-on-edit').show();
html.find('input').removeClass('white-input');//.css('background', 'none');
if (html.find('.saves-div .hidable[data-hidable-attr="1"]').length == 0) {
html.find('.saves-div').hide();
}
if (html.find('.skills-div .hidable[data-hidable-attr="1"], .skills-div .hidable[data-hidable-attr="0.5"], .skills-div .hidable[data-hidable-attr="2"]').length == 0) {
html.find('.skills-div').hide();
}
}
}
/**
* Organize and classify Items for NPC sheets
* @private
*/
_prepareItems(actorData) {
// Features
const features = [];
// Weapons
const weapons = [];
const legendarys = [];
const reactions = [];
const lair = [];
const loot = [];
// Spellbook
const spellbook = {};
// Iterate through items, allocating to containers
for (let i of actorData.items) {
i.img = i.img || DEFAULT_TOKEN;
// Spells
if (i.type === "spell") {
if (!hasProperty(i, 'flags.betternpcsheet5e')) {
i.flags.betternpcsheet5e = {};
}
if (!hasProperty(i, 'flags.betternpcsheet5e.showItemSummary')) {
i.flags.betternpcsheet5e.showItemSummary = game.settings.get("betternpcsheet5e", "expandSpells");
}
let lvl = i.data.level || 0;
let section = lvl;
let sectionLabel = CONFIG.DND5E.spellLevels[lvl];
let isCantrip = lvl === 0 ? true : false;
switch (i.data.preparation.mode) {
case 'always':
section = 'always';
sectionLabel = 'At Will';
isCantrip = true; break;
case 'innate':
section = 'innate';
sectionLabel = 'Innate Spellcasting';
isCantrip = true; break;
case 'pact':
section = 'pact';
sectionLabel = 'Pact';
isCantrip = true; break;
}
let uses = {
value: 0,
max:0
}
if (!isCantrip) {
uses.value = actorData.data.spells["spell" + lvl].value;
uses.max = actorData.data.spells["spell" + lvl].max;
}
spellbook[section] = spellbook[section] || {
isCantrip: isCantrip,
label: sectionLabel,
spells: [],
uses: uses.value,
slots: uses.max
};
//i.data.school.str = CONFIG.DND5E.spellSchools[i.data.school.value];
spellbook[section].spells.push(i);
continue;
}
// Features
let flag = getProperty(i, 'flags.adnd5e.itemInfo.type');
switch (flag) {
case 'trait': features.push(i); break;
case 'action': weapons.push(i); break;
case 'legendary': legendarys.push(i); break;
case 'reaction': reactions.push(i); break;
case 'lair': lair.push(i); break;
case 'loot': loot.push(i); break;
default: {
let type = getProperty(i, 'data.activation.type');
switch (type) {
case "legendary": legendarys.push(i); continue;
case "lair": lair.push(i); continue;
case "action": weapons.push(i); continue;
default: {
if (i.type === "weapon") weapons.push(i);
else if (i.type === "feat") features.push(i);
else if (i.type === "loot") loot.push(i);
else if (["equipment", "consumable", "tool", "backpack"].includes(i.type)) features.push(i);
}
}
}
}
}
let expandItem = game.settings.get("betternpcsheet5e", "expandFeats");
for (let i of features) {
if (!hasProperty(i, 'flags.betternpcsheet5e')) {
i.flags.betternpcsheet5e = {};
}
if (!hasProperty(i, 'flags.betternpcsheet5e.showItemSummary')) {
i.flags.betternpcsheet5e.showItemSummary = expandItem;
}
}
expandItem = game.settings.get("betternpcsheet5e", "expandAttacks");
for (let i of weapons) {
if (!hasProperty(i, 'flags.betternpcsheet5e')) {
i.flags.betternpcsheet5e = {};
}
if (!hasProperty(i, 'flags.betternpcsheet5e.showItemSummary')) {
i.flags.betternpcsheet5e.showItemSummary = expandItem;
}
}
expandItem = game.settings.get("betternpcsheet5e", "expandReactions");
for (let i of reactions) {
if (!hasProperty(i, 'flags.betternpcsheet5e')) {
i.flags.betternpcsheet5e = {};
}
if (!hasProperty(i, 'flags.betternpcsheet5e.showItemSummary')) {
i.flags.betternpcsheet5e.showItemSummary = expandItem;
}
}
expandItem = game.settings.get("betternpcsheet5e", "expandLegendary");
for (let i of legendarys) {
if (!hasProperty(i, 'flags.betternpcsheet5e')) {
i.flags.betternpcsheet5e = {};
}
if (!hasProperty(i, 'flags.betternpcsheet5e.showItemSummary')) {
i.flags.betternpcsheet5e.showItemSummary = expandItem;
}
}
expandItem = game.settings.get("betternpcsheet5e", "expandLair");
for (let i of lair) {
if (!hasProperty(i, 'flags.betternpcsheet5e')) {
i.flags.betternpcsheet5e = {};
}
if (!hasProperty(i, 'flags.betternpcsheet5e.showItemSummary')) {
i.flags.betternpcsheet5e.showItemSummary = expandItem;
}
}
expandItem = game.settings.get("betternpcsheet5e", "expandLoot");
for (let i of loot) {
if (!hasProperty(i, 'flags.betternpcsheet5e')) {
i.flags.betternpcsheet5e = {};
}
if (!hasProperty(i, 'flags.betternpcsheet5e.showItemSummary')) {
i.flags.betternpcsheet5e.showItemSummary = expandItem;
}
}
// Assign the items
actorData.actor.features = features;
actorData.actor.spellbook = spellbook;
actorData.actor.weapons = weapons;
actorData.actor.legendarys = legendarys;
actorData.actor.reactions = reactions;
actorData.actor.lair = lair;
actorData.actor.loot = loot;
}
_onItemCreate(event) {
event.preventDefault();
let itemType = $(event.target).parents('.body-tile').attr('data-tile');
let header = event.currentTarget;
let data = duplicate(header.dataset);
data.flags = {
'adnd5e': {
'itemInfo': {
'type': itemType
}
}
}
data["name"] = `New ${itemType.capitalize()}`;
if (itemType === 'legendary' || itemType === 'lair') {
data["name"] += ' Action';
}
this.actor.createOwnedItem(data, { renderSheet: true });
}
toggleEditMoed() {
if (this.editMode == true) {
for (let obj of html.find('.hidable')) {
if (obj.querySelector('.hidable-attr') != undefined &&
(obj.querySelector('.hidable-attr').value == '' || obj.querySelector('.hidable-attr').value == 0)) {
obj.style.display = "none";
}
}
html.find('.show-on-edit').hide(100);
html.find('.hide-on-edit').show(100);
this.editMode = false;
} else {
for (let obj of html.find('.hidable')) {
if (obj.querySelector('.hidable-attr') != undefined &&
(obj.querySelector('.hidable-attr').value == '' || obj.querySelector('.hidable-attr').value == 0)) {
obj.style.display = "inline-block";
}
}
html.find('.show-on-edit').show(100);
html.find('.hide-on-edit').hide(100);
this.editMode = true;
}
}
}
Actors.registerSheet("dnd5e", BetterNPCActor5eSheet, {
types: ["npc"],
makeDefault: true
});
Hooks.on('ready',()=> {
game.settings.register("betternpcsheet5e", "expandFeats", {
name: game.i18n.localize("BNPCSheet.featSetting"),
hint: game.i18n.localize("BNPCSheet.featSettingHelp"),
scope: "world",
config: true,
default: false,
type: Boolean
});
game.settings.register("betternpcsheet5e", "expandAttacks", {
name: game.i18n.localize("BNPCSheet.attackSetting"),
hint: game.i18n.localize("BNPCSheet.attackSettingHelp"),
scope: "world",
config: true,
default: false,
type: Boolean
});
game.settings.register("betternpcsheet5e", "expandReactions", {
name: game.i18n.localize("BNPCSheet.reactionSetting"),
hint: game.i18n.localize("BNPCSheet.reactionSettingHelp"),
scope: "world",
config: true,
default: false,
type: Boolean
});
game.settings.register("betternpcsheet5e", "expandLegendary", {
name: game.i18n.localize("BNPCSheet.legendarySetting"),
hint: game.i18n.localize("BNPCSheet.legendarySettingHelp"),
scope: "world",
config: true,
default: false,
type: Boolean
});
game.settings.register("betternpcsheet5e", "expandLair", {
name: game.i18n.localize("BNPCSheet.lairSetting"),
hint: game.i18n.localize("BNPCSheet.lairSettingHelp"),
scope: "world",
config: true,
default: false,
type: Boolean
});
game.settings.register("betternpcsheet5e", "expandSpells", {
name: game.i18n.localize("BNPCSheet.spellsSetting"),
hint: game.i18n.localize("BNPCSheet.spellsSettingHelp"),
scope: "world",
config: true,
default: false,
type: Boolean
});
game.settings.register("betternpcsheet5e", "expandLoot", {
name: game.i18n.localize("BNPCSheet.lootSetting"),
hint: game.i18n.localize("BNPCSheet.lootSettingHelp"),
scope: "world",
config: true,
default: false,
type: Boolean
});
});