1 ////////////////////////////////////////////////////////////////////////////////
2 //            Copyright (C) 2004-2011 by The Allacrost Project
3 //            Copyright (C) 2012-2018 by Bertram (Valyria Tear)
4 //                         All Rights Reserved
5 //
6 // This code is licensed under the GNU GPL version 2. It is free software and
7 // 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 #include "modes/battle/finish/battle_defeat.h"
12 
13 #include "engine/mode_manager.h"
14 #include "modes/battle/battle.h"
15 #include "modes/boot/boot.h"
16 
17 #include "common/gui/menu_window.h"
18 #include "engine/video/video_utils.h"
19 #include "engine/system.h"
20 #include "engine/audio/audio.h"
21 #include "engine/input.h"
22 
23 #include "utils/utils_common.h"
24 
25 using namespace vt_gui;
26 using namespace vt_video;
27 using namespace vt_system;
28 using namespace vt_audio;
29 using namespace vt_input;
30 using namespace vt_mode_manager;
31 using namespace vt_boot;
32 
33 namespace vt_battle
34 {
35 
36 namespace private_battle
37 {
38 
39 //! \brief Draw position and dimension constants used for GUI objects
40 //@{
41 const float TOP_WINDOW_XPOS        = 512.0f;
42 const float TOP_WINDOW_YPOS        = 104.0f;
43 const float TOP_WINDOW_WIDTH       = 512.0f;
44 const float TOP_WINDOW_HEIGHT      = 64.0f;
45 
46 const float TOOLTIP_WINDOW_XPOS    = TOP_WINDOW_XPOS;
47 const float TOOLTIP_WINDOW_YPOS    = TOP_WINDOW_YPOS + TOP_WINDOW_HEIGHT - 16.0f;
48 const float TOOLTIP_WINDOW_WIDTH   = TOP_WINDOW_WIDTH;
49 const float TOOLTIP_WINDOW_HEIGHT  = 112.0f;
50 //@}
51 
52 //! \brief The set of defeat options that the player can select
53 //@{
54 //! Retry the battle
55 const uint32_t DEFEAT_OPTION_RETRY     = 0;
56 //! End game and return to boot menu
57 const uint32_t DEFEAT_OPTION_END       = 1;
58 //@}
59 
BattleDefeat()60 BattleDefeat::BattleDefeat() :
61     _state(DEFEAT_INVALID)
62 {
63     _outcome_text.SetPosition(TOP_WINDOW_XPOS - TOP_WINDOW_WIDTH / 2.0f, 48.0f);
64     _outcome_text.SetDimensions(TOP_WINDOW_WIDTH, 50.0f);
65     _outcome_text.SetTextStyle(TextStyle("text24", Color::white));
66     _outcome_text.SetDisplayMode(VIDEO_TEXT_INSTANT);
67     _outcome_text.SetDisplayText(UTranslate("The heroes fell in battle..."));
68 
69     _options_window.Create(TOP_WINDOW_WIDTH, TOP_WINDOW_HEIGHT, ~VIDEO_MENU_EDGE_BOTTOM, VIDEO_MENU_EDGE_BOTTOM);
70     _options_window.SetPosition(TOP_WINDOW_XPOS, TOP_WINDOW_YPOS);
71     _options_window.SetAlignment(VIDEO_X_CENTER, VIDEO_Y_TOP);
72     _options_window.Show();
73 
74     _tooltip_window.Create(TOOLTIP_WINDOW_WIDTH, TOOLTIP_WINDOW_HEIGHT);
75     _tooltip_window.SetPosition(TOOLTIP_WINDOW_XPOS, TOOLTIP_WINDOW_YPOS);
76     _tooltip_window.SetAlignment(VIDEO_X_CENTER, VIDEO_Y_TOP);
77     _tooltip_window.Show();
78 
79     _defeat_options.SetOwner(&_options_window);
80     _defeat_options.SetPosition(TOP_WINDOW_WIDTH / 2, 28.0f);
81     _defeat_options.SetDimensions(TOP_WINDOW_WIDTH, 50.0f, 2, 1, 2, 1);
82     _defeat_options.SetTextStyle(TextStyle("text22", Color::white, VIDEO_TEXT_SHADOW_DARK));
83     _defeat_options.SetAlignment(VIDEO_X_CENTER, VIDEO_Y_CENTER);
84     _defeat_options.SetOptionAlignment(VIDEO_X_CENTER, VIDEO_Y_CENTER);
85     _defeat_options.SetSelectMode(VIDEO_SELECT_SINGLE);
86     _defeat_options.SetHorizontalWrapMode(VIDEO_WRAP_MODE_STRAIGHT);
87     _defeat_options.SetCursorOffset(-60.0f, -25.0f);
88     _defeat_options.AddOption(UTranslate("Retry"));
89     _defeat_options.AddOption(UTranslate("End"));
90     _defeat_options.SetSelection(0);
91 
92     _confirm_options.SetOwner(&_options_window);
93     _confirm_options.SetPosition(TOP_WINDOW_WIDTH / 2, 28.0f);
94     _confirm_options.SetDimensions(TOP_WINDOW_WIDTH, 50.0f, 2, 1, 2, 1);
95     _confirm_options.SetTextStyle(TextStyle("text22", Color::white, VIDEO_TEXT_SHADOW_DARK));
96     _confirm_options.SetAlignment(VIDEO_X_CENTER, VIDEO_Y_CENTER);
97     _confirm_options.SetOptionAlignment(VIDEO_X_CENTER, VIDEO_Y_CENTER);
98     _confirm_options.SetSelectMode(VIDEO_SELECT_SINGLE);
99     _confirm_options.SetHorizontalWrapMode(VIDEO_WRAP_MODE_STRAIGHT);
100     _confirm_options.SetCursorOffset(-60.0f, -25.0f);
101     _confirm_options.AddOption(UTranslate("OK"));
102     _confirm_options.AddOption(UTranslate("Cancel"));
103     _confirm_options.SetSelection(0);
104 
105     _tooltip.SetOwner(&_tooltip_window);
106     _tooltip.SetPosition(32.0f, 40.0f);
107     _tooltip.SetDimensions(480.0f, 80.0f);
108     _tooltip.SetAlignment(VIDEO_X_LEFT, VIDEO_Y_TOP);
109     _tooltip.SetTextAlignment(VIDEO_X_LEFT, VIDEO_Y_TOP);
110     _tooltip.SetDisplaySpeed(SystemManager->GetMessageSpeed());
111     _tooltip.SetTextStyle(TextStyle("text20", Color::white));
112     _tooltip.SetDisplayMode(VIDEO_TEXT_INSTANT);
113 }
114 
~BattleDefeat()115 BattleDefeat::~BattleDefeat()
116 {
117     _options_window.Destroy();
118     _tooltip_window.Destroy();
119 }
120 
Initialize()121 void BattleDefeat::Initialize()
122 {
123     // Start interaction
124     _state = DEFEAT_SELECT;
125 
126     _SetTooltipText();
127 
128     _options_window.Show();
129     _tooltip_window.Show();
130 }
131 
Update()132 void BattleDefeat::Update()
133 {
134     switch(_state) {
135     case DEFEAT_SELECT:
136         _defeat_options.Update();
137         if(InputManager->ConfirmPress()) {
138             if(!_defeat_options.IsOptionEnabled(_defeat_options.GetSelection())) {
139                 AudioManager->PlaySound("data/sounds/cancel.wav");
140             } else {
141                 _state = DEFEAT_CONFIRM;
142                 // Set default confirm option to "Cancel"
143                 if(_defeat_options.GetSelection() == (int32_t)DEFEAT_OPTION_END)
144                     _confirm_options.SetSelection(1);
145                 else
146                     _confirm_options.SetSelection(0);
147 
148                 _SetTooltipText();
149             }
150         }
151 
152         else if(InputManager->LeftPress()) {
153             _defeat_options.InputLeft();
154             _SetTooltipText();
155         } else if(InputManager->RightPress()) {
156             _defeat_options.InputRight();
157             _SetTooltipText();
158         }
159 
160         break;
161 
162     case DEFEAT_CONFIRM:
163         _confirm_options.Update();
164         if(InputManager->ConfirmPress()) {
165             switch(_confirm_options.GetSelection()) {
166             case 0: // "OK"
167                 _state = DEFEAT_END;
168                 _options_window.Hide();
169                 _tooltip_window.Hide();
170                 break;
171             case 1: // "Cancel"
172                 _state = DEFEAT_SELECT;
173                 _SetTooltipText();
174                 break;
175             default:
176                 PRINT_WARNING
177                         << "invalid confirm option selection: "
178                         << _confirm_options.GetSelection() << std::endl;
179                 break;
180             }
181         }
182         else if(InputManager->CancelPress()) {
183             _state = DEFEAT_SELECT;
184             _SetTooltipText();
185         }
186         else if(InputManager->LeftPress()) {
187             _confirm_options.InputLeft();
188         } else if(InputManager->RightPress()) {
189             _confirm_options.InputRight();
190         }
191         break;
192 
193     case DEFEAT_END:
194         switch(_defeat_options.GetSelection()) {
195         case DEFEAT_OPTION_RETRY:
196             BattleMode::CurrentInstance()->RestartBattle();
197             break;
198         case DEFEAT_OPTION_END:
199             ModeManager->PopAll();
200             ModeManager->Push(new vt_boot::BootMode(), false, true);
201             break;
202         default:
203             IF_PRINT_WARNING(BATTLE_DEBUG)
204                     << "invalid defeat option selected: "
205                     << _defeat_options.GetSelection() << std::endl;
206             break;
207         }
208         break;
209 
210     default:
211         PRINT_WARNING << "invalid finish state: " << _state << std::endl;
212         break;
213     }
214 }
215 
Draw()216 void BattleDefeat::Draw()
217 {
218     _outcome_text.Draw();
219     _options_window.Draw();
220     _tooltip_window.Draw();
221 
222     if(_state == DEFEAT_SELECT) {
223         _defeat_options.Draw();
224     } else if(_state == DEFEAT_CONFIRM) {
225         _confirm_options.Draw();
226     }
227 
228     _tooltip.Draw();
229 }
230 
_SetTooltipText()231 void BattleDefeat::_SetTooltipText()
232 {
233     if(_state == DEFEAT_SELECT) {
234         switch(_defeat_options.GetSelection()) {
235         case DEFEAT_OPTION_RETRY:
236             _tooltip.SetDisplayText(Translate("Start over from the beginning of this battle."));
237             break;
238         case DEFEAT_OPTION_END:
239             _tooltip.SetDisplayText(UTranslate("Exit to main menu."));
240             break;
241         default:
242             _tooltip.SetDisplayText("");
243             break;
244         }
245     } else if(_state == DEFEAT_CONFIRM) {
246         switch(_defeat_options.GetSelection()) {
247         case DEFEAT_OPTION_RETRY:
248             _tooltip.SetDisplayText(UTranslate("Confirm: retry battle."));
249             break;
250         case DEFEAT_OPTION_END:
251             _tooltip.SetDisplayText(UTranslate("Confirm: return to main menu."));
252             break;
253         default:
254             _tooltip.SetDisplayText("");
255             break;
256         }
257     }
258 }
259 
260 } // namespace private_battle
261 
262 } // namespace vt_battle
263