-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
158 lines (134 loc) · 6.15 KB
/
main.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
const tokenHUDWildcard = {
registerSettings: () => {
game.settings.register('token-hud-wildcard', 'imageDisplay', {
name: game.i18n.format('THWildcard.DisplaySettingName'),
hint: game.i18n.format('THWildcard.DisplaySettingHint'),
scope: 'world',
config: true,
type: Boolean,
default: true
})
game.settings.register("token-hud-wildcard", "imageOpacity", {
name: game.i18n.format('THWildcard.OpacitySettingName'),
hint: game.i18n.format('THWildcard.OpacitySettingHint'),
scope: "world",
config: true,
range: { min: 0, max: 100, step: 1 },
type: Number,
default: 50
})
}
}
const getTokenDimensions = (token, imgName) => {
const height = imgName.match(/_height(.*)_/)
const width = imgName.match(/_width(.*)_/)
const scale = imgName.match(/_scale(.*)_/)
const prototypeData = token._actor.data.token
return {
height: height ? parseFloat(height[1]) : prototypeData.height,
width: width ? parseFloat(width[1]) : prototypeData.width,
scale: scale ? parseFloat(scale[1]) : prototypeData.scale,
}
}
Hooks.on('init', () => {
tokenHUDWildcard.registerSettings()
})
Hooks.on('ready', () => {
const renderTokenConfig = 'renderTokenConfig'
Hooks.on(renderTokenConfig, WildcardDefault.render)
WildcardDefault._hookPreTokenCreate()
})
const WildcardDefault = {
getDefaultImg: async (token) => {
const flag = token.getFlag('token-hud-wildcard', 'default') || ''
return flag
},
_hookPreTokenCreate () {
Hooks.on('preCreateToken', (parent, data, options, userId) => {
const defaultValue = parent?.actor?.data?.token?.flags['token-hud-wildcard'] ? parent.actor.data.token.flags['token-hud-wildcard'].default : ''
if (defaultValue !== '' && parent?.actor?.data?.token?.randomImg) {
const dimensions = getTokenDimensions(parent, defaultValue)
let updateInfo = { img: defaultValue , ...dimensions }
mergeObject(data, updateInfo)
parent.data.update(updateInfo)
}
})
},
render: async (config, html) => {
const defaultImg = await WildcardDefault.getDefaultImg(config.token)
if (config.token.data._id) {
return
}
const imageDataTab = html.find('.tab[data-tab="appearance"],.tab[data-tab="image"]')
const checkBoxWildcard = imageDataTab.find('input[name="randomImg"]')
const configField = await renderTemplate('/modules/token-hud-wildcard/templates/configField.html', { defaultImg, available: checkBoxWildcard[0].checked })
imageDataTab.append(configField)
const defaultConfig = imageDataTab.find('.thwildcard-default')
defaultConfig.find('button').click(event => {
event.preventDefault()
const input = defaultConfig.find('input')[0]
const fp = new FilePicker({ current: input.value, field: input })
fp.browse(defaultImg)
})
checkBoxWildcard.click(event => {
if (event.target.checked) {
defaultConfig[0].classList.add('active')
} else {
defaultConfig[0].classList.remove('active')
}
})
}
}
Hooks.on('renderTokenHUD', async (hud, html, token) => {
const actor = game.actors.get(token.actorId)
const images = await actor?.getTokenImages() ?? []
if (images.length < 2) {
return
}
const imageDisplay = game.settings.get('token-hud-wildcard', 'imageDisplay')
const imageOpacity = game.settings.get('token-hud-wildcard', 'imageOpacity')/100
const imagesParsed = images.map(im => {
const split = im.split('/')
var extension = im.split('.')
extension = extension[extension.length - 1]
const img = ['jpg', 'jpeg', 'png', 'svg', 'webp'].includes(extension)
const vid = ['webm', 'mp4', 'm4v'].includes(extension)
return { route: im, name: split[split.length - 1], used: im === token.img, img, vid, type: img || vid }
})
const wildcardDisplay = await renderTemplate('/modules/token-hud-wildcard/templates/hud.html', { imagesParsed, imageDisplay, imageOpacity })
const is080 = !isNewerVersion("0.8.0", game.data.version)
html.find('div.right')
.append(wildcardDisplay)
.click((event) => {
let activeButton, clickedButton, tokenButton;
for ( const button of html.find('div.control-icon') ) {
if (button.classList.contains('active')) activeButton = button
if (button === event.target.parentElement) clickedButton = button
if (button.dataset.action === 'thwildcard-selector') tokenButton = button
}
if (clickedButton === tokenButton && activeButton !== tokenButton) {
tokenButton.classList.add('active')
html.find('.thwildcard-selector-wrap')[0].classList.add('active')
const effectSelector = is080 ? '[data-action="effects"]' : '.effects'
html.find(`.control-icon${effectSelector}`)[0].classList.remove('active')
html.find('.status-effects')[0].classList.remove('active')
} else {
tokenButton.classList.remove('active')
html.find('.thwildcard-selector-wrap')[0].classList.remove('active')
}
})
const buttons = html.find('.thwildcard-button-select')
buttons.map((button) => {
buttons[button].addEventListener('click', function (event) {
event.preventDefault()
event.stopPropagation()
const controlled = canvas.tokens.controlled
const index = controlled.findIndex(x => x.data._id === token._id)
const tokenToChange = controlled[index]
const updateTarget = is080 ? tokenToChange.document : tokenToChange
const dimensions = getTokenDimensions(updateTarget, event.target.dataset.name)
let updateInfo = { img: event.target.dataset.name, ...dimensions }
updateTarget.update(updateInfo)
})
})
})