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"], classes: ["rdd", "sheet", "actor"],
template: "systems/foundryvtt-reve-de-dragon/templates/actor-sheet.html", template: "systems/foundryvtt-reve-de-dragon/templates/actor-sheet.html",
width: 640, width: 640,
//height: 720,
tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "carac" }], tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "carac" }],
dragDrop: [{ dragSelector: ".item-list .item", dropSelector: null }], dragDrop: [{ dragSelector: ".item-list .item", dropSelector: null }],
editCaracComp: false, editCaracComp: false,
@ -132,13 +131,15 @@ export class RdDActorSheet extends ActorSheet {
/* -------------------------------------------- */ /* -------------------------------------------- */
async _onDropActor(event, dragData) { 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); this.actor.addSubacteur(dragData.id || dragData.data._id);
super._onDropActor(event, dragData); super._onDropActor(event, dragData);
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
async _onDropItem(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) { if (callSuper) {
await super._onDropItem(event, dragData) await super._onDropItem(event, dragData)
} }

View File

@ -70,7 +70,9 @@ export class RdDActorVehiculeSheet extends ActorSheet {
/* -------------------------------------------- */ /* -------------------------------------------- */
async _onDropItem(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) { if (callSuper) {
await super._onDropItem(event, dragData) await super._onDropItem(event, dragData)
} }

View File

@ -1159,7 +1159,7 @@ export class RdDActor extends Actor {
data2use.data.contenu.push(item.id); data2use.data.contenu.push(item.id);
item.data.estContenu = true; item.data.estContenu = true;
await this.updateEmbeddedDocuments('Item', [data2use]); 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 targetActorId = this.id;
const sourceActorId = params.sourceActorId; const sourceActorId = params.sourceActorId;
const itemId = params.itemId; const itemId = params.itemId;
@ -1899,6 +1899,7 @@ export class RdDActor extends Actor {
case 'nourritureboisson': return await this.actionNourritureboisson(item, onActionItem); case 'nourritureboisson': return await this.actionNourritureboisson(item, onActionItem);
case 'potion': return await this.consommerPotion(item, onActionItem); case 'potion': return await this.consommerPotion(item, onActionItem);
case 'livre': return await this.actionLire(item); 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", template: "systems/foundryvtt-reve-de-dragon/templates/item-sheet.html",
width: 550, width: 550,
height: 550 height: 550
//tabs: [{navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description"}]
}); });
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
_getHeaderButtons() { _getHeaderButtons() {
let buttons = super._getHeaderButtons(); let buttons = super._getHeaderButtons();
@ -130,6 +130,11 @@ export class RdDItemSheet extends ItemSheet {
activateListeners(html) { activateListeners(html) {
super.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; let itemSheetDialog = this;
HtmlUtility._showControlWhen($(".item-cout"), ReglesOptionelles.isUsing('afficher-prix-joueurs') || game.user.isGM || !this.object.isOwned); 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() { get template() {
//console.log(this);
let type = this.object.data.type; let type = this.object.data.type;
return `systems/foundryvtt-reve-de-dragon/templates/item-${type}-sheet.html`; return `systems/foundryvtt-reve-de-dragon/templates/item-${type}-sheet.html`;
} }
@ -230,12 +234,56 @@ export class RdDItemSheet extends ItemSheet {
/** @override */ /** @override */
_updateObject(event, formData) { // Deprecated en v0.8 à clarifier _updateObject(event, formData) { // Deprecated en v0.8 à clarifier
//console.log("UPDATE !", formData);
// Données de bonus de cases ? // Données de bonus de cases ?
formData = RdDItemSort.buildBonusCaseStringFromFormData(formData); formData = RdDItemSort.buildBonusCaseStringFromFormData(formData);
return this.object.update(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,14 +104,15 @@ export class RdDItem extends Item {
prepareDerivedData() { prepareDerivedData() {
super.prepareDerivedData(); super.prepareDerivedData();
if (this.isEquipement(this)) { if (this.isEquipement()) {
this._calculsEquipement(); this._calculsEquipement();
if (this.isPotion()) {
this.prepareDataPotion()
}
const itemData = Misc.data(this);
itemData.data.actionPrincipale = this.getActionPrincipale({ warnIfNot: false });
} }
if (this.isPotion()) {
this.prepareDataPotion()
}
const itemData = Misc.data(this);
itemData.data.actionPrincipale = this.getActionPrincipale({ warnIfNot: false });
} }
prepareDataPotion() { prepareDataPotion() {
@ -139,7 +140,7 @@ export class RdDItem extends Item {
getActionPrincipale(options = { warnIfNot: true }) { getActionPrincipale(options = { warnIfNot: true }) {
const itemData = Misc.data(this); const itemData = Misc.data(this);
if ((itemData.data.quantite ?? 0) <= 0) { if (itemData.type != 'conteneur' && (itemData.data.quantite ?? 0) <= 0) {
if (options.warnIfNot) { if (options.warnIfNot) {
ui.notifications.warn(`Vous n'avez plus de ${itemData.name}.`); 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 'nourritureboisson': return itemData.data.boisson ? 'Boire' : 'Manger';
case 'potion': return 'Boire'; case 'potion': return 'Boire';
case 'livre': return 'Lire'; case 'livre': return 'Lire';
case 'conteneur': return 'Ouvrir';
} }
if (options.warnIfNot) { 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; return undefined;
@ -200,7 +202,7 @@ export class RdDItem extends Item {
.filter(([key, value]) => value != otherTplData[key]); .filter(([key, value]) => value != otherTplData[key]);
if (differences.length > 0) { if (differences.length > 0) {
let message = `Impossible de regrouper les ${itemData.type} ${itemData.name}: `; let message = `Impossible de regrouper les ${itemData.type} ${itemData.name}: `;
for (const [key, value] of differences){ for (const [key, value] of differences) {
message += `<br>${key}: ${value} vs ${otherTplData[key]}`; message += `<br>${key}: ${value} vs ${otherTplData[key]}`;
} }
ui.notifications.info(message) ui.notifications.info(message)
@ -236,7 +238,7 @@ export class RdDItem extends Item {
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
async postItem( modeOverride ) { async postItem(modeOverride) {
console.log(this); console.log(this);
let chatData = duplicate(Misc.data(this)); let chatData = duplicate(Misc.data(this));
const properties = this.getProprietes(); const properties = this.getProprietes();
@ -568,11 +570,11 @@ export class RdDItem extends Item {
`<b>Dommages</b>: ${tplData.dommages}` `<b>Dommages</b>: ${tplData.dommages}`
] ]
if (tplData.remedesconnus) { if (tplData.remedesconnus) {
properties.push(`<b>Remedes</b>: ${tplData.remedes}` ) properties.push(`<b>Remedes</b>: ${tplData.remedes}`)
} }
} else { } else {
properties = [ properties = [
`<b>Inconnue</b>` ] `<b>Inconnue</b>`]
} }
return properties; return properties;
} }

View File

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

View File

@ -225,7 +225,7 @@ export class RdDUtility {
Handlebars.registerHelper('un', str => Grammar.articleIndetermine(str)); Handlebars.registerHelper('un', str => Grammar.articleIndetermine(str));
Handlebars.registerHelper('accord', (genre, ...args) => Grammar.accord(genre, args)); Handlebars.registerHelper('accord', (genre, ...args) => Grammar.accord(genre, args));
Handlebars.registerHelper('buildConteneur', (objet) => { return new Handlebars.SafeString(RdDUtility.buildConteneur(objet)); }); Handlebars.registerHelper('buildConteneur', (objet) => { return new Handlebars.SafeString(RdDUtility.buildConteneur(objet)); });
Handlebars.registerHelper('buildContenu', (objet) => { return new Handlebars.SafeString(RdDUtility.buildContenu(objet, 1, true)); }); Handlebars.registerHelper('buildContenu', (objet) => { return new Handlebars.SafeString(RdDUtility.buildContenu(objet, 1, true)); });
Handlebars.registerHelper('caseTmr-label', coord => TMRUtility.getTMRLabel(coord)); Handlebars.registerHelper('caseTmr-label', coord => TMRUtility.getTMRLabel(coord));
Handlebars.registerHelper('caseTmr-type', coord => TMRUtility.getTMRType(coord)); Handlebars.registerHelper('caseTmr-type', coord => TMRUtility.getTMRType(coord));
Handlebars.registerHelper('typeTmr-name', coord => TMRUtility.typeTmrName(coord)); Handlebars.registerHelper('typeTmr-name', coord => TMRUtility.typeTmrName(coord));
@ -272,7 +272,7 @@ export class RdDUtility {
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
static async selectObjetType( actorSheet) { static async selectObjetType(actorSheet) {
let typeObjets = RdDItem.getTypeObjetsEquipement(); let typeObjets = RdDItem.getTypeObjetsEquipement();
let options = `<span class="competence-label">Selectionnez le type d'équipement</span><select class="item-type">`; let options = `<span class="competence-label">Selectionnez le type d'équipement</span><select class="item-type">`;
for (let typeName of typeObjets) { for (let typeName of typeObjets) {
@ -291,10 +291,10 @@ export class RdDUtility {
} }
}); });
d.render(true); d.render(true);
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
static async selectTypeOeuvre( actorSheet) { static async selectTypeOeuvre(actorSheet) {
let typeObjets = RdDItem.getTypesOeuvres(); let typeObjets = RdDItem.getTypesOeuvres();
let options = `<span class="competence-label">Selectionnez le type d'oeuvre</span><select class="item-type">`; let options = `<span class="competence-label">Selectionnez le type d'oeuvre</span><select class="item-type">`;
for (let typeName of typeObjets) { for (let typeName of typeObjets) {
@ -313,7 +313,7 @@ export class RdDUtility {
} }
}); });
d.render(true); d.render(true);
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
static buildListOptions(min, max) { static buildListOptions(min, max) {
@ -378,7 +378,7 @@ export class RdDUtility {
formData.maladies = this.checkNull(formData.itemsByType['maladie']); formData.maladies = this.checkNull(formData.itemsByType['maladie']);
formData.poisons = this.checkNull(formData.itemsByType['poison']); formData.poisons = this.checkNull(formData.itemsByType['poison']);
formData.possessions = this.checkNull(formData.itemsByType['possession']); formData.possessions = this.checkNull(formData.itemsByType['possession']);
formData.maladiesPoisons = formData.maladies.concat( formData.poisons); formData.maladiesPoisons = formData.maladies.concat(formData.poisons);
formData.competences = (formData.itemsByType.competence ?? []).concat(formData.itemsByType.competencecreature ?? []); formData.competences = (formData.itemsByType.competence ?? []).concat(formData.itemsByType.competencecreature ?? []);
} }
@ -440,11 +440,11 @@ export class RdDUtility {
.map(id => Misc.data(objets.find(it => (id == it._id)))) .map(id => Misc.data(objets.find(it => (id == it._id))))
.filter(it => it); .filter(it => it);
let enc = Number(itemData.data.encombrement ?? 0) * Number(itemData.data.quantite ?? 1); let enc = Number(itemData.data.encombrement ?? 0) * Number(itemData.data.quantite ?? 1);
for (let itemData of contenuDatas){ for (let itemData of contenuDatas) {
if (itemData.type == 'conteneur') { if (itemData.type == 'conteneur') {
enc += RdDUtility.calculEncContenu(itemData, objets); enc += RdDUtility.calculEncContenu(itemData, objets);
} }
else{ else {
enc += Number(itemData.data.encombrement ?? 0) * Number(itemData.data.quantite ?? 1); enc += Number(itemData.data.encombrement ?? 0) * Number(itemData.data.quantite ?? 1);
} }
} }
@ -692,8 +692,8 @@ export class RdDUtility {
// gestion bouton tchat Acheter // gestion bouton tchat Acheter
html.on("click", '.defense-possession', event => { html.on("click", '.defense-possession', event => {
let actorId = event.currentTarget.attributes['data-defenderId'].value; let actorId = event.currentTarget.attributes['data-defenderId'].value;
let possessionId = event.currentTarget.attributes['data-possessionId'].value; let possessionId = event.currentTarget.attributes['data-possessionId'].value;
RdDPossession.onDefensePossession(actorId, possessionId); RdDPossession.onDefensePossession(actorId, possessionId);
}); });
@ -850,7 +850,7 @@ export class RdDUtility {
let itemId = li.data("item-id"); let itemId = li.data("item-id");
let objet = actorSheet.actor.getObjet(itemId); let objet = actorSheet.actor.getObjet(itemId);
if ( objet.type == 'monnaie' && Monnaie.isSystemMonnaie(objet) ) { if (objet.type == 'monnaie' && Monnaie.isSystemMonnaie(objet)) {
ui.notifications.warn("Suppression des monnaies de base impossible"); ui.notifications.warn("Suppression des monnaies de base impossible");
return; return;
} }
@ -896,7 +896,7 @@ export class RdDUtility {
/* -------------------------------------------- */ /* -------------------------------------------- */
static afficherHeuresChanceMalchance(heureNaissance) { static afficherHeuresChanceMalchance(heureNaissance) {
if ( game.user.isGM) { if (game.user.isGM) {
let heure = game.system.rdd.calendrier.findHeure(heureNaissance); let heure = game.system.rdd.calendrier.findHeure(heureNaissance);
if (heureNaissance && heure) { if (heureNaissance && heure) {
let ajustement = game.system.rdd.calendrier.getAjustementAstrologique(heureNaissance); let ajustement = game.system.rdd.calendrier.getAjustementAstrologique(heureNaissance);
@ -907,7 +907,7 @@ export class RdDUtility {
}); });
} }
else if (heureNaissance) { else if (heureNaissance) {
ui.notifications.warn(heureNaissance+" ne correspond pas à une heure de naissance"); ui.notifications.warn(heureNaissance + " ne correspond pas à une heure de naissance");
} }
else { else {
ui.notifications.warn("Pas d'heure de naissance selectionnée"); ui.notifications.warn("Pas d'heure de naissance selectionnée");

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}}"/> <img class="sheet-competence-img" src="{{item.img}}" title="{{item.name}}"/>
{{#if (eq item.type 'conteneur')}} {{#if (eq item.type 'conteneur')}}

View File

@ -648,31 +648,31 @@
<div> <div>
<span class="item-name">Encombrement total/max : {{numberFormat calc.encTotal decimals=2}} / {{data.attributs.encombrement.value}} <b>{{calc.surEncombrementMessage}}</b></span> - <span class="item-name">Encombrement total/max : {{numberFormat calc.encTotal decimals=2}} / {{data.attributs.encombrement.value}} <b>{{calc.surEncombrementMessage}}</b></span> -
<span class="item-name">Estimation de l'équipement : {{numberFormat calc.prixTotalEquipement decimals=2}} Sols</span> <span class="item-name">Estimation de l'équipement : {{numberFormat calc.prixTotalEquipement decimals=2}} Sols</span>
</div> </div>
<div> <div>
<span class="item-name"><a class="creer-un-objet">Créer un objet</a></span> <span class="item-name"><a class="creer-un-objet">Créer un objet</a></span>
{{#if options.isGM}} {{#if options.isGM}}
<span class="item-name"> - <a id="nettoyer-conteneurs">Vider tout les conteneurs</a></span> <span class="item-name"> - <a id="nettoyer-conteneurs">Vider tout les conteneurs</a></span>
{{/if}} {{/if}}
</div> </div>
<ul class="item-list alterne-list"> <ul class="item-list alterne-list">
<li class="competence-header flexrow"> <li class="competence-header flexrow">
<img class="sheet-competence-img" src="{{img}}" title="Equipement"/> <img class="sheet-competence-img" src="{{img}}" title="Equipement"/>
<span class="competence-title flex-grow">Nom</span> <span class="competence-title flex-grow">Nom</span>
<span class="competence-title">Q.</span> <span class="competence-title">Q.</span>
<span class="competence-title">Enc.</span> <span class="competence-title">Enc.</span>
<span class="competence-title flex-grow">Equiper/Editer/Suppr.</span> <span class="competence-title flex-grow">Equiper/Editer/Suppr.</span>
</li> </li>
{{#each objets as |item id|}} {{#each objets as |item id|}}
{{#unless item.estContenu}} {{#unless item.estContenu}}
{{#if (ne item.type 'conteneur')}} {{#if (ne item.type 'conteneur')}}
{{> "systems/foundryvtt-reve-de-dragon/templates/actor-sheet-inventaire-conteneur.html" item=item }} {{> "systems/foundryvtt-reve-de-dragon/templates/actor-sheet-inventaire-conteneur.html" item=item }}
{{/if}} {{/if}}
{{/unless}} {{/unless}}
{{/each}} {{/each}}
{{#each conteneurs as |conteneur id|}} {{#each conteneurs as |conteneur id|}}
{{buildConteneur this}} {{buildConteneur this}}
{{/each}} {{/each}}
</ul> </ul>
<span class="item-name"><h4>Compagnons animaux</h4></span> <span class="item-name"><h4>Compagnons animaux</h4></span>

View File

@ -27,7 +27,16 @@
{{#if isOwned}} {{#if isOwned}}
<div class="flexcol"> <div class="flexcol">
<span><label>Contenu : </label></span> <span><label>Contenu : </label></span>
{{buildContenu this}} <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> </div>
{{/if}} {{/if}}