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 "NewManufactureListState.h"
20 #include <algorithm>
21 #include "../Interface/Window.h"
22 #include "../Interface/TextButton.h"
23 #include "../Interface/Text.h"
24 #include "../Interface/TextList.h"
25 #include "../Engine/Game.h"
26 #include "../Engine/Language.h"
27 #include "../Engine/Palette.h"
28 #include "../Engine/Options.h"
29 #include "../Resource/ResourcePack.h"
30 #include "../Ruleset/RuleManufacture.h"
31 #include "../Ruleset/Ruleset.h"
32 #include "../Savegame/SavedGame.h"
33 #include "../Savegame/Production.h"
34 #include "../Savegame/Base.h"
35 #include "ManufactureStartState.h"
36 #include "../Menu/ErrorMessageState.h"
37 
38 namespace OpenXcom
39 {
40 
41 /**
42  * Initializes all the elements in the productions list screen.
43  * @param game Pointer to the core game.
44  * @param base Pointer to the base to get info from.
45  */
NewManufactureListState(Game * game,Base * base)46 NewManufactureListState::NewManufactureListState(Game *game, Base *base) : State(game), _base(base)
47 {
48 	_screen = false;
49 
50 	_window = new Window(this, 320, 140, 0, 30, POPUP_BOTH);
51 	_btnOk = new TextButton(304, 16, 8, 146);
52 	_txtTitle = new Text(320, 17, 0, 38);
53 	_txtItem = new Text(156, 9, 10, 54);
54 	_txtCategory = new Text(130, 9, 166, 54);
55 	_lstManufacture = new TextList(288, 80, 8, 62);
56 
57 	// Set palette
58 	setPalette("PAL_BASESCAPE", 6);
59 
60 	add(_window);
61 	add(_btnOk);
62 	add(_txtTitle);
63 	add(_txtItem);
64 	add(_txtCategory);
65 	add(_lstManufacture);
66 
67 	centerAllSurfaces();
68 
69 	_window->setColor(Palette::blockOffset(15)+1);
70 	_window->setBackground(_game->getResourcePack()->getSurface("BACK17.SCR"));
71 	_txtTitle->setColor(Palette::blockOffset(15)+1);
72 	_txtTitle->setText(tr("STR_PRODUCTION_ITEMS"));
73 	_txtTitle->setBig();
74 	_txtTitle->setAlign(ALIGN_CENTER);
75 
76 	_txtItem->setColor(Palette::blockOffset(15)+1);
77 	_txtItem->setText(tr("STR_ITEM"));
78 
79 	_txtCategory->setColor(Palette::blockOffset(15)+1);
80 	_txtCategory->setText(tr("STR_CATEGORY"));
81 
82 	_lstManufacture->setColumns(2, 156, 130);
83 	_lstManufacture->setSelectable(true);
84 	_lstManufacture->setBackground(_window);
85 	_lstManufacture->setMargin(2);
86 	_lstManufacture->setColor(Palette::blockOffset(13));
87 	_lstManufacture->setArrowColor(Palette::blockOffset(15)+1);
88 	_lstManufacture->onMouseClick((ActionHandler)&NewManufactureListState::lstProdClick);
89 
90 	_btnOk->setColor(Palette::blockOffset(13)+10);
91 	_btnOk->setText(tr("STR_OK"));
92 	_btnOk->onMouseClick((ActionHandler)&NewManufactureListState::btnOkClick);
93 	_btnOk->onKeyboardPress((ActionHandler)&NewManufactureListState::btnOkClick, Options::keyCancel);
94 }
95 
96 /**
97  * Initializes state (fills list of possible productions).
98  */
init()99 void NewManufactureListState::init()
100 {
101 	State::init();
102 	fillProductionList();
103 }
104 
105 /**
106  * Returns to the previous screen.
107  * @param action A pointer to an Action.
108  */
btnOkClick(Action *)109 void NewManufactureListState::btnOkClick(Action *)
110 {
111 	_game->popState();
112 }
113 
114 /**
115  * Opens the Production settings screen.
116  * @param action A pointer to an Action.
117 */
lstProdClick(Action *)118 void NewManufactureListState::lstProdClick (Action *)
119 {
120 	RuleManufacture *rule = _possibleProductions[_lstManufacture->getSelectedRow()];
121 	if (rule->getCategory() == "STR_CRAFT" && _base->getAvailableHangars() - _base->getUsedHangars() == 0)
122 	{
123 		_game->pushState(new ErrorMessageState(_game, "STR_NO_FREE_HANGARS_FOR_CRAFT_PRODUCTION", _palette, Palette::blockOffset(15)+1, "BACK17.SCR", 6));
124 	}
125 	else if (rule->getRequiredSpace() > _base->getFreeWorkshops())
126 	{
127 		_game->pushState(new ErrorMessageState(_game, "STR_NOT_ENOUGH_WORK_SPACE", _palette, Palette::blockOffset(15)+1, "BACK17.SCR", 6));
128 	}
129 	else
130 	{
131 		_game->pushState(new ManufactureStartState(_game, _base, rule));
132 	}
133 }
134 
135 
136 /**
137  * Fills the list of possible productions.
138  */
fillProductionList()139 void NewManufactureListState::fillProductionList()
140 {
141 	_lstManufacture->clearList();
142 	_possibleProductions.clear();
143 	_game->getSavedGame()->getAvailableProductions(_possibleProductions, _game->getRuleset(), _base);
144 
145 	for (std::vector<RuleManufacture *>::iterator it = _possibleProductions.begin (); it != _possibleProductions.end (); ++it)
146 	{
147 		_lstManufacture->addRow(2, tr((*it)->getName()).c_str(), tr((*it)->getCategory ()).c_str());
148 	}
149 }
150 
151 }
152