Préparer l'ouverture de conteneurs

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
This commit is contained in:
Vincent Vandemeulebrouck 2021-12-05 01:48:26 +01:00
parent f1608170d1
commit ef56b58d72
3 changed files with 89 additions and 57 deletions

View File

@ -14,6 +14,7 @@ import { RdDCarac } from "./rdd-carac.js";
import { DialogSplitItem } from "./dialog-split-item.js"; import { DialogSplitItem } from "./dialog-split-item.js";
import { ReglesOptionelles } from "./regles-optionelles.js"; import { ReglesOptionelles } from "./regles-optionelles.js";
import { DialogRepos } from "./dialog-repos.js"; import { DialogRepos } from "./dialog-repos.js";
import { RdDSheetUtility } from "./rdd-sheet-utility.js";
/* -------------------------------------------- */ /* -------------------------------------------- */
export class RdDActorSheet extends ActorSheet { export class RdDActorSheet extends ActorSheet {
@ -138,7 +139,7 @@ export class RdDActorSheet extends ActorSheet {
/* -------------------------------------------- */ /* -------------------------------------------- */
async _onDropItem(event, dragData) { 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) { if (callSuper) {
await super._onDropItem(event, dragData) await super._onDropItem(event, dragData)
} }
@ -165,43 +166,34 @@ export class RdDActorSheet extends ActorSheet {
if (!this.options.editable) return; if (!this.options.editable) return;
html.find('.item-split').click(async event => { html.find('.item-split').click(async event => {
const li = $(event.currentTarget).parents(".item"); const item = RdDSheetUtility.getItem(event, this.actor);
const item = this.actor.items.get(li.data("item-id")); RdDSheetUtility.splitItem(item, this.actor);
this.splitItem(item);
}); });
html.find('.item-edit').click(async event => { html.find('.item-edit').click(async event => {
const li = $(event.currentTarget).parents(".item"); const item = RdDSheetUtility.getItem(event, this.actor);
const item = this.actor.items.get(li.data("item-id"));
item.sheet.render(true); item.sheet.render(true);
}); });
html.find('.display-label a').click(async event => { html.find('.display-label a').click(async event => {
let myID = event.currentTarget.attributes['data-item-id'].value; const item = RdDSheetUtility.getItem(event, this.actor);
const item = this.actor.getEmbeddedDocument('Item', myID);
item.sheet.render(true); item.sheet.render(true);
}); });
html.find('.rencontre-delete').click(async event => { html.find('.rencontre-delete').click(async event => {
const li = $(event.currentTarget).parents(".item"); this.actor.deleteTMRRencontre(RdDSheetUtility.getItemId(event));
const rencontreKey = li.data("item-id");
this.actor.deleteTMRRencontre(rencontreKey);
}); });
html.find('.item-delete').click(async event => { html.find('.item-delete').click(async event => {
const li = $(event.currentTarget).parents(".item"); const li = RdDSheetUtility.getEventElement(event);
RdDUtility.confirmerSuppression(this, li); RdDUtility.confirmerSuppression(this, li);
}); });
html.find('.item-vendre').click(async event => { html.find('.item-vendre').click(async event => {
const li = $(event.currentTarget).parents(".item"); const item = RdDSheetUtility.getItem(event, this.actor);
const itemId = li.data("item-id");
const item = this.actor.getObjet(itemId);
item?.proposerVente(); item?.proposerVente();
}); });
html.find('.item-action').click(async event => { html.find('.item-action').click(async event => {
const li = $(event.currentTarget).parents(".item"); const item = RdDSheetUtility.getItem(event, this.actor);
const itemId = li.data("item-id");
const item = this.actor.getObjet(itemId);
this.actor.actionItem(item); this.actor.actionItem(item);
}); });
html.find('.subacteur-delete').click(async event => { html.find('.subacteur-delete').click(async event => {
const li = $(event.currentTarget).parents(".item"); const li = RdDSheetUtility.getEventElement(event);
RdDUtility.confirmerSuppressionSubacteur(this, li); RdDUtility.confirmerSuppressionSubacteur(this, li);
}); });
@ -254,8 +246,7 @@ export class RdDActorSheet extends ActorSheet {
// Equip Inventory Item // Equip Inventory Item
html.find('.item-equip').click(async event => { html.find('.item-equip').click(async event => {
const li = $(event.currentTarget).parents(".item"); this.actor.equiperObjet(RdDSheetUtility.getItemId(event));
this.actor.equiperObjet(li.data("item-id"));
}); });
// Roll Carac // Roll Carac
@ -278,34 +269,34 @@ export class RdDActorSheet extends ActorSheet {
// Roll Skill // Roll Skill
html.find('a.competence-label').click(async event => { 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 => { 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 => { 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 => { 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 => { 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 => { 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 => { 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 => { 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 => { 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 => { 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); let actor = game.actors.get(actorId);
if (actor) { if (actor) {
actor.sheet.render(true); actor.sheet.render(true);
@ -366,7 +357,7 @@ export class RdDActorSheet extends ActorSheet {
this.actor.enleverTousLesEffets(); this.actor.enleverTousLesEffets();
}); });
html.find('.conteneur-name a').click(async event => { html.find('.conteneur-name a').click(async event => {
RdDUtility.toggleAfficheContenu(this._getItemId(event)); RdDUtility.toggleAfficheContenu(RdDSheetUtility.getItemId(event));
this.render(true); this.render(true);
}); });
html.find('.carac-xp-augmenter').click(async event => { html.find('.carac-xp-augmenter').click(async event => {
@ -374,10 +365,10 @@ export class RdDActorSheet extends ActorSheet {
this.actor.updateCaracXPAuto(caracName); this.actor.updateCaracXPAuto(caracName);
}); });
html.find('.competence-xp-augmenter').click(async event => { 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 => { html.find('.competence-stress-augmenter').click(async event => {
this.actor.updateCompetenceStress(this._getItemId(event)); this.actor.updateCompetenceStress(RdDSheetUtility.getItemId(event));
}); });
if (this.options.editCaracComp) { if (this.options.editCaracComp) {
@ -482,12 +473,10 @@ export class RdDActorSheet extends ActorSheet {
}); });
html.find('.monnaie-plus').click(async event => { html.find('.monnaie-plus').click(async event => {
const li = $(event.currentTarget).parents(".item"); this.actor.monnaieIncDec(RdDSheetUtility.getItemId(event), 1);
this.actor.monnaieIncDec(li.data("item-id"), 1);
}); });
html.find('.monnaie-moins').click(async event => { html.find('.monnaie-moins').click(async event => {
const li = $(event.currentTarget).parents(".item"); this.actor.monnaieIncDec(RdDSheetUtility.getItemId(event), -1);
this.actor.monnaieIncDec(li.data("item-id"), -1);
}); });
html.find('.vie-plus').click(async event => { 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) { _getEventArmeCombat(event) {
const li = $(event.currentTarget)?.parents(".item"); const li = $(event.currentTarget)?.parents(".item");
let armeName = li.data("arme-name"); let armeName = li.data("arme-name");

View File

@ -6,6 +6,7 @@
import { RdDUtility } from "./rdd-utility.js"; import { RdDUtility } from "./rdd-utility.js";
import { HtmlUtility } from "./html-utility.js"; import { HtmlUtility } from "./html-utility.js";
import { Misc } from "./misc.js"; import { Misc } from "./misc.js";
import { RdDSheetUtility } from "./rdd-sheet-utility.js";
/* -------------------------------------------- */ /* -------------------------------------------- */
export class RdDActorVehiculeSheet extends ActorSheet { export class RdDActorVehiculeSheet extends ActorSheet {
@ -69,7 +70,7 @@ export class RdDActorVehiculeSheet extends ActorSheet {
/* -------------------------------------------- */ /* -------------------------------------------- */
async _onDropItem(event, dragData) { 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) { if (callSuper) {
await super._onDropItem(event, dragData) await super._onDropItem(event, dragData)
} }
@ -101,13 +102,12 @@ export class RdDActorVehiculeSheet extends ActorSheet {
// Update Inventory Item // Update Inventory Item
html.find('.item-edit').click(ev => { html.find('.item-edit').click(ev => {
const li = $(ev.currentTarget).parents(".item"); const item = RdDSheetUtility.getItem(event, this.actor);
const item = this.actor.getEmbeddedDocument('Item', li.data("itemId"));
item.sheet.render(true); item.sheet.render(true);
}); });
// Delete Inventory Item // Delete Inventory Item
html.find('.item-delete').click(ev => { html.find('.item-delete').click(ev => {
const li = $(ev.currentTarget).parents(".item"); const li = RdDSheetUtility.getEventElement(event);
RdDUtility.confirmerSuppression(this, li); RdDUtility.confirmerSuppression(this, li);
}); });
@ -119,18 +119,15 @@ export class RdDActorVehiculeSheet extends ActorSheet {
}); });
html.find('.monnaie-plus').click(async event => { html.find('.monnaie-plus').click(async event => {
const li = $(event.currentTarget).parents(".item"); this.actor.monnaieIncDec(RdDSheetUtility.getItemId(event), 1);
this.actor.monnaieIncDec(li.data("item-id"), 1);
}); });
html.find('.monnaie-moins').click(async event => { html.find('.monnaie-moins').click(async event => {
const li = $(event.currentTarget).parents(".item"); this.actor.monnaieIncDec(RdDSheetUtility.getItemId(event), -1);
this.actor.monnaieIncDec(li.data("item-id"), -1);
}); });
// Display info about queue // Display info about queue
html.find('.conteneur-name a').click((event) => { html.find('.conteneur-name a').click((event) => {
let myID = event.currentTarget.attributes['data-item-id'].value; RdDUtility.toggleAfficheContenu(RdDSheetUtility.getItemId(event));
RdDUtility.toggleAfficheContenu(myID);
this.render(true); this.render(true);
}); });

View File

@ -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])
}
}
}