diff --git a/module/applications/sheets-configs/action-base-config.mjs b/module/applications/sheets-configs/action-base-config.mjs index 5fa37915..49aa36fb 100644 --- a/module/applications/sheets-configs/action-base-config.mjs +++ b/module/applications/sheets-configs/action-base-config.mjs @@ -18,6 +18,75 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2) return `${game.i18n.localize('DAGGERHEART.GENERAL.Tabs.settings')}: ${this.action.name}`; } + /* Needs to consider effect altOutcomes aswell */ + static getOutcomeTabs(action) { + const outcomeKeys = [ + 'default', + ...Object.keys(action.damage?.altOutcomes ?? {}).filter(key => action.damage.altOutcomes[key]) + ]; + return outcomeKeys.reduce((acc, key, index) => { + acc[key] = { + active: index === 0, + cssClass: '', + group: 'outcomes', + id: key, + icon: null, + label: game.i18n.localize(CONFIG.DH.ACTIONS.outcomeTypes[key].label) + }; + return acc; + }, {}); + } + + /* Needs to consider effect altOutcomes aswell */ + static selectOutcome(action, callback) { + const choices = Object.entries(CONFIG.DH.ACTIONS.outcomeTypes).reduce((acc, [key, value]) => { + if (action.damage.altOutcomes[key] === null) acc.push({ id: key, label: game.i18n.localize(value.label) }); + + return acc; + }, []); + const content = new foundry.data.fields.StringField({ + label: game.i18n.localize('Outcome'), + choices, + required: true + }).toFormGroup( + {}, + { + name: 'outcome', + localize: true, + nameAttr: 'value', + labelAttr: 'label' + } + ).outerHTML; + + const callbackWrapper = (_, button) => { + const choiceIndex = button.form.elements.outcome.value; + callback(choices[choiceIndex]?.id); + }; + + const typeDialog = new foundry.applications.api.DialogV2({ + buttons: [ + foundry.utils.mergeObject( + { + action: 'ok', + label: 'Confirm', + icon: 'fas fa-check', + default: true + }, + { callback: callbackWrapper } + ) + ], + content: content, + rejectClose: false, + modal: false, + window: { + title: game.i18n.localize('Add Outcome') + }, + position: { width: 300 } + }); + + typeDialog.render(true); + } + static DEFAULT_OPTIONS = { tag: 'form', classes: ['daggerheart', 'dh-style', 'action-config', 'dialog', 'max-800'], @@ -28,11 +97,16 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2) position: { width: 600, height: 'auto' }, actions: { toggleSection: this.toggleSection, + addEffect: this.addEffect, + removeEffect: this.removeEffect, addElement: this.addElement, removeElement: this.removeElement, removeTransformActor: this.removeTransformActor, - addDamage: this.onAddDamage, - removeDamage: this.onRemoveDamage, + addOutcome: this.addOutcome, + removeOutcome: this.removeOutcome, + editEffect: this.editEffect, + addDamage: this.addDamage, + removeDamage: this.removeDamage, editDoc: this.editDoc, addTrigger: this.addTrigger, removeTrigger: this.removeTrigger, @@ -121,75 +195,6 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2) static CLEAN_ARRAYS = ['cost', 'effects', 'summon']; - /* Needs to consider effect altOutcomes aswell */ - static getOutcomeTabs(action) { - const outcomeKeys = [ - 'default', - ...Object.keys(action.damage?.altOutcomes ?? {}).filter(key => action.damage.altOutcomes[key]) - ]; - return outcomeKeys.reduce((acc, key, index) => { - acc[key] = { - active: index === 0, - cssClass: '', - group: 'outcomes', - id: key, - icon: null, - label: game.i18n.localize(CONFIG.DH.ACTIONS.outcomeTypes[key].label) - }; - return acc; - }, {}); - } - - /* Needs to consider effect altOutcomes aswell */ - static selectOutcome(action, callback) { - const choices = Object.entries(CONFIG.DH.ACTIONS.outcomeTypes).reduce((acc, [key, value]) => { - if (action.damage.altOutcomes[key] === null) acc.push({ id: key, label: game.i18n.localize(value.label) }); - - return acc; - }, []); - const content = new foundry.data.fields.StringField({ - label: game.i18n.localize('Outcome'), - choices, - required: true - }).toFormGroup( - {}, - { - name: 'outcome', - localize: true, - nameAttr: 'value', - labelAttr: 'label' - } - ).outerHTML; - - const callbackWrapper = (_, button) => { - const choiceIndex = button.form.elements.outcome.value; - callback(choices[choiceIndex]?.id); - }; - - const typeDialog = new foundry.applications.api.DialogV2({ - buttons: [ - foundry.utils.mergeObject( - { - action: 'ok', - label: 'Confirm', - icon: 'fas fa-check', - default: true - }, - { callback: callbackWrapper } - ) - ], - content: content, - rejectClose: false, - modal: false, - window: { - title: game.i18n.localize('Add Outcome') - }, - position: { width: 300 } - }); - - typeDialog.render(true); - } - _getTabs(tabs) { for (const v of Object.values(tabs)) { v.active = this.tabGroups[v.group] ? this.tabGroups[v.group] === v.id : v.active; @@ -384,12 +389,14 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2) this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) }); } - static onAddDamage(_event, button) { + static addDamage(_event, button) { if (!this.action.damage.parts) return; const outcome = button.dataset.outcome; - const source = this.action._source; - const outcomeParts = outcome === 'default' ? source.damage.parts : source.damage.altOutcomes[outcome].parts; + const outcomeParts = + outcome === 'default' + ? this.action._source.damage.parts + : this.action._source.damage.altOutcomes[outcome].parts; const choices = getUnusedDamageTypes(outcomeParts); const content = new foundry.data.fields.StringField({ label: game.i18n.localize('Damage Type'), @@ -410,23 +417,32 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2) const type = choices[button.form.elements.type.value].value; const part = this.action.schema.fields.damage.fields.parts.element.getInitialValue(); part.applyTo = type; - if (type === CONFIG.DH.GENERAL.healingTypes.hitPoints.id) { + if (type === CONFIG.DH.GENERAL.healingTypes.hitPoints.id) part.type = this.action.schema.fields.damage.fields.parts.element.fields.type.element.initial; + + if (outcome === 'default') data.damage.parts[type] = part; + else { + if (!data.damage.altOutcomes[outcome]) { + data.damage.altOutcomes[outcome] = new AltDamageOutcome(); + } + + data.damage.altOutcomes[outcome].parts[type] = part; } - if (outcome !== 'default') data.damage.altOutcomes[outcome] ??= new AltDamageOutcome(); - (outcome === 'default' ? data.damage.parts : data.damage.altOutcomes[outcome])[type] = part; + this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) }); }; const typeDialog = new foundry.applications.api.DialogV2({ buttons: [ - { - action: 'ok', - label: 'Confirm', - icon: 'fas fa-check', - default: true, - callback - } + foundry.utils.mergeObject( + { + action: 'ok', + label: 'Confirm', + icon: 'fas fa-check', + default: true + }, + { callback: callback } + ) ], content: content, rejectClose: false, @@ -440,12 +456,18 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2) typeDialog.render(true); } - static onRemoveDamage(_event, button) { + static removeDamage(_event, button) { if (!this.action.damage.parts) return; const data = this.action.toObject(); const { key, outcome } = button.dataset; - const parts = outcome === 'default' ? data.damage.parts : data.damage.altOutcomes[outcome].parts; - parts[key] = _del; + if (outcome === 'default') { + delete data.damage.parts[key]; + data.damage.parts[`${key}`] = _del; + } else { + delete data.damage.altOutcomes[outcome].parts[key]; + data.damage.altOutcomes[outcome].parts[`${key}`] = _del; + } + this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) }); } @@ -535,6 +557,13 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2) this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) }); } + /** Specific implementation in extending classes **/ + static addOutcome(_event) {} + static removeOutcome(_event) {} + static async addEffect(_event) {} + static removeEffect(_event, _button) {} + static editEffect(_event) {} + async close(options) { this.tabGroups.primary = 'base'; await super.close(options); diff --git a/module/applications/sheets-configs/action-config.mjs b/module/applications/sheets-configs/action-config.mjs index a4b0a6c4..833c3ebd 100644 --- a/module/applications/sheets-configs/action-config.mjs +++ b/module/applications/sheets-configs/action-config.mjs @@ -6,8 +6,8 @@ export default class DHActionConfig extends DHActionBaseConfig { ...DHActionBaseConfig.DEFAULT_OPTIONS, actions: { ...DHActionBaseConfig.DEFAULT_OPTIONS.actions, - addOutcome: this.onAddOutcome, - removeOutcome: this.onRemoveOutcome, + addOutcome: this.addOutcome, + removeOutcome: this.removeOutcome, addEffect: this.addEffect, removeEffect: this.removeEffect, editEffect: this.editEffect @@ -22,7 +22,7 @@ export default class DHActionConfig extends DHActionBaseConfig { return context; } - static onAddOutcome() { + static addOutcome() { const data = this.action.toObject(); DHActionBaseConfig.selectOutcome(this.action, key => { @@ -34,7 +34,7 @@ export default class DHActionConfig extends DHActionBaseConfig { }); } - static onRemoveOutcome(_event, button) { + static removeOutcome(_event, button) { const { outcome } = button.dataset; const data = this.action.toObject();