소스 검색

Merge pull request #2869 from Laserlicht/audio

disable audio on lost focus
Nordsoft91 2 년 전
부모
커밋
5a8c3b5ae2

+ 8 - 1
client/adventureMap/CInGameConsole.cpp

@@ -22,6 +22,7 @@
 #include "../gui/TextAlignment.h"
 #include "../render/Colors.h"
 #include "../render/Canvas.h"
+#include "../render/IScreenHandler.h"
 #include "../adventureMap/AdventureMapInterface.h"
 #include "../windows/CMessage.h"
 
@@ -105,7 +106,13 @@ void CInGameConsole::print(const std::string & txt)
 	}
 
 	GH.windows().totalRedraw(); // FIXME: ingame console has no parent widget set
-	CCS->soundh->playSound(AudioPath::builtin("CHAT"));
+
+	int volume = CCS->soundh->getVolume();
+	if(volume == 0)
+		CCS->soundh->setVolume(settings["general"]["sound"].Integer());
+	int handle = CCS->soundh->playSound(AudioPath::builtin("CHAT"));
+	if(volume == 0)
+		CCS->soundh->setCallback(handle, [&]() { if(!GH.screenHandler().hasFocus()) CCS->soundh->setVolume(0); });
 }
 
 bool CInGameConsole::captureThisKey(EShortcut key)

+ 13 - 4
client/adventureMap/CInfoBar.cpp

@@ -25,6 +25,7 @@
 #include "../PlayerLocalState.h"
 #include "../gui/CGuiHandler.h"
 #include "../gui/WindowHandler.h"
+#include "../render/IScreenHandler.h"
 
 #include "../../CCallback.h"
 #include "../../lib/CConfigHandler.h"
@@ -228,14 +229,22 @@ CInfoBar::VisibleComponentInfo::VisibleComponentInfo(const std::vector<Component
 
 void CInfoBar::playNewDaySound()
 {
+	int volume = CCS->soundh->getVolume();
+	int handle = -1;
+	if(volume == 0)
+		CCS->soundh->setVolume(settings["general"]["sound"].Integer());
+
 	if(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK) != 1) // not first day of the week
-		CCS->soundh->playSound(soundBase::newDay);
+		handle = CCS->soundh->playSound(soundBase::newDay);
 	else if(LOCPLINT->cb->getDate(Date::WEEK) != 1) // not first week in month
-		CCS->soundh->playSound(soundBase::newWeek);
+		handle = CCS->soundh->playSound(soundBase::newWeek);
 	else if(LOCPLINT->cb->getDate(Date::MONTH) != 1) // not first month
-		CCS->soundh->playSound(soundBase::newMonth);
+		handle = CCS->soundh->playSound(soundBase::newMonth);
 	else
-		CCS->soundh->playSound(soundBase::newDay);
+		handle = CCS->soundh->playSound(soundBase::newDay);
+
+	if(volume == 0)
+		CCS->soundh->setCallback(handle, [&]() { if(!GH.screenHandler().hasFocus()) CCS->soundh->setVolume(0); });
 }
 
 void CInfoBar::reset()

+ 13 - 0
client/eventsSDL/InputHandler.cpp

@@ -24,6 +24,7 @@
 #include "../CMT.h"
 #include "../CPlayerInterface.h"
 #include "../CGameInfo.h"
+#include "../CMusicHandler.h"
 
 #include "../../lib/CConfigHandler.h"
 
@@ -153,6 +154,18 @@ void InputHandler::preprocessEvent(const SDL_Event & ev)
 			}
 #endif
 			break;
+		case SDL_WINDOWEVENT_FOCUS_GAINED:
+			if(settings["general"]["enableUiEnhancements"].Bool()) {
+				CCS->musich->setVolume(settings["general"]["music"].Integer());
+				CCS->soundh->setVolume(settings["general"]["sound"].Integer());
+			}
+			break;
+		case SDL_WINDOWEVENT_FOCUS_LOST:
+			if(settings["general"]["enableUiEnhancements"].Bool()) {
+				CCS->musich->setVolume(0);
+				CCS->soundh->setVolume(0);
+			}
+			break;
 		}
 		return;
 	}

+ 3 - 0
client/render/IScreenHandler.h

@@ -40,4 +40,7 @@ public:
 
 	/// Dimensions of render output
 	virtual Point getRenderResolution() const = 0;
+
+	/// Window has focus
+	virtual bool hasFocus() = 0;
 };

+ 6 - 0
client/renderSDL/ScreenHandler.cpp

@@ -565,3 +565,9 @@ std::vector<Point> ScreenHandler::getSupportedResolutions( int displayIndex) con
 
 	return result;
 }
+
+bool ScreenHandler::hasFocus()
+{
+	ui32 flags = SDL_GetWindowFlags(mainWindow);
+	return flags & SDL_WINDOW_INPUT_FOCUS;
+}

+ 3 - 0
client/renderSDL/ScreenHandler.h

@@ -86,6 +86,9 @@ public:
 	/// Dimensions of render output, usually same as window size except for high-DPI screens on macOS / iOS
 	Point getRenderResolution() const final;
 
+	/// Window has focus
+	bool hasFocus() final;
+
 	std::vector<Point> getSupportedResolutions() const final;
 	std::vector<Point> getSupportedResolutions(int displayIndex) const;
 	std::tuple<int, int> getSupportedScalingRange() const final;