guiVolumeChange.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. Permission to use, copy, modify, and distribute this software for any
  7. purpose with or without fee is hereby granted, provided that the above
  8. copyright notice and this permission notice appear in all copies.
  9. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "guiVolumeChange.h"
  18. #include "debug.h"
  19. #include "guiButton.h"
  20. #include "serialization.h"
  21. #include <string>
  22. #include <IGUICheckBox.h>
  23. #include <IGUIButton.h>
  24. #include <IGUIScrollBar.h>
  25. #include <IGUIStaticText.h>
  26. #include <IGUIFont.h>
  27. #include "settings.h"
  28. #include "gettext.h"
  29. const int ID_soundText = 263;
  30. const int ID_soundExitButton = 264;
  31. const int ID_soundSlider = 265;
  32. const int ID_soundMuteButton = 266;
  33. GUIVolumeChange::GUIVolumeChange(gui::IGUIEnvironment* env,
  34. gui::IGUIElement* parent, s32 id,
  35. IMenuManager *menumgr, ISimpleTextureSource *tsrc
  36. ):
  37. GUIModalMenu(env, parent, id, menumgr),
  38. m_tsrc(tsrc)
  39. {
  40. }
  41. GUIVolumeChange::~GUIVolumeChange()
  42. {
  43. removeChildren();
  44. }
  45. void GUIVolumeChange::removeChildren()
  46. {
  47. if (gui::IGUIElement *e = getElementFromId(ID_soundText))
  48. e->remove();
  49. if (gui::IGUIElement *e = getElementFromId(ID_soundExitButton))
  50. e->remove();
  51. if (gui::IGUIElement *e = getElementFromId(ID_soundSlider))
  52. e->remove();
  53. if (gui::IGUIElement *e = getElementFromId(ID_soundMuteButton))
  54. e->remove();
  55. }
  56. void GUIVolumeChange::regenerateGui(v2u32 screensize)
  57. {
  58. /*
  59. Remove stuff
  60. */
  61. removeChildren();
  62. /*
  63. Calculate new sizes and positions
  64. */
  65. const float s = m_gui_scale;
  66. DesiredRect = core::rect<s32>(
  67. screensize.X / 2 - 380 * s / 2,
  68. screensize.Y / 2 - 200 * s / 2,
  69. screensize.X / 2 + 380 * s / 2,
  70. screensize.Y / 2 + 200 * s / 2
  71. );
  72. recalculateAbsolutePosition(false);
  73. v2s32 size = DesiredRect.getSize();
  74. int volume = (int)(g_settings->getFloat("sound_volume") * 100);
  75. /*
  76. Add stuff
  77. */
  78. {
  79. core::rect<s32> rect(0, 0, 160 * s, 20 * s);
  80. rect = rect + v2s32(size.X / 2 - 80 * s, size.Y / 2 - 70 * s);
  81. const wchar_t *text = wgettext("Sound Volume: ");
  82. core::stringw volume_text = text;
  83. delete [] text;
  84. volume_text += core::stringw(volume) + core::stringw("%");
  85. Environment->addStaticText(volume_text.c_str(), rect, false,
  86. true, this, ID_soundText);
  87. }
  88. {
  89. core::rect<s32> rect(0, 0, 80 * s, 30 * s);
  90. rect = rect + v2s32(size.X / 2 - 80 * s / 2, size.Y / 2 + 55 * s);
  91. const wchar_t *text = wgettext("Exit");
  92. GUIButton::addButton(Environment, rect, m_tsrc, this, ID_soundExitButton, text);
  93. delete[] text;
  94. }
  95. {
  96. core::rect<s32> rect(0, 0, 300 * s, 20 * s);
  97. rect = rect + v2s32(size.X / 2 - 150 * s, size.Y / 2);
  98. gui::IGUIScrollBar *e = Environment->addScrollBar(true,
  99. rect, this, ID_soundSlider);
  100. e->setMax(100);
  101. e->setPos(volume);
  102. }
  103. {
  104. core::rect<s32> rect(0, 0, 160 * s, 20 * s);
  105. rect = rect + v2s32(size.X / 2 - 80 * s, size.Y / 2 - 35 * s);
  106. const wchar_t *text = wgettext("Muted");
  107. Environment->addCheckBox(g_settings->getBool("mute_sound"), rect, this,
  108. ID_soundMuteButton, text);
  109. delete[] text;
  110. }
  111. }
  112. void GUIVolumeChange::drawMenu()
  113. {
  114. gui::IGUISkin* skin = Environment->getSkin();
  115. if (!skin)
  116. return;
  117. video::IVideoDriver* driver = Environment->getVideoDriver();
  118. video::SColor bgcolor(140, 0, 0, 0);
  119. driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
  120. gui::IGUIElement::draw();
  121. }
  122. bool GUIVolumeChange::OnEvent(const SEvent& event)
  123. {
  124. if (event.EventType == EET_KEY_INPUT_EVENT) {
  125. if (event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown) {
  126. quitMenu();
  127. return true;
  128. }
  129. if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
  130. quitMenu();
  131. return true;
  132. }
  133. } else if (event.EventType == EET_GUI_EVENT) {
  134. if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) {
  135. gui::IGUIElement *e = getElementFromId(ID_soundMuteButton);
  136. if (e != NULL && e->getType() == gui::EGUIET_CHECK_BOX) {
  137. g_settings->setBool("mute_sound", ((gui::IGUICheckBox*)e)->isChecked());
  138. }
  139. Environment->setFocus(this);
  140. return true;
  141. }
  142. if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
  143. if (event.GUIEvent.Caller->getID() == ID_soundExitButton) {
  144. quitMenu();
  145. return true;
  146. }
  147. Environment->setFocus(this);
  148. }
  149. if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
  150. && isVisible()) {
  151. if (!canTakeFocus(event.GUIEvent.Element)) {
  152. infostream << "GUIVolumeChange: Not allowing focus change."
  153. << std::endl;
  154. // Returning true disables focus change
  155. return true;
  156. }
  157. }
  158. if (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED) {
  159. if (event.GUIEvent.Caller->getID() == ID_soundSlider) {
  160. s32 pos = ((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
  161. g_settings->setFloat("sound_volume", (float) pos / 100);
  162. gui::IGUIElement *e = getElementFromId(ID_soundText);
  163. const wchar_t *text = wgettext("Sound Volume: ");
  164. core::stringw volume_text = text;
  165. delete [] text;
  166. volume_text += core::stringw(pos) + core::stringw("%");
  167. e->setText(volume_text.c_str());
  168. return true;
  169. }
  170. }
  171. }
  172. return Parent ? Parent->OnEvent(event) : false;
  173. }