1 ////////////////////////////////////////////////////////////////////////////////
2 // Copyright (C) 2004-2011 by The Allacrost Project
3 // Copyright (C) 2012-2016 by Bertram (Valyria Tear)
4 // All Rights Reserved
5 //
6 // This code is licensed under the GNU GPL version 2. It is free software
7 // and you may modify it and/or redistribute it under the terms of this license.
8 // See https://www.gnu.org/copyleft/gpl.html for details.
9 ////////////////////////////////////////////////////////////////////////////////
10
11 /** ****************************************************************************
12 *** \file pause.cpp
13 *** \author Tyler Olsen, roots@allacrost.org
14 *** \author Yohann Ferreira, yohann ferreira orange fr
15 *** \brief Source file for pause mode interface.
16 *** ***************************************************************************/
17
18 #include "modes/pause.h"
19
20 #include "engine/audio/audio.h"
21 #include "engine/video/video.h"
22 #include "engine/input.h"
23 #include "engine/system.h"
24 #include "modes/boot/boot.h"
25
26 #include "common/global/global.h"
27
28 using namespace vt_utils;
29 using namespace vt_audio;
30 using namespace vt_video;
31 using namespace vt_gui;
32 using namespace vt_mode_manager;
33 using namespace vt_input;
34 using namespace vt_system;
35 using namespace vt_boot;
36
37 namespace vt_pause
38 {
39
40 bool PAUSE_DEBUG = false;
41
42 /** \name Quit Options Menu Constants
43 *** These constants represent the OptionBox selection indeces of the three different options
44 *** presented to the player while the _quit_state member is active.
45 **/
46 //@{
47 const uint8_t QUIT_CANCEL = 0;
48 const uint8_t QUIT_OPTIONS = 1;
49 const uint8_t QUIT_TO_BOOT = 2;
50 const uint8_t QUIT_GAME = 3;
51 //@}
52
PauseMode(bool quit_state,bool pause_audio)53 PauseMode::PauseMode(bool quit_state, bool pause_audio) :
54 GameMode(MODE_MANAGER_PAUSE_MODE),
55 _quit_state(quit_state),
56 _audio_paused(pause_audio),
57 _music_volume(1.0f),
58 _dim_color(0.35f, 0.35f, 0.35f, 1.0f), // A grayish opaque color
59 _option_selected(false),
60 _options_handler(this)
61 {
62 // Save a copy of the current screen to use as the backdrop.
63 try {
64 _screen_capture = VideoManager->CaptureScreen();
65 }
66 catch (const Exception &e) {
67 IF_PRINT_WARNING(PAUSE_DEBUG) << e.ToString() << std::endl;
68 }
69
70 // Render the paused string in white text
71 _paused_text.SetStyle(TextStyle("title28", Color::white,
72 VIDEO_TEXT_SHADOW_BLACK));
73 _paused_text.SetText(UTranslate("Paused"));
74
75 // Initialize the quit options box
76 _quit_options.SetPosition(512.0f, 384.0f);
77 _quit_options.SetDimensions(420.0f, 300.0f, 1, 4, 1, 4);
78 _quit_options.SetTextStyle(TextStyle("title24", Color::white,
79 VIDEO_TEXT_SHADOW_BLACK));
80
81 _quit_options.SetAlignment(VIDEO_X_CENTER, VIDEO_Y_CENTER);
82 _quit_options.SetOptionAlignment(VIDEO_X_CENTER, VIDEO_Y_CENTER);
83 _quit_options.SetSelectMode(VIDEO_SELECT_SINGLE);
84 _quit_options.SetCursorOffset(-58.0f, -18.0f);
85
86 _SetupOptions();
87 }
88
_SetupOptions()89 void PauseMode::_SetupOptions()
90 {
91 _quit_options.ClearOptions();
92 _quit_options.AddOption(UTranslate("Cancel"));
93 _quit_options.AddOption(UTranslate("Options"));
94 _quit_options.AddOption(UTranslate("Quit to Main Menu"));
95 _quit_options.AddOption(UTranslate("Quit Game"));
96 _quit_options.SetSelection(QUIT_CANCEL);
97 }
98
ReloadTranslatedTexts()99 void PauseMode::ReloadTranslatedTexts()
100 {
101 _SetupOptions();
102 }
103
~PauseMode()104 PauseMode::~PauseMode()
105 {
106 if(_audio_paused)
107 AudioManager->ResumeAudio();
108 else {
109 MusicDescriptor *active_music = AudioManager->GetActiveMusic();
110 if (!active_music)
111 return;
112 active_music->SetVolume(_music_volume);
113 }
114 }
115
Reset()116 void PauseMode::Reset()
117 {
118 if (_audio_paused) {
119 AudioManager->PauseAudio();
120 }
121 else {
122 MusicDescriptor *active_music = AudioManager->GetActiveMusic();
123 if (active_music) {
124 _music_volume = active_music->GetVolume();
125 if (active_music->GetVolume() > 0.3f)
126 active_music->SetVolume(0.3f);
127 }
128 else {
129 _music_volume = 0.0f;
130 }
131 }
132
133 VideoManager->DisableFadeEffect();
134 }
135
Update()136 void PauseMode::Update()
137 {
138 // If an option has been selected, don't handle input until it has finished.
139 if(_option_selected)
140 return;
141
142 if(!_quit_state) {
143 if(InputManager->QuitPress()) {
144 _quit_state = true;
145 } else if(InputManager->PausePress()) {
146 _option_selected = true;
147 ModeManager->Pop();
148 }
149
150 return;
151 }
152
153 // (_quit_state)
154
155 // Handles the options menu
156 if (_options_handler.IsActive()) {
157 _options_handler.Update();
158 return;
159 }
160
161 vt_global::GlobalMedia& media = vt_global::GlobalManager->Media();
162
163 _quit_options.Update();
164
165 if(InputManager->QuitPress()) {
166 _option_selected = true;
167 ModeManager->Pop();
168 return;
169 } else if(InputManager->ConfirmPress()) {
170 media.PlaySound("confirm");
171 _option_selected = true;
172 switch(_quit_options.GetSelection()) {
173 case QUIT_CANCEL:
174 ModeManager->Pop();
175 break;
176 case QUIT_TO_BOOT:
177 // Disable potential previous effects
178 VideoManager->DisableFadeEffect();
179 ModeManager->PopAll();
180
181 // This will permit the fade system to start updating again.
182 _mode_type = MODE_MANAGER_DUMMY_MODE;
183
184 ModeManager->Push(new BootMode(), true, true);
185 break;
186 case QUIT_OPTIONS:
187 _option_selected = false;
188 _options_handler.Activate();
189 break;
190 case QUIT_GAME:
191 SystemManager->ExitGame();
192 break;
193 default:
194 IF_PRINT_WARNING(PAUSE_DEBUG) << "unknown quit option selected: "
195 << _quit_options.GetSelection()
196 << std::endl;
197 break;
198 }
199 return;
200 } else if(InputManager->CancelPress()) {
201 media.PlaySound("cancel");
202 _option_selected = true;
203 ModeManager->Pop();
204 return;
205 } else if(InputManager->UpPress()) {
206 media.PlaySound("bump");
207 _quit_options.InputUp();
208 } else if(InputManager->DownPress()) {
209 media.PlaySound("bump");
210 _quit_options.InputDown();
211 }
212 }
213
DrawPostEffects()214 void PauseMode::DrawPostEffects()
215 {
216 // Draw the background image. Set the system coordinates to the size of the window (same as the screen backdrop)
217 VideoManager->SetCoordSys(0.0f, static_cast<float>(VideoManager->GetViewportWidth()),
218 static_cast<float>(VideoManager->GetViewportHeight()), 0.0f);
219 VideoManager->SetDrawFlags(VIDEO_X_LEFT, VIDEO_Y_TOP, VIDEO_BLEND, 0);
220
221 vt_video::DrawCapturedBackgroundImage(_screen_capture, 0.0f, 0.0f, _dim_color);
222
223 VideoManager->SetStandardCoordSys();
224 VideoManager->SetDrawFlags(VIDEO_X_CENTER, VIDEO_Y_CENTER, VIDEO_BLEND, 0);
225 VideoManager->Move(512.0f, 384.0f);
226
227 if(!_quit_state) {
228 _paused_text.Draw();
229 } else {
230 _quit_options.Draw();
231
232 if (_options_handler.IsActive())
233 _options_handler.Draw();
234 }
235 }
236
237 } // namespace vt_pause
238