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 "BaseNameState.h"
20 #include "../Engine/Game.h"
21 #include "../Engine/Action.h"
22 #include "../Resource/ResourcePack.h"
23 #include "../Engine/Language.h"
24 #include "../Engine/Palette.h"
25 #include "../Interface/Window.h"
26 #include "../Interface/Text.h"
27 #include "../Interface/TextEdit.h"
28 #include "../Interface/TextButton.h"
29 #include "../Savegame/Base.h"
30 #include "../Basescape/PlaceLiftState.h"
31 #include "../Engine/Options.h"
32
33 namespace OpenXcom
34 {
35
36 /**
37 * Initializes all the elements in a Base Name window.
38 * @param game Pointer to the core game.
39 * @param base Pointer to the base to name.
40 * @param globe Pointer to the Geoscape globe.
41 * @param first Is this the first base in the game?
42 */
BaseNameState(Game * game,Base * base,Globe * globe,bool first)43 BaseNameState::BaseNameState(Game *game, Base *base, Globe *globe, bool first) : State(game), _base(base), _globe(globe), _first(first)
44 {
45 _globe->onMouseOver(0);
46
47 _screen = false;
48
49 // Create objects
50 _window = new Window(this, 192, 80, 32, 60, POPUP_BOTH);
51 _btnOk = new TextButton(162, 12, 47, 118);
52 _txtTitle = new Text(182, 17, 37, 70);
53 _edtName = new TextEdit(this, 127, 16, 59, 94);
54
55 // Set palette
56 setPalette("PAL_GEOSCAPE", 0);
57
58 add(_window);
59 add(_btnOk);
60 add(_txtTitle);
61 add(_edtName);
62
63 centerAllSurfaces();
64
65 // Set up objects
66 _window->setColor(Palette::blockOffset(8)+5);
67 _window->setBackground(_game->getResourcePack()->getSurface("BACK01.SCR"));
68
69 _btnOk->setColor(Palette::blockOffset(8)+5);
70 _btnOk->setText(tr("STR_OK"));
71 _btnOk->onMouseClick((ActionHandler)&BaseNameState::btnOkClick);
72 //_btnOk->onKeyboardPress((ActionHandler)&BaseNameState::btnOkClick, Options::keyOk);
73 _btnOk->onKeyboardPress((ActionHandler)&BaseNameState::btnOkClick, Options::keyCancel);
74
75 //something must be in the name before it is acceptable
76 _btnOk->setVisible(false);
77
78 _txtTitle->setColor(Palette::blockOffset(8)+5);
79 _txtTitle->setAlign(ALIGN_CENTER);
80 _txtTitle->setBig();
81 _txtTitle->setText(tr("STR_BASE_NAME"));
82
83 _edtName->setColor(Palette::blockOffset(8)+5);
84 _edtName->setBig();
85 _edtName->setFocus(true, false);
86 _edtName->onChange((ActionHandler)&BaseNameState::edtNameChange);
87 }
88
89 /**
90 *
91 */
~BaseNameState()92 BaseNameState::~BaseNameState()
93 {
94
95 }
96
97 /**
98 * Updates the base name and disables the OK button
99 * if no name is entered.
100 * @param action Pointer to an action.
101 */
edtNameChange(Action * action)102 void BaseNameState::edtNameChange(Action *action)
103 {
104 _base->setName(_edtName->getText());
105 if (action->getDetails()->key.keysym.sym == SDLK_RETURN ||
106 action->getDetails()->key.keysym.sym == SDLK_KP_ENTER)
107 {
108 if (!_edtName->getText().empty())
109 {
110 btnOkClick(action);
111 }
112 }
113 else
114 {
115 _btnOk->setVisible(!_edtName->getText().empty());
116 }
117 }
118
119 /**
120 * Returns to the previous screen
121 * @param action Pointer to an action.
122 */
btnOkClick(Action *)123 void BaseNameState::btnOkClick(Action *)
124 {
125 if (!_edtName->getText().empty())
126 {
127 _game->popState();
128 _game->popState();
129 if (!_first || Options::customInitialBase)
130 {
131 if (!_first)
132 {
133 _game->popState();
134 }
135 _game->pushState(new PlaceLiftState(_game, _base, _globe, _first));
136 }
137 }
138 }
139
140 }
141