forked from OpenXcom/OpenXcom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.cpp
More file actions
226 lines (204 loc) · 5.92 KB
/
Copy pathState.cpp
File metadata and controls
226 lines (204 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright 2010-2013 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenXcom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "State.h"
#include "InteractiveSurface.h"
#include "Game.h"
#include "Screen.h"
#include "Surface.h"
#include "Font.h"
#include "Language.h"
#include "LocalizedText.h"
#include "../Resource/ResourcePack.h"
#include "../Interface/Text.h"
#include "../Interface/TextButton.h"
#include "../Interface/TextEdit.h"
#include "../Interface/TextList.h"
#include "../Basescape/BaseView.h"
#include "../Battlescape/WarningMessage.h"
namespace OpenXcom
{
/**
* Initializes a brand new state with no child elements.
* By default states are full-screen.
* @param game Pointer to the core game.
*/
State::State(Game *game) : _game(game), _surfaces(), _screen(true)
{
}
/**
* Deletes all the child elements contained in the state.
*/
State::~State()
{
for (std::vector<Surface*>::iterator i = _surfaces.begin(); i < _surfaces.end(); ++i)
{
delete *i;
}
}
/**
* Adds a new child surface for the state to take care of,
* giving it the game's display palette. Once associated,
* the state handles all of the surface's behaviour
* and management automatically.
* @param surface Child surface.
* @note Since visible elements can overlap one another,
* they have to be added in ascending Z-Order to be blitted
* correctly onto the screen.
*/
void State::add(Surface *surface)
{
// Set palette
surface->setPalette(_game->getScreen()->getPalette());
// Set default fonts
if (_game->getResourcePack())
surface->setFonts(_game->getResourcePack()->getFont("Big.fnt"), _game->getResourcePack()->getFont("Small.fnt"));
_surfaces.push_back(surface);
}
/**
* Returns whether this is a full-screen state.
* This is used to optimize the state machine since full-screen
* states automatically cover the whole screen, (whether they
* actually use it all or not) so states behind them can be
* safely ignored since they'd be covered up.
* @return True if it's a screen, False otherwise.
*/
bool State::isScreen() const
{
return _screen;
}
/**
* Toggles the full-screen flag. Used by windows to
* keep the previous screen is display while the window
* is still "popping up".
*/
void State::toggleScreen()
{
_screen = !_screen;
}
/**
* Initializes the state and its child elements. This is
* used for settings that have to be reset every time the
* state is returned to focus (eg. palettes), so can't
* just be put in the constructor (remember there's a stack
* of states, so they can be created once while being
* repeatedly switched back into focus).
*/
void State::init()
{
}
/**
* Runs any code the state needs to keep updating every
* game cycle, like timers and other real-time elements.
*/
void State::think()
{
for (std::vector<Surface*>::iterator i = _surfaces.begin(); i != _surfaces.end(); ++i)
(*i)->think();
}
/**
* Takes care of any events from the core game engine,
* and passes them on to its InteractiveSurface child elements.
* @param action Pointer to an action.
*/
void State::handle(Action *action)
{
for (std::vector<Surface*>::reverse_iterator i = _surfaces.rbegin(); i != _surfaces.rend(); ++i)
{
InteractiveSurface* j = dynamic_cast<InteractiveSurface*>(*i);
if (j != 0)
j->handle(action, this);
}
}
/**
* Blits all the visible Surface child elements onto the
* display screen, by order of addition.
*/
void State::blit()
{
for (std::vector<Surface*>::iterator i = _surfaces.begin(); i != _surfaces.end(); ++i)
(*i)->blit(_game->getScreen()->getSurface());
}
/**
* Hides all the Surface child elements on display.
*/
void State::hideAll()
{
for (std::vector<Surface*>::iterator i = _surfaces.begin(); i != _surfaces.end(); ++i)
(*i)->setHidden(true);
}
/**
* Shows all the hidden Surface child elements.
*/
void State::showAll()
{
for (std::vector<Surface*>::iterator i = _surfaces.begin(); i != _surfaces.end(); ++i)
(*i)->setHidden(false);
}
/**
* Resets the status of all the Surface child elements,
* like unpressing buttons.
*/
void State::resetAll()
{
for (std::vector<Surface*>::iterator i = _surfaces.begin(); i != _surfaces.end(); ++i)
{
InteractiveSurface *s = dynamic_cast<InteractiveSurface*>(*i);
if (s != 0)
{
s->unpress(this);
}
}
}
/**
* Get the localized text for dictionary key @a id.
* This function forwards the call to Language::getString(const std::string &).
* @param id The dictionary key to search for.
* @return A reference to the localized text.
*/
const LocalizedText &State::tr(const std::string &id) const
{
return _game->getLanguage()->getString(id);
}
/**
* Get a modifiable copy of the localized text for dictionary key @a id.
* This function forwards the call to Language::getString(const std::string &, unsigned).
* @param id The dictionary key to search for.
* @param n The number to use for the proper version.
* @return A copy of the localized text.
*/
LocalizedText State::tr(const std::string &id, unsigned n) const
{
return _game->getLanguage()->getString(id, n);
}
void State::centerAllSurfaces()
{
for (std::vector<Surface*>::iterator i = _surfaces.begin(); i != _surfaces.end(); ++i)
{
(*i)->setX((*i)->getX() + Screen::getDX());
(*i)->setY((*i)->getY() + Screen::getDY());
}
}
void State::lowerAllSurfaces()
{
for (std::vector<Surface*>::iterator i = _surfaces.begin(); i != _surfaces.end(); ++i)
{
(*i)->setY((*i)->getY() + Screen::getDY() / 2);
}
}
}