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 "CraftsState.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 "../Engine/Options.h"
26 #include "../Interface/TextButton.h"
27 #include "../Interface/Window.h"
28 #include "../Interface/Text.h"
29 #include "../Interface/TextList.h"
30 #include "../Savegame/Craft.h"
31 #include "../Ruleset/RuleCraft.h"
32 #include "../Savegame/Base.h"
33 #include "../Menu/ErrorMessageState.h"
34 #include "CraftInfoState.h"
35 #include "SellState.h"
36 #include "../Savegame/SavedGame.h"
37 
38 namespace OpenXcom
39 {
40 
41 /**
42  * Initializes all the elements in the Equip Craft screen.
43  * @param game Pointer to the core game.
44  * @param base Pointer to the base to get info from.
45  */
CraftsState(Game * game,Base * base)46 CraftsState::CraftsState(Game *game, Base *base) : State(game), _base(base)
47 {
48 	// Create objects
49 	_window = new Window(this, 320, 200, 0, 0);
50 	_btnOk = new TextButton(288, 16, 16, 176);
51 	_txtTitle = new Text(298, 17, 16, 8);
52 	_txtBase = new Text(298, 17, 16, 24);
53 	_txtName = new Text(94, 9, 16, 40);
54 	_txtStatus = new Text(50, 9, 110, 40);
55 	_txtWeapon = new Text(50, 17, 160, 40);
56 	_txtCrew = new Text(58, 9, 210, 40);
57 	_txtHwp = new Text(46, 9, 268, 40);
58 	_lstCrafts = new TextList(288, 118, 8, 58);
59 
60 	// Set palette
61 	setPalette("PAL_BASESCAPE", 3);
62 
63 	add(_window);
64 	add(_btnOk);
65 	add(_txtTitle);
66 	add(_txtBase);
67 	add(_txtName);
68 	add(_txtStatus);
69 	add(_txtWeapon);
70 	add(_txtCrew);
71 	add(_txtHwp);
72 	add(_lstCrafts);
73 
74 	centerAllSurfaces();
75 
76 	// Set up objects
77 	_window->setColor(Palette::blockOffset(15)+1);
78 	_window->setBackground(_game->getResourcePack()->getSurface("BACK14.SCR"));
79 
80 	_btnOk->setColor(Palette::blockOffset(13)+10);
81 	_btnOk->setText(tr("STR_OK"));
82 	_btnOk->onMouseClick((ActionHandler)&CraftsState::btnOkClick);
83 	_btnOk->onKeyboardPress((ActionHandler)&CraftsState::btnOkClick, Options::keyCancel);
84 
85 	_txtTitle->setColor(Palette::blockOffset(15)+1);
86 	_txtTitle->setBig();
87 	_txtTitle->setText(tr("STR_INTERCEPTION_CRAFT"));
88 
89 	_txtBase->setColor(Palette::blockOffset(15)+1);
90 	_txtBase->setBig();
91 	_txtBase->setText(tr("STR_BASE_").arg(_base->getName()));
92 
93 	_txtName->setColor(Palette::blockOffset(15)+1);
94 	_txtName->setText(tr("STR_NAME_UC"));
95 
96 	_txtStatus->setColor(Palette::blockOffset(15)+1);
97 	_txtStatus->setText(tr("STR_STATUS"));
98 
99 	_txtWeapon->setColor(Palette::blockOffset(15)+1);
100 	_txtWeapon->setText(tr("STR_WEAPON_SYSTEMS"));
101 	_txtWeapon->setWordWrap(true);
102 
103 	_txtCrew->setColor(Palette::blockOffset(15)+1);
104 	_txtCrew->setText(tr("STR_CREW"));
105 
106 	_txtHwp->setColor(Palette::blockOffset(15)+1);
107 	_txtHwp->setText(tr("STR_HWPS"));
108 
109 	_lstCrafts->setColor(Palette::blockOffset(13)+10);
110 	_lstCrafts->setArrowColor(Palette::blockOffset(15)+1);
111 	_lstCrafts->setColumns(5, 94, 68, 44, 46, 28);
112 	_lstCrafts->setSelectable(true);
113 	_lstCrafts->setBackground(_window);
114 	_lstCrafts->setMargin(8);
115 	_lstCrafts->onMouseClick((ActionHandler)&CraftsState::lstCraftsClick);
116 }
117 
118 /**
119  *
120  */
~CraftsState()121 CraftsState::~CraftsState()
122 {
123 
124 }
125 
126 /**
127  * The soldier names can change
128  * after going into other screens.
129  */
init()130 void CraftsState::init()
131 {
132 	State::init();
133 	_lstCrafts->clearList();
134 	for (std::vector<Craft*>::iterator i = _base->getCrafts()->begin(); i != _base->getCrafts()->end(); ++i)
135 	{
136 		std::wostringstream ss, ss2, ss3;
137 		ss << (*i)->getNumWeapons() << "/" << (*i)->getRules()->getWeapons();
138 		ss2 << (*i)->getNumSoldiers();
139 		ss3 << (*i)->getNumVehicles();
140 		_lstCrafts->addRow(5, (*i)->getName(_game->getLanguage()).c_str(), tr((*i)->getStatus()).c_str(), ss.str().c_str(), ss2.str().c_str(), ss3.str().c_str());
141 	}
142 }
143 
144 /**
145  * Returns to the previous screen.
146  * @param action Pointer to an action.
147  */
btnOkClick(Action *)148 void CraftsState::btnOkClick(Action *)
149 {
150 	_game->popState();
151 
152 	if (_game->getSavedGame()->getMonthsPassed() > -1 && Options::storageLimitsEnforced && _base->storesOverfull())
153 	{
154 		_game->pushState(new SellState(_game, _base));
155 		_game->pushState(new ErrorMessageState(_game, tr("STR_STORAGE_EXCEEDED").arg(_base->getName()).c_str(), _palette, Palette::blockOffset(15)+1, "BACK01.SCR", 0));
156 	}
157 }
158 
159 /**
160  * Shows the selected craft's info.
161  * @param action Pointer to an action.
162  */
lstCraftsClick(Action *)163 void CraftsState::lstCraftsClick(Action *)
164 {
165 	if (_base->getCrafts()->at(_lstCrafts->getSelectedRow())->getStatus() != "STR_OUT")
166 	{
167 		_game->pushState(new CraftInfoState(_game, _base, _lstCrafts->getSelectedRow()));
168 	}
169 }
170 
171 }
172