Support drag-drop vers conteneur

This commit is contained in:
Vincent Vandemeulebrouck 2021-12-05 16:48:18 +01:00
parent 009ec47709
commit 9de1e7dae5
10 changed files with 126 additions and 65 deletions

View File

@ -26,7 +26,6 @@ export class RdDActorSheet extends ActorSheet {
classes: ["rdd", "sheet", "actor"],
template: "systems/foundryvtt-reve-de-dragon/templates/actor-sheet.html",
width: 640,
//height: 720,
tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "carac" }],
dragDrop: [{ dragSelector: ".item-list .item", dropSelector: null }],
editCaracComp: false,
@ -132,13 +131,15 @@ export class RdDActorSheet extends ActorSheet {
/* -------------------------------------------- */
async _onDropActor(event, dragData) {
console.log("DRAG", this.actor.id, dragData);
console.log("_onDropActor", this.actor.id, dragData);
this.actor.addSubacteur(dragData.id || dragData.data._id);
super._onDropActor(event, dragData);
}
/* -------------------------------------------- */
async _onDropItem(event, dragData) {
const callSuper = await this.actor.processDropItem(RdDSheetUtility.extractItemDropParameters(event, dragData, this.actor, this.objetVersConteneur));
const destItemId = RdDSheetUtility.getItemId(event);
const dropParams = RdDSheetUtility.prepareItemDropParameters(destItemId, this.actor.id, dragData, this.objetVersConteneur);
const callSuper = await this.actor.processDropItem(dropParams);
if (callSuper) {
await super._onDropItem(event, dragData)
}

View File

@ -70,7 +70,9 @@ export class RdDActorVehiculeSheet extends ActorSheet {
/* -------------------------------------------- */
async _onDropItem(event, dragData) {
const callSuper = await this.actor.processDropItem(RdDSheetUtility.extractItemDropParameters(event, dragData, this.actor, this.objetVersConteneur));
const destItemId = RdDSheetUtility.getItemId(event);
const dropParams = RdDSheetUtility.prepareItemDropParameters(destItemId, this.actor.id, dragData, this.objetVersConteneur);
const callSuper = await this.actor.processDropItem(dropParams);
if (callSuper) {
await super._onDropItem(event, dragData)
}

View File

@ -1159,7 +1159,7 @@ export class RdDActor extends Actor {
data2use.data.contenu.push(item.id);
item.data.estContenu = true;
await this.updateEmbeddedDocuments('Item', [data2use]);
onAjouterDansConteneur(conteneur.id);
onAjouterDansConteneur(item.id, conteneur.id);
}
}
@ -1181,7 +1181,7 @@ export class RdDActor extends Actor {
}
}
async processDropItem(params, onEnleverConteneur, onAjouterDansConteneur) {
async processDropItem(params) {
const targetActorId = this.id;
const sourceActorId = params.sourceActorId;
const itemId = params.itemId;
@ -1899,6 +1899,7 @@ export class RdDActor extends Actor {
case 'nourritureboisson': return await this.actionNourritureboisson(item, onActionItem);
case 'potion': return await this.consommerPotion(item, onActionItem);
case 'livre': return await this.actionLire(item);
case 'conteneur': return await item.sheet.render(true);
}
}

View File

@ -23,10 +23,10 @@ export class RdDItemSheet extends ItemSheet {
template: "systems/foundryvtt-reve-de-dragon/templates/item-sheet.html",
width: 550,
height: 550
//tabs: [{navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description"}]
});
}
/* -------------------------------------------- */
_getHeaderButtons() {
let buttons = super._getHeaderButtons();
@ -130,6 +130,11 @@ export class RdDItemSheet extends ItemSheet {
activateListeners(html) {
super.activateListeners(html);
if (this.object.type == 'conteneur') {
this.form.ondragstart = (event) => this._onDragStart(event);
this.form.ondrop = (event) => this._onDrop(event);
}
let itemSheetDialog = this;
HtmlUtility._showControlWhen($(".item-cout"), ReglesOptionelles.isUsing('afficher-prix-joueurs') || game.user.isGM || !this.object.isOwned);
@ -221,7 +226,6 @@ export class RdDItemSheet extends ItemSheet {
/* -------------------------------------------- */
get template() {
//console.log(this);
let type = this.object.data.type;
return `systems/foundryvtt-reve-de-dragon/templates/item-${type}-sheet.html`;
}
@ -230,12 +234,56 @@ export class RdDItemSheet extends ItemSheet {
/** @override */
_updateObject(event, formData) { // Deprecated en v0.8 à clarifier
//console.log("UPDATE !", formData);
// Données de bonus de cases ?
formData = RdDItemSort.buildBonusCaseStringFromFormData(formData);
return this.object.update(formData);
}
async _onDragStart(event) {
console.log("_onDragStart", event);
if ( event.target.classList.contains("entity-link") ) return;
const itemId = event.srcElement?.attributes["data-item-id"].value;
const item = this.actor.items.get(itemId);
// Create drag data
const dragData = {
actorId: this.actor.id,
type: "Item",
data: item.data
};
event.dataTransfer.setData("text/plain", JSON.stringify(dragData));
}
async _onDrop(event) {
// Try to extract the data
let data;
try {
data = JSON.parse(event.dataTransfer.getData('text/plain'));
} catch (err) {
return false;
}
const allowed = Hooks.call("dropActorSheetData", this.actor, this, data);
if ( allowed === false ) return;
// Handle different data types
switch ( data.type ) {
case "Item":
return this._onDropItem(event, data);
}
return super._onDrop(event);
}
/* -------------------------------------------- */
async _onDropItem(event, dragData) {
if (this.actor) {
const dropParams = RdDSheetUtility.prepareItemDropParameters(this.object.id, this.actor.id, dragData, this.objetVersConteneur);
await this.actor.processDropItem(dropParams);
await this.render(true);
}
}
}

View File

@ -104,15 +104,16 @@ export class RdDItem extends Item {
prepareDerivedData() {
super.prepareDerivedData();
if (this.isEquipement(this)) {
if (this.isEquipement()) {
this._calculsEquipement();
}
if (this.isPotion()) {
this.prepareDataPotion()
}
const itemData = Misc.data(this);
itemData.data.actionPrincipale = this.getActionPrincipale({ warnIfNot: false });
}
}
prepareDataPotion() {
const tplData = Misc.templateData(this);
@ -139,7 +140,7 @@ export class RdDItem extends Item {
getActionPrincipale(options = { warnIfNot: true }) {
const itemData = Misc.data(this);
if ((itemData.data.quantite ?? 0) <= 0) {
if (itemData.type != 'conteneur' && (itemData.data.quantite ?? 0) <= 0) {
if (options.warnIfNot) {
ui.notifications.warn(`Vous n'avez plus de ${itemData.name}.`);
}
@ -149,9 +150,10 @@ export class RdDItem extends Item {
case 'nourritureboisson': return itemData.data.boisson ? 'Boire' : 'Manger';
case 'potion': return 'Boire';
case 'livre': return 'Lire';
case 'conteneur': return 'Ouvrir';
}
if (options.warnIfNot) {
ui.notifications.warn(`Impossible d'utilise un ${itemData.name}, aucune action associée définie.`);
ui.notifications.warn(`Impossible d'utiliser un ${itemData.name}, aucune action associée définie.`);
}
return undefined;

View File

@ -20,18 +20,16 @@ export class RdDSheetUtility {
return $(event.currentTarget)?.parents(".item");
}
static extractItemDropParameters(event, dragData, actor, objetVersConteneur) {
console.log("extractItemDropParameters", actor.id, dragData);
static prepareItemDropParameters(destItemId, actorId, dragData, objetVersConteneur) {
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,
targetActorId: actorId,
itemId: itemId,
sourceActorId: dragData.actorId,
srcId: objetVersConteneur[itemId],
onEnleverConteneur: () => { delete objetVersConteneur[itemId]; },
onAjouterDansConteneur: (conteneurId) => { objetVersConteneur[itemId] = conteneurId; }
onAjouterDansConteneur: (itemId, conteneurId) => { objetVersConteneur[itemId] = conteneurId; }
}
}

View File

@ -1,4 +1,4 @@
<li class="item flexrow list-item" data-item-id="{{item._id}}">
<li class="item flexrow list-item" data-item-id="{{item._id}}" draggable="true">
<img class="sheet-competence-img" src="{{item.img}}" title="{{item.name}}"/>
{{#if (eq item.type 'conteneur')}}

View File

@ -27,7 +27,16 @@
{{#if isOwned}}
<div class="flexcol">
<span><label>Contenu : </label></span>
<ul class="item-list alterne-list">
<li class="competence-header flexrow">
<img class="sheet-competence-img" src="{{img}}" title="Equipement"/>
<span class="competence-title flex-grow">Nom</span>
<span class="competence-title">Q.</span>
<span class="competence-title">Enc.</span>
<span class="competence-title flex-grow">Equiper/Editer/Suppr.</span>
</li>
{{buildContenu this}}
</ul>
</div>
{{/if}}