Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Haxxer committed Jan 11, 2022
1 parent 6488348 commit 1eb1329
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 109 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Item Piles Changelog

## Version 1.1.3
- Adjusted display one-type item piles to also take into account dynamic attributes (gold piles!)
- Fixed prototype tokens not being updated when editing an item pile through its sheet
- Fixed item piles with both "Is Container" and "Override single item token scale" enabled acting strange - item piles will now prioritize the container images over "Display Single Item Image" when "Is Container" is enabled
- Added warning to point out the above
- Adjusted Item Pile UI to be editable even when not enabled

## Version 1.1.2
- Fixed dropping items onto piles not working when it had an interaction distance of infinite
Expand Down
1 change: 1 addition & 0 deletions languages/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"DisplayOneExplanation": "Besteht der Stapel aus einem einzigen Gegenstand, wird das Bild des Stapels auf das Bild des Gegenstandes gesetzt.",
"OverrideSingleItemScale": "Einzelne Gegenstands-Token-Skalierung außer Kraft setzen",
"SingleItemScale": "Gegenstands-Token-Skalierung",
"DisplayOneContainerWarning": "Achtung! Du hast sowohl die Einstellung \"Einzelnes Artikelbild anzeigen\" als auch \"Ist Container\" aktiviert. In diesem Fall werden Bilder für den Container bevorzugt verwendet.",
"IsContainer": "Ist Container",
"Locked": "Ist verschlossen",
"Closed": "Ist geschlossen",
Expand Down
1 change: 1 addition & 0 deletions languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"DisplayOneExplanation": "If the pile is made up of a single item, this sets the pile token's image to be the image of the item.",
"OverrideSingleItemScale": "Override single item token scale",
"SingleItemScale": "Single item token scale",
"DisplayOneContainerWarning": "Warning! You have both \"Display Single Item Image\" and \"Is Container\" enabled. In this case, the container images takes priority.",
"IsContainer": "Is Container",
"Locked": "Is Locked",
"Closed": "Is Closed",
Expand Down
1 change: 1 addition & 0 deletions languages/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"DisplayOneExplanation": "Si la pile est composée d'un seul objet, l'image du token de la pile sera l'image de l'objet.",
"OverrideSingleItemScale": "Remplacer l'échelle du Token objet unique",
"SingleItemScale": "Échelle du Token",
"DisplayOneContainerWarning": "Attention ! Vous avez activé à la fois les options \"Afficher l'image d'un seul élément\" et \"Est un conteneur\". Dans ce cas, l'images du conteneur est prioritaires.",
"IsContainer": "Est un conteneur",
"Locked": "est verrouillé",
"Closed": "Est fermé",
Expand Down
1 change: 1 addition & 0 deletions languages/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"DisplayOneExplanation": "お宝の中身が単体のアイテムの場合、そのアイテムの画像がお宝の画像そのものになります。",
"OverrideSingleItemScale": "単体アイテムのコマサイズ調整",
"SingleItemScale": "単体アイテムコマサイズ",
"DisplayOneContainerWarning": "警告:\"単体時、アイテム画像を表示\"\"コンテナである\"の両方の設定が有効化されています。この場合、コンテナ画像のほうが優先されますのでご注意ください。",
"IsContainer": "コンテナである",
"Locked": "ロックされている",
"Closed": "閉まっている",
Expand Down
21 changes: 16 additions & 5 deletions scripts/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { itemPileSocket, SOCKET_HANDLERS } from "./socket.js";
import { ItemPileInventory } from "./formapplications/itemPileInventory.js";
import DropDialog from "./formapplications/dropDialog.js";
import { HOOKS } from "./hooks.js";
import { getItemPileTokenImage, getItemPileTokenScale, tokens_close_enough } from "./lib/lib.js";
import { getItemPileAttributes, getItemPileTokenImage, getItemPileTokenScale, tokens_close_enough } from "./lib/lib.js";

export default class API {

Expand Down Expand Up @@ -207,7 +207,7 @@ export default class API {

await pileActor.update({
"token": {
name: "ItemPile",
name: "Item Pile",
actorLink: false,
bar1: { attribute: "" },
vision: false,
Expand All @@ -233,20 +233,31 @@ export default class API {
overrideData['actorData'] = { "items": items || {} };

const pileConfig = lib.getItemPileData(pileActor);
const attributes = getItemPileAttributes(pileActor);

if (items.length === 1 && pileConfig.displayOne) {
overrideData["img"] = items[0].img
const numItems = items.length + attributes.length;

if (pileConfig.displayOne && numItems === 1) {
overrideData["img"] = items.length > 0
? items[0].img
: attributes[0].img;
if (pileConfig.overrideSingleItemScale) {
overrideData["scale"] = pileConfig.singleItemScale;
}
}else{
}

if (pileConfig.isContainer) {

overrideData["img"] = lib.getItemPileTokenImage(pileActor);
overrideData["scale"] = lib.getItemPileTokenScale(pileActor);

}

} else {

overrideData["img"] = lib.getItemPileTokenImage(pileActor);
overrideData["scale"] = lib.getItemPileTokenScale(pileActor);

}

const tokenData = await pileActor.getTokenData(overrideData);
Expand Down
70 changes: 31 additions & 39 deletions scripts/formapplications/itemPileConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,25 @@ export class ItemPileConfig extends FormApplication {
const slider = html.find("#scaleRange");
const input = html.find("#scaleInput");

let firstTime = true;
enabledCheckbox.change(async function () {
let isEnabled = $(this).is(":checked");
if(!firstTime){
const existingData = lib.getItemPileData(self.document);
if(isEnabled && !existingData?.enabled) {
const isLinked = self.document instanceof Actor
? self.document.data.token.actorLink
: self.document.isLinked;
if(isLinked){
const doContinue = await Dialog.confirm({
title: game.i18n.localize("ITEM-PILES.Dialogs.LinkedActorWarning.Title"),
content: lib.dialogWarning(game.i18n.localize("ITEM-PILES.Dialogs.LinkedActorWarning.Content")),
defaultYes: false
});
if (!doContinue) {
isEnabled = false;
$(this).prop("checked", false);
}
const existingData = lib.getItemPileData(self.document);
if(isEnabled && !existingData?.enabled) {
const isLinked = self.document instanceof Actor
? self.document.data.token.actorLink
: self.document.isLinked;
if(isLinked){
const doContinue = await Dialog.confirm({
title: game.i18n.localize("ITEM-PILES.Dialogs.LinkedActorWarning.Title"),
content: lib.dialogWarning(game.i18n.localize("ITEM-PILES.Dialogs.LinkedActorWarning.Content")),
defaultYes: false
});
if (!doContinue) {
$(this).prop("checked", false);
}
}
}
firstTime = false;
html.find('.tab-body').find('input, button, select').not($(this)).each(function () {
$(this).prop('disabled', !isEnabled);
$(this).closest('.form-group').toggleClass("item-pile-disabled", !isEnabled);
});
}).change();
})

scaleCheckbox.change(function () {
let isDisabled = !$(this).is(":checked") || !displayOneCheckbox.is(":checked");
Expand All @@ -100,32 +91,33 @@ export class ItemPileConfig extends FormApplication {
}).change();

displayOneCheckbox.change(function () {
let isDisabled = !$(this).is(":checked");
html.find('.item-pile-display-one-settings').children().each(function () {
$(this).toggleClass("item-pile-disabled", isDisabled);
$(this).find('input, button').prop("disabled", isDisabled);
scaleCheckbox.change();
});
let isEnabled = $(this).is(":checked");
let isScale = scaleCheckbox.is(":checked");
let isContainer = containerCheckbox.is(":checked");

slider.prop('disabled', !isEnabled || !isScale);
input.prop('disabled', !isEnabled || !isScale);
slider.parent().toggleClass("item-pile-disabled", !isEnabled || !isScale);

scaleCheckbox.prop('disabled', !isEnabled);
scaleCheckbox.parent().toggleClass("item-pile-disabled", !isEnabled);

html.find(".display-one-warning").css("display", isEnabled && isContainer ? "block" : "none");
self.setPosition();
}).change();

const containerSettings = html.find('.item-pile-container-settings');
containerCheckbox.change(function () {
let isDisabled = !$(this).is(":checked");
containerSettings.children().each(function () {
$(this).toggleClass("item-pile-disabled", isDisabled);
$(this).find('input, button, select').prop("disabled", isDisabled);
});
let isEnabled = $(this).is(":checked");
let isDisplayOne = displayOneCheckbox.is(":checked");
html.find(".display-one-warning").css("display", isEnabled && isDisplayOne ? "block" : "none");
}).change();

overrideAttributesEnabledCheckbox.change(function () {
let isChecked = $(this).is(":checked");
html.find(".item-pile-config-configure-override-attributes")
.toggleClass("item-pile-disabled", !isChecked)
.prop("disabled", !isChecked);
if (isChecked) {
self.pileData.overrideAttributes = game.settings.get(CONSTANTS.MODULE_NAME, "dynamicAttributes");
}
}).change();
})

html.find(".item-pile-config-configure-override-attributes").click(function () {
self.showAttributeEditor();
Expand Down
51 changes: 23 additions & 28 deletions scripts/formapplications/itemPileInventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class ItemPileInventory extends FormApplication {
const foundItem = self.pile.actor.items.get(id) || lib.getSimilarItem(pileItems, { itemName: name, itemType: type });

let itemQuantity;
let maxQuantity;
let quantity;

itemQuantity = $(this).find('input').val();

Expand All @@ -91,15 +91,15 @@ export class ItemPileInventory extends FormApplication {
name = foundItem.name;
img = foundItem.data.img;
type = getProperty(foundItem.data, API.ITEM_TYPE_ATTRIBUTE);
maxQuantity = Number(getProperty(foundItem.data, API.ITEM_QUANTITY_ATTRIBUTE));
quantity = Number(getProperty(foundItem.data, API.ITEM_QUANTITY_ATTRIBUTE));
}else{
itemQuantity = 0;
maxQuantity = 0;
quantity = 0;
}

const currentQuantity = Math.min(maxQuantity, Math.max(itemQuantity, 1));
const currentQuantity = Math.min(quantity, Math.max(itemQuantity, 1));

self.items.push({ id, name, type, img, currentQuantity, maxQuantity });
self.items.push({ id, name, type, img, currentQuantity, quantity });

});

Expand All @@ -118,7 +118,7 @@ export class ItemPileInventory extends FormApplication {
type: item.type,
img: item.data?.img ?? "",
currentQuantity: 1,
maxQuantity: Number(getProperty(item.data, API.ITEM_QUANTITY_ATTRIBUTE) ?? 1)
quantity: Number(getProperty(item.data, API.ITEM_QUANTITY_ATTRIBUTE) ?? 1)
};
})
}
Expand All @@ -135,11 +135,11 @@ export class ItemPileInventory extends FormApplication {
const img = $(this).find('.item-piles-img').attr('src');

const itemQuantity = $(this).find('input').val();
const maxQuantity = Number(getProperty(self.pile.actor.data, path)) ?? 0;
const quantity = Number(getProperty(self.pile.actor.data, path)) ?? 0;

const currentQuantity = Math.min(maxQuantity, Math.max(itemQuantity, 0));
const currentQuantity = Math.min(quantity, Math.max(itemQuantity, 0));

self.attributes.push({ name, path, img, currentQuantity, maxQuantity });
self.attributes.push({ name, path, img, currentQuantity, quantity });

});

Expand All @@ -149,23 +149,18 @@ export class ItemPileInventory extends FormApplication {

}

validAttribute(attribute) {
return lib.hasNonzeroAttribute(this.pile.actor, attribute) && (!this.recipientActor || hasProperty(this.recipientActor.data, attribute));
}

getPileAttributeData() {
const attributes = API.getItemPileAttributes(this.pile);
return attributes.map(attribute => {
if (!this.validAttribute(attribute.path)) return false;
const localizedName = game.i18n.has(attribute.name) ? game.i18n.localize(attribute.name) : attribute.name;
return {
name: localizedName,
path: attribute.path,
img: attribute.img,
currentQuantity: 1,
maxQuantity: Number(getProperty(this.pile.actor.data, attribute.path) ?? 1)
}
}).filter(Boolean);
return attributes
.filter(attribute => {
return !this.recipientActor || (this.recipientActor && hasProperty(this.recipientActor.data, attribute.path));
})
.map(attribute => {
return {
...attribute,
currentQuantity: 1
}
});
}

_onDrop(event) {
Expand Down Expand Up @@ -276,16 +271,16 @@ export class ItemPileInventory extends FormApplication {
this.saveItems();
this.saveAttributes();
const item = this.pile.actor.items.get(itemId);
const maxQuantity = Number(getProperty(item.data, API.ITEM_QUANTITY_ATTRIBUTE) ?? 1);
const quantity = Math.min(inputQuantity, maxQuantity);
let quantity = Number(getProperty(item.data, API.ITEM_QUANTITY_ATTRIBUTE) ?? 1);
quantity = Math.min(inputQuantity, quantity);
await API.transferItems(this.pile, this.recipient, [{ _id: itemId, quantity }]);
}

async takeAttribute(attribute, inputQuantity) {
this.saveItems();
this.saveAttributes();
const maxQuantity = Number(getProperty(this.pile.actor.data, attribute) ?? 0);
const quantity = Math.min(inputQuantity, maxQuantity);
let quantity = Number(getProperty(this.pile.actor.data, attribute) ?? 0);
quantity = Math.min(inputQuantity, quantity);
await API.transferAttributes(this.pile, this.recipient, { [attribute]: quantity });
}

Expand Down
Loading

0 comments on commit 1eb1329

Please sign in to comment.