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