From ef56b58d72fec8f0bca299423b711b63472e1559 Mon Sep 17 00:00:00 2001 From: Vincent Vandemeulebrouck Date: Sun, 5 Dec 2021 01:48:26 +0100 Subject: [PATCH] =?UTF-8?q?Pr=C3=A9parer=20l'ouverture=20de=20conteneurs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Centraliser les méthodes pour retrouver un item, splitter, etc Les mêmes actions seront disponibles dans la fenêtre des conteneurs Préparer aussi le drop sur la fenêtre d'un conteneur --- module/actor-sheet.js | 74 +++++++++++++--------------------- module/actor-vehicule-sheet.js | 17 ++++---- module/rdd-sheet-utility.js | 55 +++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 57 deletions(-) create mode 100644 module/rdd-sheet-utility.js diff --git a/module/actor-sheet.js b/module/actor-sheet.js index 7b2a7557..5c9d99b4 100644 --- a/module/actor-sheet.js +++ b/module/actor-sheet.js @@ -14,6 +14,7 @@ import { RdDCarac } from "./rdd-carac.js"; import { DialogSplitItem } from "./dialog-split-item.js"; import { ReglesOptionelles } from "./regles-optionelles.js"; import { DialogRepos } from "./dialog-repos.js"; +import { RdDSheetUtility } from "./rdd-sheet-utility.js"; /* -------------------------------------------- */ export class RdDActorSheet extends ActorSheet { @@ -138,7 +139,7 @@ export class RdDActorSheet extends ActorSheet { /* -------------------------------------------- */ async _onDropItem(event, dragData) { - const callSuper = await this.actor.processDropItem(event, dragData, this.objetVersConteneur); + const callSuper = await this.actor.processDropItem(RdDSheetUtility.extractItemDropParameters(event, dragData, this.actor, this.objetVersConteneur)); if (callSuper) { await super._onDropItem(event, dragData) } @@ -165,43 +166,34 @@ export class RdDActorSheet extends ActorSheet { if (!this.options.editable) return; html.find('.item-split').click(async event => { - const li = $(event.currentTarget).parents(".item"); - const item = this.actor.items.get(li.data("item-id")); - this.splitItem(item); + const item = RdDSheetUtility.getItem(event, this.actor); + RdDSheetUtility.splitItem(item, this.actor); }); html.find('.item-edit').click(async event => { - const li = $(event.currentTarget).parents(".item"); - const item = this.actor.items.get(li.data("item-id")); + const item = RdDSheetUtility.getItem(event, this.actor); item.sheet.render(true); }); html.find('.display-label a').click(async event => { - let myID = event.currentTarget.attributes['data-item-id'].value; - const item = this.actor.getEmbeddedDocument('Item', myID); + const item = RdDSheetUtility.getItem(event, this.actor); item.sheet.render(true); }); html.find('.rencontre-delete').click(async event => { - const li = $(event.currentTarget).parents(".item"); - const rencontreKey = li.data("item-id"); - this.actor.deleteTMRRencontre(rencontreKey); + this.actor.deleteTMRRencontre(RdDSheetUtility.getItemId(event)); }); html.find('.item-delete').click(async event => { - const li = $(event.currentTarget).parents(".item"); + const li = RdDSheetUtility.getEventElement(event); RdDUtility.confirmerSuppression(this, li); }); html.find('.item-vendre').click(async event => { - const li = $(event.currentTarget).parents(".item"); - const itemId = li.data("item-id"); - const item = this.actor.getObjet(itemId); + const item = RdDSheetUtility.getItem(event, this.actor); item?.proposerVente(); }); html.find('.item-action').click(async event => { - const li = $(event.currentTarget).parents(".item"); - const itemId = li.data("item-id"); - const item = this.actor.getObjet(itemId); + const item = RdDSheetUtility.getItem(event, this.actor); this.actor.actionItem(item); }); html.find('.subacteur-delete').click(async event => { - const li = $(event.currentTarget).parents(".item"); + const li = RdDSheetUtility.getEventElement(event); RdDUtility.confirmerSuppressionSubacteur(this, li); }); @@ -254,8 +246,7 @@ export class RdDActorSheet extends ActorSheet { // Equip Inventory Item html.find('.item-equip').click(async event => { - const li = $(event.currentTarget).parents(".item"); - this.actor.equiperObjet(li.data("item-id")); + this.actor.equiperObjet(RdDSheetUtility.getItemId(event)); }); // Roll Carac @@ -278,34 +269,34 @@ export class RdDActorSheet extends ActorSheet { // Roll Skill html.find('a.competence-label').click(async event => { - this.actor.rollCompetence(this._getItemId(event)); + this.actor.rollCompetence(RdDSheetUtility.getItemId(event)); }); html.find('.tache-label a').click(async event => { - this.actor.rollTache(this._getItemId(event)); + this.actor.rollTache(RdDSheetUtility.getItemId(event)); }); html.find('.meditation-label a').click(async event => { - this.actor.rollMeditation(this._getItemId(event)); + this.actor.rollMeditation(RdDSheetUtility.getItemId(event)); }); html.find('.chant-label a').click(async event => { - this.actor.rollChant(this._getItemId(event)); + this.actor.rollChant(RdDSheetUtility.getItemId(event)); }); html.find('.danse-label a').click(async event => { - this.actor.rollDanse(this._getItemId(event)); + this.actor.rollDanse(RdDSheetUtility.getItemId(event)); }); html.find('.musique-label a').click(async event => { - this.actor.rollMusique(this._getItemId(event)); + this.actor.rollMusique(RdDSheetUtility.getItemId(event)); }); html.find('.oeuvre-label a').click(async event => { - this.actor.rollOeuvre(this._getItemId(event)); + this.actor.rollOeuvre(RdDSheetUtility.getItemId(event)); }); html.find('.jeu-label a').click(async event => { - this.actor.rollJeu(this._getItemId(event)); + this.actor.rollJeu(RdDSheetUtility.getItemId(event)); }); html.find('.recettecuisine-label a').click(async event => { - this.actor.rollRecetteCuisine(this._getItemId(event)); + this.actor.rollRecetteCuisine(RdDSheetUtility.getItemId(event)); }); html.find('.subacteur-label a').click(async event => { - let actorId = this._getEventItemData(event, 'actor-id'); + let actorId = RdDSheetUtility.getEventItemData(event, 'actor-id'); let actor = game.actors.get(actorId); if (actor) { actor.sheet.render(true); @@ -366,7 +357,7 @@ export class RdDActorSheet extends ActorSheet { this.actor.enleverTousLesEffets(); }); html.find('.conteneur-name a').click(async event => { - RdDUtility.toggleAfficheContenu(this._getItemId(event)); + RdDUtility.toggleAfficheContenu(RdDSheetUtility.getItemId(event)); this.render(true); }); html.find('.carac-xp-augmenter').click(async event => { @@ -374,10 +365,10 @@ export class RdDActorSheet extends ActorSheet { this.actor.updateCaracXPAuto(caracName); }); html.find('.competence-xp-augmenter').click(async event => { - this.actor.updateCompetenceXPAuto(this._getItemId(event)); + this.actor.updateCompetenceXPAuto(RdDSheetUtility.getItemId(event)); }); html.find('.competence-stress-augmenter').click(async event => { - this.actor.updateCompetenceStress(this._getItemId(event)); + this.actor.updateCompetenceStress(RdDSheetUtility.getItemId(event)); }); if (this.options.editCaracComp) { @@ -482,12 +473,10 @@ export class RdDActorSheet extends ActorSheet { }); html.find('.monnaie-plus').click(async event => { - const li = $(event.currentTarget).parents(".item"); - this.actor.monnaieIncDec(li.data("item-id"), 1); + this.actor.monnaieIncDec(RdDSheetUtility.getItemId(event), 1); }); html.find('.monnaie-moins').click(async event => { - const li = $(event.currentTarget).parents(".item"); - this.actor.monnaieIncDec(li.data("item-id"), -1); + this.actor.monnaieIncDec(RdDSheetUtility.getItemId(event), -1); }); html.find('.vie-plus').click(async event => { @@ -516,15 +505,6 @@ export class RdDActorSheet extends ActorSheet { }); } - _getItemId(event) { - return this._getEventItemData(event, "item-id"); - } - - _getEventItemData(event, property) { - const li = $(event.currentTarget)?.parents(".item"); - return li?.data(property); - } - _getEventArmeCombat(event) { const li = $(event.currentTarget)?.parents(".item"); let armeName = li.data("arme-name"); diff --git a/module/actor-vehicule-sheet.js b/module/actor-vehicule-sheet.js index b07c666c..2db4466f 100644 --- a/module/actor-vehicule-sheet.js +++ b/module/actor-vehicule-sheet.js @@ -6,6 +6,7 @@ import { RdDUtility } from "./rdd-utility.js"; import { HtmlUtility } from "./html-utility.js"; import { Misc } from "./misc.js"; +import { RdDSheetUtility } from "./rdd-sheet-utility.js"; /* -------------------------------------------- */ export class RdDActorVehiculeSheet extends ActorSheet { @@ -69,7 +70,7 @@ export class RdDActorVehiculeSheet extends ActorSheet { /* -------------------------------------------- */ async _onDropItem(event, dragData) { - const callSuper = await this.actor.processDropItem(event, dragData, this.objetVersConteneur); + const callSuper = await this.actor.processDropItem(RdDSheetUtility.extractItemDropParameters(event, dragData, this.actor, this.objetVersConteneur)); if (callSuper) { await super._onDropItem(event, dragData) } @@ -101,13 +102,12 @@ export class RdDActorVehiculeSheet extends ActorSheet { // Update Inventory Item html.find('.item-edit').click(ev => { - const li = $(ev.currentTarget).parents(".item"); - const item = this.actor.getEmbeddedDocument('Item', li.data("itemId")); + const item = RdDSheetUtility.getItem(event, this.actor); item.sheet.render(true); }); // Delete Inventory Item html.find('.item-delete').click(ev => { - const li = $(ev.currentTarget).parents(".item"); + const li = RdDSheetUtility.getEventElement(event); RdDUtility.confirmerSuppression(this, li); }); @@ -119,18 +119,15 @@ export class RdDActorVehiculeSheet extends ActorSheet { }); html.find('.monnaie-plus').click(async event => { - const li = $(event.currentTarget).parents(".item"); - this.actor.monnaieIncDec(li.data("item-id"), 1); + this.actor.monnaieIncDec(RdDSheetUtility.getItemId(event), 1); }); html.find('.monnaie-moins').click(async event => { - const li = $(event.currentTarget).parents(".item"); - this.actor.monnaieIncDec(li.data("item-id"), -1); + this.actor.monnaieIncDec(RdDSheetUtility.getItemId(event), -1); }); // Display info about queue html.find('.conteneur-name a').click((event) => { - let myID = event.currentTarget.attributes['data-item-id'].value; - RdDUtility.toggleAfficheContenu(myID); + RdDUtility.toggleAfficheContenu(RdDSheetUtility.getItemId(event)); this.render(true); }); diff --git a/module/rdd-sheet-utility.js b/module/rdd-sheet-utility.js new file mode 100644 index 00000000..5f38157b --- /dev/null +++ b/module/rdd-sheet-utility.js @@ -0,0 +1,55 @@ +import { DialogSplitItem } from "./dialog-split-item.js"; +import { Misc } from "./misc.js"; + +export class RdDSheetUtility { + + static getItem(event, actor) { + return actor.items.get(RdDSheetUtility.getItemId(event)); + } + + static getItemId(event) { + return RdDSheetUtility.getEventItemData(event, "item-id"); + } + + static getEventItemData(event, property) { + const node = RdDSheetUtility.getEventElement(event); + return node?.data(property); + } + + static getEventElement(event) { + return $(event.currentTarget)?.parents(".item"); + } + + static extractItemDropParameters(event, dragData, actor, objetVersConteneur) { + console.log("extractItemDropParameters", actor.id, dragData); + const itemId = dragData.id || dragData.data._id; + const destItemId = $(event.target).parents(".item").attr("data-item-id"); + return { + sourceActorId: dragData.actorId, + targetActorId: actor.id, + itemId: itemId, + destId: destItemId, + srcId: objetVersConteneur[itemId], + onEnleverConteneur: () => { delete objetVersConteneur[itemId]; }, + onAjouterDansConteneur: (conteneurId) => { objetVersConteneur[itemId] = conteneurId; } + } + } + + static async splitItem(item, actor, onSplit = ()=>{}) { + const dialog = await DialogSplitItem.create(item, async (item, split) => { + await RdDSheetUtility._onSplitItem(item, split, actor); + onSplit(); + }); + dialog.render(true); + } + + static async _onSplitItem(item, split, actor) { + if (split >= 1 && split < Misc.data(item).data.quantite) { + await item.diminuerQuantite(split); + const itemData = duplicate(Misc.data(item)); + // todo: ajouter dans le même conteneur? + itemData.data.quantite = split; + await actor.createEmbeddedDocuments('Item', [itemData]) + } + } +} \ No newline at end of file