Add option to mute sounds when game is not focused

Closes #823
1164-fix
Krzysztof Dermont 2020-07-05 13:39:39 +02:00 committed by Mateusz Przybył
parent e108715760
commit 04b1944939
7 changed files with 69 additions and 0 deletions

View File

@ -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";

View File

@ -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,

View File

@ -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");

View File

@ -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)
{

View File

@ -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;

View File

@ -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;
}

View File

@ -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<CWindow*>(m_interface->SearchControl(EVENT_WINDOW5));
@ -151,6 +167,12 @@ void CScreenSetupSound::UpdateSetupButtons()
value = static_cast<float>(m_sound->GetMusicVolume());
ps->SetVisibleValue(value);
}
pc = static_cast<CCheck*>(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.