all repos — simplyrpg @ 64aa0ad8b0a16f76608fcb23845924baaad9b572

Fork of https://github.com/harperd1/simplyRPG — multi-map VTT, token visibility, dark theme, stream HUD

libraries/dynamicDivModule.js (view raw)

  1class DynamicDiv {
  2    constructor (id) {
  3        this.subElements = [];
  4        this.visible = true;
  5
  6        this.div = document.createElement("div");
  7        this.div.classList.add("dynamicDiv");
  8        if (document.getElementById(id)) {
  9            document.getElementById(id).appendChild(this.div);
 10        }
 11    }
 12
 13    addDiv () {
 14        let localDiv = document.createElement("div");
 15        this.subElements.push(localDiv);
 16        this.div.appendChild(localDiv);
 17        return localDiv;
 18    }
 19
 20    addText (innerHTML="") {
 21        let localP = document.createElement("p");
 22        localP.innerHTML = innerHTML;
 23        this.subElements.push(localP);
 24        this.div.appendChild(localP);
 25        return localP;
 26    }
 27
 28    addButton (innerHTML="") {
 29        let localButton = document.createElement("button");
 30        localButton.innerHTML = innerHTML;
 31        this.subElements.push(localButton);
 32        this.div.appendChild(localButton);
 33        localButton.classList.add('dynamicButton')
 34        return localButton;
 35    }
 36
 37    addTextbox (value="") {
 38        let localBox = document.createElement("input");
 39        localBox.type="text";
 40        localBox.value= value;
 41        localBox.classList.add('textbox');
 42        this.subElements.push(localBox);
 43        this.div.appendChild(localBox);
 44        return localBox;        
 45    }
 46
 47    addSlider (value=0) {
 48        let localSlider = document.createElement("input");
 49        localSlider.type = "range";
 50        localSlider.min = 0;
 51        localSlider.max = 50;
 52        localSlider.value = 0;
 53        this.subElements.push(localSlider);
 54        this.div.appendChild(localSlider);
 55        return localSlider
 56    }
 57
 58    hide () {
 59        this.visible = false;
 60        this.div.style.display = "none";
 61    }
 62
 63    show () {
 64        this.visible = true;
 65        this.div.style.display = "block";
 66    }
 67
 68    toggleVisible () {
 69        if (this.visible) {
 70            this.hide()
 71        } else {
 72            this.show()
 73        }
 74    }
 75}
 76
 77class SymbolHandle extends DynamicDiv {
 78    constructor (circle, id, notes) {
 79        super("symbolMenu");
 80        this.namedSubElements = {};
 81        this.namedSubElements.circle = circle;
 82        this.id = id;
 83        this.tokenImage = null;
 84        this.tokenHandle = null;
 85
 86        this.addTokenHandle(this.namedSubElements.circle.circleColor,this.namedSubElements.circle.circleType);
 87
 88
 89        //Currently the width of the Symbol manager is 18em and the height is 2.7em
 90        //1% of width corresponds to 6.67% of height
 91        let labelbox = this.addTextbox();
 92        labelbox.style.position = "absolute";
 93        labelbox.style.left = "2.5em";  
 94        labelbox.style.width = "2.2em";  
 95        labelbox.style.top = ".1em"
 96        labelbox.oninput = this.labelChangeListener;
 97        labelbox.value = this.namedSubElements.circle.circleText;
 98        this.namedSubElements.labelbox = labelbox;
 99
100        let label = this.addText('Label');
101        label.style.position = "absolute";
102        label.style.left = "2.5em";
103        label.style.width = "2.2em";
104        label.style.top = "2em"
105        label.style.color = "rgb(130,130,130)";
106        label.style.textAlign = "center";
107        label.style.fontSize = "small";
108        this.namedSubElements.label = label;
109
110        let notesBox = this.addTextbox();
111        notesBox.style.position = "absolute";
112        notesBox.style.left = "5.2em";
113        notesBox.style.width = "9.2em";
114
115        notesBox.style.top = ".1em"
116        notesBox.value = notes;
117        this.namedSubElements.notesBox = notesBox;
118
119        let notesLabel = this.addText('Notes');
120        notesLabel.style.position = "absolute";
121        notesLabel.style.left = "5.2em";
122        notesLabel.style.width = "9.2em";
123        notesLabel.style.top = "2em";
124        notesLabel.style.color = "#888888";
125        notesLabel.style.textAlign = "center";
126        notesLabel.style.fontSize = "small";
127        this.namedSubElements.notesLabel = notesLabel;
128
129        let eyeButton = this.addButton("");
130        eyeButton.classList.add('eyeButton');
131        eyeButton.title = "Скрыть / показать токен игрокам";
132        eyeButton.id = this.id;
133        eyeButton.onclick = this.toggleVisibilityFromButton;
134        this.namedSubElements.eyeButton = eyeButton;
135
136        let button = this.addButton("");
137        button.classList.add('deleteButton');
138        button.title = "Удалить токен";
139        button.id = this.id;
140        button.onclick = this.removeFromButton;
141        this.namedSubElements.button = button;
142
143        if (circle && circle.visible === false) {
144            eyeButton.classList.add('isHidden');
145            this.div.classList.add('tokenHidden');
146        }
147    }
148
149    addTokenHandle(color='blue',type='circle') {
150        let circle = this.addDiv();
151        if (type === 'circle') {
152            circle.classList.add("circleHandle");
153            circle.style.backgroundColor = color;
154        } else if (type === 'square') {
155            circle.classList.add("squareHandle");
156            circle.style.backgroundColor = color;
157        } else if (type === 'cone') {
158            circle.classList.add("coneHandle");
159            circle.style.borderRight = `${1.25}em solid ${color}`;
160        } else if (type === 'custom') {
161            circle.classList.add("squareHandle")
162            this.tokenImage = new Image();
163            this.tokenImage.id = this.id;
164            circle.appendChild(this.tokenImage);
165        } else if (type === 'customCircle') {
166            circle.classList.add("circleHandle")
167            circle.classList.add("centering") //centers the token in the div, so that it gets cutoff as expected
168            this.tokenImage = new Image();
169            this.tokenImage.id = this.id;
170            circle.appendChild(this.tokenImage);
171        } else {
172            console.log('Circle type not recognized for dynamic div handle! '+this.namedSubElements.circle.circleType )
173        }
174        this.tokenHandle = circle;
175    }
176
177    redraw () {
178        //Redraws the circle, updating the text to match 
179        let updatedText = this.namedSubElements.labelbox.value;
180        if (!updatedText) {
181            updatedText = "";
182        } else if (updatedText.length > 2) {
183            updatedText = updatedText.slice(0,2);
184        }
185        this.namedSubElements.circle.circleText = updatedText;
186        this.namedSubElements.circle.redraw();
187    }
188
189    labelChangeListener () {
190        redrawCircles();
191        postLabels();
192    }
193
194    removeFromButton () {
195        // Hacky solution to getting this object since .this refers to the button pressed
196        let thisSymbolHandle = globs.circleHandles[this.id]
197        thisSymbolHandle.removeCircle();
198        thisSymbolHandle.removeHandle();
199        thisSymbolHandle.removeFromMasterList();
200        broadcastChannel.postMessage(['removeCircle',this.id]);
201        if (typeof renderSceneTabs === 'function') {
202            renderSceneTabs();
203        }
204    }
205
206    removeCircle () {
207        this.namedSubElements.circle.remove();
208    }
209
210    removeHandle () {
211        this.div.remove();
212    }
213
214    removeFromMasterList () {
215        globs.circleHandles[this.id] = null;
216    }
217
218    toggleVisibilityFromButton () {
219        let thisSymbolHandle = globs.circleHandles[this.id];
220        if (!thisSymbolHandle) return;
221        let nextVisible = !thisSymbolHandle.namedSubElements.circle.visible;
222        thisSymbolHandle.setVisibility(nextVisible);
223    }
224
225    setVisibility (isVisible, broadcast=true) {
226        let visible = Boolean(isVisible);
227        this.namedSubElements.circle.setVisibility(visible);
228        if (this.namedSubElements.eyeButton) {
229            this.namedSubElements.eyeButton.classList.toggle('isHidden', !visible);
230        }
231        this.div.classList.toggle('tokenHidden', !visible);
232        if (broadcast && this.namedSubElements.circle.windowLocation === 'main') {
233            broadcastChannel.postMessage(['setCircleVisibility', this.id, visible]);
234        }
235        updateCanvas();
236    }
237
238    updateLocation (x,y) {
239        this.namedSubElements.circle.updateLocation(x,y);
240    }
241
242    updateRotation (rot) {
243        this.namedSubElements.circle.updateRotation(rot);
244    }
245
246    processCustomImageLoad () {
247        if (this.namedSubElements.circle.needsRedraw) {
248            this.namedSubElements.circle.redraw();
249        }
250
251        function loadImageToken() {
252            circleHandle = globs.circleHandles[this.id];
253            let divHeight = circleHandle.tokenHandle.clientHeight;
254            let divWidth = circleHandle.tokenHandle.clientWidth;
255
256            this.height = divHeight;
257            this.width = divWidth;
258        }
259        this.tokenImage.src = URL.createObjectURL(this.namedSubElements.circle.imageFile);
260        this.tokenImage.onload = loadImageToken;
261    }
262
263    manuallyAppendImageFile (file) {
264        this.namedSubElements.circle.manuallyAppendImageFile(file);
265    }
266}
267
268class MusicHandle extends DynamicDiv {
269    constructor (musicObject, id) {
270        super("musicMenu");
271        this.namedSubElements = {};
272        this.file = musicObject.file;
273        this.name = musicObject.name;
274        this.playlist = musicObject.playlist;
275        this.audioObject = null;
276        this.id = id;
277        this.currentTime = 0.;
278        this.active = false;
279        this.duration = null;
280
281        if (this.name.length > 20) {
282            this.shortName = this.name.slice(0,17)+'...';
283        } else {
284            this.shortName = this.name;
285        }
286
287        this.div.classList.add("indented");
288        this.div.style.height = "3.5em";
289
290        let playButton = this.addButton("");
291        playButton.classList.add('playButton');
292        playButton.id = this.id;
293        playButton.onclick = this.startMusicFromButton;
294        this.namedSubElements.playButton = playButton;
295
296        let pauseButton = this.addButton("")
297        pauseButton.classList.add('pauseButton');
298        pauseButton.onclick = this.stopMusicFromButton;
299        pauseButton.id = this.id;
300        this.namedSubElements.pauseButton = pauseButton;
301
302        let songName = this.addText(this.shortName);
303        songName.classList.add("songName");
304        this.namedSubElements.songName = songName;
305
306        let songSlider = this.addSlider();
307        songSlider.classList.add('songProgressBar');
308        songSlider.id = this.id;
309        songSlider.onchange = this.updateSongTimeFromSlider;
310        songSlider.oninput = this.activelyMovingSlider;
311        songSlider.value = this.currentTime;
312        this.namedSubElements.songSlider = songSlider;
313
314        let songCurrentPosition = this.addText('0:00');
315        songCurrentPosition.classList.add('songCurrentPosition');
316        this.namedSubElements.songCurrentPosition = songCurrentPosition;
317
318        let songTotalLength = this.addText('0:00');
319        songTotalLength.classList.add('songTotalLength');
320        this.namedSubElements.songTotalLength = songTotalLength;
321
322        this.hide();
323    }
324
325    startMusic(inputFromSlider=false,cancelActivePlaylists=true) {
326        if (globs.musicManager.activeMusicHandle) {
327            globs.musicManager.activeMusicHandle.stopMusic()
328        }
329        if (cancelActivePlaylists && globs.musicManager.activePlaylistHandle) {
330            globs.musicManager.activePlaylistHandle.stop()
331        }
332        //If you play a song that's already at it's end, reset to it's beginning
333        if (this.duration && !inputFromSlider) {
334            if (2. > (this.duration-this.currentTime)) {
335                this.updateSongTime(0)
336            }
337        }
338        globs.musicManager.activeMusicHandle = this;
339
340        globs.musicManager.activeAudio.src = URL.createObjectURL(this.file);
341        globs.musicManager.activeAudio.load()
342        globs.musicManager.activeAudio.currentTime = this.currentTime;
343
344        this.active = true
345        this.div.classList.add("activeItem");
346        this.div.style.backgroundColor = "";
347        globs.musicManager.activeAudio.play();
348    }
349
350    startMusicFromButton(event) {
351        let musicHandle = globs.musicManager.musicHandles[this.id];
352        musicHandle.startMusic();
353    }
354
355    stopMusic() {
356        if (this.active) {
357            this.div.classList.remove("activeItem");
358            this.div.style.backgroundColor = "";
359            this.active = false
360            globs.musicManager.activeAudio.pause();
361            if (globs.musicManager.activePlaylistHandle) {
362                globs.musicManager.activePlaylistHandle.stop()
363            }
364        }
365    }
366    stopMusicFromButton(event) {
367        let musicHandle = globs.musicManager.musicHandles[this.id];
368        musicHandle.stopMusic();
369    }
370
371    updateSongTime(seconds) {
372        //Sets the slider to the given value. Value is seconds as a number
373        //Also sets the time object of this music id to match
374        this.namedSubElements.songSlider.value = seconds;
375        this.currentTime = seconds;
376        this.updateCurrentTimeLabel(seconds);
377    }
378
379    updateDuration(seconds) {
380        this.namedSubElements.songTotalLength.innerHTML = convertSecondsToMinutes(seconds);
381        this.namedSubElements.songSlider.max = seconds;
382        this.duration = seconds;
383    }
384
385    updateSongTimeFromSlider(event) {
386        //When the slider changes, set the current music time to match
387        let musicHandle = globs.musicManager.musicHandles[this.id];
388        let sliderValue = musicHandle.namedSubElements.songSlider.value;
389
390        musicHandle.currentTime = sliderValue;
391        musicHandle.updateCurrentTimeLabel(sliderValue);
392        //restart the music from the new time if it's the active song
393        if (musicHandle.active) {
394            musicHandle.startMusic(true)
395        }
396        musicHandle.unPauseAutoUpdates();
397    }
398
399    updateCurrentTimeLabel (seconds) {
400        this.namedSubElements.songCurrentPosition.innerHTML = convertSecondsToMinutes(seconds);
401    }
402
403    activelyMovingSlider (event) {
404        let musicHandle = globs.musicManager.musicHandles[this.id];
405        musicHandle.pauseAutoUpdates();
406        musicHandle.updateCurrentTimeLabel(musicHandle.namedSubElements.songSlider.value)
407    }
408
409    pauseAutoUpdates () {
410        //Pause automatic slider updates to prevent them happening while we are moving the slider
411        globs.musicManager.pauseAutoSliderUpdates = true;
412    }
413
414    unPauseAutoUpdates () {
415        globs.musicManager.pauseAutoSliderUpdates = false;
416    }
417}
418
419class PlaylistHandle extends DynamicDiv {
420        constructor (musicObject,id) {
421            super("musicMenu");
422            this.playlist = musicObject.playlist;
423            this.namedSubElements = {};
424            this.id = id;
425            this.active = false;
426
427            if (this.playlist.length > 20) {
428                this.shortName = this.playlist.slice(0,17)+'...'
429            } else {
430                this.shortName = this.playlist
431            }
432
433            let playlistExpanderButton = this.addButton("");
434            playlistExpanderButton.classList.add('songExpander');
435            playlistExpanderButton.onclick = this.toggleVisible;
436            playlistExpanderButton.id = this.id;
437            this.namedSubElements.playlistExpanderButton = playlistExpanderButton;
438
439            let shufflePlay = this.addButton("");
440            shufflePlay.classList.add('shufflePlay');
441            shufflePlay.onclick = this.shufflePlaylistFromButton;
442            shufflePlay.id = this.id;
443            this.namedSubElements.shufflePlay = shufflePlay;
444
445            let loopPlay = this.addButton("")
446            loopPlay.classList.add('loopPlay');
447            loopPlay.onclick = this.loopPlaylistFromButton;
448            loopPlay.id = this.id;
449            this.namedSubElements.loopPlay = loopPlay;
450
451            let playlistName = this.addText(this.shortName);
452            playlistName.classList.add("playlistName");
453            this.namedSubElements.playlistName = playlistName;
454        }
455
456        shufflePlaylist () {
457            if (globs.musicManager.activeMusicHandle) {
458                globs.musicManager.activeMusicHandle.stopMusic();
459            }
460            this.start();
461            let musicHandles = Array.from(globs.musicManager.categorizedPlaylists[this.playlist]);
462            musicHandles = shuffle(musicHandles);
463            musicHandles[0].startMusic(false,false)
464
465            musicHandles.shift()
466            globs.musicManager.songsToPlayNext = musicHandles;
467        }
468
469        shufflePlaylistFromButton (event) {
470            let playlistHandle = globs.musicManager.playlistHandles[this.id];
471            playlistHandle.shufflePlaylist()
472        }
473
474        loopPlaylist () {
475            if (globs.musicManager.activeMusicHandle) {
476                globs.musicManager.activeMusicHandle.stopMusic();
477            }
478            this.start();
479            let musicHandles = Array.from(globs.musicManager.categorizedPlaylists[this.playlist]);
480            musicHandles[0].startMusic(false,false)
481
482            musicHandles.shift()
483            globs.musicManager.songsToPlayNext = musicHandles;
484        }
485
486        loopPlaylistFromButton (event) {
487            let playlistHandle = globs.musicManager.playlistHandles[this.id];
488            playlistHandle.loopPlaylist();
489        }
490
491        start () {
492            this.div.classList.add("activeItem");
493            this.div.style.backgroundColor = "";
494            globs.musicManager.activePlaylistHandle = this;
495            this.active = true;
496        }
497
498        stop () {
499            if (this.active) {
500                this.div.classList.remove("activeItem");
501                this.div.style.backgroundColor = "";
502                globs.musicManager.activePlaylistHandle = null;
503                this.active = false;
504            }
505        }
506
507        toggleVisible (event) {
508            let playlist = globs.musicManager.playlistHandles[this.id];
509            let musicHandles = globs.musicManager.categorizedPlaylists[playlist.playlist];
510
511            for (let i=0;i<musicHandles.length;i+=1) {
512                musicHandles[i].toggleVisible()
513            }
514        }
515    }