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 "SoldierArmorState.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 "../Ruleset/Ruleset.h"
31 #include "../Ruleset/Armor.h"
32 #include "../Savegame/SavedGame.h"
33 #include "../Savegame/Soldier.h"
34 #include "../Savegame/Base.h"
35 #include "../Savegame/ItemContainer.h"
36 
37 namespace OpenXcom
38 {
39 
40 /**
41  * Initializes all the elements in the Soldier Armor window.
42  * @param game Pointer to the core game.
43  * @param base Pointer to the base to get info from.
44  * @param soldier ID of the selected soldier.
45  */
SoldierArmorState(Game * game,Base * base,size_t soldier)46 SoldierArmorState::SoldierArmorState(Game *game, Base *base, size_t soldier) : State(game), _base(base), _soldier(soldier)
47 {
48 	_screen = false;
49 
50 	// Create objects
51 	_window = new Window(this, 192, 120, 64, 40, POPUP_BOTH);
52 	_btnCancel = new TextButton(140, 16, 90, 136);
53 	_txtTitle = new Text(182, 16, 69, 48);
54 	_txtType = new Text(90, 9, 80, 72);
55 	_txtQuantity = new Text(70, 9, 177, 72);
56 	_lstArmor = new TextList(160, 40, 73, 88);
57 
58 	// Set palette
59 	setPalette("PAL_BASESCAPE", 4);
60 
61 	add(_window);
62 	add(_btnCancel);
63 	add(_txtTitle);
64 	add(_txtType);
65 	add(_txtQuantity);
66 	add(_lstArmor);
67 
68 	centerAllSurfaces();
69 
70 	// Set up objects
71 	_window->setColor(Palette::blockOffset(13)+10);
72 	_window->setBackground(_game->getResourcePack()->getSurface("BACK14.SCR"));
73 
74 	_btnCancel->setColor(Palette::blockOffset(13)+5);
75 	_btnCancel->setText(tr("STR_CANCEL_UC"));
76 	_btnCancel->onMouseClick((ActionHandler)&SoldierArmorState::btnCancelClick);
77 	_btnCancel->onKeyboardPress((ActionHandler)&SoldierArmorState::btnCancelClick, Options::keyCancel);
78 
79 	Soldier *s = _base->getSoldiers()->at(_soldier);
80 	_txtTitle->setColor(Palette::blockOffset(13)+5);
81 	_txtTitle->setAlign(ALIGN_CENTER);
82 	_txtTitle->setText(tr("STR_SELECT_ARMOR_FOR_SOLDIER").arg(s->getName()));
83 
84 	_txtType->setColor(Palette::blockOffset(13)+5);
85 	_txtType->setText(tr("STR_TYPE"));
86 
87 	_txtQuantity->setColor(Palette::blockOffset(13)+5);
88 	_txtQuantity->setText(tr("STR_QUANTITY_UC"));
89 
90 	_lstArmor->setColor(Palette::blockOffset(13));
91 	_lstArmor->setArrowColor(Palette::blockOffset(13)+5);
92 	_lstArmor->setColumns(2, 112, 41);
93 	_lstArmor->setSelectable(true);
94 	_lstArmor->setBackground(_window);
95 	_lstArmor->setMargin(8);
96 
97 	const std::vector<std::string> &armors = _game->getRuleset()->getArmorsList();
98 	for (std::vector<std::string>::const_iterator i = armors.begin(); i != armors.end(); ++i)
99 	{
100 		Armor *a = _game->getRuleset()->getArmor(*i);
101 		if (_base->getItems()->getItem(a->getStoreItem()) > 0)
102 		{
103 			_armors.push_back(a);
104 			std::wostringstream ss;
105 			if (_game->getSavedGame()->getMonthsPassed() > -1)
106 			{
107 				ss << _base->getItems()->getItem(a->getStoreItem());
108 			}
109 			else
110 			{
111 				ss << "-";
112 			}
113 			_lstArmor->addRow(2, tr(a->getType()).c_str(), ss.str().c_str());
114 		}
115 		else if (a->getStoreItem() == "STR_NONE")
116 		{
117 			_armors.push_back(a);
118 			_lstArmor->addRow(1, tr(a->getType()).c_str());
119 		}
120 	}
121 	_lstArmor->onMouseClick((ActionHandler)&SoldierArmorState::lstArmorClick);
122 }
123 
124 /**
125  *
126  */
~SoldierArmorState()127 SoldierArmorState::~SoldierArmorState()
128 {
129 
130 }
131 
132 /**
133  * Returns to the previous screen.
134  * @param action Pointer to an action.
135  */
btnCancelClick(Action *)136 void SoldierArmorState::btnCancelClick(Action *)
137 {
138 	_game->popState();
139 }
140 
141 /**
142  * Equips the armor on the soldier and returns to the previous screen.
143  * @param action Pointer to an action.
144  */
lstArmorClick(Action *)145 void SoldierArmorState::lstArmorClick(Action *)
146 {
147 	Soldier *soldier = _base->getSoldiers()->at(_soldier);
148 	if (_game->getSavedGame()->getMonthsPassed() != -1)
149 	{
150 		if (soldier->getArmor()->getStoreItem() != "STR_NONE")
151 		{
152 			_base->getItems()->addItem(soldier->getArmor()->getStoreItem());
153 		}
154 		if (_armors[_lstArmor->getSelectedRow()]->getStoreItem() != "STR_NONE")
155 		{
156 			_base->getItems()->removeItem(_armors[_lstArmor->getSelectedRow()]->getStoreItem());
157 		}
158 	}
159 	soldier->setArmor(_armors[_lstArmor->getSelectedRow()]);
160 
161 	_game->popState();
162 }
163 
164 }
165