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 "ManufactureStartState.h"
20 #include "../Interface/Window.h"
21 #include "../Interface/TextButton.h"
22 #include "../Interface/Text.h"
23 #include "../Interface/TextList.h"
24 #include "../Engine/Game.h"
25 #include "../Engine/Language.h"
26 #include "../Engine/Palette.h"
27 #include "../Engine/Options.h"
28 #include "../Resource/ResourcePack.h"
29 #include "../Ruleset/RuleManufacture.h"
30 #include "../Savegame/Base.h"
31 #include "../Savegame/ItemContainer.h"
32 #include "ManufactureInfoState.h"
33 #include "../Savegame/SavedGame.h"
34 #include <sstream>
35 
36 namespace OpenXcom
37 {
38 
39 /**
40  * Initializes all the elements in the productions start screen.
41  * @param game Pointer to the core game.
42  * @param base Pointer to the base to get info from.
43  * @param item The RuleManufacture to produce.
44  */
ManufactureStartState(Game * game,Base * base,RuleManufacture * item)45 ManufactureStartState::ManufactureStartState(Game * game, Base * base, RuleManufacture * item) : State(game), _base(base), _item(item)
46 {
47 	_screen = false;
48 
49 	_window = new Window(this, 320, 160, 0, 20);
50 	_btnCancel = new TextButton(136, 16, 16, 155);
51 	_txtTitle = new Text(320, 17, 0, 30);
52 	_txtManHour = new Text(290, 9, 16, 50);
53 	_txtCost = new Text(290, 9, 16, 60);
54 	_txtWorkSpace = new Text(290, 9, 16, 70);
55 
56 	_txtRequiredItemsTitle = new Text(290, 9, 16, 84);
57 	_txtItemNameColumn = new Text(60, 16, 30, 92);
58 	_txtUnitRequiredColumn = new Text(60, 16, 155, 92);
59 	_txtUnitAvailableColumn = new Text(60, 16, 230, 92);
60 	_lstRequiredItems = new TextList(270, 40, 30, 108);
61 
62 	_btnStart = new TextButton(136, 16, 168, 155);
63 
64 	// Set palette
65 	setPalette("PAL_BASESCAPE", 6);
66 
67 	add(_window);
68 	add(_txtTitle);
69 	add(_txtManHour);
70 	add(_txtCost);
71 	add(_txtWorkSpace);
72 	add(_btnCancel);
73 
74 	add(_txtRequiredItemsTitle);
75 	add(_txtItemNameColumn);
76 	add(_txtUnitRequiredColumn);
77 	add(_txtUnitAvailableColumn);
78 	add(_lstRequiredItems);
79 
80 	add(_btnStart);
81 
82 	centerAllSurfaces();
83 
84 	_window->setColor(Palette::blockOffset(13)+10);
85 	_window->setBackground(_game->getResourcePack()->getSurface("BACK17.SCR"));
86 
87 	_txtTitle->setColor(Palette::blockOffset(13)+10);
88 	_txtTitle->setText(tr(_item->getName()));
89 	_txtTitle->setBig();
90 	_txtTitle->setAlign(ALIGN_CENTER);
91 
92 	_txtManHour->setColor(Palette::blockOffset(13)+10);
93 	_txtManHour->setText(tr("STR_ENGINEER_HOURS_TO_PRODUCE_ONE_UNIT").arg(_item->getManufactureTime()));
94 
95 	_txtCost->setColor(Palette::blockOffset(13)+10);
96 	_txtCost->setSecondaryColor(Palette::blockOffset(13));
97 	_txtCost->setText(tr("STR_COST_PER_UNIT_").arg(Text::formatFunding(_item->getManufactureCost())));
98 
99 	_txtWorkSpace->setColor(Palette::blockOffset(13)+10);
100 	_txtWorkSpace->setSecondaryColor(Palette::blockOffset(13));
101 	_txtWorkSpace->setText(tr("STR_WORK_SPACE_REQUIRED").arg(_item->getRequiredSpace()));
102 
103 	_btnCancel->setColor(Palette::blockOffset(13)+10);
104 	_btnCancel->setText(tr("STR_CANCEL_UC"));
105 	_btnCancel->onMouseClick((ActionHandler)&ManufactureStartState::btnCancelClick);
106 	_btnCancel->onKeyboardPress((ActionHandler)&ManufactureStartState::btnCancelClick, Options::keyCancel);
107 
108 	const std::map<std::string, int> & requiredItems (_item->getRequiredItems());
109 	int availableWorkSpace = _base->getFreeWorkshops();
110 	bool productionPossible (game->getSavedGame()->getFunds() > _item->getManufactureCost ());
111 	productionPossible &= (availableWorkSpace > 0);
112 
113 	_txtRequiredItemsTitle->setColor(Palette::blockOffset(13)+10);
114 	_txtRequiredItemsTitle->setText(tr("STR_SPECIAL_MATERIALS_REQUIRED"));
115 	_txtRequiredItemsTitle->setAlign(ALIGN_CENTER);
116 	_txtItemNameColumn->setColor(Palette::blockOffset(13)+10);
117 	_txtItemNameColumn->setText(tr("STR_ITEM_REQUIRED"));
118 	_txtItemNameColumn->setWordWrap(true);
119 	_txtUnitRequiredColumn->setColor(Palette::blockOffset(13)+10);
120 	_txtUnitRequiredColumn->setText(tr("STR_UNITS_REQUIRED"));
121 	_txtUnitRequiredColumn->setWordWrap(true);
122 	_txtUnitAvailableColumn->setColor(Palette::blockOffset(13)+10);
123 	_txtUnitAvailableColumn->setText(tr("STR_UNITS_AVAILABLE"));
124 	_txtUnitAvailableColumn->setWordWrap(true);
125 
126 	_lstRequiredItems->setColumns(3, 140, 75, 55);
127 	_lstRequiredItems->setBackground(_window);
128 	_lstRequiredItems->setColor(Palette::blockOffset(13));
129 	_lstRequiredItems->setArrowColor(Palette::blockOffset(15)+1);
130 
131 	ItemContainer * itemContainer (base->getItems());
132 	int row = 0;
133 	for(std::map<std::string, int>::const_iterator iter = requiredItems.begin ();
134 		iter != requiredItems.end ();
135 		++iter)
136 	{
137 		std::wostringstream s1, s2;
138 		s1 << iter->second;
139 		s2 << itemContainer->getItem(iter->first);
140 		productionPossible &= (itemContainer->getItem(iter->first) >= iter->second);
141 		_lstRequiredItems->addRow(3, tr(iter->first).c_str(), s1.str().c_str(), s2.str().c_str());
142 		_lstRequiredItems->setCellColor(row, 0, Palette::blockOffset(13)+10);
143 		row++;
144 	}
145 	_txtRequiredItemsTitle->setVisible(!requiredItems.empty());
146 	_txtItemNameColumn->setVisible(!requiredItems.empty());
147 	_txtUnitRequiredColumn->setVisible(!requiredItems.empty());
148 	_txtUnitAvailableColumn->setVisible(!requiredItems.empty());
149 	_lstRequiredItems->setVisible(!requiredItems.empty());
150 
151 	_btnStart->setColor(Palette::blockOffset(13)+10);
152 	_btnStart->setText(tr("STR_START_PRODUCTION"));
153 	_btnStart->onMouseClick((ActionHandler)&ManufactureStartState::btnStartClick);
154 	_btnStart->onKeyboardPress((ActionHandler)&ManufactureStartState::btnStartClick, Options::keyOk);
155 	_btnStart->setVisible(productionPossible);
156 }
157 
158 /**
159  * Returns to previous screen.
160  * @param action A pointer to an Action.
161  */
btnCancelClick(Action *)162 void ManufactureStartState::btnCancelClick(Action *)
163 {
164 	_game->popState();
165 }
166 
167 /**
168  * Go to the Production settings screen.
169  * @param action A pointer to an Action.
170  */
btnStartClick(Action *)171 void ManufactureStartState::btnStartClick(Action *)
172 {
173 	_game->pushState(new ManufactureInfoState(_game, _base, _item));
174 }
175 }
176