Make compendium droppable :D
This commit is contained in:
@@ -2,8 +2,23 @@
|
|||||||
* Base Sheet for Actor and Npc
|
* Base Sheet for Actor and Npc
|
||||||
*/
|
*/
|
||||||
export class BaseSheetL5r5e extends ActorSheet {
|
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
|
* Commons datas
|
||||||
|
* @override
|
||||||
*/
|
*/
|
||||||
getData() {
|
getData() {
|
||||||
const sheetData = super.getData();
|
const sheetData = super.getData();
|
||||||
@@ -40,7 +55,7 @@ export class BaseSheetL5r5e extends ActorSheet {
|
|||||||
*/
|
*/
|
||||||
async _onDrop(event) {
|
async _onDrop(event) {
|
||||||
// Check item type and subtype
|
// Check item type and subtype
|
||||||
const item = game.l5r5e.HelpersL5r5e.getDragnDropTargetObject(event);
|
const item = await game.l5r5e.HelpersL5r5e.getDragnDropTargetObject(event);
|
||||||
if (
|
if (
|
||||||
!item ||
|
!item ||
|
||||||
item.entity !== "Item" ||
|
item.entity !== "Item" ||
|
||||||
@@ -73,26 +88,21 @@ export class BaseSheetL5r5e extends ActorSheet {
|
|||||||
// Babele and properties specific
|
// Babele and properties specific
|
||||||
if (item.data.data.properties && typeof Babele !== "undefined") {
|
if (item.data.data.properties && typeof Babele !== "undefined") {
|
||||||
item.data.data.properties = await Promise.all(
|
item.data.data.properties = await Promise.all(
|
||||||
item.data.data.properties.map(async (prop) => {
|
item.data.data.properties.map(async (property) => {
|
||||||
let gameProp = game.items.get(prop.id);
|
const gameProp = await game.l5r5e.HelpersL5r5e.getObjectGameOrPack(property.id, "Item");
|
||||||
if (gameProp) {
|
if (gameProp) {
|
||||||
// Live item
|
return { id: gameProp._id, name: gameProp.name };
|
||||||
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 prop;
|
return property;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ok add item - Foundry override cause props
|
// Ok add item - Foundry override cause props
|
||||||
const allowed = Hooks.call("dropActorSheetData", this.actor, this, item);
|
const allowed = Hooks.call("dropActorSheetData", this.actor, this, item);
|
||||||
if (allowed === false) return;
|
if (allowed === false) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
return this._onDropItem(event, item);
|
return this._onDropItem(event, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,10 +9,6 @@ export class CharacterSheetL5r5e extends BaseSheetL5r5e {
|
|||||||
return mergeObject(super.defaultOptions, {
|
return mergeObject(super.defaultOptions, {
|
||||||
classes: ["l5r5e", "sheet", "actor"],
|
classes: ["l5r5e", "sheet", "actor"],
|
||||||
template: CONFIG.l5r5e.paths.templates + "actors/character-sheet.html",
|
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 }],
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,10 +13,6 @@ export class NpcSheetL5r5e extends BaseSheetL5r5e {
|
|||||||
return mergeObject(super.defaultOptions, {
|
return mergeObject(super.defaultOptions, {
|
||||||
classes: ["l5r5e", "sheet", "npc"],
|
classes: ["l5r5e", "sheet", "npc"],
|
||||||
template: CONFIG.l5r5e.paths.templates + "actors/npc-sheet.html",
|
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 }],
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,5 @@
|
|||||||
export const L5R5E = {};
|
export const L5R5E = {};
|
||||||
|
|
||||||
L5R5E.packsIds = {
|
|
||||||
properties: {
|
|
||||||
core: "l5r5e.core-properties",
|
|
||||||
},
|
|
||||||
techniques: {
|
|
||||||
core: "l5r5e.core-techniques",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
L5R5E.paths = {
|
L5R5E.paths = {
|
||||||
assets: `systems/l5r5e/assets/`,
|
assets: `systems/l5r5e/assets/`,
|
||||||
templates: `systems/l5r5e/templates/`,
|
templates: `systems/l5r5e/templates/`,
|
||||||
|
|||||||
@@ -54,34 +54,72 @@ export class HelpersL5r5e {
|
|||||||
/**
|
/**
|
||||||
* Return the target object on a drag n drop event, or null if not found
|
* Return the target object on a drag n drop event, or null if not found
|
||||||
*/
|
*/
|
||||||
static getDragnDropTargetObject(event) {
|
static async getDragnDropTargetObject(event) {
|
||||||
let data = null;
|
const data = JSON.parse(event.dataTransfer.getData("text/plain"));
|
||||||
let targetItem = null;
|
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 {
|
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) {
|
} catch (err) {
|
||||||
return null;
|
console.warn(err);
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
switch (data.type) {
|
/**
|
||||||
case "Actor":
|
* Make a temporary item for compendium drag n drop
|
||||||
targetItem = game.actors.get(data.id);
|
*/
|
||||||
break;
|
static async createItemFromCompendium(data) {
|
||||||
|
if (!["item", "armor", "weapon", "technique", "peculiarity", "property"].includes(data.type)) {
|
||||||
case "Item":
|
return data;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
const item = await Item.create(data, { temporary: true });
|
||||||
|
|
||||||
return targetItem;
|
// reinject compendium id
|
||||||
|
item.data._id = data._id;
|
||||||
|
|
||||||
|
return item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ export class ItemSheetL5r5e extends ItemSheet {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @override */
|
||||||
async getData() {
|
async getData() {
|
||||||
const sheetData = super.getData();
|
const sheetData = super.getData();
|
||||||
|
|
||||||
@@ -37,18 +38,10 @@ export class ItemSheetL5r5e extends ItemSheet {
|
|||||||
if (Array.isArray(sheetData.data.properties)) {
|
if (Array.isArray(sheetData.data.properties)) {
|
||||||
const props = [];
|
const props = [];
|
||||||
for (const property of sheetData.data.properties) {
|
for (const property of sheetData.data.properties) {
|
||||||
let item = game.items.get(property.id);
|
const gameProp = await game.l5r5e.HelpersL5r5e.getObjectGameOrPack(property.id, "Item");
|
||||||
if (item) {
|
if (gameProp) {
|
||||||
// Live item
|
sheetData.data.propertiesList.push(gameProp);
|
||||||
sheetData.data.propertiesList.push(item);
|
props.push({ id: gameProp._id, name: gameProp.name });
|
||||||
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 });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sheetData.data.properties = props;
|
sheetData.data.properties = props;
|
||||||
@@ -114,9 +107,9 @@ export class ItemSheetL5r5e extends ItemSheet {
|
|||||||
* Handle dropped data on the Item sheet, only "property" allowed.
|
* Handle dropped data on the Item sheet, only "property" allowed.
|
||||||
* Also a property canot be on another property
|
* Also a property canot be on another property
|
||||||
*/
|
*/
|
||||||
_onDrop(event) {
|
async _onDrop(event) {
|
||||||
// Check item type and subtype
|
// 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") {
|
if (!item || item.entity !== "Item" || item.data.type !== "property" || this.item.type === "property") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user