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 #include "modes/menu/menu_states/menu_abstract_state.h"
12 
13 #include "modes/menu/menu_mode.h"
14 
15 #include "engine/video/video.h"
16 #include "common/global/global.h"
17 #include "engine/input.h"
18 
19 namespace vt_menu {
20 
21 namespace private_menu {
22 
SetupOptionBoxCommonSettings(vt_gui::OptionBox * ob)23 void SetupOptionBoxCommonSettings(vt_gui::OptionBox* ob)
24 {
25     // Set all the default options
26     ob->SetTextStyle(vt_video::TextStyle("text24"));
27     ob->SetPosition(142.0f, 85.0f);
28     ob->SetDimensions(115.0f, 50.0f, 1, 1, 1, 1);
29     ob->SetAlignment(vt_video::VIDEO_X_LEFT, vt_video::VIDEO_Y_CENTER);
30     ob->SetOptionAlignment(vt_video::VIDEO_X_CENTER, vt_video::VIDEO_Y_CENTER);
31     ob->SetSelectMode(vt_gui::VIDEO_SELECT_SINGLE);
32     ob->SetHorizontalWrapMode(vt_gui::VIDEO_WRAP_MODE_STRAIGHT);
33     ob->SetCursorOffset(-52.0f, -20.0f);
34 }
35 
AbstractMenuState(const std::string & state_name,MenuMode * menu_mode)36 AbstractMenuState::AbstractMenuState(const std::string& state_name, MenuMode* menu_mode):
37     _state_name(state_name),
38     _menu_mode(menu_mode),
39     _from_state(nullptr)
40 {
41 }
42 
Update()43 void AbstractMenuState::Update()
44 {
45     vt_global::GlobalMedia& media = vt_global::GlobalManager->Media();
46 
47     // if the current state is set to active, to an active update instead and return
48     if(_IsActive())
49     {
50         _ActiveWindowUpdate();
51         return;
52     }
53 
54     // handle a cancel press. in the case that we are at the main_menu state, pop the ModeManager off
55     // the Mode stack as well.
56     if(vt_input::InputManager->CancelPress())
57     {
58         media.PlaySound("cancel");
59         if(_menu_mode->_current_menu_state == &(_menu_mode->_main_menu_state))
60             vt_mode_manager::ModeManager->Pop();
61         // do instance specific cancel logic
62         _OnCancel();
63         return;
64     }
65     // handle left / right option box movement
66     else if(vt_input::InputManager->LeftPress())
67     {
68         media.PlaySound("bump");
69         _options.InputLeft();
70         return;
71     }
72     else if(vt_input::InputManager->RightPress())
73     {
74         media.PlaySound("bump");
75         _options.InputRight();
76         return;
77     }
78     // play a sound if the option is selected
79     else if(vt_input::InputManager->ConfirmPress())
80     {
81         if(_options.IsOptionEnabled((_options.GetSelection())))
82             media.PlaySound("confirm");
83         _options.InputConfirm();
84     }
85     // return the event type from the option
86     int32_t event = _options.GetEvent();
87     // update the current option box for this state, thus clearing the event flag
88     // if we don't do this, then upon return we enter right back into the state we wanted
89     // to return from
90     _options.Update();
91 
92     if(event == vt_gui::VIDEO_OPTION_CONFIRM) {
93         uint32_t selection = _options.GetSelection();
94         AbstractMenuState* next_state = GetTransitionState(selection);
95         // if the next state is the state we came from, it is similar to "cancel"
96         if(next_state == _from_state)
97         {
98             _OnCancel();
99             return;
100         }
101         // otherwise, if the state is valid and not this state itself, handle the transition
102         else if(next_state != nullptr && next_state != this)
103         {
104             // change the static current menu state
105             _menu_mode->_current_menu_state = next_state;
106 
107             next_state->_from_state = this;
108             next_state->Reset();
109         }
110         // When we change the state, update the time immediately to avoid
111         // showing outdated or empty time info
112         _menu_mode->ForceUpdateOfTime();
113     }
114 
115     // update the current state
116     _OnUpdateState();
117     // update the options for the currently active state
118     _menu_mode->_current_menu_state->GetOptions()->Update();
119 
120     _menu_mode->UpdateTimeAndDrunes();
121 }
122 
Draw()123 void AbstractMenuState::Draw()
124 {
125     // Draw the saved screen background
126     // For that, set the system coordinates to the size of the window (same with the save-screen)
127     int32_t width = vt_video::VideoManager->GetViewportWidth();
128     int32_t height = vt_video::VideoManager->GetViewportHeight();
129     vt_video::VideoManager->SetCoordSys(0.0f, static_cast<float>(width), 0.0f, static_cast<float>(height));
130     vt_video::VideoManager->SetDrawFlags(vt_video::VIDEO_X_LEFT,
131                                          vt_video::VIDEO_Y_BOTTOM,
132                                          vt_video::VIDEO_BLEND, 0);
133 
134     vt_video::DrawCapturedBackgroundImage(_menu_mode->_saved_screen, 0.0f, 0.0f);
135 
136     // Restore the Coordinate system (that one is menu mode coordinate system).
137     vt_video::VideoManager->SetStandardCoordSys();
138     vt_video::VideoManager->SetDrawFlags(vt_video::VIDEO_X_LEFT,
139                                          vt_video::VIDEO_Y_TOP,
140                                          vt_video::VIDEO_BLEND, 0);
141 
142     // Move to the top left corner
143     vt_video::VideoManager->Move(0.0f, 0.0f);
144 
145     _menu_mode->_main_options_window.Draw();
146 
147     // do instance specific main window rendering
148     _OnDrawMainWindow();
149     // do instance specific side window rendering
150     _OnDrawSideWindow();
151     // Draw currently active options box
152     _options.Draw();
153 }
154 
_OnDrawSideWindow()155 void AbstractMenuState::_OnDrawSideWindow()
156 {
157     _menu_mode->_character_window0.Draw();
158     _menu_mode->_character_window1.Draw();
159     _menu_mode->_character_window2.Draw();
160     _menu_mode->_character_window3.Draw();
161 }
162 
_DrawBottomMenu()163 void AbstractMenuState::_DrawBottomMenu()
164 {
165     _menu_mode->_bottom_window.Draw();
166 
167     vt_video::VideoManager->SetDrawFlags(vt_video::VIDEO_X_LEFT,
168                                          vt_video::VIDEO_Y_BOTTOM, 0);
169     vt_video::VideoManager->Move(150.0f, 580.0f);
170     // Display Location
171     _menu_mode->_locale_name.Draw();
172 
173     // Draw Played Time
174     _menu_mode->_time_text.Draw();
175 
176     // Display the current funds that the party has
177     _menu_mode->_drunes_text.Draw();
178 
179     vt_video::VideoManager->MoveRelative(-50.0f, 60.0f);
180     _menu_mode->_clock_icon->Draw();
181     vt_video::VideoManager->MoveRelative(0.0f, 30.0f);
182     _menu_mode->_drunes_icon->Draw();
183 
184     if(!_menu_mode->_locale_graphic.GetFilename().empty()) {
185         vt_video::VideoManager->SetDrawFlags(vt_video::VIDEO_X_RIGHT,
186                                              vt_video::VIDEO_Y_BOTTOM, 0);
187         vt_video::VideoManager->SetDrawFlags(vt_video::VIDEO_X_LEFT,
188                                              vt_video::VIDEO_Y_BOTTOM, 0);
189         vt_video::VideoManager->Move(390.0f, 685.0f);
190         _menu_mode->_locale_graphic.Draw();
191     }
192 }
193 
_OnCancel()194 void AbstractMenuState::_OnCancel()
195 {
196     // as long as the calling state is valid and not equal to this, simply switch back to it
197     if(_from_state && _from_state != this)
198         _menu_mode->_current_menu_state = _from_state;
199 }
200 
201 } // namespace private_menu
202 
203 } // namespace vt_menu
204