fvtt-pegasus-rpg/modules/pegasus-item-sheet.js

488 lines
17 KiB
JavaScript
Raw Normal View History

2021-12-02 07:38:59 +01:00
import { PegasusUtility } from "./pegasus-utility.js";
/**
* Extend the basic ItemSheet with some very simple modifications
* @extends {ItemSheet}
*/
export class PegasusItemSheet extends ItemSheet {
/** @override */
static get defaultOptions() {
return mergeObject(super.defaultOptions, {
classes: ["fvtt-pegasus-rpg", "sheet", "item"],
template: "systems/fvtt-pegasus-rpg/templates/item-sheet.html",
dragDrop: [{dragSelector: null, dropSelector: null}],
width: 620,
height: 550
//tabs: [{navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description"}]
});
}
/* -------------------------------------------- */
_getHeaderButtons() {
let buttons = super._getHeaderButtons();
// Add "Post to chat" button
// We previously restricted this to GM and editable items only. If you ever find this comment because it broke something: eh, sorry!
buttons.unshift(
{
class: "post",
icon: "fas fa-comment",
onclick: ev => {}
})
return buttons
}
/* -------------------------------------------- */
/** @override */
setPosition(options={}) {
const position = super.setPosition(options);
const sheetBody = this.element.find(".sheet-body");
const bodyHeight = position.height - 192;
sheetBody.css("height", bodyHeight);
if ( this.item.type.includes('weapon')) {
position.width = 640;
}
return position;
}
/* -------------------------------------------- */
async getData() {
const objectData = PegasusUtility.data(this.object);
let itemData = foundry.utils.deepClone(PegasusUtility.templateData(this.object));
let formData = {
title: this.title,
id: this.id,
type: objectData.type,
img: objectData.img,
name: objectData.name,
editable: this.isEditable,
cssClass: this.isEditable ? "editable" : "locked",
2021-12-02 20:18:21 +01:00
optionsDiceList: PegasusUtility.getOptionsDiceList(),
2021-12-05 20:36:34 +01:00
optionsStatusList: PegasusUtility.getOptionsStatusList(),
2021-12-02 07:38:59 +01:00
data: itemData,
limited: this.object.limited,
options: this.options,
owner: this.document.isOwner,
2022-01-12 16:25:55 +01:00
mr: (this.object.type == 'specialisation'),
2021-12-02 07:38:59 +01:00
isGM: game.user.isGM
}
this.options.editable = !(this.object.data.origin == "embeddedItem");
console.log("ITEM DATA", formData, this);
return formData;
}
/* -------------------------------------------- */
_getHeaderButtons() {
let buttons = super._getHeaderButtons();
buttons.unshift({
class: "post",
icon: "fas fa-comment",
onclick: ev => this.postItem()
});
return buttons
}
/* -------------------------------------------- */
postItem() {
let chatData = duplicate(PegasusUtility.data(this.item));
if (this.actor) {
chatData.actor = { id: this.actor.id };
}
// Don't post any image for the item (which would leave a large gap) if the default image is used
if (chatData.img.includes("/blank.png")) {
chatData.img = null;
}
// JSON object for easy creation
chatData.jsondata = JSON.stringify(
{
compendium: "postedItem",
payload: chatData,
});
renderTemplate('systems/fvtt-pegasus-rpg/templates/post-item.html', chatData).then(html => {
2022-02-10 19:03:09 +01:00
let chatOptions = PegasusUtility.chatDataSetup(html);
2021-12-02 07:38:59 +01:00
ChatMessage.create(chatOptions)
});
}
2021-12-08 13:53:22 +01:00
/* -------------------------------------------- */
async viewSubitem(ev) {
let field = $(ev.currentTarget).data('type');
let idx = Number($(ev.currentTarget).data('index'));
let itemData = this.object.data.data[field][idx];
if ( itemData.name != 'None') {
let spec = await Item.create(itemData, {temporary: true});
spec.data.origin = "embeddedItem";
new PegasusItemSheet(spec).render(true);
}
}
/* -------------------------------------------- */
async deleteSubitem(ev) {
let field = $(ev.currentTarget).data('type');
let idx = Number($(ev.currentTarget).data('index'));
let oldArray = this.object.data.data[field];
let itemData = this.object.data.data[field][idx];
if ( itemData.name != 'None') {
let newArray = [];
for( var i = 0; i < oldArray.length; i++) {
if ( i != idx) {
newArray.push( oldArray[i]);
}
}
this.object.update( { [`data.${field}`]: newArray} );
}
}
/* -------------------------------------------- */
async manageSpec( ) {
let itemData = this.object.data.data.specialisation[0];
if ( itemData.name != 'None') {
let spec = await Item.create(itemData, {temporary: true});
spec.data.origin = "embeddedItem";
new PegasusItemSheet(spec).render(true);
}
}
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
/** @override */
activateListeners(html) {
super.activateListeners(html);
// Everything below here is only needed if the sheet is editable
if (!this.options.editable) return;
// Update Inventory Item
html.find('.item-edit').click(ev => {
const li = $(ev.currentTarget).parents(".item");
const item = this.object.options.actor.getOwnedItem(li.data("item-id"));
item.sheet.render(true);
});
2021-12-08 13:53:22 +01:00
html.find('.delete-spec').click( ev => {
this.object.update( {"data.specialisation": [ { name: 'None'} ]} );
});
html.find('.delete-subitem').click( ev => {
this.deleteSubitem( ev );
});
html.find('.stat-choice-flag').click( ev => {
let idx = $(ev.currentTarget).data("stat-idx");
let array = duplicate(this.object.data.data.statincreasechoice);
array[Number(idx)].flag = !array[Number(idx)].flag;
this.object.update( {"data.statincreasechoice": array} );
});
2021-12-02 07:38:59 +01:00
// Update Inventory Item
html.find('.item-delete').click(ev => {
const li = $(ev.currentTarget).parents(".item");
let itemId = li.data("item-id");
let itemType = li.data("item-type");
});
2021-12-08 13:53:22 +01:00
html.find('.view-subitem').click(ev => {
this.viewSubitem( ev );
2021-12-02 07:38:59 +01:00
});
2021-12-08 13:53:22 +01:00
html.find('.view-spec').click(ev => {
this.manageSpec( );
2021-12-02 07:38:59 +01:00
});
}
2021-12-09 18:40:50 +01:00
/* -------------------------------------------- */
2022-01-07 20:40:40 +01:00
async addAbility(event, item, dataItem) {
2021-12-09 18:40:50 +01:00
let newItem = duplicate(item.data);
newItem._id = randomID( dataItem.id.length );
2022-01-07 20:40:40 +01:00
console.log("ABB", event, item, dataItem)
if ( event.toElement.className == 'drop-abilities') {
let abilityArray = duplicate(this.object.data.data.abilities);
abilityArray.push( newItem);
await this.object.update( { 'data.abilities': abilityArray} );
}
if ( event.toElement.className == 'drop-optionnal-abilities') {
let abilityArray = duplicate(this.object.data.data.optionnalabilities);
abilityArray.push( newItem);
await this.object.update( { 'data.optionnalabilities': abilityArray} );
}
2021-12-09 18:40:50 +01:00
}
2022-01-07 20:40:40 +01:00
2022-01-08 18:28:01 +01:00
/* -------------------------------------------- */
async addRacePerk(event, item, dataItem) {
let newItem = duplicate(item.data);
newItem._id = randomID( dataItem.id.length );
if ( event.toElement.className == 'drop-race-perk') {
let perkArray = duplicate(this.object.data.data.perks);
perkArray.push( newItem);
await this.object.update( { 'data.perks': perkArray} );
}
}
2021-12-08 13:53:22 +01:00
/* -------------------------------------------- */
async addSpecialisation(item, dataItem) {
let newItem = duplicate(item.data);
newItem._id = randomID( dataItem.id.length );
let specArray = [ newItem ];
await this.object.update( { 'data.specialisation': specArray} );
}
/* -------------------------------------------- */
async addRoleSpecialisation(event, item, dataItem) {
let newItem = duplicate(item.data);
newItem._id = randomID( dataItem.id.length );
console.log("Add spec", event, newItem);
if ( event.toElement.className == 'drop-spec1') {
let specArray = duplicate(this.object.data.data.specialisationsplus1);
specArray.push( newItem );
await this.object.update( { 'data.specialisationsplus1': specArray} );
}
if ( event.toElement.className == 'drop-spec2') {
let specArray = duplicate(this.object.data.data.specincrease);
specArray.push( newItem );
await this.object.update( { 'data.specincrease': specArray} );
}
}
/* -------------------------------------------- */
async addRolePerk(event, item, dataItem) {
let newItem = duplicate(item.data);
newItem._id = randomID( dataItem.id.length );
console.log("Add spec", event, newItem);
if ( event.toElement.className == 'drop-perk2') {
let perkArray = duplicate(this.object.data.data.perks);
perkArray.push( newItem );
await this.object.update( { 'data.perks': perkArray} );
2021-12-09 18:40:50 +01:00
}
if ( event.toElement.className == 'drop-specialperk1') {
let perkArray = duplicate(this.object.data.data.specialperk);
perkArray.push( newItem );
await this.object.update( { 'data.specialperk': perkArray} );
2021-12-08 13:53:22 +01:00
}
}
2021-12-16 21:41:09 +01:00
/* -------------------------------------------- */
async addPower( event, item, dataItem) {
let newItem = duplicate(item.data);
newItem._id = randomID( dataItem.id.length );
if ( event.toElement.className == 'drop-spec-power') {
let powArray = duplicate(this.object.data.data.powers);
powArray.push( newItem );
await this.object.update( { 'data.powers': powArray} );
}
}
/* -------------------------------------------- */
async addAbilityPower( event, item, dataItem) {
let newItem = duplicate(item.data);
newItem._id = randomID( dataItem.id.length );
if ( event.toElement.className == 'drop-ability-power') {
let powArray = duplicate(this.object.data.data.powersgained);
powArray.push( newItem );
await this.object.update( { 'data.powersgained': powArray} );
}
}
2022-02-10 15:53:42 +01:00
/* -------------------------------------------- */
async addAbilityEffect( event, item, dataItem) {
let newItem = duplicate(item.data);
newItem._id = randomID( dataItem.id.length );
if ( event.toElement.className == 'drop-ability-effect') {
let powArray = duplicate(this.object.data.data.effectsgained);
powArray.push( newItem );
await this.object.update( { 'data.effectsgained': powArray} );
}
}
2022-01-08 18:28:01 +01:00
2021-12-16 21:41:09 +01:00
/* -------------------------------------------- */
async addAbilitySpec( event, item, dataItem) {
let newItem = duplicate(item.data);
newItem._id = randomID( dataItem.id.length );
if ( event.toElement.className == 'drop-ability-spec') {
let powArray = duplicate(this.object.data.data.specialisations);
powArray.push( newItem );
await this.object.update( { 'data.specialisations': powArray} );
}
}
2022-01-08 18:28:01 +01:00
/* -------------------------------------------- */
async addAbilityWeaponArmor( event, item, dataItem) {
let newItem = duplicate(item.data);
newItem._id = randomID( dataItem.id.length );
if ( event.toElement.className == 'drop-ability-weapon') {
let weaponArray = duplicate(this.object.data.data.attackgained);
weaponArray.push( newItem );
await this.object.update( { 'data.attackgained': weaponArray} );
}
if ( event.toElement.className == 'drop-ability-armor') {
let armorArray = duplicate(this.object.data.data.armorgained);
armorArray.push( newItem );
await this.object.update( { 'data.armorgained': armorArray} );
}
}
2022-02-10 19:03:09 +01:00
2021-12-19 15:35:54 +01:00
/* -------------------------------------------- */
async addPerkSpecialisation( event, item, dataItem) {
let newItem = duplicate(item.data);
2021-12-23 15:31:56 +01:00
if ( event.toElement.className == 'drop-spec-perk') {
//console.log("PER SPEC", event)
let key = event.toElement.dataset["key"];
if ( key == 'affectedspec') {
await this.object.update( { 'data.features.affectedspec.value': newItem.name} );
} else {
await this.object.update( { 'data.features.gainspecdice.value': newItem.name} );
}
}
2021-12-19 15:35:54 +01:00
}
2022-01-12 16:25:55 +01:00
2022-02-10 19:03:09 +01:00
/* -------------------------------------------- */
async addPerkEffect( event, item, dataItem) {
let newItem = duplicate(item.data)
if ( event.toElement.className == 'drop-perk-effect') {
let effectArray = duplicate(this.object.data.data.effectsgained)
effectArray.push( newItem )
await this.object.update( { 'data.effectsgained': effectArray} )
}
}
2022-02-10 21:58:19 +01:00
/* -------------------------------------------- */
async addEffectPower( event, item, dataItem) {
let newItem = duplicate(item.data)
if ( event.toElement.className == 'drop-power-effect') {
let effectArray = duplicate(this.object.data.data.effectsgained)
effectArray.push( newItem );
await this.object.update( { 'data.effectsgained': effectArray} )
}
}
2022-01-12 16:25:55 +01:00
/* -------------------------------------------- */
async addEffectSpec( event, item, dataItem) {
let newItem = duplicate(item.data);
if ( event.toElement.className == 'drop-effect-spec') {
let specArray = duplicate(this.object.data.data.recoveryrollspec);
specArray.push( newItem );
await this.object.update( { 'data.recoveryrollspec': specArray} );
}
2022-01-12 17:21:37 +01:00
if ( event.toElement.className =='drop-effect-specaffected') {
let specArray = duplicate(this.object.data.data.specaffected);
specArray.push( newItem );
await this.object.update( { 'data.specaffected': specArray} );
}
2022-01-12 16:25:55 +01:00
}
2021-12-19 15:35:54 +01:00
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
async _onDrop(event) {
2022-02-10 21:58:19 +01:00
if (this.object.type == 'power' ) {
let data = event.dataTransfer.getData('text/plain');
if (data) {
let dataItem = JSON.parse( data );
let item = await PegasusUtility.searchItem( dataItem);
if ( item.data.type == 'effect') {
return this.addEffectPower( event, item, dataItem);
}
}
}
2022-01-12 16:25:55 +01:00
if (this.object.type == 'effect' ) {
let data = event.dataTransfer.getData('text/plain');
if (data) {
let dataItem = JSON.parse( data );
2022-01-30 09:44:37 +01:00
let item = await PegasusUtility.searchItem( dataItem);
2022-01-12 16:25:55 +01:00
if ( item.data.type == 'specialisation') {
return this.addEffectSpec( event, item, dataItem);
}
}
}
2022-01-07 20:40:40 +01:00
if (this.object.type == 'race' ) {
2021-12-09 18:40:50 +01:00
let data = event.dataTransfer.getData('text/plain');
if (data) {
let dataItem = JSON.parse( data );
2022-01-30 09:44:37 +01:00
let item = await PegasusUtility.searchItem( dataItem);
2021-12-09 18:40:50 +01:00
if ( item.data.type == 'ability') {
2022-01-07 20:40:40 +01:00
return this.addAbility( event, item, dataItem);
2021-12-09 18:40:50 +01:00
}
2022-01-08 18:28:01 +01:00
if ( item.data.type == 'perk') {
return this.addRacePerk( event, item, dataItem);
}
2021-12-09 18:40:50 +01:00
}
}
2021-12-19 15:35:54 +01:00
if (this.object.type == 'perk' ) {
2022-02-10 19:03:09 +01:00
let data = event.dataTransfer.getData('text/plain')
2021-12-19 15:35:54 +01:00
if (data) {
let dataItem = JSON.parse( data );
2022-02-10 19:03:09 +01:00
let item = await PegasusUtility.searchItem( dataItem)
2021-12-19 15:35:54 +01:00
if ( item.data.type == 'specialisation') {
2022-02-10 19:03:09 +01:00
return this.addPerkSpecialisation( event, item, dataItem)
2021-12-19 15:35:54 +01:00
}
2022-02-10 19:03:09 +01:00
if ( item.data.type == 'effect') {
return this.addPerkEffect( event, item, dataItem);
}
2021-12-19 15:35:54 +01:00
}
}
2021-12-16 21:41:09 +01:00
if (this.object.type == 'specialisation' ) {
let data = event.dataTransfer.getData('text/plain');
if (data) {
let dataItem = JSON.parse( data );
2022-01-30 09:44:37 +01:00
let item = await PegasusUtility.searchItem( dataItem);
2021-12-16 21:41:09 +01:00
if ( item.data.type == 'power') {
return this.addPower( event, item, dataItem);
}
}
}
if (this.object.type == 'ability' ) {
let data = event.dataTransfer.getData('text/plain');
if (data) {
let dataItem = JSON.parse( data );
2022-01-30 09:44:37 +01:00
let item = await PegasusUtility.searchItem( dataItem);
2022-02-10 15:53:42 +01:00
if ( item.data.type == 'effect') {
return this.addAbilityEffect( event, item, dataItem);
}
2021-12-16 21:41:09 +01:00
if ( item.data.type == 'power') {
return this.addAbilityPower( event, item, dataItem);
}
if ( item.data.type == 'specialisation') {
return this.addAbilitySpec( event, item, dataItem);
}
2022-01-08 18:28:01 +01:00
if ( item.data.type == 'weapon' || item.data.type == 'armor') {
return this.addAbilityWeaponArmor( event, item, dataItem);
}
2021-12-16 21:41:09 +01:00
}
}
2021-12-09 18:40:50 +01:00
2021-12-08 13:53:22 +01:00
if (this.object.type == 'role' ) {
let data = event.dataTransfer.getData('text/plain');
if (data) {
let dataItem = JSON.parse( data );
2022-01-30 09:44:37 +01:00
let item = await PegasusUtility.searchItem( dataItem);
2021-12-08 13:53:22 +01:00
if ( item.data.type == 'specialisation') {
return this.addRoleSpecialisation( event, item, dataItem);
}
if ( item.data.type == 'perk') {
return this.addRolePerk( event, item, dataItem);
}
}
}
2021-12-02 07:38:59 +01:00
2021-12-08 13:53:22 +01:00
ui.notifications.warn("This item can not be dropped over another item");
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
get template() {
let type = this.item.type;
return `systems/fvtt-pegasus-rpg/templates/item-${type}-sheet.html`;
}
/* -------------------------------------------- */
/** @override */
_updateObject(event, formData) {
return this.object.update(formData);
}
}