]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiVolumeChange.cpp
9428cde83cffcb05544cd26a0cdde7ab86bbbafd
[dragonfireclient.git] / src / gui / guiVolumeChange.cpp
1 /*
2 Part of Minetest
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>
6
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.
10
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.
18 */
19
20 #include "guiVolumeChange.h"
21 #include "debug.h"
22 #include "guiButton.h"
23 #include "serialization.h"
24 #include <string>
25 #include <IGUICheckBox.h>
26 #include <IGUIButton.h>
27 #include <IGUIScrollBar.h>
28 #include <IGUIStaticText.h>
29 #include <IGUIFont.h>
30 #include "settings.h"
31
32 #include "gettext.h"
33
34 const int ID_soundText = 263;
35 const int ID_soundExitButton = 264;
36 const int ID_soundSlider = 265;
37 const int ID_soundMuteButton = 266;
38
39 GUIVolumeChange::GUIVolumeChange(gui::IGUIEnvironment* env,
40                 gui::IGUIElement* parent, s32 id,
41                 IMenuManager *menumgr
42 ):
43         GUIModalMenu(env, parent, id, menumgr)
44 {
45 }
46
47 GUIVolumeChange::~GUIVolumeChange()
48 {
49         removeChildren();
50 }
51
52 void GUIVolumeChange::removeChildren()
53 {
54         if (gui::IGUIElement *e = getElementFromId(ID_soundText))
55                 e->remove();
56
57         if (gui::IGUIElement *e = getElementFromId(ID_soundExitButton))
58                 e->remove();
59
60         if (gui::IGUIElement *e = getElementFromId(ID_soundSlider))
61                 e->remove();
62
63         if (gui::IGUIElement *e = getElementFromId(ID_soundMuteButton))
64                 e->remove();
65 }
66
67 void GUIVolumeChange::regenerateGui(v2u32 screensize)
68 {
69         /*
70                 Remove stuff
71         */
72         removeChildren();
73         /*
74                 Calculate new sizes and positions
75         */
76         const float s = m_gui_scale;
77         DesiredRect = core::rect<s32>(
78                 screensize.X / 2 - 380 * s / 2,
79                 screensize.Y / 2 - 200 * s / 2,
80                 screensize.X / 2 + 380 * s / 2,
81                 screensize.Y / 2 + 200 * s / 2
82         );
83         recalculateAbsolutePosition(false);
84
85         v2s32 size = DesiredRect.getSize();
86         int volume = (int)(g_settings->getFloat("sound_volume") * 100);
87
88         /*
89                 Add stuff
90         */
91         {
92                 core::rect<s32> rect(0, 0, 160 * s, 20 * s);
93                 rect = rect + v2s32(size.X / 2 - 80 * s, size.Y / 2 - 70 * s);
94
95                 const wchar_t *text = wgettext("Sound Volume: ");
96                 core::stringw volume_text = text;
97                 delete [] text;
98
99                 volume_text += core::stringw(volume) + core::stringw("%");
100                 Environment->addStaticText(volume_text.c_str(), rect, false,
101                                 true, this, ID_soundText);
102         }
103         {
104                 core::rect<s32> rect(0, 0, 80 * s, 30 * s);
105                 rect = rect + v2s32(size.X / 2 - 80 * s / 2, size.Y / 2 + 55 * s);
106                 const wchar_t *text = wgettext("Exit");
107                 GUIButton::addButton(Environment, rect, this, ID_soundExitButton, text);
108                 delete[] text;
109         }
110         {
111                 core::rect<s32> rect(0, 0, 300 * s, 20 * s);
112                 rect = rect + v2s32(size.X / 2 - 150 * s, size.Y / 2);
113                 gui::IGUIScrollBar *e = Environment->addScrollBar(true,
114                         rect, this, ID_soundSlider);
115                 e->setMax(100);
116                 e->setPos(volume);
117         }
118         {
119                 core::rect<s32> rect(0, 0, 160 * s, 20 * s);
120                 rect = rect + v2s32(size.X / 2 - 80 * s, size.Y / 2 - 35 * s);
121                 const wchar_t *text = wgettext("Muted");
122                 Environment->addCheckBox(g_settings->getBool("mute_sound"), rect, this,
123                                 ID_soundMuteButton, text);
124                 delete[] text;
125         }
126 }
127
128 void GUIVolumeChange::drawMenu()
129 {
130         gui::IGUISkin* skin = Environment->getSkin();
131         if (!skin)
132                 return;
133         video::IVideoDriver* driver = Environment->getVideoDriver();
134         video::SColor bgcolor(140, 0, 0, 0);
135         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
136         gui::IGUIElement::draw();
137 }
138
139 bool GUIVolumeChange::OnEvent(const SEvent& event)
140 {
141         if (event.EventType == EET_KEY_INPUT_EVENT) {
142                 if (event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown) {
143                         quitMenu();
144                         return true;
145                 }
146
147                 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
148                         quitMenu();
149                         return true;
150                 }
151         } else if (event.EventType == EET_GUI_EVENT) {
152                 if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) {
153                         gui::IGUIElement *e = getElementFromId(ID_soundMuteButton);
154                         if (e != NULL && e->getType() == gui::EGUIET_CHECK_BOX) {
155                                 g_settings->setBool("mute_sound", ((gui::IGUICheckBox*)e)->isChecked());
156                         }
157
158                         Environment->setFocus(this);
159                         return true;
160                 }
161
162                 if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
163                         if (event.GUIEvent.Caller->getID() == ID_soundExitButton) {
164                                 quitMenu();
165                                 return true;
166                         }
167                         Environment->setFocus(this);
168                 }
169
170                 if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
171                                 && isVisible()) {
172                         if (!canTakeFocus(event.GUIEvent.Element)) {
173                                 dstream << "GUIMainMenu: Not allowing focus change."
174                                 << std::endl;
175                                 // Returning true disables focus change
176                                 return true;
177                         }
178                 }
179                 if (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED) {
180                         if (event.GUIEvent.Caller->getID() == ID_soundSlider) {
181                                 s32 pos = ((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
182                                 g_settings->setFloat("sound_volume", (float) pos / 100);
183
184                                 gui::IGUIElement *e = getElementFromId(ID_soundText);
185                                 const wchar_t *text = wgettext("Sound Volume: ");
186                                 core::stringw volume_text = text;
187                                 delete [] text;
188
189                                 volume_text += core::stringw(pos) + core::stringw("%");
190                                 e->setText(volume_text.c_str());
191                                 return true;
192                         }
193                 }
194
195         }
196
197         return Parent ? Parent->OnEvent(event) : false;
198 }