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