diff --git a/README.md b/README.md index c352af5..08e5766 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/lang/en.json b/src/lang/en.json index f505393..7caa45b 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -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.", @@ -19,7 +20,8 @@ "imageSize": "Default 100", "fontSize": "Default 32", "addPlayerName": "When on, the format of the pin label is \" /n \" 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\"" } } } diff --git a/src/module/player-pin-defaults.js b/src/module/player-pin-defaults.js index 7282933..07abbb7 100644 --- a/src/module/player-pin-defaults.js +++ b/src/module/player-pin-defaults.js @@ -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'), @@ -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)); diff --git a/src/module/settings.js b/src/module/settings.js index 0e3866d..370f734 100644 --- a/src/module/settings.js +++ b/src/module/settings.js @@ -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', @@ -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, + }); + } }