all repos — simplyrpg @ 05a250383b5037effb602fc3ca4d2bb1cdf4da90

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