Make compendium droppable :D

This commit is contained in:
Vlyan
2020-12-26 14:31:31 +01:00
parent 74ee70d73a
commit 6842673ee1
6 changed files with 90 additions and 66 deletions

View File

@@ -2,8 +2,23 @@
* Base Sheet for Actor and Npc
*/
export class BaseSheetL5r5e extends ActorSheet {
/**
* Commons options
*/
static get defaultOptions() {
return mergeObject(super.defaultOptions, {
classes: ["l5r5e", "sheet", "actor"],
// template: CONFIG.l5r5e.paths.templates + "actors/character-sheet.html",
width: 600,
height: 800,
tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description" }],
dragDrop: [{ dragSelector: ".item-list .item", dropSelector: null }],
});
}
/**
* Commons datas
* @override
*/
getData() {
const sheetData = super.getData();
@@ -40,7 +55,7 @@ export class BaseSheetL5r5e extends ActorSheet {
*/
async _onDrop(event) {
// Check item type and subtype
const item = game.l5r5e.HelpersL5r5e.getDragnDropTargetObject(event);
const item = await game.l5r5e.HelpersL5r5e.getDragnDropTargetObject(event);
if (
!item ||
item.entity !== "Item" ||
@@ -73,26 +88,21 @@ export class BaseSheetL5r5e extends ActorSheet {
// Babele and properties specific
if (item.data.data.properties && typeof Babele !== "undefined") {
item.data.data.properties = await Promise.all(
item.data.data.properties.map(async (prop) => {
let gameProp = game.items.get(prop.id);
item.data.data.properties.map(async (property) => {
const gameProp = await game.l5r5e.HelpersL5r5e.getObjectGameOrPack(property.id, "Item");
if (gameProp) {
// Live item
return { id: gameProp.id, name: gameProp.name };
} else {
// Pack item
gameProp = await game.packs.get(CONFIG.l5r5e.packsIds.properties.core).getEntry(prop.id);
if (gameProp) {
return { id: gameProp._id, name: gameProp.name };
}
return { id: gameProp._id, name: gameProp.name };
}
return prop;
return property;
})
);
}
// Ok add item - Foundry override cause props
const allowed = Hooks.call("dropActorSheetData", this.actor, this, item);
if (allowed === false) return;
if (allowed === false) {
return;
}
return this._onDropItem(event, item);
}

View File

@@ -9,10 +9,6 @@ export class CharacterSheetL5r5e extends BaseSheetL5r5e {
return mergeObject(super.defaultOptions, {
classes: ["l5r5e", "sheet", "actor"],
template: CONFIG.l5r5e.paths.templates + "actors/character-sheet.html",
width: 600,
height: 800,
tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description" }],
dragDrop: [{ dragSelector: ".item-list .item", dropSelector: null }],
});
}

View File

@@ -13,10 +13,6 @@ export class NpcSheetL5r5e extends BaseSheetL5r5e {
return mergeObject(super.defaultOptions, {
classes: ["l5r5e", "sheet", "npc"],
template: CONFIG.l5r5e.paths.templates + "actors/npc-sheet.html",
width: 600,
height: 800,
tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description" }],
dragDrop: [{ dragSelector: ".item-list .item", dropSelector: null }],
});
}

View File

@@ -1,14 +1,5 @@
export const L5R5E = {};
L5R5E.packsIds = {
properties: {
core: "l5r5e.core-properties",
},
techniques: {
core: "l5r5e.core-techniques",
},
};
L5R5E.paths = {
assets: `systems/l5r5e/assets/`,
templates: `systems/l5r5e/templates/`,

View File

@@ -54,34 +54,72 @@ export class HelpersL5r5e {
/**
* Return the target object on a drag n drop event, or null if not found
*/
static getDragnDropTargetObject(event) {
let data = null;
let targetItem = null;
static async getDragnDropTargetObject(event) {
const data = JSON.parse(event.dataTransfer.getData("text/plain"));
return await HelpersL5r5e.getObjectGameOrPack(data.id, data.type, data.pack);
}
/**
* Return the object from Game or Pack by his ID, or null if not found
*/
static async getObjectGameOrPack(id, type, pack = null) {
try {
data = JSON.parse(event.dataTransfer.getData("text/plain"));
// Named pack
if (pack) {
const data = await game.packs.get(pack).getEntry(id);
if (data) {
return HelpersL5r5e.createItemFromCompendium(data);
}
}
// Game object
let item = null;
switch (type) {
case "Actor":
item = game.actors.get(id);
break;
case "Item":
item = game.items.get(id);
break;
case "JournalEntry":
item = game.journal.get(id);
break;
case "Macro":
item = game.macros.get(id);
break;
}
if (item) {
return item;
}
// Unknown pack object, iterate all packs
for (const comp of game.packs) {
const data = await comp.getEntity(id);
if (data) {
return HelpersL5r5e.createItemFromCompendium(data);
}
}
} catch (err) {
return null;
console.warn(err);
}
return null;
}
switch (data.type) {
case "Actor":
targetItem = game.actors.get(data.id);
break;
case "Item":
targetItem = game.items.get(data.id);
break;
case "JournalEntry":
targetItem = game.journal.get(data.id);
break;
case "Macro":
targetItem = game.macros.get(data.id);
break;
/**
* Make a temporary item for compendium drag n drop
*/
static async createItemFromCompendium(data) {
if (!["item", "armor", "weapon", "technique", "peculiarity", "property"].includes(data.type)) {
return data;
}
const item = await Item.create(data, { temporary: true });
return targetItem;
// reinject compendium id
item.data._id = data._id;
return item;
}
}

View File

@@ -14,6 +14,7 @@ export class ItemSheetL5r5e extends ItemSheet {
});
}
/** @override */
async getData() {
const sheetData = super.getData();
@@ -37,18 +38,10 @@ export class ItemSheetL5r5e extends ItemSheet {
if (Array.isArray(sheetData.data.properties)) {
const props = [];
for (const property of sheetData.data.properties) {
let item = game.items.get(property.id);
if (item) {
// Live item
sheetData.data.propertiesList.push(item);
props.push({ id: property.id, name: item.name });
} else {
// Pack item
item = await game.packs.get(CONFIG.l5r5e.packsIds.properties.core).getEntry(property.id);
if (item) {
sheetData.data.propertiesList.push(item);
props.push({ id: item._id, name: item.name });
}
const gameProp = await game.l5r5e.HelpersL5r5e.getObjectGameOrPack(property.id, "Item");
if (gameProp) {
sheetData.data.propertiesList.push(gameProp);
props.push({ id: gameProp._id, name: gameProp.name });
}
}
sheetData.data.properties = props;
@@ -114,9 +107,9 @@ export class ItemSheetL5r5e extends ItemSheet {
* Handle dropped data on the Item sheet, only "property" allowed.
* Also a property canot be on another property
*/
_onDrop(event) {
async _onDrop(event) {
// Check item type and subtype
const item = game.l5r5e.HelpersL5r5e.getDragnDropTargetObject(event);
const item = await game.l5r5e.HelpersL5r5e.getDragnDropTargetObject(event);
if (!item || item.entity !== "Item" || item.data.type !== "property" || this.item.type === "property") {
return;
}