import '@/components/settings.css' const getPercentage = (value) => parseInt(value.replace("%", "")) export default class Settings { #el #logoEl #defaultLogoImage #defaultFps = 60 #defaultRatio = 61.8 #defaultOpacity = 30 #defaulthideLogo = false #defaultBackgroundImage = getComputedStyle(document.body).backgroundImage #defaultInvertFilter = import.meta.env.VITE_INVERT_FILTER === "true" #defaultPadLeft = getPercentage(`${import.meta.env.VITE_VIEWPORT_LEFT}%`) #defaultPadRight = getPercentage(`${import.meta.env.VITE_VIEWPORT_RIGHT}%`) #defaultPadTop = getPercentage(`${import.meta.env.VITE_VIEWPORT_TOP}%`) #defaultPadBottom = getPercentage(`${import.meta.env.VITE_VIEWPORT_BOTTOM}%`) #defaultViewport = { debugRender: false, padLeft: `${this.#defaultPadLeft}%`, padRight: `${this.#defaultPadRight}%`, padTop: `${this.#defaultPadTop}%`, padBottom: `${this.#defaultPadBottom}%`, x: 0, y: 0, } #hidden = !((new URLSearchParams(window.location.search)).has("settings") || import.meta.env.MODE === 'development') #fps = this.#defaultFps #ratio = this.#defaultRatio #opacity = this.#defaultOpacity #padLeft = this.#defaultPadLeft #padRight = this.#defaultPadRight #padTop = this.#defaultPadTop #padBottom = this.#defaultPadBottom constructor(el, logoEl) { this.#el = el this.#logoEl = logoEl this.spinePlayer = null this.#defaultLogoImage = this.#logoEl.src this.#init() } #init() { const _this = this window.addEventListener("contextmenu", e => e.preventDefault()); document.addEventListener("gesturestart", e => e.preventDefault()); const resize = () => { _this.#resize(_this) } window.addEventListener("resize", resize, true); resize() this.#setLogoInvertFilter(this.#defaultInvertFilter) this.setLogoOpacity(this.#defaultOpacity) this.#insertHTML() } setFPS(value) { this.#fps = value this.spinePlayer.setFps(value) } setLogoDisplay(flag) { this.#logoEl.hidden = flag; } #resize(_this, value) { _this = _this || this _this.#logoEl.width = window.innerWidth / 2 * (value || _this.#ratio) / 100 } #setLogoInvertFilter(flag) { if (!flag) { this.#logoEl.style.filter = "invert(0)" } else { this.#logoEl.style.filter = "invert(1)" } } setLogo(src, invert_filter) { this.#logoEl.src = src this.#resize() this.#setLogoInvertFilter(invert_filter) } #readFile(e, onload, callback) { const file = e.target.files[0] if (!file) return const reader = new FileReader() reader.readAsDataURL(file); reader.onload = readerEvent => onload(readerEvent) callback() } setLogoImage(e) { this.#readFile( e, (readerEvent) => { const content = readerEvent.target.result; this.setLogo(content, false) }, () => document.getElementById("logo_image_clear").disabled = false ) } resetLogoImage() { this.setLogo(this.#defaultLogoImage, this.#defaultInvertFilter) document.getElementById("logo_image_clear").disabled = true } setLogoRatio(value) { this.#ratio = value this.#resize(this, value) } setLogoOpacity(value) { this.#logoEl.style.opacity = value / 100 this.#opacity = value } setBackgoundImage(v) { document.body.style.backgroundImage = v } setDefaultBackground(e) { const backgroundURL = `url("${import.meta.env.BASE_URL}assets/${e}")` if (document.getElementById("custom_background_clear").disabled && !document.body.style.backgroundImage.startsWith("url(\"file:")) { this.setBackgoundImage(backgroundURL) } this.#defaultBackgroundImage = backgroundURL } setBackground(e) { this.#readFile( e, (readerEvent) => { const content = readerEvent.target.result; this.setBackgoundImage(`url("${content}")`) }, () => document.getElementById("custom_background_clear").disabled = false ) } resetBackground() { this.setBackgoundImage(this.#defaultBackgroundImage) document.getElementById("custom_background").value = "" document.getElementById("custom_background_clear").disabled = true } positionPadding(key, value) { switch (key) { case "left": this.#padLeft = value this.spinePlayer.updateViewport({ padLeft: `${value}%`, padRight: `${this.#padRight}%`, padTop: `${this.#padTop}%`, padBottom: `${this.#padBottom}%`, }) break; case "right": this.#padRight = value this.spinePlayer.updateViewport({ padLeft: `${this.#padLeft}%`, padRight: `${value}%`, padTop: `${this.#padTop}%`, padBottom: `${this.#padBottom}%`, }) break; case "top": this.#padTop = value this.spinePlayer.updateViewport({ padLeft: `${this.#padLeft}%`, padRight: `${this.#padRight}%`, padTop: `${value}%`, padBottom: `${this.#padBottom}%`, }) break; case "bottom": this.#padBottom = value this.spinePlayer.updateViewport({ padLeft: `${this.#padLeft}%`, padRight: `${this.#padRight}%`, padTop: `${this.#padTop}%`, padBottom: `${value}%`, }) break; } } positionReset() { this.#padLeft = this.#defaultPadLeft this.#padRight = this.#defaultPadRight this.#padTop = this.#defaultPadTop this.#padBottom = this.#defaultPadBottom this.spinePlayer.updateViewport(this.#defaultViewport) document.getElementById("position_padding_left_slider").value = this.#defaultPadLeft document.getElementById("position_padding_left_input").value = this.#defaultPadLeft document.getElementById("position_padding_right_slider").value = this.#defaultPadRight document.getElementById("position_padding_right_input").value = this.#defaultPadRight document.getElementById("position_padding_top_slider").value = this.#defaultPadTop document.getElementById("position_padding_top_input").value = this.#defaultPadTop document.getElementById("position_padding_bottom_slider").value = this.#defaultPadBottom document.getElementById("position_padding_bottom_input").value = this.#defaultPadBottom } open() { this.#el.hidden = false; } close() { this.#el.hidden = true; } reset() { this.positionReset() this.setLogoRatio(this.#defaultRatio) document.getElementById("logo_ratio_slider").value = this.#defaultRatio document.getElementById("logo_ratio_input").value = this.#defaultRatio this.setLogoOpacity(this.#defaultOpacity) document.getElementById("logo_opacity_slider").value = this.#defaultOpacity document.getElementById("logo_opacity_input").value = this.#defaultOpacity this.resetLogoImage() this.resetBackground() this.setLogoDisplay(this.#defaulthideLogo) this.setFPS(this.#defaultFps) document.getElementById("fps_slider").value = this.#defaultFps document.getElementById("fps_input").value = this.#defaultFps this.spinePlayer.play() } #insertHTML() { this.#el.innerHTML = `