1 /*
2  * Copyright 2010-2014 OpenXcom Developers.
3  *
4  * This file is part of OpenXcom.
5  *
6  * OpenXcom 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  * OpenXcom 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.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with OpenXcom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "MultipleTargetsState.h"
20 #include <sstream>
21 #include "../Engine/Game.h"
22 #include "../Resource/ResourcePack.h"
23 #include "../Engine/Language.h"
24 #include "../Engine/Palette.h"
25 #include "../Interface/TextButton.h"
26 #include "../Interface/Window.h"
27 #include "../Savegame/Target.h"
28 #include "../Savegame/Base.h"
29 #include "../Savegame/Craft.h"
30 #include "../Savegame/Ufo.h"
31 #include "GeoscapeState.h"
32 #include "ConfirmDestinationState.h"
33 #include "InterceptState.h"
34 #include "UfoDetectedState.h"
35 #include "GeoscapeCraftState.h"
36 #include "TargetInfoState.h"
37 #include "../Engine/Options.h"
38 #include "../Engine/Action.h"
39 
40 namespace OpenXcom
41 {
42 
43 /**
44  * Initializes all the elements in the Multiple Targets window.
45  * @param game Pointer to the core game.
46  * @param targets List of targets to display.
47  * @param craft Pointer to craft to retarget (NULL if none).
48  * @param state Pointer to the Geoscape state.
49  */
MultipleTargetsState(Game * game,std::vector<Target * > targets,Craft * craft,GeoscapeState * state)50 MultipleTargetsState::MultipleTargetsState(Game *game, std::vector<Target*> targets, Craft *craft, GeoscapeState *state) : State(game), _targets(targets), _craft(craft), _state(state)
51 {
52 	_screen = false;
53 
54 	if (_targets.size() > 1)
55 	{
56 		int winHeight = BUTTON_HEIGHT * _targets.size() + SPACING * (_targets.size() - 1) + MARGIN * 2;
57 		int winY = (200 - winHeight) / 2;
58 		int btnY = winY + MARGIN;
59 
60 		// Create objects
61 		_window = new Window(this, 136, winHeight, 60, winY, POPUP_VERTICAL);
62 
63 		// Set palette
64 		setPalette("PAL_GEOSCAPE", 7);
65 
66 		add(_window);
67 
68 		// Set up objects
69 		_window->setColor(Palette::blockOffset(8) + 5);
70 		_window->setBackground(_game->getResourcePack()->getSurface("BACK15.SCR"));
71 
72 		int y = btnY;
73 		for (size_t i = 0; i < _targets.size(); ++i)
74 		{
75 			TextButton *button = new TextButton(116, BUTTON_HEIGHT, 70, y);
76 			button->setColor(Palette::blockOffset(8) + 5);
77 			button->setText(_targets[i]->getName(_game->getLanguage()));
78 			button->onMouseClick((ActionHandler)&MultipleTargetsState::btnTargetClick);
79 			add(button);
80 
81 			_btnTargets.push_back(button);
82 
83 			y += button->getHeight() + SPACING;
84 		}
85 		_btnTargets[0]->onKeyboardPress((ActionHandler)&MultipleTargetsState::btnCancelClick, Options::keyCancel);
86 
87 		centerAllSurfaces();
88 	}
89 }
90 
91 /**
92  *
93  */
~MultipleTargetsState()94 MultipleTargetsState::~MultipleTargetsState()
95 {
96 
97 }
98 
99 /**
100  * Resets the palette and ignores the window
101  * if there's only one target.
102  */
init()103 void MultipleTargetsState::init()
104 {
105 	if (_targets.size() == 1)
106 	{
107 		popupTarget(*_targets.begin());
108 	}
109 	else
110 	{
111 		State::init();
112 	}
113 }
114 
115 /**
116  * Displays the right popup for a specific target.
117  * @param target Pointer to target.
118  */
popupTarget(Target * target)119 void MultipleTargetsState::popupTarget(Target *target)
120 {
121 	_game->popState();
122 	if (_craft == 0)
123 	{
124 		Base* b = dynamic_cast<Base*>(target);
125 		Craft* c = dynamic_cast<Craft*>(target);
126 		Ufo* u = dynamic_cast<Ufo*>(target);
127 		if (b != 0)
128 		{
129 			_game->pushState(new InterceptState(_game, _state->getGlobe(), b));
130 		}
131 		else if (c != 0)
132 		{
133 			_game->pushState(new GeoscapeCraftState(_game, c, _state->getGlobe(), 0));
134 		}
135 		else if (u != 0)
136 		{
137 			_game->pushState(new UfoDetectedState(_game, u, _state, false, u->getHyperDetected()));
138 		}
139 		else
140 		{
141 			_game->pushState(new TargetInfoState(_game, target, _state->getGlobe()));
142 		}
143 	}
144 	else
145 	{
146 		_game->pushState(new ConfirmDestinationState(_game, _craft, target));
147 	}
148 }
149 
150 /**
151  * Returns to the previous screen.
152  * @param action Pointer to an action.
153  */
btnCancelClick(Action *)154 void MultipleTargetsState::btnCancelClick(Action *)
155 {
156 	_game->popState();
157 }
158 
159 /**
160  * Pick a target to display.
161  * @param action Pointer to an action.
162  */
btnTargetClick(Action * action)163 void MultipleTargetsState::btnTargetClick(Action *action)
164 {
165 	for (size_t i = 0; i < _btnTargets.size(); ++i)
166 	{
167 		if (action->getSender() == _btnTargets[i])
168 		{
169 			popupTarget(_targets[i]);
170 			break;
171 		}
172 	}
173 }
174 
175 }
176