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

376 lines
12 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,
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() {
console.log(this.item);
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 => {
let chatOptions = WotGUtility.chatDataSetup(html);
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-08 13:53:22 +01:00
/* -------------------------------------------- */
async searchItem( dataItem) {
let item;
if (dataItem.pack) {
2021-12-09 18:40:50 +01:00
item = await fromUuid("Compendium."+dataItem.pack+"."+dataItem.id);
2021-12-08 13:53:22 +01:00
} else {
item = game.items.get(dataItem.id )
}
return item;
}
2021-12-09 18:40:50 +01:00
/* -------------------------------------------- */
async addAbility(item, dataItem) {
let abilityArray = duplicate(this.object.data.data.abilities);
let newItem = duplicate(item.data);
newItem._id = randomID( dataItem.id.length );
abilityArray.push( newItem);
await this.object.update( { 'data.abilities': abilityArray} );
}
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} );
}
}
/* -------------------------------------------- */
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} );
}
}
2021-12-19 15:35:54 +01:00
/* -------------------------------------------- */
async addPerkSpecialisation( event, item, dataItem) {
let newItem = duplicate(item.data);
await this.object.update( { 'data.features.affectedspec.value': newItem.name} );
}
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
async _onDrop(event) {
2021-12-09 18:40:50 +01:00
//console.log(event);
if (this.object.type == 'race' ) {
let data = event.dataTransfer.getData('text/plain');
if (data) {
let dataItem = JSON.parse( data );
let item = await this.searchItem( dataItem);
if ( item.data.type == 'ability') {
return this.addAbility( item, dataItem);
}
}
}
2021-12-19 15:35:54 +01:00
if (this.object.type == 'perk' ) {
let data = event.dataTransfer.getData('text/plain');
if (data) {
let dataItem = JSON.parse( data );
let item = await this.searchItem( dataItem);
if ( item.data.type == 'specialisation') {
return this.addPerkSpecialisation( event, item, dataItem);
}
}
}
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 );
let item = await this.searchItem( dataItem);
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 );
let item = await this.searchItem( dataItem);
if ( item.data.type == 'power') {
return this.addAbilityPower( event, item, dataItem);
}
if ( item.data.type == 'specialisation') {
return this.addAbilitySpec( event, item, dataItem);
}
}
}
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 );
let item = await this.searchItem( dataItem);
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);
}
}