mirror of
https://github.com/Foundryborne/daggerheart.git
synced 2026-01-12 03:31:07 +01:00
Initial commit
This commit is contained in:
commit
aa4021d1a2
163 changed files with 26530 additions and 0 deletions
105
module/helpers/handlebarsHelper.mjs
Normal file
105
module/helpers/handlebarsHelper.mjs
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
import { getWidthOfText } from "./utils.mjs";
|
||||
|
||||
export default class RegisterHandlebarsHelpers {
|
||||
static registerHelpers(){
|
||||
Handlebars.registerHelper({
|
||||
looseEq: this.looseEq,
|
||||
times: this.times,
|
||||
join: this.join,
|
||||
add: this.add,
|
||||
subtract: this.subtract,
|
||||
objectSelector: this.objectSelector,
|
||||
includes: this.includes,
|
||||
simpleEditor: this.simpleEditor,
|
||||
debug: this.debug,
|
||||
});
|
||||
};
|
||||
|
||||
static looseEq(a, b){
|
||||
return a == b;
|
||||
}
|
||||
|
||||
static times(nr, block){
|
||||
var accum = '';
|
||||
for(var i = 0; i < nr; ++i)
|
||||
accum += block.fn(i);
|
||||
return accum;
|
||||
}
|
||||
|
||||
static join(...options){
|
||||
return options.slice(0, options.length-1);
|
||||
}
|
||||
|
||||
static add(a, b){
|
||||
const aNum = Number.parseInt(a);
|
||||
const bNum = Number.parseInt(b);
|
||||
return (Number.isNaN(aNum) ? 0 : aNum) + (Number.isNaN(bNum) ? 0 : bNum);
|
||||
}
|
||||
|
||||
static subtract(a, b){
|
||||
const aNum = Number.parseInt(a);
|
||||
const bNum = Number.parseInt(b);
|
||||
return (Number.isNaN(aNum) ? 0 : aNum) - (Number.isNaN(bNum) ? 0 : bNum);
|
||||
}
|
||||
|
||||
static objectSelector(options){
|
||||
let { title, values, titleFontSize, ids, style } = options.hash;
|
||||
|
||||
const titleLength = getWidthOfText(title, titleFontSize, true, true);
|
||||
const margins = 12;
|
||||
|
||||
const buttons = options.fn();
|
||||
const nrButtons = Math.max($(buttons).length-1, 1);
|
||||
const iconWidth = 26;
|
||||
|
||||
const texts = values.reduce((acc, x, index) => {
|
||||
if(x){
|
||||
acc.push(`<span class="object-select-item" data-action="viewObject" data-value="${ids[index]}">${x}</span>`);
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, []).join(' ');
|
||||
|
||||
const html =
|
||||
`<div ${style ? 'style="'+style+'"' : ''}">
|
||||
<div class="object-select-display iconbar">
|
||||
<span class="object-select-title">${title}</span>
|
||||
<div class="object-select-text" style="padding-left: ${titleLength+margins}px; padding-right: ${nrButtons*iconWidth}px;">
|
||||
${texts}
|
||||
</div>
|
||||
${buttons}
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
return new Handlebars.SafeString(html);
|
||||
}
|
||||
|
||||
static rangePicker(options) {
|
||||
let {name, value, min, max, step} = options.hash;
|
||||
name = name || "range";
|
||||
value = value ?? "";
|
||||
if ( Number.isNaN(value) ) value = "";
|
||||
const html =
|
||||
`<input type="range" name="${name}" value="${value}" min="${min}" max="${max}" step="${step}"/>
|
||||
<span class="range-value">${value}</span>`;
|
||||
return new Handlebars.SafeString(html);
|
||||
}
|
||||
|
||||
static includes(list, item){
|
||||
return list.includes(item);
|
||||
}
|
||||
|
||||
static simpleEditor(content, options) {
|
||||
const { target, editable=true, button, engine="tinymce", collaborate=false, class: cssClass } = options.hash;
|
||||
const config = {name: target, value: content, button, collaborate, editable, engine};
|
||||
const element = foundry.applications.fields.createEditorInput(config);
|
||||
if ( cssClass ) element.querySelector(".editor-content").classList.add(cssClass);
|
||||
return new Handlebars.SafeString(element.outerHTML);
|
||||
}
|
||||
|
||||
static debug(a) {
|
||||
console.log(JSON.stringify(a));
|
||||
return a;
|
||||
}
|
||||
}
|
||||
20
module/helpers/socket.mjs
Normal file
20
module/helpers/socket.mjs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
export function handleSocketEvent({action=null, data={}}={}) {
|
||||
switch (action) {
|
||||
case socketEvent.GMUpdate:
|
||||
Hooks.callAll(socketEvent.GMUpdate, data.action, data.uuid, data.update);
|
||||
break;
|
||||
case socketEvent.DhpFearUpdate:
|
||||
Hooks.callAll(socketEvent.DhpFearUpdate);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
export const socketEvent = {
|
||||
GMUpdate: "DhpGMUpdate",
|
||||
DhpFearUpdate: "DhpFearUpdate",
|
||||
};
|
||||
|
||||
export const GMUpdateEvent = {
|
||||
UpdateDocument: "DhpGMUpdateDocument",
|
||||
UpdateFear: "DhpUpdateFear"
|
||||
};
|
||||
82
module/helpers/utils.mjs
Normal file
82
module/helpers/utils.mjs
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
export const loadCompendiumOptions = async (compendiums) => {
|
||||
const compendiumValues = [];
|
||||
|
||||
for(var compendium of compendiums){
|
||||
const values = await getCompendiumOptions(compendium);
|
||||
compendiumValues.push(values);
|
||||
}
|
||||
|
||||
return compendiumValues;
|
||||
};
|
||||
|
||||
const getCompendiumOptions = async (compendium) => {
|
||||
const compendiumPack = await game.packs.get(compendium);
|
||||
|
||||
const values = [];
|
||||
for(var value of compendiumPack.index){
|
||||
const document = await compendiumPack.getDocument(value._id);
|
||||
values.push(document);
|
||||
}
|
||||
|
||||
return values;
|
||||
};
|
||||
|
||||
export const getWidthOfText = (txt, fontsize, allCaps, bold) => {
|
||||
// if(getWidthOfText.e === undefined){
|
||||
// getWidthOfText.e = document.createElement('span');
|
||||
// getWidthOfText.e.style.display = "none";
|
||||
// document.body.appendChild(getWidthOfText.e);
|
||||
// }
|
||||
// if(getWidthOfText.e.style.fontSize !== fontsize)
|
||||
// getWidthOfText.e.style.fontSize = fontsize;
|
||||
// if(getWidthOfText.e.style.fontFamily !== 'Signika, sans-serif')
|
||||
// getWidthOfText.e.style.fontFamily = 'Signika, sans-serif';
|
||||
// getWidthOfText.e.innerText = txt;
|
||||
// return getWidthOfText.e.offsetWidth;
|
||||
const text = allCaps ? txt.toUpperCase() : txt;
|
||||
if(getWidthOfText.c === undefined){
|
||||
getWidthOfText.c=document.createElement('canvas');
|
||||
getWidthOfText.ctx=getWidthOfText.c.getContext('2d');
|
||||
}
|
||||
var fontspec = `${bold ? 'bold': ''} ${fontsize}px` + ' ' + 'Signika, sans-serif';
|
||||
if(getWidthOfText.ctx.font !== fontspec)
|
||||
getWidthOfText.ctx.font = fontspec;
|
||||
|
||||
return getWidthOfText.ctx.measureText(text).width;
|
||||
}
|
||||
|
||||
export const padArray = (arr, len, fill) => {
|
||||
return arr.concat(Array(len).fill(fill)).slice(0,len);
|
||||
}
|
||||
|
||||
export const getTier = (level, asNr) => {
|
||||
switch(Math.floor((level+1)/3)){
|
||||
case 1:
|
||||
return asNr ? 1 : 'tier1';
|
||||
case 2:
|
||||
return asNr ? 2 : 'tier2';
|
||||
case 3:
|
||||
return asNr ? 3 : 'tier3';
|
||||
default:
|
||||
return asNr ? 0 : 'tier0';
|
||||
}
|
||||
}
|
||||
|
||||
export const capitalize = (string) => {
|
||||
return string.charAt(0).toUpperCase() + string.slice(1);
|
||||
}
|
||||
|
||||
export const getPathValue = (path, entity, numeric) => {
|
||||
const pathValue = foundry.utils.getProperty(entity, path);
|
||||
if(pathValue) return numeric ? Number.parseInt(pathValue) : pathValue;
|
||||
|
||||
return numeric ? Number.parseInt(path) : path;
|
||||
};
|
||||
|
||||
export const generateId = (title, length) => {
|
||||
const id = title.split(" ").map((w, i) => {
|
||||
const p = w.slugify({replacement: "", strict: true});
|
||||
return i ? p.titleCase() : p;
|
||||
}).join("");
|
||||
return Number.isNumeric(length) ? id.slice(0, length).padEnd(length, "0") : id;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue