Allow creating custom embedded documents of any type from the sheet, and add functionality to the aspect create button

This commit is contained in:
Oliver-Akins 2024-01-12 23:26:55 -07:00
parent 134b13756a
commit 7d990fe64b
7 changed files with 52 additions and 48 deletions

View file

@ -273,6 +273,9 @@
"defaults": { "defaults": {
"spell": { "spell": {
"name": "(Unnamed Spell)" "name": "(Unnamed Spell)"
},
"aspect": {
"name": "(Unnamed Aspect)"
} }
} }
}, },

View file

@ -34,8 +34,10 @@ export class ActorHandler extends Actor {
this.fn.genericEmbeddedDelete.bind(this)($event); this.fn.genericEmbeddedDelete.bind(this)($event);
}; };
async createCustomSpell($event) { async genericEmbeddedCreate($event) {
if (!this.fn?.createCustomSpell) return; const data = $event.currentTarget.dataset;
this.fn.createCustomSpell.bind(this)($event); console.log(data)
if (!this.fn?.[`createCustom${data.embeddedCreate}`]) return;
this.fn?.[`createCustom${data.embeddedCreate}`].bind(this)($event);
}; };
}; };

View file

@ -37,40 +37,35 @@ export class PlayerActor {
}; };
/** @this {Actor} */ /** @this {Actor} */
static async createCustomItem(defaults) { static async createCustomItem(defaults, opts = {}) {
let items = await this.createEmbeddedDocuments(`Item`, defaults); let items = await this.createEmbeddedDocuments(`Item`, defaults);
if (items.length == 0) { if (items.length == 0) {
throw new Error(); throw new Error();
}; };
this.sheet.render(); this.sheet.render();
if (game.settings.get(`dotdungeon`, `openEmbeddedOnCreate`)) { if (
game.settings.get(`dotdungeon`, `openEmbeddedOnCreate`)
&& !opts.overrideSheetOpen
) {
for (const item of items) { for (const item of items) {
item.sheet.render(true); item.sheet.render(true);
}; };
}; };
}; };
/** @this {Actor} */
static async createCustomAspect() {
await PlayerActor.createCustomItem.bind(this)([{
type: `aspect`,
name: game.i18n.format(`dotdungeon.defaults.aspect.name`),
}]);
};
/** @this {Actor} */ /** @this {Actor} */
static async createCustomSpell() { static async createCustomSpell() {
let items = await this.createEmbeddedDocuments( await PlayerActor.createCustomItem.bind(this)([{
"Item", type: `spell`,
[{ name: game.i18n.format(`dotdungeon.defaults.spell.name`),
type: `spell`, }]);
name: game.i18n.format(`dotdungeon.defaults.spell.name`),
}]
);
if (items.length == 0) {
ui.notifications.error(
`dotdungeon.notifications.error.spell-create-failed`,
{ localize: true, console: false }
);
return;
};
this.sheet.render();
if (game.settings.get(`dotdungeon`, `openEmbeddedOnCreate`)) {
for (const item of items) {
item.sheet.render(true);
};
};
}; };
}; };

View file

@ -43,6 +43,14 @@ export class GenericActorSheet extends ActorSheet {
html.find(`summary`).on(`click`, this._handleSummaryToggle.bind(this)); html.find(`summary`).on(`click`, this._handleSummaryToggle.bind(this));
html.find(`.roll`).on(`click`, this._handleRoll.bind(this)); html.find(`.roll`).on(`click`, this._handleRoll.bind(this));
html.find(`[data-embedded-update]`)
.on(`change`, this.actor.genericEmbeddedUpdate.bind(this.actor));
html.find(`[data-embedded-delete]`)
.on(`click`, this.actor.genericEmbeddedDelete.bind(this.actor));
html.find(`[data-embedded-create]`)
.on(`click`, this.actor.genericEmbeddedCreate.bind(this.actor));
html.find(`[data-embedded-edit]`)
.on(`click`, this.actor.openEmbeddedSheet.bind(this.actor));
}; };
async _handleRoll($e) { async _handleRoll($e) {

View file

@ -18,15 +18,6 @@ export class PlayerSheet extends GenericActorSheet {
if (this.document.isEmbedded) return; if (this.document.isEmbedded) return;
if (!this.isEditable) return; if (!this.isEditable) return;
console.debug(`.dungeon | Adding event listeners for Actor: ${this.id}`); console.debug(`.dungeon | Adding event listeners for Actor: ${this.id}`);
html.find(`.add-spell`).on(`click`, this.actor.createCustomSpell.bind(this.actor));
// TODO: Apparently the `change` event is bad to use in Foundry
// html.find(`[data-embedded-update]`)
// .on(`change`, this.actor.genericEmbeddedUpdate.bind(this.actor));
html.find(`[data-embedded-delete]`)
.on(`click`, this.actor.genericEmbeddedDelete.bind(this.actor));
html.find(`[data-embedded-edit]`)
.on(`click`, this.actor.openEmbeddedSheet.bind(this.actor));
}; };
async getData() { async getData() {

View file

@ -42,11 +42,17 @@
{{localize "dotdungeon.actor.pc.aspect.empty"}} {{localize "dotdungeon.actor.pc.aspect.empty"}}
</p> </p>
{{/each}} {{/each}}
{{#if (or computed.canAddAspect (not (dd-empty items.aspect)))}} {{#if (or computed.canAddAspect (dd-empty items.aspect))}}
<button <button
class="confirm" class="confirm"
data-embedded-create="Aspect"
> >
{{localize "dotdungeon.actor.pc.aspect.add"}} <div aria-hidden="true" class="icon icon--20">
{{{ icons.create }}}
</div>
<span>
{{localize "dotdungeon.actor.pc.aspect.add"}}
</span>
</button> </button>
{{/if}} {{/if}}
{{/ dotdungeon.panel}} {{/ dotdungeon.panel}}

View file

@ -67,16 +67,15 @@
{{/each}} {{/each}}
<div class="flex-row"> <button
<button class="confirm"
class="confirm add-spell" data-embedded-create="Spell"
> >
<div aria-hidden="true" class="icon icon--20"> <div aria-hidden="true" class="icon icon--20">
{{{ icons.create }}} {{{ icons.create }}}
</div> </div>
<span> <span>
{{localize "dotdungeon.actor.pc.spells.add.label"}} {{localize "dotdungeon.actor.pc.spells.add.label"}}
</span> </span>
</button> </button>
</div>
{{/ dotdungeon.panel}} {{/ dotdungeon.panel}}