mirror of
https://github.com/Foundryborne/daggerheart.git
synced 2026-01-12 03:31:07 +01:00
Finalized functionality
This commit is contained in:
parent
f47a869af3
commit
27d49d35fa
11 changed files with 296 additions and 128 deletions
|
|
@ -15,7 +15,7 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
|
|||
|
||||
static DEFAULT_OPTIONS = {
|
||||
tag: 'form',
|
||||
classes: ['daggerheart', 'dh-style', 'dialog', 'max-800'],
|
||||
classes: ['daggerheart', 'dh-style', 'dialog', 'action-config', 'max-800'],
|
||||
window: {
|
||||
icon: 'fa-solid fa-wrench',
|
||||
resizable: false
|
||||
|
|
@ -37,7 +37,7 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
|
|||
submitOnChange: true,
|
||||
closeOnSubmit: false
|
||||
},
|
||||
dragDrop: [{ dragSelector: null, dropSelector: '.summon-entry', handlers: ['_onDrop'] }]
|
||||
dragDrop: [{ dragSelector: null, dropSelector: '#summon-drop-zone', handlers: ['_onDrop'] }]
|
||||
};
|
||||
|
||||
static PARTS = {
|
||||
|
|
@ -87,7 +87,7 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
|
|||
}
|
||||
};
|
||||
|
||||
static CLEAN_ARRAYS = ['damage.parts', 'cost', 'effects','summon'];
|
||||
static CLEAN_ARRAYS = ['damage.parts', 'cost', 'effects', 'summon'];
|
||||
|
||||
_getTabs(tabs) {
|
||||
for (const v of Object.values(tabs)) {
|
||||
|
|
@ -98,23 +98,24 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
|
|||
return tabs;
|
||||
}
|
||||
|
||||
_attachPartListeners(partId, htmlElement, options) {
|
||||
super._attachPartListeners(partId, htmlElement, options);
|
||||
|
||||
htmlElement.querySelectorAll('.summon-count-wrapper input').forEach(element => {
|
||||
element.addEventListener('change', this.updateSummonCount.bind(this));
|
||||
});
|
||||
}
|
||||
|
||||
async _prepareContext(_options) {
|
||||
const context = await super._prepareContext(_options, 'action');
|
||||
context.source = this.action.toObject(true);
|
||||
// Resolving summon entries so actions can read entry.name / entry.img / entry.uuid
|
||||
if (Array.isArray(context.source.summon)) {
|
||||
context.source.summon = await Promise.all(context.source.summon.map(async entry => {
|
||||
if (!entry) return entry;
|
||||
const uuid = entry.actorUUID ?? entry.uuid;
|
||||
entry.uuid = uuid;
|
||||
try {
|
||||
const doc = await foundry.utils.fromUuid(uuid);
|
||||
entry.name = entry.name ?? doc?.name;
|
||||
entry.img = entry.img ?? (doc?.img ?? doc?.prototypeToken?.texture?.src ?? null);
|
||||
} catch (_) {}
|
||||
return entry;
|
||||
}));
|
||||
context.summons = [];
|
||||
for (const summon of context.source.summon) {
|
||||
const actor = await foundry.utils.fromUuid(summon.actorUUID);
|
||||
context.summons.push({ actor, count: summon.count });
|
||||
}
|
||||
|
||||
context.openSection = this.openSection;
|
||||
context.tabs = this._getTabs(this.constructor.TABS);
|
||||
context.config = CONFIG.DH;
|
||||
|
|
@ -197,8 +198,9 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
|
|||
}
|
||||
|
||||
static async updateForm(event, _, formData) {
|
||||
const submitData = this._prepareSubmitData(event, formData),
|
||||
data = foundry.utils.mergeObject(this.action.toObject(), submitData);
|
||||
const submitData = this._prepareSubmitData(event, formData);
|
||||
|
||||
const data = foundry.utils.mergeObject(this.action.toObject(), submitData);
|
||||
this.action = await this.action.update(data);
|
||||
|
||||
this.sheetUpdate?.(this.action);
|
||||
|
|
@ -234,13 +236,13 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
|
|||
static async editDoc(event, button) {
|
||||
event.stopPropagation();
|
||||
const uuid = button?.dataset.itemUuid ?? button?.dataset.uuid; // Obtain uuid from dataset
|
||||
if (!uuid) return;
|
||||
if (!uuid) return;
|
||||
//Try catching errors
|
||||
try {
|
||||
const doc = await foundry.utils.fromUuid(uuid);
|
||||
if (doc?.sheet) return doc.sheet.render({ force: true });
|
||||
} catch (err) {
|
||||
console.warn("editDoc action failed for", uuid, err);
|
||||
console.warn('editDoc action failed for', uuid, err);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -261,6 +263,14 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
|
|||
this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) });
|
||||
}
|
||||
|
||||
updateSummonCount(event) {
|
||||
const wrapper = event.target.closest('.summon-count-wrapper');
|
||||
const index = wrapper.dataset.index;
|
||||
const data = this.action.toObject();
|
||||
data.summon[index].count = Number.parseInt(event.target.value);
|
||||
this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) });
|
||||
}
|
||||
|
||||
/** Specific implementation in extending classes **/
|
||||
static async addEffect(_event) {}
|
||||
static removeEffect(_event, _button) {}
|
||||
|
|
@ -271,28 +281,28 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
|
|||
await super.close(options);
|
||||
}
|
||||
|
||||
/** Implementation for dragdrop for summon actor selection **/
|
||||
async _onDrop(event) {
|
||||
const data = foundry.applications.ux.TextEditor.getDragEventData(event);
|
||||
const item=await foundry.utils.fromUuid(data.uuid);
|
||||
const item = await foundry.utils.fromUuid(data.uuid);
|
||||
if (!(item instanceof game.system.api.documents.DhpActor)) {
|
||||
ui.notifications.warn(game.i18n.localize("DAGGERHEART.ACTIONS.TYPES.summon.invalidDrop"));
|
||||
ui.notifications.warn(game.i18n.localize('DAGGERHEART.ACTIONS.TYPES.summon.invalidDrop'));
|
||||
return;
|
||||
}
|
||||
//Add to summon array
|
||||
const actionData = this.action.toObject(); // Get current action data
|
||||
//checking to see if actor is already in summon list add 1 to count instead of adding new entry
|
||||
|
||||
const actionData = this.action.toObject();
|
||||
let countvalue = 1;
|
||||
for (const entry of actionData.summon) {
|
||||
if (entry.actorUUID === data.uuid) {
|
||||
entry.count += 1;
|
||||
countvalue = entry.count;
|
||||
await this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(actionData) });
|
||||
await this.constructor.updateForm.bind(this)(null, null, {
|
||||
object: foundry.utils.flattenObject(actionData)
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
actionData.summon.push({ actorUUID: data.uuid, count: countvalue });// Add new summon entry
|
||||
await this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(actionData) }); // Update the form with new data
|
||||
|
||||
actionData.summon.push({ actorUUID: data.uuid, count: countvalue });
|
||||
await this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(actionData) });
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
export * as placeables from './placeables/_module.mjs';
|
||||
export { default as DhTokenLayer } from './tokens.mjs';
|
||||
|
|
|
|||
16
module/canvas/tokens.mjs
Normal file
16
module/canvas/tokens.mjs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
export default class DhTokenLayer extends foundry.canvas.layers.TokenLayer {
|
||||
async _createPreview(createData, options) {
|
||||
if (options.actor) {
|
||||
const tokenSizes = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Homebrew).tokenSizes;
|
||||
if (options.actor?.system.metadata.usesSize) {
|
||||
const tokenSize = tokenSizes[options.actor.system.size];
|
||||
if (tokenSize && options.actor.system.size !== CONFIG.DH.ACTOR.tokenSize.custom.id) {
|
||||
createData.width = tokenSize;
|
||||
createData.height = tokenSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return super._createPreview(createData, options);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
const fields = foundry.data.fields;
|
||||
import DHSummonDialog from '../../../applications/dialogs/summonDialog.mjs';
|
||||
|
||||
export default class DHSummonField extends fields.ArrayField {
|
||||
/**
|
||||
|
|
@ -24,71 +23,47 @@ export default class DHSummonField extends fields.ArrayField {
|
|||
}
|
||||
|
||||
static async execute() {
|
||||
if(!canvas.scene){
|
||||
ui.notifications.warn(game.i18n.localize("DAGGERHEART.ACTIONS.TYPES.summon.error"));
|
||||
if (!canvas.scene) {
|
||||
ui.notifications.warn(game.i18n.localize('DAGGERHEART.ACTIONS.TYPES.summon.error'));
|
||||
return;
|
||||
}
|
||||
const validSummons = this.summon.filter(entry => entry.actorUUID);
|
||||
if (validSummons.length === 0) {
|
||||
console.log("No actors configured for this Summon action.");
|
||||
|
||||
if (this.summon.length === 0) {
|
||||
ui.notifications.warn('No actors configured for this Summon action.');
|
||||
return;
|
||||
}
|
||||
|
||||
for (const entry of validSummons) {
|
||||
const actor = await fromUuid(entry.actorUUID);
|
||||
}
|
||||
|
||||
// //Open Summon Dialog
|
||||
// const summonData = { summons: validSummons };
|
||||
// console.log(summonData);
|
||||
// const dialog = new DHSummonDialog(summonData);
|
||||
// dialog.render(true);
|
||||
const summonData = [];
|
||||
for (const summon of this.summon) {
|
||||
/* Possibly check for any available instances in the world with prepared images */
|
||||
let actor = await foundry.utils.fromUuid(summon.actorUUID);
|
||||
|
||||
// Create folder and add tokens to actor folder
|
||||
const rootFolderName = game.i18n.localize("DAGGERHEART.APPLICATIONS.Summon.title");
|
||||
let rootFolder = game.folders.find(f => f.name === rootFolderName && f.type === 'Actor');
|
||||
if (!rootFolder) {
|
||||
rootFolder = await Folder.create({
|
||||
name: rootFolderName,
|
||||
type: 'Actor',
|
||||
});
|
||||
}
|
||||
const parentName = this.item.name ?? "Unkown Feature";
|
||||
const actionName = this.name ?? "Unkown Action";
|
||||
const subFolderName = `${parentName} - ${actionName}`;
|
||||
|
||||
let subFolder = game.folders.find(f => f.name === subFolderName && f.type === 'Actor' && f.folder?.id === rootFolder.id);
|
||||
if (!subFolder) {
|
||||
subFolder = await Folder.create({
|
||||
name: subFolderName,
|
||||
type: 'Actor',
|
||||
folder: rootFolder.id
|
||||
});
|
||||
const actorsToSummon = [];
|
||||
for (const entry of validSummons) {
|
||||
const sourceActor = await fromUuid(entry.actorUUID);
|
||||
if (!sourceActor) {
|
||||
console.warn('DHSummonField: Could not find actor for UUID', entry.actorUUID);
|
||||
continue;
|
||||
}
|
||||
const actorData = sourceActor.toObject();
|
||||
delete actorData._id; // Remove _id to create a new Actor
|
||||
actorData.folder = subFolder.id;
|
||||
|
||||
for (let i = 0; i < entry.count; i++) {
|
||||
const newActor = foundry.utils.deepClone(actorData);
|
||||
if (entry.count > 1) {
|
||||
newActor.name = `${actorData.name} ${i + 1}`;
|
||||
}
|
||||
actorsToSummon.push(newActor);
|
||||
}
|
||||
/* Check for any available instances of the actor present in the world if we're missing artwork in the compendium */
|
||||
const dataType = game.system.api.data.actors[`Dh${actor.type.capitalize()}`];
|
||||
if (actor.inCompendium && dataType && actor.img === dataType.DEFAULT_ICON) {
|
||||
const worldActorCopy = game.actors.find(x => x.name === actor.name);
|
||||
actor = worldActorCopy ?? actor;
|
||||
}
|
||||
if (actorsToSummon.length > 0) {
|
||||
await Actor.createDocuments(actorsToSummon);
|
||||
ui.notifications.info(`Summoned ${actorsToSummon.length} actors successfully in folder ${subFolder.name}.`);
|
||||
}
|
||||
} else {
|
||||
ui.notifications.info(`Summon actors already exist in folder ${subFolder.name}.`);
|
||||
|
||||
summonData.push({ actor, count: summon.count });
|
||||
}
|
||||
|
||||
const handleSummon = async summonIndex => {
|
||||
const summon = summonData[summonIndex];
|
||||
const result = await CONFIG.ux.TokenManager.createPreviewAsync(summon.actor, {
|
||||
name: `${summon.actor.prototypeToken.name}${summon.count > 1 ? ` (${summon.count}x)` : ''}`
|
||||
});
|
||||
if (!result) return;
|
||||
|
||||
summon.count--;
|
||||
if (summon.count === 0) {
|
||||
summonIndex++;
|
||||
if (summonIndex === summonData.length) return;
|
||||
}
|
||||
|
||||
handleSummon(summonIndex);
|
||||
};
|
||||
|
||||
handleSummon(0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,3 +8,4 @@ export { default as DhScene } from './scene.mjs';
|
|||
export { default as DhToken } from './token.mjs';
|
||||
export { default as DhTooltipManager } from './tooltipManager.mjs';
|
||||
export { default as DhTemplateManager } from './templateManager.mjs';
|
||||
export { default as DhTokenManager } from './tokenManager.mjs';
|
||||
|
|
|
|||
103
module/documents/tokenManager.mjs
Normal file
103
module/documents/tokenManager.mjs
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
/**
|
||||
* A singleton class that handles preview tokens.
|
||||
*/
|
||||
|
||||
export default class DhTokenManager {
|
||||
#activePreview;
|
||||
#actor;
|
||||
#resolve;
|
||||
|
||||
/**
|
||||
* Create a template preview, deactivating any existing ones.
|
||||
* @param {object} data
|
||||
*/
|
||||
async createPreview(actor, tokenData) {
|
||||
this.#actor = actor;
|
||||
const token = await canvas.tokens._createPreview(
|
||||
{
|
||||
...actor.prototypeToken,
|
||||
displayName: 50,
|
||||
...tokenData
|
||||
},
|
||||
{ renderSheet: false, actor }
|
||||
);
|
||||
|
||||
this.#activePreview = {
|
||||
document: token.document,
|
||||
object: token,
|
||||
origin: { x: token.document.x, y: token.document.y }
|
||||
};
|
||||
|
||||
this.#activePreview.events = {
|
||||
contextmenu: this.#cancelTemplate.bind(this),
|
||||
mousedown: this.#confirmTemplate.bind(this),
|
||||
mousemove: this.#onDragMouseMove.bind(this)
|
||||
};
|
||||
|
||||
canvas.stage.on('mousemove', this.#activePreview.events.mousemove);
|
||||
canvas.stage.on('mousedown', this.#activePreview.events.mousedown);
|
||||
canvas.app.view.addEventListener('contextmenu', this.#activePreview.events.contextmenu);
|
||||
}
|
||||
|
||||
async createPreviewAsync(actor, tokenData) {
|
||||
return new Promise(resolve => {
|
||||
this.#resolve = resolve;
|
||||
this.createPreview(actor, tokenData);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the movement of the token preview on mousedrag.
|
||||
* @param {mousemove Event} event
|
||||
*/
|
||||
#onDragMouseMove(event) {
|
||||
event.stopPropagation();
|
||||
const { moveTime, object } = this.#activePreview;
|
||||
const update = {};
|
||||
|
||||
const now = Date.now();
|
||||
if (now - (moveTime || 0) <= 16) return;
|
||||
this.#activePreview.moveTime = now;
|
||||
|
||||
let cursor = event.getLocalPosition(canvas.templates);
|
||||
|
||||
Object.assign(update, canvas.grid.getTopLeftPoint(cursor));
|
||||
|
||||
object.document.updateSource(update);
|
||||
object.renderFlags.set({ refresh: true });
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the preview token on right-click.
|
||||
* @param {contextmenu Event} event
|
||||
*/
|
||||
#cancelTemplate(_event, resolved) {
|
||||
const { mousemove, mousedown, contextmenu } = this.#activePreview.events;
|
||||
this.#activePreview.object.destroy();
|
||||
|
||||
canvas.stage.off('mousemove', mousemove);
|
||||
canvas.stage.off('mousedown', mousedown);
|
||||
canvas.app.view.removeEventListener('contextmenu', contextmenu);
|
||||
if (this.#resolve && !resolved) this.#resolve(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a real Actor and token at the preview location and cancels the preview.
|
||||
* @param {click Event} event
|
||||
*/
|
||||
async #confirmTemplate(event) {
|
||||
event.stopPropagation();
|
||||
this.#cancelTemplate(event, true);
|
||||
|
||||
const actor = this.#actor.inCompendium
|
||||
? await game.system.api.documents.DhpActor.create(this.#actor.toObject())
|
||||
: this.#actor;
|
||||
const tokenData = await actor.getTokenDocument();
|
||||
const token = await canvas.scene.createEmbeddedDocuments('Token', [
|
||||
{ ...tokenData, x: this.#activePreview.document.x, y: this.#activePreview.document.y }
|
||||
]);
|
||||
|
||||
this.#activePreview = undefined;
|
||||
if (this.#resolve) this.#resolve(token);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue