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 "ConfirmNewBaseState.h"
20 #include "../Engine/Game.h"
21 #include "../Resource/ResourcePack.h"
22 #include "../Engine/Language.h"
23 #include "../Engine/Palette.h"
24 #include "../Engine/Surface.h"
25 #include "../Interface/Window.h"
26 #include "../Interface/Text.h"
27 #include "../Interface/TextButton.h"
28 #include "../Savegame/SavedGame.h"
29 #include "../Savegame/Region.h"
30 #include "../Ruleset/RuleRegion.h"
31 #include "../Savegame/Base.h"
32 #include "BaseNameState.h"
33 #include "../Menu/ErrorMessageState.h"
34 #include "../Engine/Options.h"
35 
36 namespace OpenXcom
37 {
38 
39 /**
40  * Initializes all the elements in the Confirm New Base window.
41  * @param game Pointer to the core game.
42  * @param base Pointer to the base to place.
43  * @param globe Pointer to the Geoscape globe.
44  */
ConfirmNewBaseState(Game * game,Base * base,Globe * globe)45 ConfirmNewBaseState::ConfirmNewBaseState(Game *game, Base *base, Globe *globe) : State(game), _base(base), _globe(globe), _cost(0)
46 {
47 	_screen = false;
48 
49 	// Create objects
50 	_window = new Window(this, 224, 72, 16, 64);
51 	_btnOk = new TextButton(54, 12, 68, 104);
52 	_btnCancel = new TextButton(54, 12, 138, 104);
53 	_txtCost = new Text(120, 9, 68, 80);
54 	_txtArea = new Text(120, 9, 68, 90);
55 
56 	// Set palette
57 	setPalette("PAL_GEOSCAPE", 0);
58 
59 	add(_window);
60 	add(_btnOk);
61 	add(_btnCancel);
62 	add(_txtCost);
63 	add(_txtArea);
64 
65 	centerAllSurfaces();
66 
67 	// Set up objects
68 	_window->setColor(Palette::blockOffset(15)-1);
69 	_window->setBackground(_game->getResourcePack()->getSurface("BACK01.SCR"));
70 
71 	_btnOk->setColor(Palette::blockOffset(15)-1);
72 	_btnOk->setText(tr("STR_OK"));
73 	_btnOk->onMouseClick((ActionHandler)&ConfirmNewBaseState::btnOkClick);
74 	_btnOk->onKeyboardPress((ActionHandler)&ConfirmNewBaseState::btnOkClick, Options::keyOk);
75 
76 	_btnCancel->setColor(Palette::blockOffset(15)-1);
77 	_btnCancel->setText(tr("STR_CANCEL_UC"));
78 	_btnCancel->onMouseClick((ActionHandler)&ConfirmNewBaseState::btnCancelClick);
79 	_btnCancel->onKeyboardPress((ActionHandler)&ConfirmNewBaseState::btnCancelClick, Options::keyCancel);
80 
81 	std::wstring area;
82 	for (std::vector<Region*>::iterator i = _game->getSavedGame()->getRegions()->begin(); i != _game->getSavedGame()->getRegions()->end(); ++i)
83 	{
84 		if ((*i)->getRules()->insideRegion(_base->getLongitude(), _base->getLatitude()))
85 		{
86 			_cost = (*i)->getRules()->getBaseCost();
87 			area = tr((*i)->getRules()->getType());
88 			break;
89 		}
90 	}
91 
92 	_txtCost->setColor(Palette::blockOffset(15)-1);
93 	_txtCost->setSecondaryColor(Palette::blockOffset(8)+10);
94 	_txtCost->setText(tr("STR_COST_").arg(Text::formatFunding(_cost)));
95 
96 	_txtArea->setColor(Palette::blockOffset(15)-1);
97 	_txtArea->setSecondaryColor(Palette::blockOffset(8)+10);
98 	_txtArea->setText(tr("STR_AREA_").arg(area));
99 }
100 
101 /**
102  *
103  */
~ConfirmNewBaseState()104 ConfirmNewBaseState::~ConfirmNewBaseState()
105 {
106 
107 }
108 
109 /**
110  * Go to the Place Access Lift screen.
111  * @param action Pointer to an action.
112  */
btnOkClick(Action *)113 void ConfirmNewBaseState::btnOkClick(Action *)
114 {
115 	if (_game->getSavedGame()->getFunds() >= _cost)
116 	{
117 		_game->getSavedGame()->setFunds(_game->getSavedGame()->getFunds() - _cost);
118 		_game->getSavedGame()->getBases()->push_back(_base);
119 		_game->pushState(new BaseNameState(_game, _base, _globe, false));
120 	}
121 	else
122 	{
123 		_game->pushState(new ErrorMessageState(_game, "STR_NOT_ENOUGH_MONEY", _palette, Palette::blockOffset(8)+10, "BACK01.SCR", 0));
124 	}
125 }
126 
127 /**
128  * Returns to the previous screen.
129  * @param action Pointer to an action.
130  */
btnCancelClick(Action *)131 void ConfirmNewBaseState::btnCancelClick(Action *)
132 {
133 	_globe->onMouseOver(0);
134 	_game->popState();
135 }
136 
137 }
138