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 "SelectStartFacilityState.h"
20 #include "../Engine/Game.h"
21 #include "../Resource/ResourcePack.h"
22 #include "../Engine/Language.h"
23 #include "../Engine/Palette.h"
24 #include "../Interface/TextButton.h"
25 #include "../Interface/Window.h"
26 #include "../Interface/Text.h"
27 #include "../Interface/TextList.h"
28 #include "../Ruleset/Ruleset.h"
29 #include "../Ruleset/RuleBaseFacility.h"
30 #include "../Savegame/SavedGame.h"
31 #include "../Savegame/Base.h"
32 #include "../Savegame/BaseFacility.h"
33 #include "../Engine/Options.h"
34 #include "PlaceStartFacilityState.h"
35 #include "PlaceLiftState.h"
36 
37 namespace OpenXcom
38 {
39 
40 /**
41  * Initializes all the elements in the Build Facilities window.
42  * @param game Pointer to the core game.
43  * @param base Pointer to the base to get info from.
44  * @param state Pointer to the base state to refresh.
45  * @param globe Pointer to the globe to refresh.
46  */
SelectStartFacilityState(Game * game,Base * base,State * state,Globe * globe)47 SelectStartFacilityState::SelectStartFacilityState(Game *game, Base *base, State *state, Globe *globe) : BuildFacilitiesState(game, base, state), _globe(globe)
48 {
49 	_facilities = _game->getRuleset()->getCustomBaseFacilities();
50 
51 	_btnOk->setText(tr("STR_RESET"));
52 	_btnOk->onMouseClick((ActionHandler)&SelectStartFacilityState::btnOkClick);
53 	_btnOk->onKeyboardPress(0, Options::keyCancel);
54 
55 	_lstFacilities->onMouseClick((ActionHandler)&SelectStartFacilityState::lstFacilitiesClick);
56 
57 	populateBuildList();
58 }
59 
60 /**
61  *
62  */
~SelectStartFacilityState()63 SelectStartFacilityState::~SelectStartFacilityState()
64 {
65 
66 }
67 
68 /**
69  * Populates the build list from the current "available" facilities.
70  */
populateBuildList()71 void SelectStartFacilityState::populateBuildList()
72 {
73 	_lstFacilities->clearList();
74 	for (std::vector<RuleBaseFacility*>::iterator i = _facilities.begin(); i != _facilities.end(); ++i)
75 	{
76 		_lstFacilities->addRow(1, tr((*i)->getType()).c_str());
77 	}
78 }
79 
80 /**
81  * Resets the base building.
82  * @param action Pointer to an action.
83  */
btnOkClick(Action *)84 void SelectStartFacilityState::btnOkClick(Action *)
85 {
86 	for (std::vector<BaseFacility*>::iterator i = _base->getFacilities()->begin(); i != _base->getFacilities()->end(); ++i)
87 	{
88 		delete *i;
89 	}
90 	_base->getFacilities()->clear();
91 	_game->popState();
92 	_game->popState();
93 	_game->pushState(new PlaceLiftState(_game, _base, _globe, true));
94 }
95 
96 /**
97  * Places the selected facility.
98  * @param action Pointer to an action.
99  */
lstFacilitiesClick(Action *)100 void SelectStartFacilityState::lstFacilitiesClick(Action *)
101 {
102 	_game->pushState(new PlaceStartFacilityState(_game, _base, this, _facilities[_lstFacilities->getSelectedRow()]));
103 }
104 
105 /**
106  * Callback from PlaceStartFacilityState.
107  * Removes placed facility from the list.
108  */
facilityBuilt()109 void SelectStartFacilityState::facilityBuilt()
110 {
111 	_facilities.erase(_facilities.begin() + _lstFacilities->getSelectedRow());
112 	if (_facilities.empty())
113 	{
114 		_game->popState();
115 		_game->popState(); // return to geoscape, force timer to start.
116 	}
117 	else
118 	{
119 		populateBuildList();
120 	}
121 }
122 
123 }
124