diff --git a/module.json b/module.json index cb6d185..6045a71 100644 --- a/module.json +++ b/module.json @@ -26,6 +26,16 @@ "verified": "3.1.2" } } + ], + "requires": [ + { + "id": "lib-wrapper", + "type": "module", + "compatibility": { + "minimum": "1.0.0.0", + "verified": "1.12.13.0" + } + } ] }, "esmodules": [ diff --git a/src/combat.mjs b/src/combat.mjs index 51a405e..d654ee9 100644 --- a/src/combat.mjs +++ b/src/combat.mjs @@ -1,4 +1,4 @@ -import {item_roll, Logger} from "./util.mjs"; +import {autoResizeApplicationExisting, item_roll, Logger} from "./util.mjs"; import {combat_hooks, socket} from "./module.mjs"; import {COMBAT_ENABLED, COMBAT_HEALTH_ESTIMATE, COMBAT_HEALTH_ESTIMATE_TYPE, ID_MAP, MODULE_ID} from "./constants.mjs"; import {actor_to_discord_ids} from "./sync.mjs"; @@ -181,6 +181,36 @@ function get_health(hp, combatHealthSetting, actorType) { } } +export function register_combat_settings_toggle() { + libWrapper.register('oronder', 'CombatTrackerConfig.prototype._updateObject', async function (wrapped, ...args) { + Logger.info(this) + Logger.info(this.form.elements.oronder_combat_tracker_toggle.checked) + Logger.info(game.settings.get(MODULE_ID, COMBAT_ENABLED)) + await game.settings.set(MODULE_ID, COMBAT_ENABLED, this.form.elements.oronder_combat_tracker_toggle.checked) + set_combat_hooks() + Logger.info(game.settings.get(MODULE_ID, COMBAT_ENABLED)) + return wrapped(...args) + }, 'WRAPPER') + + Hooks.on('renderCombatTrackerConfig', async (application, $html, data) => { + const $label = $("", { + text: 'Publish Combat Tracker to Discord', + for: 'oronder_combat_tracker_toggle' + }) + const $checkbox = $("", { + type: 'checkbox', + id: 'oronder_combat_tracker_toggle', + checked: game.settings.get(MODULE_ID, COMBAT_ENABLED) + }) + + $('
', {class: "form-group"}) + .append($label, $checkbox) + .insertBefore($html.find('form button').last()) + + autoResizeApplicationExisting(application) + }) +} + export function handle_incoming_rolls() { socket.on('roll', async data => { diff --git a/src/module.mjs b/src/module.mjs index 986b8f8..f835416 100644 --- a/src/module.mjs +++ b/src/module.mjs @@ -3,7 +3,7 @@ import {registerSettings} from "./settings.mjs" import {AUTH, MODULE_ID, ORONDER_WS_URL} from "./constants.mjs" import {del_actor, sync_actor} from "./sync.mjs" import {monks_token_bar_hooks} from "./monks_token_bar_hooks.mjs"; -import {handle_incoming_rolls, set_combat_hooks} from "./combat.mjs"; +import {handle_incoming_rolls, register_combat_settings_toggle, set_combat_hooks} from "./combat.mjs"; export let socket export let session_id @@ -32,8 +32,12 @@ function set_session(session) { Hooks.once("ready", async () => { if (game.user.isGM) { + if (!game.modules.get('lib-wrapper')?.active) { + ui.notifications.error("Oronder requires the 'libWrapper' module. Please install and activate it.") + } await registerSettings() open_socket_with_oronder() + register_combat_settings_toggle() } game.socket.on(SOCKET_NAME, data => { diff --git a/src/settings-form-application.mjs b/src/settings-form-application.mjs index b458779..13b637a 100644 --- a/src/settings-form-application.mjs +++ b/src/settings-form-application.mjs @@ -13,7 +13,7 @@ import { } from "./constants.mjs" import {full_sync, sync_actor} from "./sync.mjs" import {open_socket_with_oronder} from "./module.mjs" -import { set_combat_hooks } from "./combat.mjs" +import {set_combat_hooks} from "./combat.mjs" export class OronderSettingsFormApplication extends FormApplication { constructor(object = {}, options = {}) { @@ -29,6 +29,7 @@ export class OronderSettingsFormApplication extends FormApplication { id_map: id_map, combat_health_estimate: game.settings.get(MODULE_ID, COMBAT_HEALTH_ESTIMATE), combat_health_estimate_type: COMBAT_HEALTH_ESTIMATE_TYPE, + combat_tracking_enabled: game.settings.get(MODULE_ID, COMBAT_ENABLED), players: game.users.filter(user => user.role < 3).map(user => ({ foundry_name: user.name, foundry_id: user.id, @@ -115,7 +116,7 @@ export class OronderSettingsFormApplication extends FormApplication { this.object.guild.combat_channel_id = Array.from(this.form.elements.combat_channel).find(c => c.selected)?.value this.object.guild.roll_discord_to_foundry = this.form.elements.roll_discord_to_foundry.checked this.object.combat_health_estimate = parseInt(this.form.elements.combat_health_estimate.value) - this.object.guild.combat_tracking_enabled = this.form.elements.combat_tracking_enabled.checked + this.object.combat_tracking_enabled = this.form.elements.combat_tracking_enabled.checked } this.object.show_advanced = this.form.elements.show_advanced.checked @@ -196,7 +197,7 @@ export class OronderSettingsFormApplication extends FormApplication { this.bind() - await game.settings.set(MODULE_ID, COMBAT_ENABLED, this.object.guild.combat_tracking_enabled) + await game.settings.set(MODULE_ID, COMBAT_ENABLED, this.object.combat_tracking_enabled) await game.settings.set(MODULE_ID, COMBAT_HEALTH_ESTIMATE, this.object.combat_health_estimate) const updated_id_map = await game.settings.set(MODULE_ID, ID_MAP, diff --git a/src/util.mjs b/src/util.mjs index 118e64b..893651b 100644 --- a/src/util.mjs +++ b/src/util.mjs @@ -59,3 +59,18 @@ export function item_roll(item) { return new Roll(formula, item.getRollData()) } + +export function autoResizeApplicationExisting(app) { + const centerPrev = app.position.top + app.position.height / 2; + + const pos = app.setPosition({ + width: app.position.width, height: "auto", + }); + + const center = pos.top + pos.height / 2; + app.setPosition({ + width: app.position.width, + height: app.position.height, + top: app.position.top + (centerPrev - center), + }); +} \ No newline at end of file diff --git a/templates/settings-form-application.hbs b/templates/settings-form-application.hbs index 18d6094..ff1968b 100644 --- a/templates/settings-form-application.hbs +++ b/templates/settings-form-application.hbs @@ -202,12 +202,12 @@ title="Oronder will post changes in the combat tracker to the Combat Channel configured below."> Publish Combat Tracker to Discord + style='float:right' {{checked combat_tracking_enabled}}>