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 <sstream>
20 #include "PsiTrainingState.h"
21 #include "AllocatePsiTrainingState.h"
22 #include "../Engine/Game.h"
23 #include "../Resource/ResourcePack.h"
24 #include "../Engine/Language.h"
25 #include "../Engine/Palette.h"
26 #include "../Interface/TextButton.h"
27 #include "../Interface/Window.h"
28 #include "../Interface/Text.h"
29 #include "../Savegame/SavedGame.h"
30 #include "../Savegame/Base.h"
31 #include "../Interface/TextList.h"
32 #include "GeoscapeState.h"
33 #include "../Savegame/Soldier.h"
34 #include "../Engine/Action.h"
35 #include "../Engine/Options.h"
36 #include "../Ruleset/Ruleset.h"
37 
38 namespace OpenXcom
39 {
40 
41 /**
42  * Initializes all the elements in the Psi Training screen.
43  * @param game Pointer to the core game.
44  * @param base Pointer to the base to handle.
45  */
AllocatePsiTrainingState(Game * game,Base * base)46 AllocatePsiTrainingState::AllocatePsiTrainingState(Game *game, Base *base) : State(game), _sel(0)
47 {
48 	_base = base;
49 	// Create objects
50 	_window = new Window(this, 320, 200, 0, 0);
51 	_txtTitle = new Text(300, 17, 10, 8);
52 	_txtRemaining = new Text(300, 10, 10, 24);
53 	_txtName = new Text(64, 10, 10, 40);
54 	_txtPsiStrength = new Text(80, 20, 124, 32);
55 	_txtPsiSkill = new Text(80, 20, 188, 32);
56 	_txtTraining = new Text(48, 20, 270, 32);
57 	_btnOk = new TextButton(160, 14, 80, 174);
58 	_lstSoldiers = new TextList(290, 112, 8, 52);
59 
60 	// Set palette
61 	setPalette("PAL_BASESCAPE", 7);
62 
63 	add(_window);
64 	add(_btnOk);
65 	add(_txtName);
66 	add(_txtTitle);
67 	add(_txtRemaining);
68 	add(_txtPsiStrength);
69 	add(_txtPsiSkill);
70 	add(_txtTraining);
71 	add(_lstSoldiers);
72 
73 	centerAllSurfaces();
74 
75 	// Set up objects
76 	_window->setColor(Palette::blockOffset(13)+10);
77 	_window->setBackground(_game->getResourcePack()->getSurface("BACK01.SCR"));
78 
79 	_btnOk->setColor(Palette::blockOffset(13)+10);
80 	_btnOk->setText(tr("STR_OK"));
81 	_btnOk->onMouseClick((ActionHandler)&AllocatePsiTrainingState::btnOkClick);
82 	_btnOk->onKeyboardPress((ActionHandler)&AllocatePsiTrainingState::btnOkClick, Options::keyCancel);
83 
84 	_txtTitle->setColor(Palette::blockOffset(13)+10);
85 	_txtTitle->setBig();
86 	_txtTitle->setAlign(ALIGN_CENTER);
87 	_txtTitle->setText(tr("STR_PSIONIC_TRAINING"));
88 
89 	_labSpace = base->getAvailablePsiLabs() - base->getUsedPsiLabs();
90 	_txtRemaining->setColor(Palette::blockOffset(13)+10);
91 	_txtRemaining->setSecondaryColor(Palette::blockOffset(13));
92 	_txtRemaining->setText(tr("STR_REMAINING_PSI_LAB_CAPACITY").arg(_labSpace));
93 
94 	_txtName->setColor(Palette::blockOffset(13)+10);
95 	_txtName->setText(tr("STR_NAME"));
96 
97 	_txtPsiStrength->setColor(Palette::blockOffset(13)+10);
98 	_txtPsiStrength->setText(tr("STR_PSIONIC__STRENGTH"));
99 
100 	_txtPsiSkill->setColor(Palette::blockOffset(13)+10);
101 	_txtPsiSkill->setText(tr("STR_PSIONIC_SKILL_IMPROVEMENT"));
102 
103 	_txtTraining->setColor(Palette::blockOffset(13)+10);
104 	_txtTraining->setText(tr("STR_IN_TRAINING"));
105 
106 	_lstSoldiers->setColor(Palette::blockOffset(13)+10);
107 	_lstSoldiers->setAlign(ALIGN_RIGHT, 3);
108 	_lstSoldiers->setColumns(4, 114, 80, 62, 30);
109 	_lstSoldiers->setSelectable(true);
110 	_lstSoldiers->setBackground(_window);
111 	_lstSoldiers->setMargin(2);
112 	_lstSoldiers->onMouseClick((ActionHandler)&AllocatePsiTrainingState::lstSoldiersClick);
113 	int row = 0;
114 	for (std::vector<Soldier*>::const_iterator s = base->getSoldiers()->begin(); s != base->getSoldiers()->end(); ++s)
115 	{
116 		std::wostringstream ssStr;
117 		std::wostringstream ssSkl;
118 		_soldiers.push_back(*s);
119 		if ((*s)->getCurrentStats()->psiSkill > 0 || (Options::psiStrengthEval && _game->getSavedGame()->isResearched(_game->getRuleset()->getPsiRequirements())))
120 		{
121 			ssStr << L"   " << (*s)->getCurrentStats()->psiStrength;
122 		}
123 		else
124 		{
125 			ssStr << tr("STR_UNKNOWN").c_str();
126 		}
127 		if ((*s)->getCurrentStats()->psiSkill > 0)
128 		{
129 			ssSkl << (*s)->getCurrentStats()->psiSkill << "/+" << (*s)->getImprovement();
130 		}
131 		else
132 		{
133 			ssSkl << "0/+0";
134 		}
135 		if ((*s)->isInPsiTraining())
136 		{
137 			_lstSoldiers->addRow(4, (*s)->getName().c_str(), ssStr.str().c_str(), ssSkl.str().c_str(), tr("STR_YES").c_str());
138 			_lstSoldiers->setRowColor(row, Palette::blockOffset(13)+5);
139 		}
140 		else
141 		{
142 			_lstSoldiers->addRow(4, (*s)->getName().c_str(), ssStr.str().c_str(), ssSkl.str().c_str(), tr("STR_NO").c_str());
143 			_lstSoldiers->setRowColor(row, Palette::blockOffset(15)+6);
144 		}
145 		row++;
146 	}
147 }
148 /**
149  *
150  */
~AllocatePsiTrainingState()151 AllocatePsiTrainingState::~AllocatePsiTrainingState()
152 {
153 
154 }
155 
156 /**
157  * Returns to the previous screen.
158  * @param action Pointer to an action.
159  */
btnOkClick(Action *)160 void AllocatePsiTrainingState::btnOkClick(Action *)
161 {
162 	_game->popState();
163 }
164 
165 /**
166  * Assigns / removes a soldier from Psi Training.
167  * @param action Pointer to an action.
168  */
lstSoldiersClick(Action * action)169 void AllocatePsiTrainingState::lstSoldiersClick(Action *action)
170 {
171 	_sel = _lstSoldiers->getSelectedRow();
172 	if (action->getDetails()->button.button == SDL_BUTTON_LEFT)
173 	{
174 		if (!_base->getSoldiers()->at(_sel)->isInPsiTraining())
175 		{
176 			if (_base->getUsedPsiLabs() < _base->getAvailablePsiLabs())
177 			{
178 				_lstSoldiers->setCellText(_sel, 3, tr("STR_YES").c_str());
179 				_lstSoldiers->setRowColor(_sel, Palette::blockOffset(13)+5);
180 				_labSpace--;
181 				_txtRemaining->setText(tr("STR_REMAINING_PSI_LAB_CAPACITY").arg(_labSpace));
182 				_base->getSoldiers()->at(_sel)->setPsiTraining();
183 			}
184 		}
185 		else
186 		{
187 			_lstSoldiers->setCellText(_sel, 3, tr("STR_NO").c_str());
188 			_lstSoldiers->setRowColor(_sel, Palette::blockOffset(15)+6);
189 			_labSpace++;
190 			_txtRemaining->setText(tr("STR_REMAINING_PSI_LAB_CAPACITY").arg(_labSpace));
191 			_base->getSoldiers()->at(_sel)->setPsiTraining();
192 		}
193 	}
194 }
195 
196 }
197