-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
about.cpp
434 lines (362 loc) · 12.2 KB
/
about.cpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
Copyright (C) 2003 - 2016 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program 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 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Show screen with scrolling credits.
*/
#include "about.hpp"
#include "global.hpp" // for false_, bool_
#include "config.hpp" // for config, etc
#include "cursor.hpp" // for setter, CURSOR_TYPE::WAIT
#include "events.hpp" // for pump, raise_draw_event, etc
#include "font.hpp" // for NORMAL_COLOR, SIZE_XLARGE
#include "game_config.hpp" // for game_title_background
#include "gettext.hpp" // for _
#include "image.hpp" // for get_image
#include "key.hpp" // for CKey
#include "marked-up_text.hpp" // for draw_text, LARGE_TEXT, etc
#include "sdl/rect.hpp" // for create_rect
#include "sdl/utils.hpp" // for surface, sdl_blit, etc
#include "serialization/string_utils.hpp" // for split, etc
#include "show_dialog.hpp" // for dialog_frame, etc
#include "tstring.hpp" // for operator==
#include "video.hpp" // for update_rect, CVideo
#include "widgets/button.hpp" // for button
#include <algorithm> // for max
#include <boost/scoped_ptr.hpp> // for scoped_ptr
#include <map> // for map, map<>::mapped_type
#include <ostream> // for operator<<, basic_ostream, etc
#include "sdl/alpha.hpp"
/**
* @namespace about
* Display credits %about all contributors.
*
* This module is used from the startup screen. \n
* When show_about() is called, a list of contributors
* to the game will be presented to the user.
*/
namespace about
{
static config about_list = config();
static std::map<std::string , std::string> images;
static std::string images_default;
/**
* Given a vector of strings, and a config representing an [about] section,
* add all the credits lines from the about section to the list of strings.
*/
static void add_lines(std::vector<std::string> &res, config const &c, bool split_multiline_headers) {
std::string title = c["title"];
if (!title.empty()) {
if(split_multiline_headers) {
// If the title is multi-line, we need to split it accordingly or we
// get slight scrolling glitches in the credits screen.
const std::vector<std::string>& lines = utils::split(c["title"], '\n');
bool first = true;
for(const std::string& line : lines) {
if(first) {
res.push_back("+" + line);
first = false;
} else {
// Don't convert other lines into headers or they get extra
// spacing on the credits screen.
res.push_back(line);
}
}
} else {
res.push_back("+" + title);
}
}
std::vector<std::string> lines = utils::split(c["text"], '\n');
for(std::string &line : lines)
{
if (line.size() > 1 && line[0] == '+')
line = "+ " + line.substr(1);
else
line = "- " + line;
if (!line.empty())
{
if (line[0] == '_')
line = translation::gettext(line.substr(1).c_str());
res.push_back(line);
}
}
for (const config &entry : c.child_range("entry")) {
res.push_back("- "+ entry["name"].str());
}
}
std::vector<std::string> get_text(const std::string &campaign, bool split_multiline_headers)
{
std::vector< std::string > res;
config::child_itors about_entries = about_list.child_range("about");
if (!campaign.empty()) {
for (const config &about : about_entries) {
// just finished a particular campaign
if (campaign == about["id"]) {
add_lines(res, about, split_multiline_headers);
}
}
}
for (const config &about : about_entries) {
add_lines(res, about, split_multiline_headers);
}
return res;
}
void set_about(const config &cfg)
{
about_list.clear();
images.clear();
images_default = "";
for (const config &about : cfg.child_range("about"))
{
about_list.add_child("about", about);
const std::string &im = about["images"];
if (!images.empty())
{
if (images_default.empty())
images_default = im;
else
images_default += ',' + im;
}
}
for (const config &campaign : cfg.child_range("campaign"))
{
config::const_child_itors abouts = campaign.child_range("about");
if (abouts.first == abouts.second) continue;
config temp;
std::ostringstream text;
const std::string &id = campaign["id"];
temp["title"] = campaign["name"];
temp["id"] = id;
std::string campaign_images;
for (const config &about : abouts)
{
const std::string &subtitle = about["title"];
if (!subtitle.empty())
{
text << '+';
if (subtitle[0] == '_')
text << translation::gettext(subtitle.substr(1, subtitle.size() - 1).c_str());
else
text << subtitle;
text << '\n';
}
for (const std::string &line : utils::split(about["text"], '\n'))
{
text << " " << line << '\n';
}
for (const config &entry : about.child_range("entry"))
{
text << " " << entry["name"] << '\n';
}
const std::string &im = about["images"];
if (!im.empty())
{
if (campaign_images.empty())
campaign_images = im;
else
campaign_images += ',' + im;
}
}
images[id] = campaign_images;
temp["text"] = text.str();
about_list.add_child("about",temp);
}
}
/**
* Show credits with list of contributors.
*
* Names of people are shown scrolling up like in movie-credits.\n
* Uses map from wesnoth or campaign as background.
*/
void show_about(CVideo &video, const std::string &campaign)
{
boost::scoped_ptr<cursor::setter> cur(new cursor::setter(cursor::WAIT));
surface& screen = video.getSurface();
if (screen == nullptr) return;
// If the title is multi-line, we need to split it accordingly or we
// get slight scrolling glitches in the credits screen.
std::vector<std::string> text = about::get_text(campaign, true);
SDL_Rect screen_rect = sdl::create_rect(0, 0, screen->w, screen->h);
const surface_restorer restorer(&video, screen_rect);
cur.reset();
std::vector<std::string> image_list;
if(campaign.size() && !images[campaign].empty()){
image_list = utils::parenthetical_split(images[campaign], ',');
} else{
image_list = utils::parenthetical_split(images_default, ',');
}
surface map_image, map_image_scaled;
if(!image_list.empty()) {
map_image = image::get_image(image_list[0]);
} else {
image_list.push_back("");
}
if(!map_image){
image_list[0]=game_config::images::game_title_background;
map_image=image::get_image(image_list[0]);
}
gui::button close(video,_("Close"));
close.set_location((screen->w/2)-(close.width()/2), screen->h - 30);
const int def_size = font::SIZE_XLARGE;
const SDL_Color def_color = font::NORMAL_COLOR;
//substitute in the correct control characters for '+' and '-'
std::string before_header(2, ' ');
before_header[0] = font::LARGE_TEXT;
for(unsigned i = 0; i < text.size(); ++i) {
std::string &s = text[i];
if (s.empty()) continue;
char &first = s[0];
if (first == '-')
first = font::SMALL_TEXT;
else if (first == '+') {
first = font::LARGE_TEXT;
text.insert(text.begin() + i, before_header);
++i;
}
}
text.insert(text.begin(), 10, before_header);
int startline = 0;
//TODO: use values proportional to screen ?
// distance from top of map image to top of scrolling text
const int top_margin = 60;
// distance from bottom of scrolling text to bottom of map image
const int bottom_margin = 40;
// distance from left of scrolling text to the frame border
const int text_left_padding = screen->w/32;
int offset = 0;
bool is_new_line = true;
int first_line_height = 0;
SDL_Rect frame_area;
// we use a dialog to contains the text. Strange idea but at least the style
// will be consistent with the titlescreen
gui::dialog_frame f(video, "", gui::dialog_frame::titlescreen_style, false);
// the text area's dimensions
SDL_Rect text_rect = { 0, 0, 0, 0 };
// we'll retain a copy to prevent SDL_blit to change its w and h
SDL_Rect text_rect_blit;
surface text_surf;
CKey key;
bool last_escape;
int image_count = 0;
int scroll_speed = 4; // scroll_speed*50 = speed of scroll in pixel per second
// Initially redraw all
bool redraw_mapimage = true;
bool update_dimensions = true;
int max_text_width = 0;
do {
last_escape = key[SDLK_ESCAPE] != 0;
// check to see if background image has changed
if(text.size() && (image_count <
((startline * static_cast<int>(image_list.size())) /
static_cast<int>(text.size())))){
image_count++;
surface temp=image::get_image(image_list[image_count]);
map_image=temp?temp:map_image;
redraw_mapimage = true;
}
if (update_dimensions) {
// rescale the background
map_image_scaled = scale_surface(map_image, screen->w, screen->h);
screen_rect = sdl::create_rect(0, 0, screen->w, screen->h);
redraw_mapimage = true;
// update the frame
frame_area = sdl::create_rect(
screen->w * 3 / 32
, top_margin
, screen->w * 13 / 16
, screen->h - top_margin - bottom_margin);
text_rect = f.layout(frame_area).interior;
// update the text area
text_rect.x += text_left_padding;
text_rect.w -= text_left_padding;
text_rect_blit = text_rect;
text_surf = create_compatible_surface(screen, text_rect.w, text_rect.h);
SDL_SetAlpha(text_surf, SDL_RLEACCEL, SDL_ALPHA_OPAQUE);
// relocate the close button
close.set_location((screen->w/2)-(close.width()/2), screen->h - 30);
update_dimensions = false;
}
if (redraw_mapimage) {
// draw map to screen, thus erasing all text
sdl_blit(map_image_scaled, nullptr, screen, nullptr);
update_rect(screen_rect);
// redraw the dialog
f.draw_background();
f.draw_border();
// cache the dialog background (alpha blending + blurred map)
sdl_blit(screen, &text_rect, text_surf, nullptr);
redraw_mapimage = false;
} else {
// redraw the saved part of the dialog where text scrolled
// thus erasing all text
SDL_Rect modified = sdl::create_rect(0, 0, max_text_width, text_rect.h);
sdl_blit(text_surf, &modified, screen, &text_rect_blit);
update_rect(text_rect);
}
int y = text_rect.y - offset;
int line = startline;
max_text_width = 0;
{
// clip to keep text into the frame (thus the new code block)
clip_rect_setter set_clip_rect(screen, &text_rect);
const int line_spacing = 5;
do {
// draw the text (with ellipsis if needed)
// update the max_text_width for future cleaning
int w = font::draw_text(&video, text_rect, def_size, def_color,
text[line], text_rect.x, y).w;
max_text_width = std::max<int>(max_text_width, w);
// since the real drawing on screen is clipped,
// we do a dummy one to get the height of the not clipped line.
// (each time because special format characters may change it)
const int line_height = font::draw_text(nullptr, text_rect, def_size, def_color,
text[line], 0,0).h;
if(is_new_line) {
is_new_line = false;
first_line_height = line_height + line_spacing;
}
line++;
if(size_t(line) > text.size()-1)
line = 0;
y += line_height + line_spacing;
} while(y < text_rect.y + text_rect.h);
}
// performs the actual scrolling
offset += scroll_speed;
if (offset>=first_line_height) {
offset -= first_line_height;
is_new_line = true;
startline++;
if(size_t(startline) == text.size()){
startline = 0;
image_count = -1;
}
}
// handle events
if (key[SDLK_UP] && scroll_speed < 20) {
++scroll_speed;
}
if (key[SDLK_DOWN] && scroll_speed > 0) {
--scroll_speed;
}
if (screen->w != screen_rect.w || screen->h != screen_rect.h) {
update_dimensions = true;
}
events::pump();
events::raise_process_event();
events::raise_draw_event();
// flip screen and wait, so the text does not scroll too fast
video.flip();
CVideo::delay(20);
} while(!close.pressed() && (last_escape || !key[SDLK_ESCAPE]));
}
} // end namespace about