From c9b9398348bb95602f17ff6c97db5db3acb6702c Mon Sep 17 00:00:00 2001 From: UnicornFreedom Date: Tue, 10 May 2022 16:55:52 +0200 Subject: [PATCH] Apply settings changes in real time --- .../desktop/ui/widget/SettingsDialog.scala | 45 +++++++++++-------- .../ocelot/desktop/util/SettingsData.scala | 5 +++ 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/main/scala/ocelot/desktop/ui/widget/SettingsDialog.scala b/src/main/scala/ocelot/desktop/ui/widget/SettingsDialog.scala index 6378f01..b58f6ca 100644 --- a/src/main/scala/ocelot/desktop/ui/widget/SettingsDialog.scala +++ b/src/main/scala/ocelot/desktop/ui/widget/SettingsDialog.scala @@ -12,17 +12,20 @@ class SettingsDialog extends ModalDialog { private val menu = new VerticalMenu menu.addEntry(new VerticalMenuButton("Sound", () => {})) - private val settingsData = new SettingsData() - settingsData.updateWith(Settings.get) + private val settingsBackup = new SettingsData(Settings.get) private def applySettings(): Unit = { - Settings.get.updateWith(settingsData) ResourceManager.forEach { case soundSource: SoundSource => soundSource.setVolume(Settings.get.volumeEnvironment * Settings.get.volumeMaster) case _ => } } + private def resetSettings(): Unit = { + Settings.get.updateWith(settingsBackup) + applySettings() + } + children :+= new PaddingBox(new Widget { override val layout = new LinearLayout(this, orientation = Orientation.Horizontal) @@ -34,19 +37,28 @@ class SettingsDialog extends ModalDialog { children :+= new PaddingBox(new Widget { override val layout = new LinearLayout(this, orientation = Orientation.Vertical) - children :+= new PaddingBox(new Slider(settingsData.volumeMaster, "Master Volume") { + children :+= new PaddingBox(new Slider(Settings.get.volumeMaster, "Master Volume") { override def minimumSize: Size2D = Size2D(512, 24) - override def onValueChanged(value: Float): Unit = settingsData.volumeMaster = value + override def onValueChanged(value: Float): Unit = { + Settings.get.volumeMaster = value + applySettings() + } }, Padding2D(bottom = 8)) - children :+= new PaddingBox(new Slider(settingsData.volumeBeep, "Beep Volume") { + children :+= new PaddingBox(new Slider(Settings.get.volumeBeep, "Beep Volume") { override def minimumSize: Size2D = Size2D(512, 24) - override def onValueChanged(value: Float): Unit = settingsData.volumeBeep = value + override def onValueChanged(value: Float): Unit = { + Settings.get.volumeBeep = value + applySettings() + } }, Padding2D(bottom = 8)) - children :+= new PaddingBox(new Slider(settingsData.volumeEnvironment, "Environment Volume") { + children :+= new PaddingBox(new Slider(Settings.get.volumeEnvironment, "Environment Volume") { override def minimumSize: Size2D = Size2D(512, 24) - override def onValueChanged(value: Float): Unit = settingsData.volumeEnvironment = value + override def onValueChanged(value: Float): Unit = { + Settings.get.volumeEnvironment = value + applySettings() + } }, Padding2D(bottom = 8)) children :+= new Widget { @@ -58,20 +70,15 @@ class SettingsDialog extends ModalDialog { children :+= new Button { override def text: String = "Ok" - override def onClick(): Unit = { - applySettings() - close() - } + override def onClick(): Unit = close() } children :+= new PaddingBox(new Button { override def text: String = "Cancel" - override def onClick(): Unit = close() - }, Padding2D(left = 8)) - - children :+= new PaddingBox(new Button { - override def text: String = "Apply" - override def onClick(): Unit = applySettings() + override def onClick(): Unit = { + resetSettings() + close() + } }, Padding2D(left = 8)) } }, Padding2D(left = 8)) diff --git a/src/main/scala/ocelot/desktop/util/SettingsData.scala b/src/main/scala/ocelot/desktop/util/SettingsData.scala index 8cd687e..d52a8e5 100644 --- a/src/main/scala/ocelot/desktop/util/SettingsData.scala +++ b/src/main/scala/ocelot/desktop/util/SettingsData.scala @@ -1,6 +1,11 @@ package ocelot.desktop.util class SettingsData { + def this(data: SettingsData) { + this() + updateWith(data) + } + var volumeMaster: Float = 1f var volumeBeep: Float = 1f var volumeEnvironment: Float = 1f