Updated NpcRollSelectionDialog.mjs to ApplicationV2

This commit is contained in:
WBHarry 2025-05-24 12:00:52 +02:00
parent 5a3bb25e53
commit 707e47bc1b
4 changed files with 48 additions and 104 deletions

View file

@ -711,6 +711,9 @@
"Title": "{actor} - Death Move", "Title": "{actor} - Death Move",
"TakeMove": "Take Death Move" "TakeMove": "Take Death Move"
}, },
"RollSelection": {
"Title": "Roll Options"
},
"Settings": { "Settings": {
"Title": "Daggerheart Settings" "Title": "Daggerheart Settings"
}, },

View file

@ -122,64 +122,3 @@ export default class DamageSelectionDialog extends HandlebarsApplicationMixin(Ap
this.close(); this.close();
} }
} }
// export default class DamageSelectionDialog extends FormApplication {
// constructor(rollString, bonusDamage, resolve){
// super({}, {});
// this.data = {
// rollString,
// bonusDamage: bonusDamage.map(x => ({
// ...x,
// hopeUses: 0
// })),
// }
// this.resolve = resolve;
// }
// get title (){
// return 'Damage Options';
// }
// static get defaultOptions() {
// const defaults = super.defaultOptions;
// const overrides = {
// height: 'auto',
// width: 400,
// id: 'damage-selection',
// template: 'systems/daggerheart/templates/views/damageSelection.hbs',
// closeOnSubmit: false,
// classes: ["daggerheart", "views", "damage-selection"],
// };
// const mergedOptions = foundry.utils.mergeObject(defaults, overrides);
// return mergedOptions;
// }
// async getData(){
// const context = super.getData();
// context.rollString = this.data.rollString;
// context.bonusDamage = this.data.bonusDamage;
// return context;
// }
// activateListeners(html) {
// super.activateListeners(html);
// html.find('.roll-button').click(this.finish.bind(this));
// html.find('.').change();
// }
// // async _updateObject(_, formData) {
// // const data = foundry.utils.expandObject(formData);
// // this.data = foundry.utils.mergeObject(this.data, data);
// // this.render(true);
// // }
// finish(){
// this.resolve(this.data);
// this.close();
// }
// }

View file

@ -1,9 +1,12 @@
export default class NpcRollSelectionDialog extends FormApplication { const { HandlebarsApplicationMixin, ApplicationV2 } = foundry.applications.api;
constructor(experiences, resolve, isNpc) {
super({}, {}); export default class NpcRollSelectionDialog extends HandlebarsApplicationMixin(ApplicationV2) {
constructor(experiences, resolve, reject) {
super({});
this.experiences = experiences; this.experiences = experiences;
this.resolve = resolve; this.resolve = resolve;
this.reject = reject;
this.selectedExperiences = []; this.selectedExperiences = [];
this.data = { this.data = {
advantage: null advantage: null
@ -11,28 +14,30 @@ export default class NpcRollSelectionDialog extends FormApplication {
} }
get title() { get title() {
return 'Roll Options'; return game.i18n.localize('DAGGERHEART.Application.Settings.Title');
} }
static get defaultOptions() { static DEFAULT_OPTIONS = {
const defaults = super.defaultOptions; tag: 'form',
const overrides = { id: 'roll-selection',
height: 'auto', classes: ['daggerheart', 'views', 'npc-roll-selection'],
width: 500, position: { width: '500', height: 'auto' },
id: 'roll-selection', actions: {
template: 'systems/daggerheart/templates/views/npcRollSelection.hbs', updateIsAdvantage: this.updateIsAdvantage,
closeOnSubmit: false, selectExperience: this.selectExperience
submitOnChange: true, },
classes: ['daggerheart', 'views', 'npc-roll-selection'] form: { handler: this.updateData, submitOnChange: false }
}; };
const mergedOptions = foundry.utils.mergeObject(defaults, overrides); static PARTS = {
main: {
id: 'main',
template: 'systems/daggerheart/templates/views/npcRollSelection.hbs'
}
};
return mergedOptions; async _prepareContext(_options) {
} const context = await super._prepareContext(_options);
async getData() {
const context = super.getData();
context.advantage = this.data.advantage; context.advantage = this.data.advantage;
context.experiences = Object.values(this.experiences).map(x => ({ context.experiences = Object.values(this.experiences).map(x => ({
...x, ...x,
@ -43,22 +48,14 @@ export default class NpcRollSelectionDialog extends FormApplication {
return context; return context;
} }
activateListeners(html) { static updateIsAdvantage(_, button) {
super.activateListeners(html); const advantage = Boolean(button.dataset.advantage);
html.find('.advantage').click(_ => this.updateIsAdvantage(true));
html.find('.disadvantage').click(_ => this.updateIsAdvantage(false));
html.find('.roll-button').click(this.finish.bind(this));
html.find('.experience-chip').click(this.selectExperience.bind(this));
}
updateIsAdvantage(advantage) {
this.data.advantage = this.data.advantage === advantage ? null : advantage; this.data.advantage = this.data.advantage === advantage ? null : advantage;
this.render(); this.render();
} }
selectExperience(event) { static selectExperience(_, button) {
const experience = Object.values(this.experiences).find(experience => experience.id === event.currentTarget.id); const experience = Object.values(this.experiences).find(experience => experience.id === button.id);
this.selectedExperiences = this.selectedExperiences.find(x => x.id === experience.id) this.selectedExperiences = this.selectedExperiences.find(x => x.id === experience.id)
? this.selectedExperiences.filter(x => x.id !== experience.id) ? this.selectedExperiences.filter(x => x.id !== experience.id)
: [...this.selectedExperiences, experience]; : [...this.selectedExperiences, experience];
@ -66,8 +63,17 @@ export default class NpcRollSelectionDialog extends FormApplication {
this.render(); this.render();
} }
finish() { static async updateData() {
this.resolve({ ...this.data, experiences: this.selectedExperiences }); this.resolve({ ...this.data, experiences: this.selectedExperiences });
this.close(); this.close({ updateClose: true });
}
async close(options = {}) {
const { updateClose, ...baseOptions } = options;
if (!updateClose) {
this.reject();
}
await super.close(baseOptions);
} }
} }

View file

@ -7,21 +7,17 @@
<div class="dice-number">d20</div> <div class="dice-number">d20</div>
</div> </div>
<div class="advantage-container"> <div class="advantage-container">
<button class="advantage-button {{#if this.advantage}}active{{/if}} advantage">{{localize "DAGGERHEART.General.Advantage.Full"}}</button> <button class="advantage-button {{#if this.advantage}}active{{/if}} advantage" data-action="updateIsAdvantage" data-advantage="true">{{localize "DAGGERHEART.General.Advantage.Full"}}</button>
<button class="advantage-button {{#if (eq this.advantage false)}}active{{/if}} disadvantage">{{localize "DAGGERHEART.General.Disadvantage.Full"}}</button> <button class="advantage-button {{#if (eq this.advantage false)}}active{{/if}} disadvantage" data-action="updateIsAdvantage">{{localize "DAGGERHEART.General.Disadvantage.Full"}}</button>
</div> </div>
</div> </div>
<div class="roll-dialog-experience-container"> <div class="roll-dialog-experience-container">
{{#each this.experiences as |experience|}} {{#each this.experiences as |experience|}}
<button class="experience-chip {{#if experience.selected}}active{{/if}}" data-action="selectExperience" id="{{id}}">{{experience.name}} {{experience.value}}</button> <button class="experience-chip {{#if experience.selected}}active{{/if}}" data-action="selectExperience" id="{{id}}">{{experience.name}} {{experience.value}}</button>
{{!-- <div class="roll-dialog-chip {{#if experience.selected}}selected{{/if}}" data-action="selectExperience" data-key="{{key}}">
<span>{{experience.name}}</span>
<i class="fa-solid fa-circle-check"></i>
</div> --}}
{{/each}} {{/each}}
</div> </div>
</div> </div>
<footer> <footer class="flexrow">
<button class="roll-button">Roll</button> <button type="submit" class="roll-button">Roll</button>
</footer> </footer>
</div> </div>