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 "DismantleFacilityState.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 "../Savegame/Base.h"
29 #include "../Savegame/BaseFacility.h"
30 #include "../Basescape/BaseView.h"
31 #include "../Ruleset/RuleBaseFacility.h"
32 #include "../Savegame/SavedGame.h"
33 
34 namespace OpenXcom
35 {
36 
37 /**
38  * Initializes all the elements in a Dismantle Facility window.
39  * @param game Pointer to the core game.
40  * @param base Pointer to the base to get info from.
41  * @param view Pointer to the baseview to update.
42  * @param fac Pointer to the facility to dismantle.
43  */
DismantleFacilityState(Game * game,Base * base,BaseView * view,BaseFacility * fac)44 DismantleFacilityState::DismantleFacilityState(Game *game, Base *base, BaseView *view, BaseFacility *fac) : State(game), _base(base), _view(view), _fac(fac)
45 {
46 	_screen = false;
47 
48 	// Create objects
49 	_window = new Window(this, 152, 80, 20, 60);
50 	_btnOk = new TextButton(44, 16, 36, 115);
51 	_btnCancel = new TextButton(44, 16, 112, 115);
52 	_txtTitle = new Text(142, 9, 25, 75);
53 	_txtFacility = new Text(142, 9, 25, 85);
54 
55 	// Set palette
56 	setPalette("PAL_BASESCAPE", 6);
57 
58 	add(_window);
59 	add(_btnOk);
60 	add(_btnCancel);
61 	add(_txtTitle);
62 	add(_txtFacility);
63 
64 	centerAllSurfaces();
65 
66 	// Set up objects
67 	_window->setColor(Palette::blockOffset(15)+1);
68 	_window->setBackground(_game->getResourcePack()->getSurface("BACK13.SCR"));
69 
70 	_btnOk->setColor(Palette::blockOffset(15)+6);
71 	_btnOk->setText(tr("STR_OK"));
72 	_btnOk->onMouseClick((ActionHandler)&DismantleFacilityState::btnOkClick);
73 	_btnOk->onKeyboardPress((ActionHandler)&DismantleFacilityState::btnOkClick, Options::keyOk);
74 
75 	_btnCancel->setColor(Palette::blockOffset(15)+6);
76 	_btnCancel->setText(tr("STR_CANCEL_UC"));
77 	_btnCancel->onMouseClick((ActionHandler)&DismantleFacilityState::btnCancelClick);
78 	_btnCancel->onKeyboardPress((ActionHandler)&DismantleFacilityState::btnCancelClick, Options::keyCancel);
79 
80 	_txtTitle->setColor(Palette::blockOffset(13)+10);
81 	_txtTitle->setAlign(ALIGN_CENTER);
82 	_txtTitle->setText(tr("STR_DISMANTLE"));
83 
84 	_txtFacility->setColor(Palette::blockOffset(13)+10);
85 	_txtFacility->setAlign(ALIGN_CENTER);
86 	_txtFacility->setText(tr(_fac->getRules()->getType()));
87 }
88 
89 /**
90  *
91  */
~DismantleFacilityState()92 DismantleFacilityState::~DismantleFacilityState()
93 {
94 
95 }
96 
97 /**
98  * Dismantles the facility and returns
99  * to the previous screen.
100  * @param action Pointer to an action.
101  */
btnOkClick(Action *)102 void DismantleFacilityState::btnOkClick(Action *)
103 {
104 	if (!_fac->getRules()->isLift())
105 	{
106 		// Give refund if this is an unstarted, queued build.
107 		if (_fac->getBuildTime() > _fac->getRules()->getBuildTime())
108 		{
109 			_game->getSavedGame()->setFunds(_game->getSavedGame()->getFunds() + _fac->getRules()->getBuildCost());
110 		}
111 
112 		for (std::vector<BaseFacility*>::iterator i = _base->getFacilities()->begin(); i != _base->getFacilities()->end(); ++i)
113 		{
114 			if (*i == _fac)
115 			{
116 				_base->getFacilities()->erase(i);
117 				_view->resetSelectedFacility();
118 				delete _fac;
119 				if (Options::allowBuildingQueue) _view->reCalcQueuedBuildings();
120 				break;
121 			}
122 		}
123 	}
124 	// Remove whole base if it's the access lift
125 	else
126 	{
127 		for (std::vector<Base*>::iterator i = _game->getSavedGame()->getBases()->begin(); i != _game->getSavedGame()->getBases()->end(); ++i)
128 		{
129 			if (*i == _base)
130 			{
131 				_game->getSavedGame()->getBases()->erase(i);
132 				delete _base;
133 				break;
134 			}
135 		}
136 	}
137 	_game->popState();
138 }
139 
140 /**
141  * Returns to the previous screen.
142  * @param action Pointer to an action.
143  */
btnCancelClick(Action *)144 void DismantleFacilityState::btnCancelClick(Action *)
145 {
146 	_game->popState();
147 }
148 
149 }
150