]> git.lizzy.rs Git - dragonfireclient.git/blob - src/guiVolumeChange.cpp
Fix OSX builds (closes #6289, fixes #6270) (#6306)
[dragonfireclient.git] / src / 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
37 GUIVolumeChange::GUIVolumeChange(gui::IGUIEnvironment* env,
38                 gui::IGUIElement* parent, s32 id,
39                 IMenuManager *menumgr
40 ):
41         GUIModalMenu(env, parent, id, menumgr)
42 {
43 }
44
45 GUIVolumeChange::~GUIVolumeChange()
46 {
47         removeChildren();
48 }
49
50 void GUIVolumeChange::removeChildren()
51 {
52         if (gui::IGUIElement *e = getElementFromId(ID_soundText))
53                 e->remove();
54
55         if (gui::IGUIElement *e = getElementFromId(ID_soundExitButton))
56                 e->remove();
57
58         if (gui::IGUIElement *e = getElementFromId(ID_soundSlider))
59                 e->remove();
60 }
61
62 void GUIVolumeChange::regenerateGui(v2u32 screensize)
63 {
64         /*
65                 Remove stuff
66         */
67         removeChildren();
68
69         /*
70                 Calculate new sizes and positions
71         */
72         DesiredRect = core::rect<s32>(
73                 screensize.X/2 - 380/2,
74                 screensize.Y/2 - 200/2,
75                 screensize.X/2 + 380/2,
76                 screensize.Y/2 + 200/2
77         );
78         recalculateAbsolutePosition(false);
79
80         v2s32 size = DesiredRect.getSize();
81         int volume = (int)(g_settings->getFloat("sound_volume") * 100);
82
83         /*
84                 Add stuff
85         */
86         {
87                 core::rect<s32> rect(0, 0, 160, 20);
88                 rect = rect + v2s32(size.X / 2 - 80, size.Y / 2 - 35);
89
90                 const wchar_t *text = wgettext("Sound Volume: ");
91                 core::stringw volume_text = text;
92                 delete [] text;
93
94                 volume_text += core::stringw(volume) + core::stringw("%");
95                 Environment->addStaticText(volume_text.c_str(), rect, false,
96                                 true, this, ID_soundText);
97         }
98         {
99                 core::rect<s32> rect(0, 0, 80, 30);
100                 rect = rect + v2s32(size.X/2-80/2, size.Y/2+55);
101                 const wchar_t *text = wgettext("Exit");
102                 Environment->addButton(rect, this, ID_soundExitButton,
103                         text);
104                 delete[] text;
105         }
106         {
107                 core::rect<s32> rect(0, 0, 300, 20);
108                 rect = rect + v2s32(size.X/2-150, size.Y/2);
109                 gui::IGUIScrollBar *e = Environment->addScrollBar(true,
110                         rect, this, ID_soundSlider);
111                 e->setMax(100);
112                 e->setPos(volume);
113         }
114 }
115
116 void GUIVolumeChange::drawMenu()
117 {
118         gui::IGUISkin* skin = Environment->getSkin();
119         if (!skin)
120                 return;
121         video::IVideoDriver* driver = Environment->getVideoDriver();
122         video::SColor bgcolor(140, 0, 0, 0);
123         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
124         gui::IGUIElement::draw();
125 }
126
127 bool GUIVolumeChange::OnEvent(const SEvent& event)
128 {
129         if (event.EventType == EET_KEY_INPUT_EVENT) {
130                 if (event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown) {
131                         quitMenu();
132                         return true;
133                 }
134
135                 if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
136                         quitMenu();
137                         return true;
138                 }
139         }
140
141         if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
142                 if (event.GUIEvent.Caller->getID() == ID_soundExitButton) {
143                         quitMenu();
144                         return true;
145                 }
146         }
147
148         if (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED) {
149                 if (event.GUIEvent.Caller->getID() == ID_soundSlider) {
150                         s32 pos = ((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
151                         g_settings->setFloat("sound_volume", (float) pos / 100);
152
153                         gui::IGUIElement *e = getElementFromId(ID_soundText);
154                         const wchar_t *text = wgettext("Sound Volume: ");
155                         core::stringw volume_text = text;
156                         delete [] text;
157
158                         volume_text += core::stringw(pos) + core::stringw("%");
159                         e->setText(volume_text.c_str());
160                         return true;
161                 }
162         }
163
164         return Parent ? Parent->OnEvent(event) : false;
165 }
166