1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2020, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
20 #include "ui/screen/screen_setup_sound.h"
21 
22 #include "app/app.h"
23 
24 #include "common/restext.h"
25 #include "common/settings.h"
26 #include "common/stringutils.h"
27 
28 #include "graphics/engine/camera.h"
29 
30 #include "sound/sound.h"
31 
32 #include "ui/controls/button.h"
33 #include "ui/controls/check.h"
34 #include "ui/controls/interface.h"
35 #include "ui/controls/label.h"
36 #include "ui/controls/slider.h"
37 #include "ui/controls/window.h"
38 
39 namespace Ui
40 {
41 
CScreenSetupSound()42 CScreenSetupSound::CScreenSetupSound()
43 {
44 }
45 
SetActive()46 void CScreenSetupSound::SetActive()
47 {
48     m_tab = PHASE_SETUPs;
49 }
50 
CreateInterface()51 void CScreenSetupSound::CreateInterface()
52 {
53     CWindow*        pw;
54     CLabel*         pl;
55     CSlider*        psl;
56     CButton*        pb;
57     CCheck*         pc;
58     Math::Point     pos, ddim;
59     std::string     name;
60 
61     CScreenSetup::CreateInterface();
62     pw = static_cast<CWindow*>(m_interface->SearchControl(EVENT_WINDOW5));
63     if ( pw == nullptr )  return;
64 
65     pos.x = ox+sx*3;
66     pos.y = 0.55f;
67     ddim.x = dim.x*4.0f;
68     ddim.y = 18.0f/480.0f;
69     psl = pw->CreateSlider(pos, ddim, 0, EVENT_INTERFACE_VOLSOUND);
70     psl->SetState(STATE_SHADOW);
71     psl->SetLimit(0.0f, MAXVOLUME);
72     psl->SetArrowStep(1.0f);
73     pos.y += ddim.y;
74     GetResource(RES_EVENT, EVENT_INTERFACE_VOLSOUND, name);
75     pl = pw->CreateLabel(pos, ddim, 0, EVENT_LABEL1, name);
76     pl->SetTextAlign(Gfx::TEXT_ALIGN_LEFT);
77 
78     pos.x = ox+sx*3;
79     pos.y = 0.40f;
80     ddim.x = dim.x*4.0f;
81     ddim.y = 18.0f/480.0f;
82     psl = pw->CreateSlider(pos, ddim, 0, EVENT_INTERFACE_VOLMUSIC);
83     psl->SetState(STATE_SHADOW);
84     psl->SetLimit(0.0f, MAXVOLUME);
85     psl->SetArrowStep(1.0f);
86     pos.y += ddim.y;
87     GetResource(RES_EVENT, EVENT_INTERFACE_VOLMUSIC, name);
88     pl = pw->CreateLabel(pos, ddim, 0, EVENT_LABEL2, name);
89     pl->SetTextAlign(Gfx::TEXT_ALIGN_LEFT);
90 
91     ddim.x = dim.x*3;
92     ddim.y = dim.y*1;
93     pos.x = ox+sx*10;
94     pos.y = oy+sy*2;
95     pb = pw->CreateButton(pos, ddim, -1, EVENT_INTERFACE_SILENT);
96     pb->SetState(STATE_SHADOW);
97     pos.x += ddim.x;
98     pb = pw->CreateButton(pos, ddim, -1, EVENT_INTERFACE_NOISY);
99     pb->SetState(STATE_SHADOW);
100 
101     ddim.x = dim.x*6;
102     ddim.y = dim.y*0.5f;
103     pos.x = ox+sx*10;
104     pos.y = 0.55f;
105     pc = pw->CreateCheck(pos, ddim, -1, EVENT_INTERFACE_BGMUTE);
106     pc->SetState(STATE_SHADOW);
107 
108     UpdateSetupButtons();
109 }
110 
EventProcess(const Event & event)111 bool CScreenSetupSound::EventProcess(const Event &event)
112 {
113     if (!CScreenSetup::EventProcess(event)) return false;
114 
115     switch( event.type )
116     {
117         case EVENT_INTERFACE_VOLSOUND:
118         case EVENT_INTERFACE_VOLMUSIC:
119             ChangeSetupButtons();
120             break;
121 
122         case EVENT_INTERFACE_SILENT:
123             m_sound->SetAudioVolume(0);
124             m_sound->SetMusicVolume(0);
125             UpdateSetupButtons();
126             break;
127         case EVENT_INTERFACE_NOISY:
128             m_sound->SetAudioVolume(MAXVOLUME);
129             m_sound->SetMusicVolume(MAXVOLUME*3/4);
130             UpdateSetupButtons();
131             break;
132 
133         case EVENT_INTERFACE_BGMUTE:
134             m_settings->SetFocusLostMute(!m_settings->GetFocusLostMute());
135             ChangeSetupButtons();
136             UpdateSetupButtons();
137             break;
138 
139         default:
140             return true;
141     }
142     return false;
143 }
144 
145 // Updates the buttons during the setup phase.
146 
UpdateSetupButtons()147 void CScreenSetupSound::UpdateSetupButtons()
148 {
149     CWindow*    pw;
150     CSlider*    ps;
151     CCheck*     pc;
152     float       value;
153 
154     pw = static_cast<CWindow*>(m_interface->SearchControl(EVENT_WINDOW5));
155     if ( pw == nullptr )  return;
156 
157     ps = static_cast<CSlider*>(pw->SearchControl(EVENT_INTERFACE_VOLSOUND));
158     if ( ps != nullptr )
159     {
160         value = static_cast<float>(m_sound->GetAudioVolume());
161         ps->SetVisibleValue(value);
162     }
163 
164     ps = static_cast<CSlider*>(pw->SearchControl(EVENT_INTERFACE_VOLMUSIC));
165     if ( ps != nullptr )
166     {
167         value = static_cast<float>(m_sound->GetMusicVolume());
168         ps->SetVisibleValue(value);
169     }
170 
171     pc = static_cast<CCheck*>(pw->SearchControl(EVENT_INTERFACE_BGMUTE));
172     if ( pc != nullptr )
173     {
174         pc->SetState(STATE_CHECK, m_settings->GetFocusLostMute());
175     }
176 
177     m_settings->SaveAudioSettings();
178 }
179 
180 // Updates the engine function of the buttons after the setup phase.
181 
ChangeSetupButtons()182 void CScreenSetupSound::ChangeSetupButtons()
183 {
184     CWindow*    pw;
185     CSlider*    ps;
186     float       value;
187 
188     pw = static_cast<CWindow*>(m_interface->SearchControl(EVENT_WINDOW5));
189     if ( pw == nullptr )  return;
190 
191     ps = static_cast<CSlider*>(pw->SearchControl(EVENT_INTERFACE_VOLSOUND));
192     if ( ps != nullptr )
193     {
194         value = ps->GetVisibleValue();
195         m_sound->SetAudioVolume(static_cast<int>(value));
196     }
197 
198     ps = static_cast<CSlider*>(pw->SearchControl(EVENT_INTERFACE_VOLMUSIC));
199     if ( ps != nullptr )
200     {
201         value = ps->GetVisibleValue();
202         m_sound->SetMusicVolume(static_cast<int>(value));
203     }
204 
205     m_settings->SaveAudioSettings();
206 }
207 
208 } // namespace Ui
209