Affichage du contenu d'un conteneur

Les actions qui modifient le contenu doivent forcer un render
This commit is contained in:
Vincent Vandemeulebrouck 2021-12-05 01:50:09 +01:00
parent ef56b58d72
commit db0f8e50d2
5 changed files with 185 additions and 118 deletions

View File

@ -1139,7 +1139,7 @@ export class RdDActor extends Actor {
/* -------------------------------------------- */
/** Supprime un item d'un conteneur, sur la base
* de leurs ID */
async enleverDeConteneur(item, conteneur, objetVersConteneur) {
async enleverDeConteneur(item, conteneur, onEnleverDeConteneur) {
if (conteneur?.isConteneur()) {
let data2use = duplicate(Misc.data(conteneur));
let contenu = data2use.data.contenu;
@ -1150,20 +1150,20 @@ export class RdDActor extends Actor {
}
item.data.estContenu = false;
await this.updateEmbeddedDocuments('Item', [data2use]);
delete objetVersConteneur[item.id];
onEnleverDeConteneur();
}
}
/* -------------------------------------------- */
/** Ajoute un item dans un conteneur, sur la base
* de leurs ID */
async ajouterDansConteneur(item, conteneur, objetVersConteneur) {
async ajouterDansConteneur(item, conteneur, onAjouterDansConteneur) {
if (conteneur?.isConteneur()) {
let data2use = duplicate(Misc.data(conteneur));
data2use.data.contenu.push(item.id);
item.data.estContenu = true;
await this.updateEmbeddedDocuments('Item', [data2use]);
objetVersConteneur[item.id] = conteneur.id;
onAjouterDansConteneur(conteneur.id);
}
}
@ -1185,27 +1185,28 @@ export class RdDActor extends Actor {
}
}
async processDropItem(event, dragData, objetVersConteneur) {
console.log("DRAG", this.id, dragData);
const itemId = dragData.id || dragData.data._id;
if (dragData.actorId && dragData.actorId != this.id) {
console.log("Moving objects", dragData);
this.moveItemsBetweenActors(itemId, dragData.actorId);
async processDropItem(params, onEnleverConteneur, onAjouterDansConteneur) {
const targetActorId = this.id;
const sourceActorId = params.sourceActorId;
const itemId = params.itemId;
const destId = params.destId;
const srcId = params.srcId;
if (sourceActorId && sourceActorId != targetActorId) {
console.log("Moving objects", sourceActorId, targetActorId, itemId);
this.moveItemsBetweenActors(itemId, sourceActorId);
return false;
}
let result = true;
const destId = $(event.target).parents(".item").attr("data-item-id");
const item = this.getObjet(itemId);
if (item?.isEquipement() && dragData.actorId == this.id) {
if (item?.isEquipement() && sourceActorId == targetActorId) {
// rangement
const srcId = objetVersConteneur[itemId];
if (srcId != destId && itemId != destId) { // déplacement de l'objet
const dest = this.getObjet(destId);
const src = this.getObjet(srcId);
// changer de conteneur
if (this.conteneurPeutContenir(dest, item)) {
await this.enleverDeConteneur(item, src, objetVersConteneur);
await this.ajouterDansConteneur(item, dest, objetVersConteneur);
await this.enleverDeConteneur(item, src, params.onEnleverConteneur);
await this.ajouterDansConteneur(item, dest, params.onAjouterDansConteneur);
}
else if (dest?.isEquipementSimilaire(item)) {
await this.regrouperEquipementsSimilaires(item, dest);
@ -1893,23 +1894,20 @@ export class RdDActor extends Actor {
}
/* -------------------------------------------- */
async actionItem(item) {
async actionItem(item, onActionItem = async ()=>{}) {
if (!item.getActionPrincipale()) return;
switch (Misc.data(item).type) {
case 'nourritureboisson': return await this.actionNourritureboisson(item);
case 'potion': return await this.actionPotion(item);
case 'nourritureboisson': return await this.actionNourritureboisson(item, onActionItem);
case 'potion': return await this.consommerPotion(item, onActionItem);
case 'livre': return await this.actionLire(item);
}
}
async actionNourritureboisson(item) {
const dialog = await DialogConsommer.create(this, item);
async actionNourritureboisson(item, onActionItem) {
const dialog = await DialogConsommer.create(this, item, onActionItem);
dialog.render(true);
}
async actionPotion(item) {
return await this.consommerPotion(item)
}
async actionLire(item) {
const tache = await this.creerTacheDepuisLivre(item, { renderSheet: false });
if (tache) {
@ -4058,7 +4056,7 @@ export class RdDActor extends Actor {
}
/* -------------------------------------------- */
async consommerPotion(potion) {
async consommerPotion(potion, onActionItem = async () => {}) {
const potionData = Misc.data(potion);
if (potionData.data.categorie.includes('Soin')) {
@ -4068,7 +4066,8 @@ export class RdDActor extends Actor {
} else {
this.consommerPotionGenerique(potionData);
}
this.diminuerQuantiteObjet(potion.id, 1, { supprimerSiZero: potionData.supprimer });
await this.diminuerQuantiteObjet(potion.id, 1, { supprimerSiZero: potionData.supprimer });
await onActionItem()
}
/* -------------------------------------------- */

View File

@ -2,22 +2,23 @@ import { Misc } from "./misc.js";
export class DialogConsommer extends Dialog {
static async create(actor, item, template = undefined, options = {}) {
const consommerData = DialogConsommer.prepareData(actor, item, options);
const html = await renderTemplate(template ?? `systems/foundryvtt-reve-de-dragon/templates/dialog-item-consommer.html`, consommerData);
return new DialogConsommer(actor, item, consommerData, html, options)
static async create(actor, item, onActionItem = async ()=>{}) {
const consommerData = DialogConsommer.prepareData(actor, item);
const html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/dialog-item-consommer.html', consommerData);
return new DialogConsommer(actor, item, consommerData, html, onActionItem)
}
constructor(actor, item, consommerData, html, options = {}) {
mergeObject(options, { classes: ["dialogconsommer"], width: 350, height: 450, 'z-index': 99999 }, { overwrite: false })
constructor(actor, item, consommerData, html, onActionItem = async ()=>{}) {
const options = { classes: ["dialogconsommer"], width: 350, height: 450, 'z-index': 99999 };
let conf = {
title: consommerData.title,
content: html,
default: consommerData.buttonName,
buttons: {
[consommerData.buttonName]: {
label: consommerData.buttonName, callback: it => this.onConsommer(it)
label: consommerData.buttonName, callback: async it => {
await this.onConsommer(it);
await onActionItem();}
}
}
};
@ -32,18 +33,18 @@ export class DialogConsommer extends Dialog {
async onConsommer(event) {
await $(".se-forcer").change();
await $(".consommer-doses").change();
this.actor.consommer(this.item, this.consommerData.choix);
await this.actor.consommer(this.item, this.consommerData.choix);
}
/* -------------------------------------------- */
static prepareData(actor, item, options) {
static prepareData(actor, item) {
const itemData = duplicate(Misc.data(item));
let consommerData = {
item: itemData,
cuisine: Misc.data(actor.getCompetence('cuisine')),
choix: {
doses: options.doses ?? 1,
seForcer: options.seForcer ?? false,
doses: 1,
seForcer: false,
}
}
switch (itemData.type) {

View File

@ -8,6 +8,7 @@ import { Misc } from "./misc.js";
import { HtmlUtility } from "./html-utility.js";
import { ReglesOptionelles } from "./regles-optionelles.js";
import { SYSTEM_RDD } from "./constants.js";
import { RdDSheetUtility } from "./rdd-sheet-utility.js";
/**
* Extend the basic ItemSheet with some very simple modifications
@ -63,15 +64,15 @@ export class RdDItemSheet extends ItemSheet {
/* -------------------------------------------- */
async getData() {
const objectData = Misc.data(this.object);
let formData = {
id: this.object.id,
title: objectData.name,
id: objectData.id,
type: objectData.type,
img: objectData.img,
name: objectData.name,
data: objectData.data,
isGM: game.user.isGM,
actorId: this.actor?.id,
owner: this.document.isOwner,
editable: this.isEditable,
cssClass: this.isEditable ? "editable" : "locked",
@ -79,7 +80,9 @@ export class RdDItemSheet extends ItemSheet {
}
if (this.actor) {
formData.isOwned = true;
formData.actorId = this.actor.id;
if (objectData.type == 'conteneur') {
this.prepareConteneurData(formData);
}
}
formData.categorieCompetences = RdDItemCompetence.getCategorieCompetences();
@ -113,11 +116,22 @@ export class RdDItemSheet extends ItemSheet {
return formData;
}
prepareConteneurData(formData) {
formData.itemsByType = Misc.classify(this.actor.items.map(i => foundry.utils.deepClone(i.data)));
RdDUtility.filterEquipementParType(formData);
this.objetVersConteneur = RdDUtility.buildArbreDeConteneurs(formData.conteneurs, formData.objets);
formData.subItems = formData.conteneurs.find(it => it._id == this.object.id)?.subItems;
}
/* -------------------------------------------- */
/** @override */
activateListeners(html) {
super.activateListeners(html);
let itemSheetDialog = this;
HtmlUtility._showControlWhen($(".item-cout"), ReglesOptionelles.isUsing('afficher-prix-joueurs') || game.user.isGM || !this.object.isOwned);
HtmlUtility._showControlWhen($(".item-magique"), this.object.isMagique());
@ -168,6 +182,30 @@ export class RdDItemSheet extends ItemSheet {
}
});
html.find('.item-split').click(async event => {
const item = RdDSheetUtility.getItem(event, this.actor);
await RdDSheetUtility.splitItem(item, this.actor, async () => itemSheetDialog.render(true));
});
html.find('.item-edit').click(async event => {
const item = RdDSheetUtility.getItem(event, this.actor);
item.sheet.render(true);
});
html.find('.item-delete').click(async event => {
const li = RdDSheetUtility.getEventElement(event);
RdDUtility.confirmerSuppression(this, li);
});
html.find('.item-vendre').click(async event => {
const item = RdDSheetUtility.getItem(event, this.actor);
item?.proposerVente();
});
html.find('.item-action').click(async event => {
const item = RdDSheetUtility.getItem(event, this.actor);
this.actor.actionItem(item, async () => itemSheetDialog.render(true));
});
html.find('.conteneur-name a').click(async event => {
RdDUtility.toggleAfficheContenu(RdDSheetUtility.getItemId(event));
this.render(true);
});
}
/* -------------------------------------------- */
@ -198,4 +236,6 @@ export class RdDItemSheet extends ItemSheet {
return this.object.update(formData);
}
}

View File

@ -224,7 +224,8 @@ export class RdDUtility {
Handlebars.registerHelper('le', str => Grammar.articleDetermine(str));
Handlebars.registerHelper('un', str => Grammar.articleIndetermine(str));
Handlebars.registerHelper('accord', (genre, ...args) => Grammar.accord(genre, args));
Handlebars.registerHelper('buildConteneur', (objet) => { return 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('caseTmr-label', coord => TMRUtility.getTMRLabel(coord));
Handlebars.registerHelper('caseTmr-type', coord => TMRUtility.getTMRType(coord));
Handlebars.registerHelper('typeTmr-name', coord => TMRUtility.typeTmrName(coord));
@ -355,15 +356,9 @@ export class RdDUtility {
/* -------------------------------------------- */
static filterItemsPerTypeForSheet(formData) {
formData.materiel = this.checkNull(formData.itemsByType['objet']);
formData.conteneurs = this.checkNull(formData.itemsByType['conteneur']);
formData.armes = this.checkNull(formData.itemsByType['arme']);
formData.armures = this.checkNull(formData.itemsByType['armure']);
formData.livres = this.checkNull(formData.itemsByType['livre']);
formData.potions = this.checkNull(formData.itemsByType['potion']);
formData.ingredients = this.checkNull(formData.itemsByType['ingredient']);
formData.munitions = this.checkNull(formData.itemsByType['munition']);
formData.herbes = this.checkNull(formData.itemsByType['herbe']);
RdDUtility.filterEquipementParType(formData);
formData.sorts = this.checkNull(formData.itemsByType['sort']);
formData.signesdraconiques = this.checkNull(formData.itemsByType['signedraconique']);
formData.queues = this.checkNull(formData.itemsByType['queue']);
@ -371,34 +366,50 @@ export class RdDUtility {
formData.ombres = this.checkNull(formData.itemsByType['ombre']);
formData.tetes = this.checkNull(formData.itemsByType['tete']);
formData.taches = this.checkNull(formData.itemsByType['tache']);
formData.monnaie = this.checkNull(formData.itemsByType['monnaie']);
formData.nourritureboissons = this.checkNull(formData.itemsByType['nourritureboisson']);
formData.meditations = this.checkNull(formData.itemsByType['meditation']);
formData.chants = this.checkNull(formData.itemsByType['chant']);
formData.danses = this.checkNull(formData.itemsByType['danse']);
formData.musiques = this.checkNull(formData.itemsByType['musique']);
formData.oeuvres = this.checkNull(formData.itemsByType['oeuvre']);
formData.jeux = this.checkNull(formData.itemsByType['jeu']);
formData.gemmes = this.checkNull(formData.itemsByType['gemme']);
formData.recettescuisine = this.checkNull(formData.itemsByType['recettecuisine']);
formData.recettesAlchimiques = this.checkNull(formData.itemsByType['recettealchimique']);
formData.maladies = this.checkNull(formData.itemsByType['maladie']);
formData.poisons = this.checkNull(formData.itemsByType['poison']);
formData.possessions = this.checkNull(formData.itemsByType['possession']);
formData.maladiesPoisons = formData.maladies.concat( formData.poisons);
formData.objets = formData.conteneurs.concat(formData.materiel)
formData.competences = (formData.itemsByType.competence ?? []).concat(formData.itemsByType.competencecreature ?? []);
}
static filterEquipementParType(formData) {
formData.conteneurs = this.checkNull(formData.itemsByType['conteneur']);
formData.materiel = this.checkNull(formData.itemsByType['objet']);
formData.armes = this.checkNull(formData.itemsByType['arme']);
formData.armures = this.checkNull(formData.itemsByType['armure']);
formData.munitions = this.checkNull(formData.itemsByType['munition']);
formData.livres = this.checkNull(formData.itemsByType['livre']);
formData.potions = this.checkNull(formData.itemsByType['potion']);
formData.ingredients = this.checkNull(formData.itemsByType['ingredient']);
formData.herbes = this.checkNull(formData.itemsByType['herbe']);
formData.monnaie = this.checkNull(formData.itemsByType['monnaie']);
formData.monnaie.sort(Monnaie.triValeurDenier());
formData.nourritureboissons = this.checkNull(formData.itemsByType['nourritureboisson']);
formData.gemmes = this.checkNull(formData.itemsByType['gemme']);
formData.objets = formData.conteneurs
.concat(formData.materiel)
.concat(formData.armes)
.concat(formData.armures)
.concat(formData.munitions)
.concat(formData.livres)
.concat(formData.potions)
.concat(formData.herbes)
.concat(formData.ingredients)
.concat(formData.herbes)
.concat(formData.monnaie)
.concat(formData.nourritureboissons)
.concat(formData.gemmes)
.concat(formData.monnaie);
formData.competences = (formData.itemsByType.competence ?? []).concat(formData.itemsByType.competencecreature ?? []);
formData.monnaie.sort(Monnaie.triValeurDenier());
.concat(formData.gemmes);
}
/* -------------------------------------------- */
@ -456,18 +467,27 @@ export class RdDUtility {
//console.log("OBJ:", objet);
let str = Handlebars.partials['systems/foundryvtt-reve-de-dragon/templates/actor-sheet-inventaire-conteneur.html']({ item: objet });
if (objet.type == 'conteneur') {
const afficherContenu = this.getAfficheContenu(objet._id);
str = str + RdDUtility.buildContenu(objet, niveau, afficherContenu);
}
return str;
}
static buildContenu(objet, niveau, afficherContenu) {
if (!niveau) niveau = 1;
objet.niveau = niveau;
let strContenu = "";
//console.log("ITEM DISPLAYED", objet );
if (this.getAfficheContenu(objet._id)) {
str = str + "<ul class='item-list alterne-list item-display-show list-item-margin" + niveau + "'>";
if (afficherContenu) {
strContenu = "<ul class='item-list alterne-list item-display-show list-item-margin" + niveau + "'>";
} else {
str = str + "<ul class='item-list alterne-list item-display-hide list-item-margin" + niveau + "'>";
strContenu = "<ul class='item-list alterne-list item-display-hide list-item-margin" + niveau + "'>";
}
for (let subItem of objet.subItems) {
str = str + this.buildConteneur(subItem, niveau + 1);
strContenu = strContenu + this.buildConteneur(subItem, niveau + 1);
}
str = str + "</ul>";
}
return new Handlebars.SafeString(str);
strContenu = strContenu + "</ul>";
return strContenu;
}
/* -------------------------------------------- */

View File

@ -24,6 +24,13 @@
<label for="xp">Prix (sols) </label>
<input class="attribute-value" type="text" name="data.cout" value="{{data.cout}}" data-dtype="Number"/>
</div>
{{#if isOwned}}
<div class="flexcol">
<span><label>Contenu : </label></span>
{{buildContenu this}}
</div>
{{/if}}
{{>"systems/foundryvtt-reve-de-dragon/templates/partial-item-description.html"}}