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 "SoldiersState.h"
20 #include <string>
21 #include "../Engine/Game.h"
22 #include "../Resource/ResourcePack.h"
23 #include "../Engine/Language.h"
24 #include "../Engine/Palette.h"
25 #include "../Engine/Options.h"
26 #include "../Geoscape/AllocatePsiTrainingState.h"
27 #include "../Interface/TextButton.h"
28 #include "../Interface/Window.h"
29 #include "../Interface/Text.h"
30 #include "../Interface/TextList.h"
31 #include "../Savegame/Base.h"
32 #include "../Savegame/Soldier.h"
33 #include "../Savegame/Craft.h"
34 #include "../Ruleset/RuleCraft.h"
35 #include "SoldierInfoState.h"
36 #include "SoldierMemorialState.h"
37 
38 namespace OpenXcom
39 {
40 
41 /**
42  * Initializes all the elements in the Soldiers screen.
43  * @param game Pointer to the core game.
44  * @param base Pointer to the base to get info from.
45  */
SoldiersState(Game * game,Base * base)46 SoldiersState::SoldiersState(Game *game, Base *base) : State(game), _base(base)
47 {
48 	bool isPsiBtnVisible = Options::anytimePsiTraining && _base->getAvailablePsiLabs() > 0;
49 
50 	// Create objects
51 	_window = new Window(this, 320, 200, 0, 0);
52 	if (isPsiBtnVisible)
53 	{
54 		_btnOk = new TextButton(96, 16, 216, 176);
55 		_btnPsiTraining = new TextButton(96, 16, 112, 176);
56 		_btnMemorial = new TextButton(96, 16, 8, 176);
57 	}
58 	else
59 	{
60 		_btnOk = new TextButton(148, 16, 164, 176);
61 		_btnPsiTraining = new TextButton(148, 16, 164, 176);
62 		_btnMemorial = new TextButton(148, 16, 8, 176);
63 	}
64 	_txtTitle = new Text(310, 17, 5, 8);
65 	_txtName = new Text(114, 9, 16, 32);
66 	_txtRank = new Text(102, 9, 130, 32);
67 	_txtCraft = new Text(82, 9, 222, 32);
68 	_lstSoldiers = new TextList(288, 128, 8, 40);
69 
70 	// Set palette
71 	setPalette("PAL_BASESCAPE", 2);
72 
73 	add(_window);
74 	add(_btnOk);
75 	add(_btnPsiTraining);
76 	add(_btnMemorial);
77 	add(_txtTitle);
78 	add(_txtName);
79 	add(_txtRank);
80 	add(_txtCraft);
81 	add(_lstSoldiers);
82 
83 	centerAllSurfaces();
84 
85 	// Set up objects
86 	_window->setColor(Palette::blockOffset(15)+1);
87 	_window->setBackground(_game->getResourcePack()->getSurface("BACK02.SCR"));
88 
89 	_btnOk->setColor(Palette::blockOffset(13)+10);
90 	_btnOk->setText(tr("STR_OK"));
91 	_btnOk->onMouseClick((ActionHandler)&SoldiersState::btnOkClick);
92 	_btnOk->onKeyboardPress((ActionHandler)&SoldiersState::btnOkClick, Options::keyCancel);
93 
94 	_btnPsiTraining->setColor(Palette::blockOffset(13)+10);
95 	_btnPsiTraining->setText(tr("STR_PSIONIC_TRAINING"));
96 	_btnPsiTraining->onMouseClick((ActionHandler)&SoldiersState::btnPsiTrainingClick);
97 	_btnPsiTraining->setVisible(isPsiBtnVisible);
98 
99 	_btnMemorial->setColor(Palette::blockOffset(13)+10);
100 	_btnMemorial->setText(tr("STR_MEMORIAL"));
101 	_btnMemorial->onMouseClick((ActionHandler)&SoldiersState::btnMemorialClick);
102 
103 	_txtTitle->setColor(Palette::blockOffset(13)+10);
104 	_txtTitle->setBig();
105 	_txtTitle->setAlign(ALIGN_CENTER);
106 	_txtTitle->setText(tr("STR_SOLDIER_LIST"));
107 
108 	_txtName->setColor(Palette::blockOffset(15)+1);
109 	_txtName->setText(tr("STR_NAME_UC"));
110 
111 	_txtRank->setColor(Palette::blockOffset(15)+1);
112 	_txtRank->setText(tr("STR_RANK"));
113 
114 	_txtCraft->setColor(Palette::blockOffset(15)+1);
115 	_txtCraft->setText(tr("STR_CRAFT"));
116 
117 	_lstSoldiers->setColor(Palette::blockOffset(13)+10);
118 	_lstSoldiers->setArrowColor(Palette::blockOffset(15)+1);
119 	_lstSoldiers->setColumns(3, 114, 92, 74);
120 	_lstSoldiers->setSelectable(true);
121 	_lstSoldiers->setBackground(_window);
122 	_lstSoldiers->setMargin(8);
123 	_lstSoldiers->onMouseClick((ActionHandler)&SoldiersState::lstSoldiersClick);
124 }
125 
126 /**
127  *
128  */
~SoldiersState()129 SoldiersState::~SoldiersState()
130 {
131 
132 }
133 
134 /**
135  * Updates the soldiers list
136  * after going to other screens.
137  */
init()138 void SoldiersState::init()
139 {
140 	State::init();
141 	int row = 0;
142 	_lstSoldiers->clearList();
143 	for (std::vector<Soldier*>::iterator i = _base->getSoldiers()->begin(); i != _base->getSoldiers()->end(); ++i)
144 	{
145 		_lstSoldiers->addRow(3, (*i)->getName(true).c_str(), tr((*i)->getRankString()).c_str(), (*i)->getCraftString(_game->getLanguage()).c_str());
146 		if ((*i)->getCraft() == 0)
147 		{
148 			_lstSoldiers->setRowColor(row, Palette::blockOffset(15)+6);
149 		}
150 		row++;
151 	}
152 	if (row > 0 && _lstSoldiers->getScroll() >= row)
153 	{
154 		_lstSoldiers->scrollTo(0);
155 	}
156 }
157 
158 /**
159  * Returns to the previous screen.
160  * @param action Pointer to an action.
161  */
btnOkClick(Action *)162 void SoldiersState::btnOkClick(Action *)
163 {
164 	_game->popState();
165 }
166 
167 /**
168  * Opens the Psionic Training screen.
169  * @param action Pointer to an action.
170  */
btnPsiTrainingClick(Action *)171 void SoldiersState::btnPsiTrainingClick(Action *)
172 {
173 	_game->pushState(new AllocatePsiTrainingState(_game, _base));
174 }
175 
176 /**
177  * Opens the Memorial screen.
178  * @param action Pointer to an action.
179  */
btnMemorialClick(Action *)180 void SoldiersState::btnMemorialClick(Action *)
181 {
182 	_game->pushState(new SoldierMemorialState(_game));
183 }
184 
185 /**
186  * Shows the selected soldier's info.
187  * @param action Pointer to an action.
188  */
lstSoldiersClick(Action *)189 void SoldiersState::lstSoldiersClick(Action *)
190 {
191 	_game->pushState(new SoldierInfoState(_game, _base, _lstSoldiers->getSelectedRow()));
192 }
193 
194 }
195