Implement Token UI adjustments
Implement the sub-module moving the attribute bars and nameplate to work well with our dynamic borders. The attribute bars are moved below the token, one on top of the other, and the nameplate is moved above. This is done by monkey patching the `_drawBar()` and `_refreshNameplate()` functions from the Token class. This is vulnerable to compatibility issues with other modules, but is the only way I found for the changes to be shown without further input. Indeed, those functions are called after the `drawToken` hook, which makes it impossible to move those UI elements there. We can do it in the `refreshToken` hook, which works *but* requires the canvas view to be moved at least once for the position change to be reflected, which looks a bit broken. So we monkey patch the functions, but bind them beforehand to be able to call them within our custom function as we don't want to duplicate what they do. Update README and translations for the added setting.
This commit is contained in:
parent
ab02dd139a
commit
22dc500801
4 changed files with 80 additions and 0 deletions
|
@ -1,2 +1,53 @@
|
|||
import * as MillsFabula from "./mills_fabula.mjs";
|
||||
|
||||
const TokenUiSettings = Object.freeze({
|
||||
Enabled: "uiAdjustEnabled",
|
||||
})
|
||||
|
||||
Hooks.once("setup", () => {
|
||||
game.settings.register(MillsFabula.id, TokenUiSettings.Enabled, {
|
||||
name: "MF.TokenUiAdjust.Settings.Enabled.Name",
|
||||
hint: "MF.TokenUiAdjust.Settings.Enabled.Hint",
|
||||
type: Boolean,
|
||||
config: true,
|
||||
scope: "world",
|
||||
requiresReload: true,
|
||||
default: true,
|
||||
})
|
||||
|
||||
if (!game.settings.get(MillsFabula.id, TokenUiSettings.Enabled)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** Padding above and below the token, percentage of a grid square. */
|
||||
const top_bot_padding = 5;
|
||||
|
||||
Hooks.on("drawToken", (drawn_token) => {
|
||||
/*
|
||||
* Drawing attribute bars and nameplates are handled by private functions called
|
||||
* after the `drawToken` hook. That means that we cannot change their position in this hook.
|
||||
*
|
||||
* We could update them in the `refreshToken` hook, which works, but is called a lot more than
|
||||
* necessary and needs a view update (move the view) from the canvas to show the updates.
|
||||
*
|
||||
* Instead : we can replace the draw functions handling the bars and nameplate.
|
||||
* They are still useful, so make of copy bound to the drawn token so that we can call them
|
||||
* in our replacement function, then make the changes that we need.
|
||||
*/
|
||||
let base_drawBar = drawn_token._drawBar.bind(drawn_token);
|
||||
drawn_token._drawBar = (number, bar, data) => {
|
||||
const token_height = drawn_token.getSize().height
|
||||
const padding = drawn_token.scene.grid.size * top_bot_padding/100;
|
||||
base_drawBar(number, bar, data)
|
||||
bar.position.set(0, token_height + padding + number * bar.height)
|
||||
}
|
||||
|
||||
let base_refreshNameplate = drawn_token._refreshNameplate.bind(drawn_token);
|
||||
drawn_token._refreshNameplate = () => {
|
||||
const token_width = drawn_token.getSize().width;
|
||||
const padding = drawn_token.scene.grid.size * top_bot_padding/100;
|
||||
base_refreshNameplate()
|
||||
drawn_token.nameplate.position.set(token_width/2, -(padding + drawn_token.nameplate.height))
|
||||
}
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue