forked from OpenXcom/OpenXcom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.cpp
More file actions
231 lines (211 loc) · 4.4 KB
/
Copy pathWindow.cpp
File metadata and controls
231 lines (211 loc) · 4.4 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
227
228
229
230
231
/*
* 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 "Window.h"
#include <SDL.h>
#include "../aresame.h"
#include "../Engine/Timer.h"
#include "../Engine/Sound.h"
#include "../Engine/RNG.h"
namespace OpenXcom
{
const double Window::POPUP_SPEED = 0.075;
Sound *Window::soundPopup[3] = {0, 0, 0};
/**
* Sets up a blank window with the specified size and position.
* @param state Pointer to state the window belongs to.
* @param width Width in pixels.
* @param height Height in pixels.
* @param x X position in pixels.
* @param y Y position in pixels.
* @param popup Popup animation.
*/
Window::Window(State *state, int width, int height, int x, int y, WindowPopup popup) : Surface(width, height, x, y), _bg(0), _color(0), _popup(popup), _popupStep(0.0), _state(state), _contrast(false), _screen(false)
{
_timer = new Timer(10);
_timer->onTimer((SurfaceHandler)&Window::popup);
if (_popup == POPUP_NONE)
{
_popupStep = 1.0;
}
else
{
_timer->start();
if (_state != 0)
{
_screen = state->isScreen();
if (_screen)
_state->toggleScreen();
}
}
}
/**
* Deletes timers.
*/
Window::~Window()
{
delete _timer;
}
/**
* Changes the surface used to draw the background of the window.
* @param bg New background.
*/
void Window::setBackground(Surface *bg)
{
if (_popupStep < 1.0)
{
_state->hideAll();
setHidden(false);
}
_bg = bg;
_redraw = true;
}
/**
* Changes the color used to draw the shaded border.
* @param color Color value.
*/
void Window::setColor(Uint8 color)
{
if (_popupStep < 1.0)
{
_state->hideAll();
setHidden(false);
}
_color = color;
_redraw = true;
}
/**
* Returns the color used to draw the shaded border.
* @return Color value.
*/
Uint8 Window::getColor() const
{
return _color;
}
/**
* Enables/disables high contrast color. Mostly used for
* Battlescape UI.
* @param contrast High contrast setting.
*/
void Window::setHighContrast(bool contrast)
{
_contrast = contrast;
_redraw = true;
}
/**
* Keeps the animation timers running.
*/
void Window::think()
{
_timer->think(0, this);
}
/**
* Plays the window popup animation.
*/
void Window::popup()
{
if ( AreSame(_popupStep, 0.0) )
{
int sound = RNG::generate(0, 2);
if (soundPopup[sound] != 0)
{
soundPopup[sound]->play();
}
}
if (_popupStep < 1.0)
{
_popupStep += POPUP_SPEED;
}
else
{
if (_screen)
{
_state->toggleScreen();
}
_state->showAll();
_popupStep = 1.0;
_timer->stop();
}
_redraw = true;
}
/**
* Draws the bordered window with a graphic background.
* The background never moves with the window, it's
* always aligned to the top-left corner of the screen
* and cropped to fit the inside area.
*/
void Window::draw()
{
Surface::draw();
SDL_Rect square;
if (_popup == POPUP_HORIZONTAL || _popup == POPUP_BOTH)
{
square.x = (int)((getWidth() - getWidth() * _popupStep) / 2);
square.w = (int)(getWidth() * _popupStep);
}
else
{
square.x = 0;
square.w = getWidth();
}
if (_popup == POPUP_VERTICAL || _popup == POPUP_BOTH)
{
square.y = (int)((getHeight() - getHeight() * _popupStep) / 2);
square.h = (int)(getHeight() * _popupStep);
}
else
{
square.y = 0;
square.h = getHeight();
}
int mul = 1;
if (_contrast)
{
mul = 2;
}
Uint8 color = _color + 3 * mul;
for (int i = 0; i < 5; ++i)
{
drawRect(&square, color);
if (i < 2)
color -= 1 * mul;
else
color += 1 * mul;
square.x++;
square.y++;
if (square.w >= 2)
square.w -= 2;
else
square.w = 1;
if (square.h >= 2)
square.h -= 2;
else
square.h = 1;
}
if (_bg != 0)
{
_bg->getCrop()->x = getX() + square.x - _dx;
_bg->getCrop()->y = getY() + square.y - _dy;
_bg->getCrop()->w = square.w ;
_bg->getCrop()->h = square.h ;
_bg->setX(square.x);
_bg->setY(square.y);
_bg->blit(this);
}
}
}