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 "BuildFacilitiesState.h"
20 #include "../Engine/Game.h"
21 #include "../Resource/ResourcePack.h"
22 #include "../Engine/Language.h"
23 #include "../Engine/Palette.h"
24 #include "../Engine/Options.h"
25 #include "../Interface/TextButton.h"
26 #include "../Interface/Window.h"
27 #include "../Interface/Text.h"
28 #include "../Interface/TextList.h"
29 #include "../Ruleset/Ruleset.h"
30 #include "../Ruleset/RuleBaseFacility.h"
31 #include "../Savegame/SavedGame.h"
32 #include "PlaceFacilityState.h"
33
34 namespace OpenXcom
35 {
36
37 /**
38 * Initializes all the elements in the Build Facilities window.
39 * @param game Pointer to the core game.
40 * @param base Pointer to the base to get info from.
41 * @param state Pointer to the base state to refresh.
42 */
BuildFacilitiesState(Game * game,Base * base,State * state)43 BuildFacilitiesState::BuildFacilitiesState(Game *game, Base *base, State *state) : State(game), _base(base), _state(state), _facilities()
44 {
45 _screen = false;
46
47 // Create objects
48 _window = new Window(this, 128, 160, 192, 40, POPUP_VERTICAL);
49 _btnOk = new TextButton(112, 16, 200, 176);
50 _lstFacilities = new TextList(100, 104, 200, 64);
51 _txtTitle = new Text(118, 17, 197, 48);
52
53 // Set palette
54 setPalette("PAL_BASESCAPE", 6);
55
56 add(_window);
57 add(_btnOk);
58 add(_txtTitle);
59 add(_lstFacilities);
60
61 centerAllSurfaces();
62
63 // Set up objects
64 _window->setColor(Palette::blockOffset(13)+5);
65 _window->setBackground(_game->getResourcePack()->getSurface("BACK05.SCR"));
66
67 _btnOk->setColor(Palette::blockOffset(13)+5);
68 _btnOk->setText(tr("STR_OK"));
69 _btnOk->onMouseClick((ActionHandler)&BuildFacilitiesState::btnOkClick);
70 _btnOk->onKeyboardPress((ActionHandler)&BuildFacilitiesState::btnOkClick, Options::keyCancel);
71
72 _txtTitle->setColor(Palette::blockOffset(13));
73 _txtTitle->setBig();
74 _txtTitle->setAlign(ALIGN_CENTER);
75 _txtTitle->setText(tr("STR_INSTALLATION"));
76
77 _lstFacilities->setColor(Palette::blockOffset(13)+5);
78 _lstFacilities->setArrowColor(Palette::blockOffset(13)+5);
79 _lstFacilities->setColumns(1, 100);
80 _lstFacilities->setSelectable(true);
81 _lstFacilities->setBackground(_window);
82 _lstFacilities->setMargin(2);
83 _lstFacilities->setWordWrap(true);
84 _lstFacilities->onMouseClick((ActionHandler)&BuildFacilitiesState::lstFacilitiesClick);
85
86 PopulateBuildList();
87 }
88
89 /**
90 *
91 */
~BuildFacilitiesState()92 BuildFacilitiesState::~BuildFacilitiesState()
93 {
94
95 }
96
97 /**
98 * Populates the build list from the current "available" facilities.
99 */
PopulateBuildList()100 void BuildFacilitiesState::PopulateBuildList()
101 {
102 const std::vector<std::string> &facilities = _game->getRuleset()->getBaseFacilitiesList();
103 for (std::vector<std::string>::const_iterator i = facilities.begin(); i != facilities.end(); ++i)
104 {
105 RuleBaseFacility *rule = _game->getRuleset()->getBaseFacility(*i);
106 if (_game->getSavedGame()->isResearched(rule->getRequirements()) && !rule->isLift())
107 _facilities.push_back(rule);
108 }
109
110 for (std::vector<RuleBaseFacility*>::iterator i = _facilities.begin(); i != _facilities.end(); ++i)
111 {
112 _lstFacilities->addRow(1, tr((*i)->getType()).c_str());
113 }
114 }
115
116 /**
117 * The player can change the selected base
118 * or change info on other screens.
119 */
init()120 void BuildFacilitiesState::init()
121 {
122 _state->init();
123 State::init();
124 }
125
126 /**
127 * Returns to the previous screen.
128 * @param action Pointer to an action.
129 */
btnOkClick(Action *)130 void BuildFacilitiesState::btnOkClick(Action *)
131 {
132 _game->popState();
133 }
134
135 /**
136 * Places the selected facility.
137 * @param action Pointer to an action.
138 */
lstFacilitiesClick(Action *)139 void BuildFacilitiesState::lstFacilitiesClick(Action *)
140 {
141 _game->pushState(new PlaceFacilityState(_game, _base, _facilities[_lstFacilities->getSelectedRow()]));
142 }
143
144 }
145