]> git.lizzy.rs Git - minetest.git/blobdiff - src/guiVolumeChange.cpp
FormSpec : Add an auto vertical scrollbar to the textarea
[minetest.git] / src / guiVolumeChange.cpp
index 99b302dd7783e1ef4d5ab12b57fe64aab3025f52..8c462312bcc4c85bcaea1e99f9b3c84fadae6673 100644 (file)
@@ -1,7 +1,7 @@
 /*
 Part of Minetest
-Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
-Copyright (C) 2011 Ciaran Gultnieks <ciaran@ciarang.com>
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2013 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
@@ -26,22 +26,20 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #include <IGUIScrollBar.h>
 #include <IGUIStaticText.h>
 #include <IGUIFont.h>
-#include "main.h"
+#include "settings.h"
 
 #include "gettext.h"
 
-const int ID_soundText1 = 263;
-const int ID_soundText2 = 264;
-const int ID_soundExitButton = 265;
-const int ID_soundSlider = 266;
+const int ID_soundText = 263;
+const int ID_soundExitButton = 264;
+const int ID_soundSlider = 265;
+const int ID_soundMuteButton = 266;
 
 GUIVolumeChange::GUIVolumeChange(gui::IGUIEnvironment* env,
                gui::IGUIElement* parent, s32 id,
-               IMenuManager *menumgr,
-               Client* client
+               IMenuManager *menumgr
 ):
-       GUIModalMenu(env, parent, id, menumgr),
-       m_client(client)
+       GUIModalMenu(env, parent, id, menumgr)
 {
 }
 
@@ -52,26 +50,14 @@ GUIVolumeChange::~GUIVolumeChange()
 
 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();
-       }
+       if (gui::IGUIElement *e = getElementFromId(ID_soundText))
+               e->remove();
+
+       if (gui::IGUIElement *e = getElementFromId(ID_soundExitButton))
+               e->remove();
+
+       if (gui::IGUIElement *e = getElementFromId(ID_soundSlider))
+               e->remove();
 }
 
 void GUIVolumeChange::regenerateGui(v2u32 screensize)
@@ -80,55 +66,60 @@ 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 = core::rect<s32>(
+               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);
+       v2s32 size = DesiredRect.getSize();
+       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, 160, 20);
+               rect = rect + v2s32(size.X / 2 - 80, size.Y / 2 - 70);
+
+               const wchar_t *text = wgettext("Sound Volume: ");
+               core::stringw volume_text = text;
+               delete [] text;
+
+               volume_text += core::stringw(volume) + core::stringw("%");
+               Environment->addStaticText(volume_text.c_str(), rect, false,
+                               true, this, ID_soundText);
        }
        {
                core::rect<s32> rect(0, 0, 80, 30);
                rect = rect + v2s32(size.X/2-80/2, size.Y/2+55);
+               const wchar_t *text = wgettext("Exit");
                Environment->addButton(rect, this, ID_soundExitButton,
-                       wgettext("Exit"));
+                       text);
+               delete[] text;
        }
        {
                core::rect<s32> rect(0, 0, 300, 20);
-               rect = rect + v2s32(size.X/2-150, size.Y/2);
+               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("");
+       {
+               core::rect<s32> rect(0, 0, 160, 20);
+               rect = rect + v2s32(size.X / 2 - 80, size.Y / 2 - 35);
+               const wchar_t *text = wgettext("Muted");
+               Environment->addCheckBox(g_settings->getBool("mute_sound"), rect, this,
+                               ID_soundMuteButton, text);
+               delete[] text;
+       }
 }
 
 void GUIVolumeChange::drawMenu()
@@ -137,45 +128,69 @@ void GUIVolumeChange::drawMenu()
        if (!skin)
                return;
        video::IVideoDriver* driver = Environment->getVideoDriver();
-       video::SColor bgcolor(140,0,0,0);
+       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)
-               {
+       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)
-               {
+
+               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;
-                               }
+       } else if (event.EventType == EET_GUI_EVENT) {
+               if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) {
+                       gui::IGUIElement *e = getElementFromId(ID_soundMuteButton);
+                       if (e != NULL && e->getType() == gui::EGUIET_CHECK_BOX) {
+                               g_settings->setBool("mute_sound", ((gui::IGUICheckBox*)e)->isChecked());
+                       }
+
+                       Environment->setFocus(this);
+                       return true;
+               }
+
+               if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
+                       if (event.GUIEvent.Caller->getID() == ID_soundExitButton) {
+                               quitMenu();
+                               return true;
+                       }
+                       Environment->setFocus(this);
                }
-       if(event.GUIEvent.EventType==gui::EGET_SCROLL_BAR_CHANGED)
-               {
-               if (event.GUIEvent.Caller->getID() == ID_soundSlider)
-                       {
+
+               if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
+                               && isVisible()) {
+                       if (!canTakeFocus(event.GUIEvent.Element)) {
+                               dstream << "GUIMainMenu: Not allowing focus change."
+                               << std::endl;
+                               // Returning true disables focus change
+                               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() );
+                               g_settings->setFloat("sound_volume", (float) pos / 100);
+
+                               gui::IGUIElement *e = getElementFromId(ID_soundText);
+                               const wchar_t *text = wgettext("Sound Volume: ");
+                               core::stringw volume_text = text;
+                               delete [] text;
+
+                               volume_text += core::stringw(pos) + core::stringw("%");
+                               e->setText(volume_text.c_str());
                                return true;
                        }
                }
+
+       }
+
        return Parent ? Parent->OnEvent(event) : false;
 }