From 3152d87078f66d118c7dd5b58f8e59e79ecdb52d Mon Sep 17 00:00:00 2001 From: Vincent Vandemeulebrouck Date: Tue, 13 Apr 2021 21:30:02 +0200 Subject: [PATCH] =?UTF-8?q?D=C3=A9placement=20Drop=20dans=20Actor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module/actor-sheet.js | 2 +- module/actor-vehicule-sheet.js | 2 +- module/actor.js | 45 +++++++++++++++++++++++++++++++--- module/rdd-utility.js | 35 -------------------------- 4 files changed, 43 insertions(+), 41 deletions(-) diff --git a/module/actor-sheet.js b/module/actor-sheet.js index 46ccb270..1f0c37a4 100644 --- a/module/actor-sheet.js +++ b/module/actor-sheet.js @@ -161,7 +161,7 @@ export class RdDActorSheet extends ActorSheet { /* -------------------------------------------- */ async _onDrop(event) { - let toSuper = await RdDUtility.processItemDropEvent(this, event); + let toSuper = await this.actor.processItemDropEvent(event, this.objetVersConteneur); if (toSuper) { super._onDrop(event); } diff --git a/module/actor-vehicule-sheet.js b/module/actor-vehicule-sheet.js index 5ef1944c..13ac3c1e 100644 --- a/module/actor-vehicule-sheet.js +++ b/module/actor-vehicule-sheet.js @@ -68,7 +68,7 @@ export class RdDActorVehiculeSheet extends ActorSheet { /* -------------------------------------------- */ async _onDrop(event) { - let toSuper = await RdDUtility.processItemDropEvent(this, event); + let toSuper = await this.actor.processItemDropEvent(event, this.objetVersConteneur); if ( toSuper) { super._onDrop(event); } diff --git a/module/actor.js b/module/actor.js index e98339a8..96dca173 100644 --- a/module/actor.js +++ b/module/actor.js @@ -828,11 +828,12 @@ export class RdDActor extends Actor { /* -------------------------------------------- */ validateConteneur(itemId, conteneurId) { const dest = this.getObjet(conteneurId); - if (dest?.type != 'conteneur') { - ui.notifications.warn(`Impossible de déplacer un objet (${item.name}) vers un ${dest.type} qui n'est pas un conteneur (${dest.name}) !`); + const item = this.getObjet(itemId); + const destData = Misc.data(dest); + if (destData?.type != 'conteneur') { + ui.notifications.warn(`Impossible de déplacer un objet (${item.name}) vers un ${destData.type} qui n'est pas un conteneur (${dest.name}) !`); return false; } - const item = this.getObjet(itemId); if (this._isConteneurContenu(item, conteneurId)) { ui.notifications.warn(`Impossible de déplacer un conteneur parent (${item.name}) dans un de ses contenus ${dest.name} !`); return false; // Loop detected ! @@ -841,7 +842,7 @@ export class RdDActor extends Actor { } _isConteneurContenu(item, conteneurId) { - if (item?.type == 'conteneur') { // Si c'est un conteneur, il faut vérifier qu'on ne le déplace pas vers un sous-conteneur lui appartenant + if (Misc.data(item)?.type == 'conteneur') { // Si c'est un conteneur, il faut vérifier qu'on ne le déplace pas vers un sous-conteneur lui appartenant for (let id of Misc.templateData(item).contenu) { let subObjet = this.getObjet(id); if (subObjet?.id == conteneurId) { @@ -972,6 +973,42 @@ export class RdDActor extends Actor { await this.updateEmbeddedDocuments('Item', conteneurFixedList); } + /* -------------------------------------------- */ + async processItemDropEvent(event, objetVersConteneur) { + let dragData = JSON.parse(event.dataTransfer.getData("text/plain")); + console.log(dragData, this.id); + let objetId = dragData.id || dragData.data._id; + console.log("DRAG", dragData); + if (dragData.type == 'Item') { + if (dragData.actorId) { + if (dragData.actorId != this.id) { + console.log("Moving objects", dragData); + this.moveItemsBetweenActors(objetId, dragData.actorId); + return false; + } + else { + // rangement + const destId = $(event.target).parents(".item").attr("data-item-id"); + const srcId = objetVersConteneur[objetId]; + + if (srcId != destId && objetId != destId && this.isConteneurSuffisant(objetId, destId)) { + if (srcId) { + await this.enleverDeConteneur(objetId, srcId); + } + if (destId) { // Dropped over an item !!! + await this.ajouterAConteneur(objetId, destId); + } + } + } + } + this.computeEncombrementTotalEtMalusArmure(); + } + if (dragData.type == "Actor") { + this.addSubacteur(objetId); + } + return true; + } + /* -------------------------------------------- */ async moveItemsBetweenActors(itemId, sourceActorId) { let itemsList = [] diff --git a/module/rdd-utility.js b/module/rdd-utility.js index a0bd3da4..db6ef01c 100644 --- a/module/rdd-utility.js +++ b/module/rdd-utility.js @@ -281,41 +281,6 @@ export class RdDUtility { formData.competences = (formData.itemsByType.competence ?? []).concat(formData.itemsByType.competencecreature ?? []); } - /* -------------------------------------------- */ - static async processItemDropEvent(actorSheet, event) { - let dragData = JSON.parse(event.dataTransfer.getData("text/plain")); - console.log(dragData, actorSheet.actor.id); - let objetId = dragData.id || dragData.data._id; - console.log("DRAG", dragData); - if (dragData.type == 'Item') { - if (dragData.actorId) { - if (dragData.actorId != actorSheet.actor.id) { - console.log("Moving objects", dragData); - actorSheet.actor.moveItemsBetweenActors(objetId, dragData.actorId); - return false; - } - else { - // rangement - const destId = $(event.target).parents(".item").attr("data-item-id"); - const srcId = actorSheet.objetVersConteneur[objetId]; - - if (srcId != destId && objetId != destId && actorSheet.actor.isConteneurSuffisant(objetId, destId)) { - if (srcId) { - await actorSheet.actor.enleverDeConteneur(objetId, srcId); - } - if (destId) { // Dropped over an item !!! - await actorSheet.actor.ajouterAConteneur(objetId, destId); - } - } - } - } - actorSheet.actor.computeEncombrementTotalEtMalusArmure(); - } else if (dragData.type == "Actor") { - actorSheet.actor.addSubacteur(objetId); - } - return true; - } - /* -------------------------------------------- */ static buildArbreDeConteneur(actorSheet, formData) { actorSheet.objetVersConteneur = {}; // Table de hash locale pour recupération rapide du conteneur parent (si existant)