-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
title_screen.cpp
562 lines (474 loc) · 14.5 KB
/
title_screen.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project https://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.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/dialogs/title_screen.hpp"
#include "addon/manager_ui.hpp"
#include "filesystem.hpp"
#include "formula/string_utils.hpp"
#include "game_config.hpp"
#include "game_config_manager.hpp"
#include "game_launcher.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/auxiliary/tips.hpp"
#include "gui/core/timer.hpp"
#include "gui/dialogs/core_selection.hpp"
#include "gui/dialogs/debug_clock.hpp"
#include "gui/dialogs/game_version_dialog.hpp"
#include "gui/dialogs/help_browser.hpp"
#include "gui/dialogs/language_selection.hpp"
#include "gui/dialogs/lua_interpreter.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/dialogs/multiplayer/mp_host_game_prompt.hpp"
#include "gui/dialogs/multiplayer/mp_method_selection.hpp"
#include "gui/dialogs/screenshot_notification.hpp"
#include "gui/dialogs/simple_item_selector.hpp"
#include "log.hpp"
#include "preferences/game.hpp"
//#define DEBUG_TOOLTIP
#ifdef DEBUG_TOOLTIP
#include "gui/dialogs/tooltip.hpp"
#endif
#include "gui/widgets/button.hpp"
#include "gui/widgets/image.hpp"
#include "gui/widgets/label.hpp"
#include "gui/widgets/multi_page.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "help/help.hpp"
#include "hotkey/hotkey_command.hpp"
#include "sdl/surface.hpp"
#include "sdl/utils.hpp"
#include "utils/functional.hpp"
#include "video.hpp"
#include <algorithm>
static lg::log_domain log_config("config");
#define ERR_CF LOG_STREAM(err, log_config)
#define WRN_CF LOG_STREAM(warn, log_config)
namespace gui2
{
namespace dialogs
{
/*WIKI
* @page = GUIWindowDefinitionWML
* @order = 2_title_screen
*
* == Title screen ==
*
* This shows the title screen.
*
* @begin{table}{dialog_widgets}
* tutorial & & button & o &
* The button to start the tutorial. $
*
* campaign & & button & o &
* The button to start a campaign. $
*
* multiplayer & & button & o &
* The button to start multiplayer mode. $
*
* load & & button & o &
* The button to load a saved game. $
*
* editor & & button & o &
* The button to start the editor. $
*
* addons & & button & o &
* The button to start managing the addons. $
*
* language & & button & o &
* The button to select the game language. $
*
* credits & & button & o &
* The button to show Wesnoth's contributors. $
*
* quit & & button & m &
* The button to quit Wesnoth. $
*
* tips & & multi_page & o &
* A multi_page to hold all tips, when this widget is used the area of
* the tips doesn't need to be resized when the next or previous button
* is pressed. $
*
* -tip & & label & o &
* Shows the text of the current tip. $
*
* -source & & label & o &
* The source (the one who's quoted or the book referenced) of the
* current tip. $
*
* next_tip & & button & o &
* The button show the next tip of the day. $
*
* previous_tip & & button & o &
* The button show the previous tip of the day. $
*
* logo & & image & o &
* The Wesnoth logo. $
*
* revision_number & & styled_widget & o &
* A widget to show the version number when the version number is
* known. $
*
* @end{table}
*/
REGISTER_DIALOG(title_screen)
bool show_debug_clock_button = false;
title_screen::title_screen(game_launcher& game)
: debug_clock_()
, game_(game)
{
set_restore(false);
// Need to set this in the constructor, pre_show() / post_build() is too late
set_allow_plugin_skip(false);
}
title_screen::~title_screen()
{
}
using btn_callback = std::function<void()>;
static void register_button(window& win, const std::string& id, hotkey::HOTKEY_COMMAND hk, btn_callback callback)
{
if(hk != hotkey::HOTKEY_NULL) {
win.register_hotkey(hk, std::bind(callback));
}
auto b = find_widget<button>(&win, id, false, false);
if(b != nullptr)
{
connect_signal_mouse_left_click(*b, std::bind(callback));
}
}
static void launch_lua_console()
{
gui2::dialogs::lua_interpreter::display(gui2::dialogs::lua_interpreter::APP);
}
static void make_screenshot(window& win)
{
surface screenshot = win.video().getSurface().clone();
if(screenshot) {
std::string filename = filesystem::get_screenshot_dir() + "/" + _("Screenshot") + "_";
filename = filesystem::get_next_filename(filename, ".png");
gui2::dialogs::screenshot_notification::display(filename, screenshot);
}
}
#ifdef DEBUG_TOOLTIP
/*
* This function is used to test the tooltip placement algorithms as
* described in the »Tooltip placement« section in the GUI2 design
* document.
*
* Use a 1024 x 768 screen size, set the maximum loop iteration to:
* - 0 to test with a normal tooltip placement.
* - 30 to test with a larger normal tooltip placement.
* - 60 to test with a huge tooltip placement.
* - 150 to test with a borderline to insanely huge tooltip placement.
* - 180 to test with an insanely huge tooltip placement.
*/
static void debug_tooltip(window& /*window*/, bool& handled, const point& coordinate)
{
std::string message = "Hello world.";
for(int i = 0; i < 0; ++i) {
message += " More greetings.";
}
gui2::tip::remove();
gui2::tip::show("tooltip", message, coordinate);
handled = true;
}
#endif
void title_screen::pre_show(window& win)
{
win.set_click_dismiss(false);
win.set_enter_disabled(true);
win.set_escape_disabled(true);
#ifdef DEBUG_TOOLTIP
win.connect_signal<event::SDL_MOUSE_MOTION>(
std::bind(debug_tooltip, std::ref(win), _3, _5),
event::dispatcher::front_child);
#endif
win.connect_signal<event::SDL_VIDEO_RESIZE>(std::bind(&title_screen::on_resize, this, std::ref(win)));
//
// General hotkeys
//
win.register_hotkey(hotkey::TITLE_SCREEN__RELOAD_WML,
std::bind(&gui2::window::set_retval, std::ref(win), RELOAD_GAME_DATA, true));
win.register_hotkey(hotkey::TITLE_SCREEN__TEST,
std::bind(&title_screen::hotkey_callback_select_tests, this, std::ref(win)));
// A wrapper is needed here since the relevant display function is overloaded, and
// since the wrapper's signature doesn't exactly match what register_hotkey expects.
win.register_hotkey(hotkey::LUA_CONSOLE, std::bind(&launch_lua_console));
win.register_hotkey(hotkey::HOTKEY_SCREENSHOT, std::bind(&make_screenshot, std::ref(win)));
//
// Background and logo images
//
if(game_config::images::game_title.empty()) {
ERR_CF << "No title image defined" << std::endl;
}
win.get_canvas(0).set_variable("title_image", wfl::variant(game_config::images::game_title));
if(game_config::images::game_title_background.empty()) {
ERR_CF << "No title background image defined" << std::endl;
}
win.get_canvas(0).set_variable("background_image", wfl::variant(game_config::images::game_title_background));
find_widget<image>(&win, "logo-bg", false).set_image(game_config::images::game_logo_background);
find_widget<image>(&win, "logo", false).set_image(game_config::images::game_logo);
//
// Version string
//
const std::string& version_string = VGETTEXT("Version $version", {{ "version", game_config::revision }});
if(label* version_label = find_widget<label>(&win, "revision_number", false, false)) {
version_label->set_label(version_string);
}
win.get_canvas(0).set_variable("revision_number", wfl::variant(version_string));
//
// Tip-of-the-day browser
//
multi_page* tip_pages = find_widget<multi_page>(&win, "tips", false, false);
if(tip_pages != nullptr) {
std::vector<game_tip> tips = tip_of_the_day::shuffle(settings::tips);
if(tips.empty()) {
WRN_CF << "There are no tips of day available." << std::endl;
}
for(const auto& tip : tips) {
string_map widget;
std::map<std::string, string_map> page;
widget["use_markup"] = "true";
widget["label"] = tip.text();
page.emplace("tip", widget);
widget["label"] = tip.source();
page.emplace("source", widget);
tip_pages->add_page(page);
}
update_tip(win, true);
}
register_button(win, "next_tip", hotkey::TITLE_SCREEN__NEXT_TIP,
std::bind(&title_screen::update_tip, this, std::ref(win), true));
register_button(win, "previous_tip", hotkey::TITLE_SCREEN__PREVIOUS_TIP,
std::bind(&title_screen::update_tip, this, std::ref(win), false));
//
// Help
//
register_button(win, "help", hotkey::HOTKEY_HELP, []() {
if(gui2::new_widgets) {
gui2::dialogs::help_browser::display();
}
help::help_manager help_manager(&game_config_manager::get()->game_config());
help::show_help();
});
//
// About
//
register_button(win, "about", hotkey::HOTKEY_NULL, std::bind(&game_version::display<>));
//
// Tutorial
//
register_button(win, "tutorial", hotkey::TITLE_SCREEN__TUTORIAL, [this, &win]() {
game_.set_tutorial();
win.set_retval(LAUNCH_GAME);
});
//
// Campaign
//
register_button(win, "campaign", hotkey::TITLE_SCREEN__CAMPAIGN, [this, &win]() {
try{
if(game_.new_campaign()) {
win.set_retval(LAUNCH_GAME);
}
} catch (const config::error& e) {
gui2::show_error_message(e.what());
}
});
//
// Multiplayer
//
register_button(win, "multiplayer", hotkey::TITLE_SCREEN__MULTIPLAYER,
std::bind(&title_screen::button_callback_multiplayer, this, std::ref(win)));
//
// Load game
//
register_button(win, "load", hotkey::HOTKEY_LOAD_GAME, [this, &win]() {
if(game_.load_game()) {
win.set_retval(LAUNCH_GAME);
} else {
game_.clear_loaded_game();
}
});
//
// Addons
//
register_button(win, "addons", hotkey::TITLE_SCREEN__ADDONS, []() {
// NOTE: we need the help_manager to get access to the Add-ons section in the game help!
help::help_manager help_manager(&game_config_manager::get()->game_config());
if(manage_addons()) {
game_config_manager::get()->reload_changed_game_config();
}
});
//
// Editor
//
register_button(win, "editor", hotkey::TITLE_SCREEN__EDITOR, [&win]() { win.set_retval(MAP_EDITOR); });
//
// Cores
//
win.register_hotkey(hotkey::TITLE_SCREEN__CORES,
std::bind(&title_screen::button_callback_cores, this));
//
// Language
//
register_button(win, "language", hotkey::HOTKEY_LANGUAGE, [this, &win]() {
try {
if(game_.change_language()) {
t_string::reset_translations();
::image::flush_cache();
sound::flush_cache();
on_resize(win);
}
} catch(const std::runtime_error& e) {
gui2::show_error_message(e.what());
}
});
//
// Preferences
//
register_button(win, "preferences", hotkey::HOTKEY_PREFERENCES, [this]() { game_.show_preferences(); });
//
// Credits
//
register_button(win, "credits", hotkey::TITLE_SCREEN__CREDITS, [&win]() { win.set_retval(SHOW_ABOUT); });
//
// Quit
//
register_button(win, "quit", hotkey::HOTKEY_QUIT_TO_DESKTOP, [&win]() { win.set_retval(QUIT_GAME); });
// A sanity check, exit immediately if the .cfg file didn't have a "quit" button.
find_widget<button>(&win, "quit", false, true);
//
// Debug clock
//
register_button(win, "clock", hotkey::HOTKEY_NULL,
std::bind(&title_screen::show_debug_clock_window, this));
auto clock = find_widget<button>(&win, "clock", false, false);
if(clock) {
clock->set_visible(show_debug_clock_button ? widget::visibility::visible : widget::visibility::invisible);
}
}
void title_screen::on_resize(window& win)
{
win.set_retval(REDRAW_BACKGROUND);
}
void title_screen::update_tip(window& win, const bool previous)
{
multi_page* tip_pages = find_widget<multi_page>(&win, "tips", false, false);
if(tip_pages == nullptr) {
return;
}
if(tip_pages->get_page_count() == 0) {
return;
}
int page = tip_pages->get_selected_page();
if(previous) {
if(page <= 0) {
page = tip_pages->get_page_count();
}
--page;
} else {
++page;
if(static_cast<unsigned>(page) >= tip_pages->get_page_count()) {
page = 0;
}
}
tip_pages->select_page(page);
/**
* @todo Look for a proper fix.
*
* This dirtying is required to avoid the blurring to be rendered wrong.
* Not entirely sure why, but since we plan to move to SDL2 that change
* will probably fix this issue automatically.
*/
win.set_is_dirty(true);
}
void title_screen::show_debug_clock_window()
{
assert(show_debug_clock_button);
if(debug_clock_) {
debug_clock_.reset(nullptr);
} else {
debug_clock_.reset(new debug_clock());
debug_clock_->show(true);
}
}
void title_screen::hotkey_callback_select_tests(window& window)
{
game_config_manager::get()->load_game_config_for_create(false, true);
std::vector<std::string> options;
for(const config &sc : game_config_manager::get()->game_config().child_range("test")) {
if(!sc["is_unit_test"].to_bool(false)) {
options.emplace_back(sc["id"]);
}
}
std::sort(options.begin(), options.end());
gui2::dialogs::simple_item_selector dlg(_("Choose Test"), "", options);
dlg.show();
int choice = dlg.selected_index();
if(choice >= 0) {
game_.set_test(options[choice]);
window.set_retval(LAUNCH_GAME);
}
}
void title_screen::button_callback_multiplayer(window& window)
{
while(true) {
gui2::dialogs::mp_method_selection dlg;
dlg.show();
if(dlg.get_retval() != gui2::retval::OK) {
return;
}
const int res = dlg.get_choice();
if(res == 2 && preferences::mp_server_warning_disabled() < 2) {
if(!gui2::dialogs::mp_host_game_prompt::execute()) {
continue;
}
}
switch(res) {
case 0:
game_.select_mp_server(preferences::server_list().front().address);
window.set_retval(MP_CONNECT);
break;
case 1:
game_.select_mp_server("");
window.set_retval(MP_CONNECT);
break;
case 2:
game_.select_mp_server("localhost");
window.set_retval(MP_HOST);
break;
case 3:
window.set_retval(MP_LOCAL);
break;
}
return;
}
}
void title_screen::button_callback_cores()
{
int current = 0;
std::vector<config> cores;
for(const config& core : game_config_manager::get()->game_config().child_range("core")) {
cores.push_back(core);
if(core["id"] == preferences::core_id()) {
current = cores.size() - 1;
}
}
gui2::dialogs::core_selection core_dlg(cores, current);
if(core_dlg.show()) {
const std::string& core_id = cores[core_dlg.get_choice()]["id"];
preferences::set_core_id(core_id);
game_config_manager::get()->reload_changed_game_config();
}
}
} // namespace dialogs
} // namespace gui2