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