daggerheart/module/applications/sheets/api/actor-setting.mjs

106 lines
3.9 KiB
JavaScript

import DHApplicationMixin from './application-mixin.mjs';
const { ActorSheetV2 } = foundry.applications.sheets;
/**
* @typedef {import('@client/applications/_types.mjs').ApplicationClickAction} ApplicationClickAction
*/
/**
* Base settings sheet for Daggerheart actors.
* @extends {DHApplicationMixin<ActorSheetV2>}
*/
export default class DHBaseActorSettings extends DHApplicationMixin(ActorSheetV2) {
/**@inheritdoc */
static DEFAULT_OPTIONS = {
classes: ['dialog'],
window: {
icon: 'fa-solid fa-wrench',
resizable: false,
title: 'DAGGERHEART.GENERAL.Tabs.settings'
},
position: { width: 455, height: 'auto' },
actions: {},
form: {
submitOnChange: true
},
dragDrop: [
{ dragSelector: null, dropSelector: '.tab.features' },
{ dragSelector: '.feature-item, .inventory-item[data-type="feature"]', dropSelector: null }
]
};
/** @inheritDoc */
_initializeApplicationOptions(options) {
options = super._initializeApplicationOptions(options);
options.classes = options.classes.filter(c => c !== 'sheet');
return options;
}
/** @returns {foundry.documents.Actor} */
get actor() {
return this.document;
}
/**@inheritdoc */
async _prepareContext(options) {
const context = await super._prepareContext(options);
context.isNPC = this.actor.isNPC;
if (context.systemFields.attack) {
context.systemFields.attack.fields = this.actor.system.attack.schema.fields;
}
// Create fake fields for actor configurable max resource value.
const resourceConfig = CONFIG.DH.RESOURCE[this.actor.type]?.all;
if (resourceConfig) {
const relevant = ['hitPoints', 'stress'].filter(r => r in resourceConfig);
context.resources = relevant.map(key => {
const data = this.actor._source.system.resources[key];
const config = resourceConfig[key];
return {
label: config.label,
name: `system.resources.${key}.max`,
value: data.max ?? config.max,
tooltip: key === 'hitPoints' ? game.i18n.localize('DAGGERHEART.UI.Tooltip.maxHPClassBound') : null,
field: new foundry.data.fields.NumberField({
initial: config.max,
integer: true,
label: game.i18n.format('DAGGERHEART.GENERAL.maxWithThing', {
thing: game.i18n.localize(config.label)
})
})
};
});
}
return context;
}
async _onDragStart(event) {
const featureItemEl = event.currentTarget.closest('.feature-item, .inventory-item[data-type="feature"]');
const feature = this.actor.items.get(featureItemEl?.dataset.itemId);
if (feature && event.target.closest('.tab.features')) {
const featureData = { ...feature.toDragData(), fromInternal: true };
event.dataTransfer.setData('text/plain', JSON.stringify(featureData));
event.dataTransfer.setDragImage(featureItemEl.querySelector('img'), 60, 0);
}
}
async _onDrop(event) {
event.stopPropagation();
const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event);
const item = await fromUuid(data.uuid);
if (item?.type === 'feature') {
if (data.fromInternal && item.parent?.uuid === this.actor.uuid) {
return super._onDrop(event);
}
const itemData = item.toObject();
delete itemData._id;
await this.actor.createEmbeddedDocuments('Item', [itemData]);
}
}
/** Setting sheets do not auto extend */
async _autoExpandDescriptions() {}
}