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