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