3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013 Ciaran Gultnieks <ciaran@ciarang.com>
5 Copyright (C) 2013 RealBadAngel, Maciej Kasatkin <mk@realbadangel.pl>
7 Permission to use, copy, modify, and distribute this software for any
8 purpose with or without fee is hereby granted, provided that the above
9 copyright notice and this permission notice appear in all copies.
11 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "guiVolumeChange.h"
22 #include "guiButton.h"
23 #include "serialization.h"
25 #include <IGUICheckBox.h>
26 #include <IGUIButton.h>
27 #include <IGUIScrollBar.h>
28 #include <IGUIStaticText.h>
34 const int ID_soundText = 263;
35 const int ID_soundExitButton = 264;
36 const int ID_soundSlider = 265;
37 const int ID_soundMuteButton = 266;
39 GUIVolumeChange::GUIVolumeChange(gui::IGUIEnvironment* env,
40 gui::IGUIElement* parent, s32 id,
41 IMenuManager *menumgr, ISimpleTextureSource *tsrc
43 GUIModalMenu(env, parent, id, menumgr),
48 GUIVolumeChange::~GUIVolumeChange()
53 void GUIVolumeChange::removeChildren()
55 if (gui::IGUIElement *e = getElementFromId(ID_soundText))
58 if (gui::IGUIElement *e = getElementFromId(ID_soundExitButton))
61 if (gui::IGUIElement *e = getElementFromId(ID_soundSlider))
64 if (gui::IGUIElement *e = getElementFromId(ID_soundMuteButton))
68 void GUIVolumeChange::regenerateGui(v2u32 screensize)
75 Calculate new sizes and positions
77 const float s = m_gui_scale;
78 DesiredRect = core::rect<s32>(
79 screensize.X / 2 - 380 * s / 2,
80 screensize.Y / 2 - 200 * s / 2,
81 screensize.X / 2 + 380 * s / 2,
82 screensize.Y / 2 + 200 * s / 2
84 recalculateAbsolutePosition(false);
86 v2s32 size = DesiredRect.getSize();
87 int volume = (int)(g_settings->getFloat("sound_volume") * 100);
93 core::rect<s32> rect(0, 0, 160 * s, 20 * s);
94 rect = rect + v2s32(size.X / 2 - 80 * s, size.Y / 2 - 70 * s);
96 const wchar_t *text = wgettext("Sound Volume: ");
97 core::stringw volume_text = text;
100 volume_text += core::stringw(volume) + core::stringw("%");
101 Environment->addStaticText(volume_text.c_str(), rect, false,
102 true, this, ID_soundText);
105 core::rect<s32> rect(0, 0, 80 * s, 30 * s);
106 rect = rect + v2s32(size.X / 2 - 80 * s / 2, size.Y / 2 + 55 * s);
107 const wchar_t *text = wgettext("Exit");
108 GUIButton::addButton(Environment, rect, m_tsrc, this, ID_soundExitButton, text);
112 core::rect<s32> rect(0, 0, 300 * s, 20 * s);
113 rect = rect + v2s32(size.X / 2 - 150 * s, size.Y / 2);
114 gui::IGUIScrollBar *e = Environment->addScrollBar(true,
115 rect, this, ID_soundSlider);
120 core::rect<s32> rect(0, 0, 160 * s, 20 * s);
121 rect = rect + v2s32(size.X / 2 - 80 * s, size.Y / 2 - 35 * s);
122 const wchar_t *text = wgettext("Muted");
123 Environment->addCheckBox(g_settings->getBool("mute_sound"), rect, this,
124 ID_soundMuteButton, text);
129 void GUIVolumeChange::drawMenu()
131 gui::IGUISkin* skin = Environment->getSkin();
134 video::IVideoDriver* driver = Environment->getVideoDriver();
135 video::SColor bgcolor(140, 0, 0, 0);
136 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
137 gui::IGUIElement::draw();
140 bool GUIVolumeChange::OnEvent(const SEvent& event)
142 if (event.EventType == EET_KEY_INPUT_EVENT) {
143 if (event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown) {
148 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
152 } else if (event.EventType == EET_GUI_EVENT) {
153 if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) {
154 gui::IGUIElement *e = getElementFromId(ID_soundMuteButton);
155 if (e != NULL && e->getType() == gui::EGUIET_CHECK_BOX) {
156 g_settings->setBool("mute_sound", ((gui::IGUICheckBox*)e)->isChecked());
159 Environment->setFocus(this);
163 if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
164 if (event.GUIEvent.Caller->getID() == ID_soundExitButton) {
168 Environment->setFocus(this);
171 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
173 if (!canTakeFocus(event.GUIEvent.Element)) {
174 dstream << "GUIMainMenu: Not allowing focus change."
176 // Returning true disables focus change
180 if (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED) {
181 if (event.GUIEvent.Caller->getID() == ID_soundSlider) {
182 s32 pos = ((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
183 g_settings->setFloat("sound_volume", (float) pos / 100);
185 gui::IGUIElement *e = getElementFromId(ID_soundText);
186 const wchar_t *text = wgettext("Sound Volume: ");
187 core::stringw volume_text = text;
190 volume_text += core::stringw(pos) + core::stringw("%");
191 e->setText(volume_text.c_str());
198 return Parent ? Parent->OnEvent(event) : false;