-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
radial-status-effects.js
188 lines (154 loc) · 5.62 KB
/
radial-status-effects.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
MIT License
Copyright (c) 2022 Dorako
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
https://github.com/Dorako/pf2e-dorako-ux/blob/main/LICENSE
*/
import { MODULE, CONSTANTS } from './constants.js'
import { getSetting, registerSetting } from './utils.js'
/**
* Register
*/
export function register () {
registerSettings()
registerPatches()
}
/**
* Register settings
*/
function registerSettings () {
registerSetting(
CONSTANTS.RADIAL_STATUS_EFFECTS.SETTING.KEY,
{
name: game.i18n.localize('CUSTOM_DND5E.setting.radialStatusEffects.name'),
hint: game.i18n.localize('CUSTOM_DND5E.setting.radialStatusEffects.hint'),
scope: 'world',
config: true,
requiresReload: true,
type: Boolean,
default: false
}
)
}
/**
* Register patches
*/
function registerPatches () {
if (!getSetting(CONSTANTS.RADIAL_STATUS_EFFECTS.SETTING.KEY)) return
libWrapper.register(MODULE.ID, 'Token.prototype._refreshEffects', tokenRefreshEffectsPatch, 'WRAPPER')
libWrapper.register(MODULE.ID, 'Token.prototype._drawEffect', tokenDrawEffectPatch, 'WRAPPER')
libWrapper.register(MODULE.ID, 'Token.prototype._drawOverlay', tokenDrawOverlayPatch, 'WRAPPER')
}
function tokenRefreshEffectsPatch (wrapped, ...args) {
wrapped(...args)
let i = 0
const gridSize = this.scene?.grid?.size ?? 100
const bg = this.effects.bg.clear()
for (const effect of this.effects.children) {
if (effect === bg) continue
// Overlay effect
if (effect === this.effects.overlay) continue
// Status effect
else {
effect.anchor.set(0.5)
const iconScale = getIconScale(Math.min(this.document?.height, this.document?.width))
const gridScale = gridSize / 100
const scaledSize = 12 * iconScale * gridScale
effect.width = scaledSize
effect.height = scaledSize
updateIconPosition(effect, i, this)
drawBackground(effect, bg, gridScale)
i++
}
}
}
async function tokenDrawEffectPatch (wrapped, src, tint, overlay = false) {
wrapped(null, tint)
if (!src) return
const tex = await loadTexture(src, { fallback: 'icons/svg/hazard.svg' })
const icon = new PIXI.Sprite(tex)
icon.tint = tint ?? 0xFFFFFF
if (!overlay && !src.endsWith('.svg')) {
const size = Math.min(icon.width, icon.height)
const radius = size / 2
const mask = new PIXI.Graphics()
mask.beginFill(0xffffff)
mask.drawCircle(0, 0, radius)
mask.endFill()
icon.addChild(mask)
icon.mask = mask
}
return this.effects.addChild(icon)
}
async function tokenDrawOverlayPatch (wrapped, src, tint) {
wrapped(null, tint)
const icon = await this._drawEffect(src, tint, true)
if (icon) icon.alpha = 0.8
this.effects.overlay = icon ?? null
return icon
}
function getIconScale (size) {
if (size >= 2.5) return size / 2
if (size >= 1.5) return size * 0.7
return 1.4
}
function updateIconPosition (icon, index, token) {
const max = getMaxIcons(token)
const ratio = index / max
const gridSize = token?.scene?.grid?.size ?? 100
const tokenTileFactor = token?.document?.width ?? 1
const sizeOffset = getSizeOffset(Math.min(token?.document?.height, token?.document?.width))
const offset = sizeOffset * tokenTileFactor * gridSize
const initialRotation = (0.5 + (1 / max) * Math.PI) * Math.PI
const { x, y } = polarToCartesian(offset, (ratio + 0) * 2 * Math.PI + initialRotation)
const halfGridSize = gridSize * tokenTileFactor / 2
icon.position.x = x / 2 + halfGridSize
icon.position.y = -y / 2 + halfGridSize
}
function getMaxIcons (token) {
if (token?.document.width < 1) return 9
if (token?.document.width === 1) return 16
if (token?.document.width === 2) return 28
return 40
}
function getSizeOffset (size) {
if (size < 1) return 1.4
if (size === 1) return 1.25
if (size === 2) return 1.125
return 1.075
}
function polarToCartesian (radius, angle) {
return {
x: radius * Math.cos(angle),
y: radius * Math.sin(angle)
}
}
/**
* Draw background
* @param {string} icon
* @param {*} background
* @param {*} gridScale
*/
function drawBackground (icon, background, gridScale) {
const radius = (icon.width) / 2 + (icon.width * 0.1)
background.beginFill(0x333333)
background.drawCircle(icon.position.x, icon.position.y, radius)
background.endFill()
background.lineStyle((2 * gridScale) / 2, 0x9f9275, 1, 0.5)
background.drawCircle(icon.position.x, icon.position.y, radius)
background.lineStyle(0)
}