From 04b1944939d14b86ad51a3636eda8e6b7883eecd Mon Sep 17 00:00:00 2001 From: Krzysztof Dermont Date: Sun, 5 Jul 2020 13:39:39 +0200 Subject: [PATCH] Add option to mute sounds when game is not focused Closes #823 --- src/common/event.cpp | 1 + src/common/event.h | 1 + src/common/restext.cpp | 1 + src/common/settings.cpp | 11 ++++++++++ src/common/settings.h | 3 +++ src/level/robotmain.cpp | 30 ++++++++++++++++++++++++++++ src/ui/screen/screen_setup_sound.cpp | 22 ++++++++++++++++++++ 7 files changed, 69 insertions(+) diff --git a/src/common/event.cpp b/src/common/event.cpp index 2c9b6dad..ebb6ed0a 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -220,6 +220,7 @@ void InitializeEventTypeTexts() EVENT_TYPE_TEXT[EVENT_INTERFACE_INVERTY] = "EVENT_INTERFACE_INVERTY"; EVENT_TYPE_TEXT[EVENT_INTERFACE_EFFECT] = "EVENT_INTERFACE_EFFECT"; EVENT_TYPE_TEXT[EVENT_INTERFACE_BGPAUSE] = "EVENT_INTERFACE_BGPAUSE"; + EVENT_TYPE_TEXT[EVENT_INTERFACE_BGMUTE] = "EVENT_INTERFACE_BGMUTE"; EVENT_TYPE_TEXT[EVENT_INTERFACE_FOG] = "EVENT_INTERFACE_FOG"; EVENT_TYPE_TEXT[EVENT_INTERFACE_EDITMODE]= "EVENT_INTERFACE_EDITMODE"; EVENT_TYPE_TEXT[EVENT_INTERFACE_EDITVALUE]= "EVENT_INTERFACE_EDITVALUE"; diff --git a/src/common/event.h b/src/common/event.h index 2fcd0628..053b9877 100644 --- a/src/common/event.h +++ b/src/common/event.h @@ -257,6 +257,7 @@ enum EventType EVENT_INTERFACE_INVERTY = 469, EVENT_INTERFACE_EFFECT = 470, EVENT_INTERFACE_BGPAUSE = 471, + EVENT_INTERFACE_BGMUTE = 472, EVENT_INTERFACE_FOG = 474, EVENT_INTERFACE_EDITMODE= 476, EVENT_INTERFACE_EDITVALUE= 477, diff --git a/src/common/restext.cpp b/src/common/restext.cpp index 09975f4e..2b04f3ac 100644 --- a/src/common/restext.cpp +++ b/src/common/restext.cpp @@ -207,6 +207,7 @@ void InitializeRestext() stringsEvent[EVENT_INTERFACE_INVERTY] = TR("Mouse inversion Y\\Inversion of the scrolling direction on the Y axis"); stringsEvent[EVENT_INTERFACE_EFFECT] = TR("Quake at explosions\\The screen shakes at explosions"); stringsEvent[EVENT_INTERFACE_BGPAUSE] = TR("Pause in background\\Pause the game when the window is unfocused"); + stringsEvent[EVENT_INTERFACE_BGMUTE] = TR("Mute sounds in background\\Mute all game sounds when the window is unfocused"); stringsEvent[EVENT_INTERFACE_EDITMODE] = TR("Automatic indent\\When program editing"); stringsEvent[EVENT_INTERFACE_EDITVALUE] = TR("Big indent\\Indent 2 or 4 spaces per level defined by braces"); stringsEvent[EVENT_INTERFACE_SOLUCE4] = TR("Access to solutions\\Show program \"4: Solution\" in the exercises"); diff --git a/src/common/settings.cpp b/src/common/settings.cpp index 488666d2..873eafe1 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp @@ -40,6 +40,7 @@ CSettings::CSettings() m_soluce4 = true; m_movies = true; m_focusLostPause = true; + m_focusLostMute = true; m_fontSize = 19.0f; m_windowPos = Math::Point(0.15f, 0.17f); @@ -79,6 +80,7 @@ void CSettings::SaveSettings() GetConfigFile().SetBoolProperty("Setup", "Soluce4", m_soluce4); GetConfigFile().SetBoolProperty("Setup", "Movies", m_movies); GetConfigFile().SetBoolProperty("Setup", "FocusLostPause", m_focusLostPause); + GetConfigFile().SetBoolProperty("Setup", "FocusLostMute", m_focusLostMute); GetConfigFile().SetBoolProperty("Setup", "OldCameraScroll", camera->GetOldCameraScroll()); GetConfigFile().SetBoolProperty("Setup", "CameraInvertX", camera->GetCameraInvertX()); GetConfigFile().SetBoolProperty("Setup", "CameraInvertY", camera->GetCameraInvertY()); @@ -157,6 +159,7 @@ void CSettings::LoadSettings() GetConfigFile().GetBoolProperty("Setup", "Soluce4", m_soluce4); GetConfigFile().GetBoolProperty("Setup", "Movies", m_movies); GetConfigFile().GetBoolProperty("Setup", "FocusLostPause", m_focusLostPause); + GetConfigFile().GetBoolProperty("Setup", "FocusLostMute", m_focusLostMute); if (GetConfigFile().GetBoolProperty("Setup", "OldCameraScroll", bValue)) camera->SetOldCameraScroll(bValue); @@ -363,6 +366,14 @@ bool CSettings::GetFocusLostPause() return m_focusLostPause; } +void CSettings::SetFocusLostMute(bool focusLostMute) +{ + m_focusLostMute = focusLostMute; +} +bool CSettings::GetFocusLostMute() +{ + return m_focusLostMute; +} void CSettings::SetFontSize(float size) { diff --git a/src/common/settings.h b/src/common/settings.h index 6d085b29..9d4dc22d 100644 --- a/src/common/settings.h +++ b/src/common/settings.h @@ -56,6 +56,8 @@ public: void SetFocusLostPause(bool focusLostPause); bool GetFocusLostPause(); + void SetFocusLostMute(bool focusLostMute); + bool GetFocusLostMute(); //! Managing the size of the default fonts //@{ @@ -97,6 +99,7 @@ protected: bool m_soluce4; bool m_movies; bool m_focusLostPause; + bool m_focusLostMute; float m_fontSize; Math::Point m_windowPos; diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index 5e3f42e3..4aeb6937 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -714,6 +714,12 @@ bool CRobotMain::ProcessEvent(Event &event) { m_focusPause = m_pause->ActivatePause(PAUSE_ENGINE); } + + if (m_settings->GetFocusLostMute()) + { + m_sound->SetAudioVolume(0); + m_sound->SetMusicVolume(0); + } return false; } @@ -725,6 +731,30 @@ bool CRobotMain::ProcessEvent(Event &event) m_pause->DeactivatePause(m_focusPause); m_focusPause = nullptr; } + + if (m_settings->GetFocusLostMute()) + { + int volume; + // Set music volume + if (GetConfigFile().GetIntProperty("Setup", "MusicVolume", volume)) + { + m_sound->SetMusicVolume(volume); + } + else + { + m_sound->SetMusicVolume(100); + } + // Set audio volume + if (GetConfigFile().GetIntProperty("Setup", "AudioVolume", volume)) + { + m_sound->SetAudioVolume(volume); + } + else + { + m_sound->SetAudioVolume(100); + } + } + return false; } diff --git a/src/ui/screen/screen_setup_sound.cpp b/src/ui/screen/screen_setup_sound.cpp index 28ad0b31..114150f9 100644 --- a/src/ui/screen/screen_setup_sound.cpp +++ b/src/ui/screen/screen_setup_sound.cpp @@ -33,6 +33,7 @@ #include "ui/controls/interface.h" #include "ui/controls/label.h" #include "ui/controls/slider.h" +#include "ui/controls/check.h" #include "ui/controls/window.h" namespace Ui @@ -53,6 +54,7 @@ void CScreenSetupSound::CreateInterface() CLabel* pl; CSlider* psl; CButton* pb; + CCheck* pc; Math::Point pos, ddim; std::string name; @@ -96,6 +98,13 @@ void CScreenSetupSound::CreateInterface() pb = pw->CreateButton(pos, ddim, -1, EVENT_INTERFACE_NOISY); pb->SetState(STATE_SHADOW); + ddim.x = dim.x*6; + ddim.y = dim.y*0.5f; + pos.x = ox+sx*10; + pos.y = 0.55f; + pc = pw->CreateCheck(pos, ddim, -1, EVENT_INTERFACE_BGMUTE); + pc->SetState(STATE_SHADOW); + UpdateSetupButtons(); } @@ -121,6 +130,12 @@ bool CScreenSetupSound::EventProcess(const Event &event) UpdateSetupButtons(); break; + case EVENT_INTERFACE_BGMUTE: + m_settings->SetFocusLostMute(!m_settings->GetFocusLostMute()); + ChangeSetupButtons(); + UpdateSetupButtons(); + break; + default: return true; } @@ -133,6 +148,7 @@ void CScreenSetupSound::UpdateSetupButtons() { CWindow* pw; CSlider* ps; + CCheck* pc; float value; pw = static_cast(m_interface->SearchControl(EVENT_WINDOW5)); @@ -151,6 +167,12 @@ void CScreenSetupSound::UpdateSetupButtons() value = static_cast(m_sound->GetMusicVolume()); ps->SetVisibleValue(value); } + + pc = static_cast(pw->SearchControl(EVENT_INTERFACE_BGMUTE)); + if ( pc != nullptr ) + { + pc->SetState(STATE_CHECK, m_settings->GetFocusLostMute()); + } } // Updates the engine function of the buttons after the setup phase.