Load the floppy sounds lazily

Closes #91
This commit is contained in:
Fingercomp 2023-06-14 21:32:45 +07:00
parent 633a0e0c4c
commit 8089a17fb6
No known key found for this signature in database
GPG Key ID: BBC71CEE45D86E37
2 changed files with 14 additions and 6 deletions

View File

@ -17,6 +17,8 @@ class SoundBuffer(val file: String) extends Resource with Logging {
} else { } else {
initFallback() initFallback()
} }
} else {
logger.debug(s"Skipping loading sound buffer from '$file' because audio is not available")
} }
} }
} }

View File

@ -1,28 +1,34 @@
package ocelot.desktop.ui.widget.slot package ocelot.desktop.ui.widget.slot
import ocelot.desktop.audio.{SoundBuffers, SoundCategory, SoundSource} import ocelot.desktop.audio.{Audio, SoundBuffers, SoundCategory, SoundSource}
import ocelot.desktop.graphics.{IconDef, Icons} import ocelot.desktop.graphics.{IconDef, Icons}
import ocelot.desktop.inventory.Inventory import ocelot.desktop.inventory.Inventory
import ocelot.desktop.inventory.item.FloppyItem import ocelot.desktop.inventory.item.FloppyItem
class FloppySlotWidget(slot: Inventory#Slot) extends SlotWidget[FloppyItem](slot) { class FloppySlotWidget(slot: Inventory#Slot) extends SlotWidget[FloppyItem](slot) {
private val soundFloppyInsert = SoundSource.fromBuffer(SoundBuffers.MachineFloppyInsert, SoundCategory.Environment) private lazy val soundFloppyInsert =
private val soundFloppyEject = SoundSource.fromBuffer(SoundBuffers.MachineFloppyEject, SoundCategory.Environment) SoundSource.fromBuffer(SoundBuffers.MachineFloppyInsert, SoundCategory.Environment)
private lazy val soundFloppyEject = SoundSource.fromBuffer(SoundBuffers.MachineFloppyEject, SoundCategory.Environment)
override def ghostIcon: Option[IconDef] = Some(Icons.FloppyIcon) override def ghostIcon: Option[IconDef] = Some(Icons.FloppyIcon)
override def onItemAdded(): Unit = { override def onItemAdded(): Unit = {
super.onItemAdded() super.onItemAdded()
// TODO: don't play the sound when loading a workspace // TODO: don't play the sound when loading a workspace
soundFloppyInsert.play() if (!Audio.isDisabled) {
soundFloppyInsert.play()
}
} }
override def onItemRemoved( override def onItemRemoved(
removedItem: FloppyItem, removedItem: FloppyItem,
replacedBy: Option[FloppyItem] replacedBy: Option[FloppyItem],
): Unit = { ): Unit = {
super.onItemRemoved(removedItem, replacedBy) super.onItemRemoved(removedItem, replacedBy)
// TODO: don't play the sound when loading a workspace // TODO: don't play the sound when loading a workspace
soundFloppyEject.play() if (!Audio.isDisabled) {
soundFloppyEject.play()
}
} }
} }