From 4d73d99ebc848e2065b38672e882f994a1d561f4 Mon Sep 17 00:00:00 2001
From: RealBadAngel <mk@realbadangel.pl>
Date: Sat, 23 Feb 2013 16:01:35 +0100
Subject: Add sound volume controls to ingame menu

---
 src/CMakeLists.txt      |   1 +
 src/game.cpp            |   8 +++
 src/guiPauseMenu.cpp    |  18 ++++-
 src/guiPauseMenu.h      |   1 +
 src/guiVolumeChange.cpp | 181 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/guiVolumeChange.h   |  53 ++++++++++++++
 src/mainmenumanager.h   |   7 ++
 7 files changed, 268 insertions(+), 1 deletion(-)
 create mode 100644 src/guiVolumeChange.cpp
 create mode 100644 src/guiVolumeChange.h

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 17102e848..c06da20c4 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -297,6 +297,7 @@ set(minetest_SRCS
 	guiFormSpecMenu.cpp
 	guiPauseMenu.cpp
 	guiPasswordChange.cpp
+	guiVolumeChange.cpp
 	guiDeathScreen.cpp
 	guiChatConsole.cpp
 	guiCreateWorld.cpp
diff --git a/src/game.cpp b/src/game.cpp
index c77754b5f..86909ccf0 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -29,6 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "server.h"
 #include "guiPauseMenu.h"
 #include "guiPasswordChange.h"
+#include "guiVolumeChange.h"
 #include "guiFormSpecMenu.h"
 #include "guiTextInputMenu.h"
 #include "guiDeathScreen.h"
@@ -1519,6 +1520,13 @@ void the_game(
 			g_gamecallback->changepassword_requested = false;
 		}
 
+		if(g_gamecallback->changevolume_requested)
+		{
+			(new GUIVolumeChange(guienv, guiroot, -1,
+				&g_menumgr, &client))->drop();
+			g_gamecallback->changevolume_requested = false;
+		}
+
 		/* Process TextureSource's queue */
 		tsrc->processQueue();
 
diff --git a/src/guiPauseMenu.cpp b/src/guiPauseMenu.cpp
index f6cbf248f..9d44b2e82 100644
--- a/src/guiPauseMenu.cpp
+++ b/src/guiPauseMenu.cpp
@@ -79,6 +79,11 @@ void GUIPauseMenu::removeChildren()
 		if(e != NULL)
 			e->remove();
 	}
+	{
+		gui::IGUIElement *e = getElementFromId(262);
+		if(e != NULL)
+			e->remove();
+	}
 }
 
 void GUIPauseMenu::regenerateGui(v2u32 screensize)
@@ -108,7 +113,7 @@ void GUIPauseMenu::regenerateGui(v2u32 screensize)
 	*/
 	const s32 btn_height = 30;
 	const s32 btn_gap = 20;
-	const s32 btn_num = m_simple_singleplayer_mode ? 3 : 4;
+	const s32 btn_num = m_simple_singleplayer_mode ? 4 : 5;
 	s32 btn_y = size.Y/2-((btn_num*btn_height+(btn_num-1)*btn_gap))/2;
 	changeCtype("");
 	{
@@ -128,6 +133,13 @@ void GUIPauseMenu::regenerateGui(v2u32 screensize)
 		}
 		btn_y += btn_height + btn_gap;
 	}
+	{
+		core::rect<s32> rect(0, 0, 140, btn_height);
+		rect = rect + v2s32(size.X/2-140/2, btn_y);
+		Environment->addButton(rect, this, 262,
+			wgettext("Sound Volume"));
+	}
+	btn_y += btn_height + btn_gap;
 	{
 		core::rect<s32> rect(0, 0, 140, btn_height);
 		rect = rect + v2s32(size.X/2-140/2, btn_y);
@@ -236,6 +248,10 @@ bool GUIPauseMenu::OnEvent(const SEvent& event)
 				quitMenu();
 				m_gamecallback->changePassword();
 				return true;
+			case 262:
+				quitMenu();
+				m_gamecallback->changeVolume();
+				return true;
 			case 260: // disconnect
 				m_gamecallback->disconnect();
 				quitMenu();
diff --git a/src/guiPauseMenu.h b/src/guiPauseMenu.h
index 4b15fc74f..578f2a324 100644
--- a/src/guiPauseMenu.h
+++ b/src/guiPauseMenu.h
@@ -29,6 +29,7 @@ public:
 	virtual void exitToOS() = 0;
 	virtual void disconnect() = 0;
 	virtual void changePassword() = 0;
+	virtual void changeVolume() = 0;
 };
 
 class GUIPauseMenu : public GUIModalMenu
diff --git a/src/guiVolumeChange.cpp b/src/guiVolumeChange.cpp
new file mode 100644
index 000000000..606aa0196
--- /dev/null
+++ b/src/guiVolumeChange.cpp
@@ -0,0 +1,181 @@
+/*
+Part of Minetest-c55
+Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2011 Ciaran Gultnieks <ciaran@ciarang.com>
+Copyright (C) 2013 RealBadAngel, Maciej Kasatkin <mk@realbadangel.pl>
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#include "guiVolumeChange.h"
+#include "debug.h"
+#include "serialization.h"
+#include <string>
+#include <IGUICheckBox.h>
+#include <IGUIButton.h>
+#include <IGUIScrollBar.h>
+#include <IGUIStaticText.h>
+#include <IGUIFont.h>
+#include "main.h"
+
+#include "gettext.h"
+
+const int ID_soundText1 = 263;
+const int ID_soundText2 = 264;
+const int ID_soundExitButton = 265;
+const int ID_soundSlider = 266;
+
+GUIVolumeChange::GUIVolumeChange(gui::IGUIEnvironment* env,
+		gui::IGUIElement* parent, s32 id,
+		IMenuManager *menumgr,
+		Client* client
+):
+	GUIModalMenu(env, parent, id, menumgr),
+	m_client(client)
+{
+}
+
+GUIVolumeChange::~GUIVolumeChange()
+{
+	removeChildren();
+}
+
+void GUIVolumeChange::removeChildren()
+{
+	{
+		gui::IGUIElement *e = getElementFromId(ID_soundText1);
+		if(e != NULL)
+			e->remove();
+	}
+	{
+		gui::IGUIElement *e = getElementFromId(ID_soundText2);
+		if(e != NULL)
+			e->remove();
+	}
+	{
+		gui::IGUIElement *e = getElementFromId(ID_soundExitButton);
+		if(e != NULL)
+			e->remove();
+	}
+	{
+		gui::IGUIElement *e = getElementFromId(ID_soundSlider);
+		if(e != NULL)
+			e->remove();
+	}
+}
+
+void GUIVolumeChange::regenerateGui(v2u32 screensize)
+{
+	/*
+		Remove stuff
+	*/
+	removeChildren();
+	
+	/*
+		Calculate new sizes and positions
+	*/
+	core::rect<s32> rect(
+			screensize.X/2 - 380/2,
+			screensize.Y/2 - 200/2,
+			screensize.X/2 + 380/2,
+			screensize.Y/2 + 200/2
+	);
+	
+	DesiredRect = rect;
+	recalculateAbsolutePosition(false);
+
+	v2s32 size = rect.getSize();
+	v2s32 topleft_client(40, 0);
+	v2s32 size_client = size - v2s32(40, 0);
+	int volume=(int)(g_settings->getFloat("sound_volume")*100);
+	/*
+		Add stuff
+	*/
+	changeCtype("");
+	{
+		core::rect<s32> rect(0, 0, 120, 20);
+		rect = rect + v2s32(size.X/2-60, size.Y/2-35);
+		Environment->addStaticText(wgettext("Sound Volume: "), rect, false,
+				true, this, ID_soundText1);
+	}
+	{
+		core::rect<s32> rect(0, 0, 30, 20);
+		rect = rect + v2s32(size.X/2+40, size.Y/2-35);
+		Environment->addStaticText(core::stringw(volume).c_str(), rect, false,
+				true, this, ID_soundText2);
+	}
+	{
+		core::rect<s32> rect(0, 0, 80, 30);
+		rect = rect + v2s32(size.X/2-80/2, size.Y/2+55);
+		Environment->addButton(rect, this, ID_soundExitButton,
+			wgettext("Exit"));
+	}
+	{
+		core::rect<s32> rect(0, 0, 300, 20);
+		rect = rect + v2s32(size.X/2-150, size.Y/2);
+		gui::IGUIScrollBar *e = Environment->addScrollBar(true,
+			rect, this, ID_soundSlider);
+		e->setMax(100);
+		e->setPos(volume);
+	}
+	changeCtype("");
+}
+
+void GUIVolumeChange::drawMenu()
+{
+	gui::IGUISkin* skin = Environment->getSkin();
+	if (!skin)
+		return;
+	video::IVideoDriver* driver = Environment->getVideoDriver();
+	video::SColor bgcolor(140,0,0,0);
+	driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
+	gui::IGUIElement::draw();
+}
+
+bool GUIVolumeChange::OnEvent(const SEvent& event)
+{
+	if(event.EventType==EET_KEY_INPUT_EVENT)
+	{
+		if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
+		{
+			quitMenu();
+			return true;
+		}
+		if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
+		{
+			quitMenu();
+			return true;
+		}
+	}
+	if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
+		{
+			if (event.GUIEvent.Caller->getID() == ID_soundExitButton)
+				{
+					quitMenu();
+					return true;
+				}
+		}
+	if(event.GUIEvent.EventType==gui::EGET_SCROLL_BAR_CHANGED)
+		{
+		if (event.GUIEvent.Caller->getID() == ID_soundSlider)
+			{
+				s32 pos = ((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
+				g_settings->setFloat("sound_volume",(float)pos/100);
+				gui::IGUIElement *e = getElementFromId(ID_soundText2);
+				e->setText( core::stringw(pos).c_str() );
+				return true;
+			}
+		}
+	return Parent ? Parent->OnEvent(event) : false;
+}
+
diff --git a/src/guiVolumeChange.h b/src/guiVolumeChange.h
new file mode 100644
index 000000000..a0a50f393
--- /dev/null
+++ b/src/guiVolumeChange.h
@@ -0,0 +1,53 @@
+/*
+Part of Minetest-c55
+Copyright (C) 2010-11 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2011 Ciaran Gultnieks <ciaran@ciarang.com>
+Copyright (C) 2013 RealBadAngel, Maciej Kasatkin <mk@realbadangel.pl>
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#ifndef GUIVOLUMECHANGE_HEADER
+#define GUIVOLUMECHANGE_HEADER
+
+#include "irrlichttypes_extrabloated.h"
+#include "modalMenu.h"
+#include "client.h"
+#include <string>
+
+class GUIVolumeChange : public GUIModalMenu
+{
+public:
+	GUIVolumeChange(gui::IGUIEnvironment* env,
+			gui::IGUIElement* parent, s32 id,
+			IMenuManager *menumgr,
+			Client* client);
+	~GUIVolumeChange();
+	
+	void removeChildren();
+	/*
+		Remove and re-add (or reposition) stuff
+	*/
+	void regenerateGui(v2u32 screensize);
+
+	void drawMenu();
+
+	bool OnEvent(const SEvent& event);
+	
+private:
+	Client* m_client;
+
+};
+
+#endif
+
diff --git a/src/mainmenumanager.h b/src/mainmenumanager.h
index a37a8300f..dc2b287d4 100644
--- a/src/mainmenumanager.h
+++ b/src/mainmenumanager.h
@@ -94,6 +94,7 @@ public:
 	MainGameCallback(IrrlichtDevice *a_device):
 		disconnect_requested(false),
 		changepassword_requested(false),
+		changevolume_requested(false),
 		device(a_device)
 	{
 	}
@@ -113,8 +114,14 @@ public:
 		changepassword_requested = true;
 	}
 
+	virtual void changeVolume()
+	{
+		changevolume_requested = true;
+	}
+	
 	bool disconnect_requested;
 	bool changepassword_requested;
+	bool changevolume_requested;
 	IrrlichtDevice *device;
 };
 
-- 
cgit v1.2.3