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