Reformat sfx.core.js with tabs instead of spaces

This commit is contained in:
SrGooglo 2025-04-24 10:17:43 +00:00
parent 8e466fedf4
commit 681de1d5e7
2 changed files with 94 additions and 77 deletions

@ -1 +1 @@
Subproject commit ff38d45b9686ccbd2e902477bde4cd7eb7d251e8 Subproject commit 57d8b4bed14b0b35d1d9753847ac39710e0d9be5

View File

@ -6,102 +6,119 @@ import store from "store"
import config from "@config" import config from "@config"
export default class SFXCore extends Core { export default class SFXCore extends Core {
static namespace = "sfx" static namespace = "sfx"
soundsPool = {} soundsPool = {}
public = { public = {
loadSoundpack: this.loadSoundpack.bind(this), loadSoundpack: this.loadSoundpack.bind(this),
play: this.play, play: this.play,
} }
onEvents = { onEvents = {
"sfx:test": (volume) => { "sfx:test": (volume) => {
// play a sound to test volume // play a sound to test volume
this.play("test", { this.play("test", {
volume: volume / 100, volume: volume / 100,
}) })
} },
} }
async loadSoundpack(soundpack) { async loadSoundpack(soundpack) {
if (!soundpack) { if (!soundpack) {
soundpack = store.get("soundpack") soundpack = store.get("soundpack")
} }
if (!soundpack) { if (!soundpack) {
soundpack = config.defaultSoundPack ?? {} soundpack = config.defaultSoundPack ?? {}
} }
// check if is valid url with regex // check if is valid url with regex
const urlRegex = /^(http|https):\/\/[^ "]+$/; const urlRegex = /^(http|https):\/\/[^ "]+$/
if (urlRegex.test(soundpack)) { if (urlRegex.test(soundpack)) {
const { data } = await axios.get(soundpack) const { data } = await axios.get(soundpack)
soundpack = data soundpack = data
} }
if (typeof soundpack.sounds !== "object") { if (typeof soundpack.sounds !== "object") {
this.console.error(`Soundpack [${soundpack.id}] is not a valid soundpack.`) this.console.error(
return false `Soundpack [${soundpack.id}] is not a valid soundpack.`,
} )
return false
}
this.console.log(`Loading soundpack [${soundpack.id} | ${soundpack.name}] by ${soundpack.author} (${soundpack.version})`) this.console.log(
`Loading soundpack [${soundpack.id} | ${soundpack.name}] by ${soundpack.author} (${soundpack.version})`,
)
for (const [name, path] of Object.entries(soundpack.sounds)) { for (const [name, path] of Object.entries(soundpack.sounds)) {
this.soundsPool[name] = new Howl({ this.soundsPool[name] = new Howl({
volume: 0.5, volume: 0.5,
src: [path], src: [path],
}) })
} }
} }
async play(name, options = {}) { async play(name, options = {}) {
if (!window.app.cores.settings.is("ui.effects", true)) { if (!window.app.cores.settings.is("ui.effects", true)) {
return false return false
} }
const audioInstance = this.soundsPool[name] const audioInstance = this.soundsPool[name]
if (!audioInstance) { if (!audioInstance) {
return false return false
} }
if (typeof options.volume !== "undefined") { if (typeof options.volume !== "undefined") {
audioInstance.volume(options.volume) audioInstance.volume(options.volume)
} else { } else {
audioInstance.volume((window.app.cores.settings.get("ui.general_volume") ?? 0) / 100) audioInstance.volume(
} (window.app.cores.settings.get("ui.general_volume") ?? 0) / 100,
)
}
audioInstance.play() audioInstance.play()
} }
async handleClick(event) { async handleClick(event) {
// search for closest button // search for closest button
const button = event.target.closest("button") || event.target.closest(".ant-btn") const button =
event.target.closest("button") || event.target.closest(".ant-btn")
// search for a slider // search for a slider
const slider = event.target.closest("input[type=range]") const slider = event.target.closest("input[type=range]")
// if button exist and has aria-checked attribute then play switch_on or switch_off // if button exist and has aria-checked attribute then play switch_on or switch_off
if (button) { if (button) {
if (button.hasAttribute("aria-checked")) { if (button.hasAttribute("aria-checked")) {
return this.play(button.getAttribute("aria-checked") === "true" ? "component.switch_off" : "component.switch_on") return this.play(
} button.getAttribute("aria-checked") === "true"
? "component.switch_off"
: "component.switch_on",
)
}
return this.play("generic_click") return this.play("generic_click")
} }
if (slider) { if (slider) {
// check if is up or down // check if is up or down
this.console.log(slider) this.console.log(slider)
} }
} }
async onInitialize() { async onInitialize() {
await this.loadSoundpack() await this.loadSoundpack()
document.addEventListener("click", (...args) => { this.handleClick(...args) }, true) document.addEventListener(
} "click",
(...args) => {
this.handleClick(...args)
},
true,
)
}
} }