Add definable node_stone to biome API, mgv5, mgv7. Reduce and correct depth of mgv7...
[oweals/minetest.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 "main.h"
30 #include "settings.h"
31
32 #include "gettext.h"
33
34 const int ID_soundText1 = 263;
35 const int ID_soundText2 = 264;
36 const int ID_soundExitButton = 265;
37 const int ID_soundSlider = 266;
38
39 GUIVolumeChange::GUIVolumeChange(gui::IGUIEnvironment* env,
40                 gui::IGUIElement* parent, s32 id,
41                 IMenuManager *menumgr,
42                 Client* client
43 ):
44         GUIModalMenu(env, parent, id, menumgr)
45 {
46 }
47
48 GUIVolumeChange::~GUIVolumeChange()
49 {
50         removeChildren();
51 }
52
53 void GUIVolumeChange::removeChildren()
54 {
55         {
56                 gui::IGUIElement *e = getElementFromId(ID_soundText1);
57                 if(e != NULL)
58                         e->remove();
59         }
60         {
61                 gui::IGUIElement *e = getElementFromId(ID_soundText2);
62                 if(e != NULL)
63                         e->remove();
64         }
65         {
66                 gui::IGUIElement *e = getElementFromId(ID_soundExitButton);
67                 if(e != NULL)
68                         e->remove();
69         }
70         {
71                 gui::IGUIElement *e = getElementFromId(ID_soundSlider);
72                 if(e != NULL)
73                         e->remove();
74         }
75 }
76
77 void GUIVolumeChange::regenerateGui(v2u32 screensize)
78 {
79         /*
80                 Remove stuff
81         */
82         removeChildren();
83         
84         /*
85                 Calculate new sizes and positions
86         */
87         core::rect<s32> rect(
88                         screensize.X/2 - 380/2,
89                         screensize.Y/2 - 200/2,
90                         screensize.X/2 + 380/2,
91                         screensize.Y/2 + 200/2
92         );
93         
94         DesiredRect = rect;
95         recalculateAbsolutePosition(false);
96
97         v2s32 size = rect.getSize();
98         v2s32 topleft_client(40, 0);
99         int volume=(int)(g_settings->getFloat("sound_volume")*100);
100         /*
101                 Add stuff
102         */
103         {
104                 core::rect<s32> rect(0, 0, 120, 20);
105                 rect = rect + v2s32(size.X/2-60, size.Y/2-35);
106                 wchar_t* text = wgettext("Sound Volume: ");
107                 Environment->addStaticText(text, rect, false,
108                                 true, this, ID_soundText1);
109                 delete[] text;
110         }
111         {
112                 core::rect<s32> rect(0, 0, 30, 20);
113                 rect = rect + v2s32(size.X/2+40, size.Y/2-35);
114                 Environment->addStaticText(core::stringw(volume).c_str(), rect, false,
115                                 true, this, ID_soundText2);
116         }
117         {
118                 core::rect<s32> rect(0, 0, 80, 30);
119                 rect = rect + v2s32(size.X/2-80/2, size.Y/2+55);
120                 wchar_t* text = wgettext("Exit");
121                 Environment->addButton(rect, this, ID_soundExitButton,
122                         text);
123                 delete[] text;
124         }
125         {
126                 core::rect<s32> rect(0, 0, 300, 20);
127                 rect = rect + v2s32(size.X/2-150, size.Y/2);
128                 gui::IGUIScrollBar *e = Environment->addScrollBar(true,
129                         rect, this, ID_soundSlider);
130                 e->setMax(100);
131                 e->setPos(volume);
132         }
133 }
134
135 void GUIVolumeChange::drawMenu()
136 {
137         gui::IGUISkin* skin = Environment->getSkin();
138         if (!skin)
139                 return;
140         video::IVideoDriver* driver = Environment->getVideoDriver();
141         video::SColor bgcolor(140,0,0,0);
142         driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
143         gui::IGUIElement::draw();
144 }
145
146 bool GUIVolumeChange::OnEvent(const SEvent& event)
147 {
148         if(event.EventType==EET_KEY_INPUT_EVENT)
149         {
150                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
151                 {
152                         quitMenu();
153                         return true;
154                 }
155                 if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
156                 {
157                         quitMenu();
158                         return true;
159                 }
160         }
161         if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
162                 {
163                         if (event.GUIEvent.Caller->getID() == ID_soundExitButton)
164                                 {
165                                         quitMenu();
166                                         return true;
167                                 }
168                 }
169         if(event.GUIEvent.EventType==gui::EGET_SCROLL_BAR_CHANGED)
170                 {
171                 if (event.GUIEvent.Caller->getID() == ID_soundSlider)
172                         {
173                                 s32 pos = ((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
174                                 g_settings->setFloat("sound_volume",(float)pos/100);
175                                 gui::IGUIElement *e = getElementFromId(ID_soundText2);
176                                 e->setText( core::stringw(pos).c_str() );
177                                 return true;
178                         }
179                 }
180         return Parent ? Parent->OnEvent(event) : false;
181 }
182