forked from orffen/basicfantasyrpg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
work in progress on moving saves customisation into a settings menu s…
…o I can have validation and better control orffen#69
- Loading branch information
Showing
5 changed files
with
158 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// not the greatest approach, but | ||
export function objectsShallowEqual (obj1, obj2) { | ||
const entries1 = Object.entries(obj1) | ||
const entries2 = Object.entries(obj2) | ||
|
||
if (entries1.length !== entries2.length) { | ||
return false | ||
} | ||
|
||
for (let [key, value] of entries1) { | ||
if (obj2[key] !== value) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// import { Helpers } from './helpers.mjs' | ||
import { objectsShallowEqual } from '../helpers/settings.mjs' | ||
import { SYSTEM_ID, SETTINGS } from './settings.mjs' | ||
|
||
// Helper array of IDs we'll use in a few places | ||
const saves = ['death', 'wands', 'paralysis', 'breath', 'spells'] | ||
|
||
export function registerSavesSettings () { | ||
// The settings menu | ||
game.settings.registerMenu(SYSTEM_ID, SETTINGS.SAVES_MENU, { | ||
name: 'BASICFANTASYRPG.Settings.SavesMenu.name', | ||
label: 'BASICFANTASYRPG.Settings.SavesMenu.label', | ||
hint: 'BASICFANTASYRPG.Settings.SavesMenu.hint', | ||
icon: 'fas fa-cog', | ||
type: SavesSettings, | ||
restricted: true // GM-only | ||
}) | ||
|
||
// the settings object | ||
game.settings.register(SYSTEM_ID, SETTINGS.SAVES_SETTINGS, { | ||
scope: 'world', | ||
config: false, | ||
type: Object, | ||
default: SavesSettings.defaultSaves | ||
}) | ||
} | ||
|
||
class SavesSettings extends FormApplication { | ||
static #defaultSaves = null | ||
static get defaultSaves () { | ||
if (!SavesSettings.#defaultSaves) { | ||
SavesSettings.#defaultSaves = {} | ||
saves.forEach(s => { | ||
SavesSettings.#defaultSaves[s] = game.i18n.localize( | ||
`BASICFANTASYRPG.Save${s.capitalize()}` | ||
) | ||
}) | ||
} | ||
return SavesSettings.#defaultSaves | ||
} | ||
|
||
static get defaultOptions () { | ||
return foundry.utils.mergeObject(super.defaultOptions, { | ||
popOut: true, | ||
width: 400, | ||
template: `systems/${SYSTEM_ID}/templates/settings/saves-settings.hbs`, | ||
id: SETTINGS.SAVES_MENU, | ||
title: 'BASICFANTASYRPG.Settings.SavesMenu.name' | ||
}) | ||
} | ||
|
||
getData () { | ||
const initialValues = game.settings.get(SYSTEM_ID, SETTINGS.SAVES_SETTINGS) | ||
// repack the current saves names into id, label and values for the form | ||
const data = {} | ||
saves.forEach((v, i) => { | ||
data[i] = { | ||
id: v, | ||
label: SavesSettings.defaultSaves[v], | ||
value: initialValues[v] | ||
} | ||
}) | ||
return data | ||
} | ||
|
||
_updateObject (event, formData) { | ||
const data = foundry.utils.expandObject(formData) | ||
const current = game.settings.get(SYSTEM_ID, SETTINGS.SAVES_SETTINGS) | ||
|
||
if (!objectsShallowEqual(data, current)) { | ||
game.settings.set(SYSTEM_ID, SETTINGS.SAVES_SETTINGS, data) | ||
SettingsConfig.reloadConfirm({ world: true }) | ||
} | ||
} | ||
|
||
activateListeners (html) { | ||
super.activateListeners(html) | ||
html.on('click', '[data-action=reset]', this._handleResetButtonClicked) | ||
} | ||
|
||
async _handleResetButtonClicked (event) { | ||
console.log('BFRPG | Reset save names to default values') | ||
saves.forEach(id => { | ||
const element = $(event.delegateTarget).find(`[name=${id}]`) | ||
if (element && element.length > 0) { | ||
element[0].value = game.i18n.localize( | ||
`BASICFANTASYRPG.Save${id.capitalize()}` | ||
) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{{#*inline "savePartial"}} | ||
<div class="form-row flexrow"> | ||
<label for="{{id}}">{{label}}:</label> | ||
<input name="{{id}}" required=true value="{{value}}" /> | ||
</div> | ||
{{/inline}} | ||
|
||
{{log this}} | ||
<form class="form-group flexcol"> | ||
<fieldset> | ||
<legend>{{localize "BASICFANTASYRPG.Settings.SavesMenu.name"}}:</legend> | ||
{{#each this}} | ||
{{> savePartial}} | ||
{{/each}} | ||
</fieldset> | ||
|
||
<footer class="sheet-footer flexrow button-container"> | ||
<button class="reset-all" type="button" name="reset" data-action="reset"> | ||
<i class="fa-solid fa-undo"></i> {{localize "SETTINGS.Reset"}} | ||
</button> | ||
<button type="submit" name="submit"> | ||
<i class="fa-solid fa-save"></i> {{localize "SETTINGS.Save"}} | ||
</button> | ||
</footer> | ||
</form> |