Add ability to sort inventories in player and party sheets

This commit is contained in:
Carlos Fernandez 2025-12-02 03:40:28 -05:00
parent 8f917c3640
commit 71ee9a0065
6 changed files with 86 additions and 143 deletions

View file

@ -214,34 +214,8 @@ export default class CharacterSheet extends DHBaseActorSheet {
context.resources.stress.emptyPips = context.resources.stress.emptyPips =
context.resources.stress.max < maxResource ? maxResource - context.resources.stress.max : 0; context.resources.stress.max < maxResource ? maxResource - context.resources.stress.max : 0;
context.inventory = { currencies: {} };
const { title, ...currencies } = game.settings.get(
CONFIG.DH.id,
CONFIG.DH.SETTINGS.gameSettings.Homebrew
).currency;
for (let key in currencies) {
context.inventory.currencies[key] = {
...currencies[key],
field: context.systemFields.gold.fields[key],
value: context.source.system.gold[key]
};
}
// context.inventory = {
// currency: {
// title: game.i18n.localize('DAGGERHEART.CONFIG.Gold.title'),
// coins: game.i18n.localize('DAGGERHEART.CONFIG.Gold.coins'),
// handfuls: game.i18n.localize('DAGGERHEART.CONFIG.Gold.handfuls'),
// bags: game.i18n.localize('DAGGERHEART.CONFIG.Gold.bags'),
// chests: game.i18n.localize('DAGGERHEART.CONFIG.Gold.chests')
// }
// };
context.beastformActive = this.document.effects.find(x => x.type === 'beastform'); context.beastformActive = this.document.effects.find(x => x.type === 'beastform');
// if (context.inventory.length === 0) {
// context.inventory = Array(1).fill(Array(5).fill([]));
// }
return context; return context;
} }
@ -918,20 +892,11 @@ export default class CharacterSheet extends DHBaseActorSheet {
super._onDragStart(event); super._onDragStart(event);
} }
async _onDrop(event) { async _onDropItem(event, item) {
// Prevent event bubbling to avoid duplicate handling if (this.document.uuid === item.parent?.uuid) {
event.preventDefault(); return this._onSortItem(event, item);
event.stopPropagation();
const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event);
const { cancel } = await super._onDrop(event);
if (cancel) return;
this._onDropItem(event, data);
} }
async _onDropItem(event, data) {
const item = await Item.implementation.fromDropData(data);
const itemData = item.toObject(); const itemData = item.toObject();
if (item.type === 'domainCard' && !this.document.system.loadoutSlot.available) { if (item.type === 'domainCard' && !this.document.system.loadoutSlot.available) {
@ -963,10 +928,7 @@ export default class CharacterSheet extends DHBaseActorSheet {
} }
} }
if (this.document.uuid === item.parent?.uuid) return this._onSortItem(event, itemData); return await this._onDropItemCreate(itemData);
const createdItem = await this._onDropItemCreate(itemData);
return createdItem;
} }
async _onDropItemCreate(itemData, event) { async _onDropItemCreate(itemData, event) {

View file

@ -93,25 +93,6 @@ export default class Party extends DHBaseActorSheet {
/* Prepare Context */ /* Prepare Context */
/* -------------------------------------------- */ /* -------------------------------------------- */
async _prepareContext(_options) {
const context = await super._prepareContext(_options);
context.inventory = { currencies: {} };
const { title, ...currencies } = game.settings.get(
CONFIG.DH.id,
CONFIG.DH.SETTINGS.gameSettings.Homebrew
).currency;
for (let key in currencies) {
context.inventory.currencies[key] = {
...currencies[key],
field: context.systemFields.gold.fields[key],
value: context.source.system.gold[key]
};
}
return context;
}
async _preparePartContext(partId, context, options) { async _preparePartContext(partId, context, options) {
context = await super._preparePartContext(partId, context, options); context = await super._preparePartContext(partId, context, options);
switch (partId) { switch (partId) {
@ -451,17 +432,8 @@ export default class Party extends DHBaseActorSheet {
super._onDragStart(event); super._onDragStart(event);
} }
async _onDrop(event) { async _onDropActor(event, document) {
// Prevent event bubbling to avoid duplicate handling
event.preventDefault();
event.stopPropagation();
const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event); const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event);
const { cancel } = await super._onDrop(event);
if (cancel) return;
const document = await foundry.utils.fromUuid(data.uuid);
if (document instanceof DhpActor && Party.ALLOWED_ACTOR_TYPES.includes(document.type)) { if (document instanceof DhpActor && Party.ALLOWED_ACTOR_TYPES.includes(document.type)) {
const currentMembers = this.document.system.partyMembers.map(x => x.uuid); const currentMembers = this.document.system.partyMembers.map(x => x.uuid);
if (currentMembers.includes(data.uuid)) { if (currentMembers.includes(data.uuid)) {
@ -469,11 +441,11 @@ export default class Party extends DHBaseActorSheet {
} }
await this.document.update({ 'system.partyMembers': [...currentMembers, document.uuid] }); await this.document.update({ 'system.partyMembers': [...currentMembers, document.uuid] });
} else if (document instanceof DHItem) {
this.document.createEmbeddedDocuments('Item', [document.toObject()]);
} else { } else {
ui.notifications.warn(game.i18n.localize('DAGGERHEART.UI.Notifications.onlyCharactersInPartySheet')); ui.notifications.warn(game.i18n.localize('DAGGERHEART.UI.Notifications.onlyCharactersInPartySheet'));
} }
return null;
} }
static async #deletePartyMember(event, target) { static async #deletePartyMember(event, target) {

View file

@ -322,10 +322,10 @@ export default function DHApplicationMixin(Base) {
_onDrop(event) { _onDrop(event) {
event.stopPropagation(); event.stopPropagation();
const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event); const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event);
if (data.fromInternal === this.document.uuid) return; if (data.type === 'ActiveEffect' && data.fromInternal !== this.document.uuid) {
if (data.type === 'ActiveEffect') {
this.document.createEmbeddedDocuments('ActiveEffect', [data.data]); this.document.createEmbeddedDocuments('ActiveEffect', [data.data]);
} else {
return super._onDrop(event);
} }
} }

View file

@ -69,6 +69,28 @@ export default class DHBaseActorSheet extends DHApplicationMixin(ActorSheetV2) {
context.showAttribution = !game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.appearance) context.showAttribution = !game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.appearance)
.hideAttribution; .hideAttribution;
// Prepare inventory data
if (["party", "character"].includes(this.document.type)) {
context.inventory = {
currencies: {},
weapons: this.document.itemTypes.weapon.sort((a, b) => a.sort - b.sort),
armor: this.document.itemTypes.armor.sort((a, b) => a.sort - b.sort),
consumables: this.document.itemTypes.consumable.sort((a, b) => a.sort - b.sort),
loot: this.document.itemTypes.loot.sort((a, b) => a.sort - b.sort),
};
const { title, ...currencies } = game.settings.get(
CONFIG.DH.id,
CONFIG.DH.SETTINGS.gameSettings.Homebrew
).currency;
for (const key in currencies) {
context.inventory.currencies[key] = {
...currencies[key],
field: context.systemFields.gold.fields[key],
value: context.source.system.gold[key]
};
}
}
return context; return context;
} }
@ -218,45 +240,39 @@ export default class DHBaseActorSheet extends DHApplicationMixin(ActorSheetV2) {
/* Application Drag/Drop */ /* Application Drag/Drop */
/* -------------------------------------------- */ /* -------------------------------------------- */
async _onDrop(event) { async _onDropItem(event, item) {
const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event); const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event);
if (data.originActor === this.document.uuid) return { cancel: true }; const physicalActorTypes = ['character', 'party'];
const originActor = item.actor;
if (item.actor?.uuid === this.document.uuid || !originActor || !physicalActorTypes.includes(this.document.type)) {
return super._onDropItem(event, item);
}
/* Handling transfer of inventoryItems */ /* Handling transfer of inventoryItems */
let cancel = false; if (item.system.metadata.isInventoryItem) {
const physicalActorTypes = ['character', 'party']; if (item.system.metadata.isQuantifiable) {
if (physicalActorTypes.includes(this.document.type)) {
const originActor = data.originActor ? await foundry.utils.fromUuid(data.originActor) : null;
if (data.originId && originActor && physicalActorTypes.includes(originActor.type)) {
const dropDocument = await foundry.utils.fromUuid(data.uuid);
if (dropDocument.system.metadata.isInventoryItem) {
cancel = true;
if (dropDocument.system.metadata.isQuantifiable) {
const actorItem = originActor.items.get(data.originId); const actorItem = originActor.items.get(data.originId);
const quantityTransfered = const quantityTransfered =
actorItem.system.quantity === 1 actorItem.system.quantity === 1
? 1 ? 1
: await game.system.api.applications.dialogs.ItemTransferDialog.configure(dropDocument); : await game.system.api.applications.dialogs.ItemTransferDialog.configure(item);
if (quantityTransfered) { if (quantityTransfered) {
if (quantityTransfered === actorItem.system.quantity) { if (quantityTransfered === actorItem.system.quantity) {
await originActor.deleteEmbeddedDocuments('Item', [data.originId]); await originActor.deleteEmbeddedDocuments('Item', [data.originId]);
} else { } else {
cancel = true;
await actorItem.update({ await actorItem.update({
'system.quantity': actorItem.system.quantity - quantityTransfered 'system.quantity': actorItem.system.quantity - quantityTransfered
}); });
} }
const existingItem = this.document.items.find(x => itemIsIdentical(x, dropDocument)); const existingItem = this.document.items.find(x => itemIsIdentical(x, item));
if (existingItem) { if (existingItem) {
cancel = true;
await existingItem.update({ await existingItem.update({
'system.quantity': existingItem.system.quantity + quantityTransfered 'system.quantity': existingItem.system.quantity + quantityTransfered
}); });
} else { } else {
const createData = dropDocument.toObject(); const createData = item.toObject();
await this.document.createEmbeddedDocuments('Item', [ await this.document.createEmbeddedDocuments('Item', [
{ {
...createData, ...createData,
@ -267,20 +283,13 @@ export default class DHBaseActorSheet extends DHApplicationMixin(ActorSheetV2) {
} }
]); ]);
} }
} else {
cancel = true;
} }
} else { } else {
await originActor.deleteEmbeddedDocuments('Item', [data.originId]); await originActor.deleteEmbeddedDocuments('Item', [data.originId]);
const createData = dropDocument.toObject(); await this.document.createEmbeddedDocuments('Item', [item.toObject()]);
await this.document.createEmbeddedDocuments('Item', [createData]);
} }
} }
} }
}
return { cancel };
}
/** /**
* On dragStart on the item. * On dragStart on the item.

View file

@ -27,7 +27,7 @@
{{> 'daggerheart.inventory-items' {{> 'daggerheart.inventory-items'
title='TYPES.Item.weapon' title='TYPES.Item.weapon'
type='weapon' type='weapon'
collection=document.itemTypes.weapon collection=@root.inventory.weapons
isGlassy=true isGlassy=true
canCreate=true canCreate=true
hideResources=true hideResources=true
@ -35,7 +35,7 @@
{{> 'daggerheart.inventory-items' {{> 'daggerheart.inventory-items'
title='TYPES.Item.armor' title='TYPES.Item.armor'
type='armor' type='armor'
collection=document.itemTypes.armor collection=@root.inventory.armor
isGlassy=true isGlassy=true
canCreate=true canCreate=true
hideResources=true hideResources=true
@ -43,14 +43,14 @@
{{> 'daggerheart.inventory-items' {{> 'daggerheart.inventory-items'
title='TYPES.Item.consumable' title='TYPES.Item.consumable'
type='consumable' type='consumable'
collection=document.itemTypes.consumable collection=@root.inventory.consumables
isGlassy=true isGlassy=true
canCreate=true canCreate=true
}} }}
{{> 'daggerheart.inventory-items' {{> 'daggerheart.inventory-items'
title='TYPES.Item.loot' title='TYPES.Item.loot'
type='loot' type='loot'
collection=document.itemTypes.loot collection=@root.inventory.loot
isGlassy=true isGlassy=true
canCreate=true canCreate=true
showActions=true showActions=true

View file

@ -52,7 +52,7 @@
title='TYPES.Item.weapon' title='TYPES.Item.weapon'
type='weapon' type='weapon'
actorType='party' actorType='party'
collection=document.itemTypes.weapon collection=@root.inventory.weapons
isGlassy=true isGlassy=true
canCreate=true canCreate=true
hideResources=true hideResources=true
@ -62,7 +62,7 @@
title='TYPES.Item.armor' title='TYPES.Item.armor'
type='armor' type='armor'
actorType='party' actorType='party'
collection=document.itemTypes.armor collection=@root.inventory.armor
isGlassy=true isGlassy=true
canCreate=true canCreate=true
hideResources=true hideResources=true
@ -72,7 +72,7 @@
title='TYPES.Item.consumable' title='TYPES.Item.consumable'
type='consumable' type='consumable'
actorType='party' actorType='party'
collection=document.itemTypes.consumable collection=@root.inventory.consumables
isGlassy=true isGlassy=true
canCreate=true canCreate=true
hideContextMenu=true hideContextMenu=true
@ -81,7 +81,7 @@
title='TYPES.Item.loot' title='TYPES.Item.loot'
type='loot' type='loot'
actorType='party' actorType='party'
collection=document.itemTypes.loot collection=@root.inventory.loot
isGlassy=true isGlassy=true
canCreate=true canCreate=true
hideContextMenu=true hideContextMenu=true