This repository was archived by the owner on Nov 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhype-track.js
192 lines (160 loc) · 5.55 KB
/
hype-track.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
189
190
191
192
class HypeTrack {
constructor() {
this.playlist = null;
}
async init() {
Hooks.on("ready", async () => {
if(!game.user.isGM) return;
const hypePlaylist = game.playlists.entities.find(p => p.name == HypeTrack.DEFAULT_CONFIG.playlistName);
if(!hypePlaylist) {
this.playlist = await Playlist.create({"name": HypeTrack.DEFAULT_CONFIG.playlistName});
} else {
this.playlist = hypePlaylist;
}
this._hookOnRenderCharacterSheets();
});
Hooks.on("updateCombat", (combat, update) => {
if(update.turn || update.round) {
const actorTrack = combat.combatant.actor.getFlag(HypeTrack.DEFAULT_CONFIG.moduleName, HypeTrack.DEFAULT_CONFIG.flagNames.track);
this.playlist.stopAll();
if(actorTrack) {
this._playTrack(actorTrack);
}
}
});
}
/**
* Hooks on render of the default Actor sheet in order to insert the DDB Button
*/
_hookOnRenderCharacterSheets() {
if(!game.user.isGM) return;
const sheetClasses = Object.values(CONFIG.Actor.sheetClasses.character);
for (let s of sheetClasses) {
const sheetClass = s.id.split(".")[1];
Hooks.on(`render${sheetClass}`, (app, html, data) => {
this._addHypeButton(app, html, data);
});
}
}
static get DEFAULT_CONFIG() {
return {
moduleName: "hype-track",
playlistName: "Hype Tracks",
buttonIcon: "fas fa-music",
buttonText: " Hype",
aTitle: "Change Actor Theme Song",
flagNames: {
track: "track"
}
}
}
/**
*
* @param {*} actor
*
*/
async _getActorTrack(actor) {
let actorTrack;
try {
actorTrack = await actor.getFlag(HypeTrack.DEFAULT_CONFIG.moduleName, HypeTrack.DEFAULT_CONFIG.flagNames.track);
return actorTrack;
} catch (e) {
console.log(e);
return;
}
}
async _addHypeButton (app, html, data) {
/**
* Finds the header and the close button
*/
const windowHeader = html.parent().parent().find(".window-header");
const windowCloseBtn = windowHeader.find(".close");
/**
* jquery reference to the D&D Beyond button to add to the sheet
*/
const hypeButton = $(
`<a class="${HypeTrack.DEFAULT_CONFIG.moduleName}" title="${HypeTrack.DEFAULT_CONFIG.aTitle}">
<i class="${HypeTrack.DEFAULT_CONFIG.buttonIcon}"></i>
<span> ${HypeTrack.DEFAULT_CONFIG.buttonText}</span>
</a>`
);
/**
* Create an instance of the hypeButton before the close button
* Removes existing instances first to avoid duplicates
*/
windowHeader.find('.hype-track').remove();
windowCloseBtn.before(hypeButton);
/**
* When the DDB button is clicked, lookup the DDB URL, then look for an existing popup,
* and if none exists, and the DDB URL is null, open the form to set the URL,
* otherwise if the popup does not exist but the DDB URL is not null, create the popup
* or else simply focus the existing popup
*/
hypeButton.click(async ev => {
const actorTrack = await this._getActorTrack(app.entity);
this._openTrackForm(app.entity, actorTrack, {closeOnSubmit: true});
});
}
_openTrackForm(actor, track, options){
const data = {
"track": track,
"playlist": this.playlist
}
new HTActorTrackForm(actor, data, options).render(true);
}
_getPlaylistSound(trackId) {
return this.playlist.sounds.find(s => s.id == trackId);
}
_playTrack(trackId) {
//const sound = this._getPlaylistSound(trackId);
if(!(trackId instanceof Number)) {
trackId = Number(trackId);
}
this.playlist.updateSound({id: trackId, playing: true});
}
}
class HTActorTrackForm extends FormApplication {
constructor(actor, data, options){
super(data, options);
this.actor = actor;
this.data = data;
}
/**
* Default Options for this FormApplication
* @todo extract to module constants
*/
static get defaultOptions() {
return mergeObject(super.defaultOptions, {
id: "hype-track-form",
title: "Character Theme Song",
template: "public/modules/hype-track/templates/hype-track-form.html",
classes: ["sheet"],
width: 500
});
}
/**
* Provide data (ddbURL if any) to the handlebars template
*/
async getData() {
const data = {
playlistTracks: this.data.playlist.sounds,
track: this.data.track
}
return data;
}
/**
* Executes on form submission.
* Attempts to set the ddbURL flag on the underlying Actor
* @param {Object} event -- the form submission event
* @param {Object} formData -- the form data
*/
async _updateObject(event, formData) {
try {
this.actor.setFlag(HypeTrack.DEFAULT_CONFIG.moduleName, HypeTrack.DEFAULT_CONFIG.flagNames.track, formData.track);
} catch (e) {
throw e;
}
}
}
const hypeTrack = new HypeTrack();
hypeTrack.init();