Skip to content

Commit

Permalink
Fixed names being appended twice, defaults are now not applied when e…
Browse files Browse the repository at this point in the history
…diting, added PinCushion text setting
  • Loading branch information
ClipplerBlood committed Jan 31, 2023
1 parent f1bd841 commit 2ab562e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 27 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Player Pin Defaults

Summary: This mod improves the defaults of player-made pins in Foundry, making it easier for players to document maps such as dungeons.
Summary: This mod improves the defaults of player-made pins in Foundry, making it easier for players to document maps
such as dungeons.
6 changes: 4 additions & 2 deletions src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"fontSize": "Font Size",
"anchorPoint": "Text Anchor Point",
"addPlayerName": "Player Name added to Text",
"playerColorText": "Player Color as Text Color"
"playerColorText": "Player Color as Text Color",
"alwaysShowText": "Text always visible"
},
"hints": {
"global": "When on, Globally Visited option is automatically applied, so all players can see this player’s notes.",
Expand All @@ -19,7 +20,8 @@
"imageSize": "Default 100",
"fontSize": "Default 32",
"addPlayerName": "When on, the format of the pin label is \"<pinlabel> /n <name of character>\" on two lines.",
"playerColorText": "When on, the player’s color is used to tint the text color"
"playerColorText": "When on, the player’s color is used to tint the text color",
"alwaysShowText": "[Requires Pin Cushion] When on, player notes text will default to \"text always visible\""
}
}
}
76 changes: 52 additions & 24 deletions src/module/player-pin-defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,84 @@ import { registerSettings } from './settings.js';

/* global libWrapper */
const modSetting = (x) => game.settings.get('player-pin-defaults', x);
export const pinCushionInstalled = () => game.modules.get('pin-cushion') != null;

Hooks.once('init', async () => {
console.log('player-pin-defaults | Initializing player-pin-defaults');
registerSettings();
libWrapper.register('player-pin-defaults', 'NoteConfig.prototype.getData', getNoteConfigData);

/*
getData wrapper.
Here we override with the custom defaults what is presented to the player in the NoteConfig.
Won't be used if GM or if the defaults have already been applied
*/
libWrapper.register('player-pin-defaults', 'NoteConfig.prototype.getData', function (wrapped, ...args) {
let noteData = wrapped(...args);

// Show only the original text, without the name
const originalText = this.document.flags['player-pin-defaults']?.originalText;
if (originalText) noteData.data.text = originalText;

const isDefaulted = this.document.flags['player-pin-defaults']?.isDefaulted;
if (game.user.isGM || isDefaulted) return noteData;
console.log(noteData);
// Apply the defaults
const defaults = getPinDefaults();
noteData = mergeObject(noteData, defaults);

return noteData;
});

/*
getSubmitData wrapper.
Here we perform operations after the note has been submitted. Operations include:
- Adding the character name
- Store the text before adding the name
- Setting a flag to indicate that the new defaults have been applied
*/
libWrapper.register('player-pin-defaults', 'NoteConfig.prototype._getSubmitData', function (wrapped, ...args) {
const data = wrapped(...args);
if (modSetting('addPlayerName') && !game.user.isGM) {
data.text += `\n${game.user.character?.name ?? game.user.name}`;

// Append name
if (modSetting('addPlayerName')) {
const characterName =
this.document.flags['player-pin-defaults']?.characterName ?? game.user.character?.name ?? game.user.name;
data['flags.player-pin-defaults.originalText'] = data.text;
data['flags.player-pin-defaults.characterName'] = characterName;
data.text += `\n${characterName}`;
}

// Set flags
if (game.user.isGM || this.document.flags['player-pin-defaults']?.isDefaulted) return data;
data['flags.player-pin-defaults.isDefaulted'] = true;
if (!game.user.isGM && modSetting('alwaysShowText') && pinCushionInstalled()) {
data['flags.pin-cushion.textAlwaysVisible'] = true;
}
return data;
});
});

/**
* Wrapper for NoteConfig.getData
* Data returned is overridden by the module's
* The wrapped function returns an object (noteData) which contains a .data field, which contains what we want to modify
* So we override noteData.data
* @param wrapped
* @param args
* @return {*}
*/
function getNoteConfigData(wrapped, ...args) {
let noteData = wrapped(...args);
if (game.user.isGM) return noteData;

const defaults = getPinDefaults();
// console.log(noteData);
noteData = mergeObject(noteData, defaults);
// console.log(noteData);
return noteData;
}

/**
* Returns the object containing the defaults used for overriding the getData in NoteConfig
*/
function getPinDefaults() {
// Grab data from user
const playerColor = game.user.color;
const tokenImg = game.user.character.prototypeToken?.texture.src;

// Icon (token or default)
const usePlayerToken = modSetting('playerToken') && tokenImg?.length > 0;
const defaultImage = modSetting('pinImage');

let customIcon = null;
if (usePlayerToken) customIcon = tokenImg;
else if (defaultImage?.length > 0) customIcon = defaultImage;

// Tint
const usePlayerColorTint = modSetting('playerColorImage');
let tintIcon = null;
if (usePlayerColorTint && !usePlayerToken) tintIcon = playerColor;

// Returned object
let defaults = {
data: {
global: modSetting('global'),
Expand All @@ -70,6 +97,7 @@ function getPinDefaults() {
},
};

// Remove nulls & return
defaults = flattenObject(defaults);
// eslint-disable-next-line no-unused-vars
defaults = Object.fromEntries(Object.entries(defaults).filter(([_, v]) => v != null));
Expand Down
13 changes: 13 additions & 0 deletions src/module/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// SPDX-License-Identifier: MIT

import { pinCushionInstalled } from './player-pin-defaults.js';

export function registerSettings() {
game.settings.register('player-pin-defaults', 'global', {
name: 'player-pin-defaults.settings.names.global',
Expand Down Expand Up @@ -90,4 +92,15 @@ export function registerSettings() {
type: Boolean,
default: false,
});

if (pinCushionInstalled()) {
game.settings.register('player-pin-defaults', 'alwaysShowText', {
name: 'player-pin-defaults.settings.names.alwaysShowText',
hint: 'player-pin-defaults.settings.hints.alwaysShowText',
scope: 'world',
config: true,
type: Boolean,
default: false,
});
}
}

0 comments on commit 2ab562e

Please sign in to comment.