The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
title_screen.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2008 - 2025
3 by Mark de Wever <koraq@xs4all.nl>
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program 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 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16#define GETTEXT_DOMAIN "wesnoth-lib"
17
19
20#include "addon/manager_ui.hpp"
21#include "filesystem.hpp"
22#include "font/font_config.hpp"
24#include "game_config.hpp"
26#include "game_launcher.hpp"
42#include "language.hpp"
43#include "log.hpp"
45//#define DEBUG_TOOLTIP
46#ifdef DEBUG_TOOLTIP
48#endif
50#include "gui/widgets/image.hpp"
51#include "gui/widgets/label.hpp"
55#include "help/help.hpp"
56#include "quit_confirmation.hpp"
57#include "sdl/surface.hpp"
59#include "video.hpp"
60#include "wml_exception.hpp"
61
62#include <algorithm>
63#include <functional>
64
65#include <boost/algorithm/string/erase.hpp>
66
67static lg::log_domain log_config("config");
68#define ERR_CF LOG_STREAM(err, log_config)
69#define WRN_CF LOG_STREAM(warn, log_config)
70
71namespace gui2::dialogs
72{
73
74REGISTER_DIALOG(title_screen)
75
77
79 : modal_dialog(window_id())
80 , debug_clock_()
81 , game_(game)
82{
83 set_allow_plugin_skip(false);
84 init_callbacks();
85}
86
90
92 const std::string& id,
94 const std::function<void()>& callback)
95{
96 register_button(id, hk, callback, callback);
97}
98
100 const std::string& id,
102 const std::function<void()>& callback_btn,
103 const std::function<void()>& callback_hotkey)
104{
105 if(hk != hotkey::HOTKEY_NULL) {
106 register_hotkey(hk, [callback_hotkey](auto&&...) { callback_hotkey(); return true; });
107 }
108
109 try {
110 button& btn = find_widget<button>(id);
111 connect_signal_mouse_left_click(btn, [callback_btn](auto&&...) {
112 std::invoke(callback_btn);
113 });
114 } catch(const wml_exception& e) {
115 ERR_GUI_P << e.user_message;
116 prefs::get().set_gui2_theme("default");
118 }
119}
120
121namespace
122{
123void show_lua_console()
124{
126}
127
128void make_screenshot()
129{
130 surface screenshot = video::read_pixels();
131 if(screenshot) {
132 std::string filename = filesystem::get_screenshot_dir() + "/" + _("Screenshot") + "_";
134 gui2::dialogs::screenshot_notification::display(filename, screenshot);
135 }
136}
137
138} // anon namespace
139
140#ifdef DEBUG_TOOLTIP
141/*
142 * This function is used to test the tooltip placement algorithms as
143 * described in the »Tooltip placement« section in the GUI2 design
144 * document.
145 *
146 * Use a 1024 x 768 screen size, set the maximum loop iteration to:
147 * - 0 to test with a normal tooltip placement.
148 * - 30 to test with a larger normal tooltip placement.
149 * - 60 to test with a huge tooltip placement.
150 * - 150 to test with a borderline to insanely huge tooltip placement.
151 * - 180 to test with an insanely huge tooltip placement.
152 */
153static void debug_tooltip(window& /*window*/, bool& handled, const point& coordinate)
154{
155 std::string message = "Hello world.";
156
157 for(int i = 0; i < 0; ++i) {
158 message += " More greetings.";
159 }
160
161 gui2::tip::remove();
162 gui2::tip::show("tooltip", message, coordinate);
163
164 handled = true;
165}
166#endif
167
169{
170 set_click_dismiss(false);
171 set_enter_disabled(true);
173
174#ifdef DEBUG_TOOLTIP
175 connect_signal<event::SDL_MOUSE_MOTION>(
176 std::bind(debug_tooltip, std::ref(*this), std::placeholders::_3, std::placeholders::_5),
178#endif
179
180 connect_signal<event::SDL_VIDEO_RESIZE>(std::bind(&title_screen::on_resize, this));
181
182 //
183 // General hotkeys
184 //
186 [this](auto&&...) { set_retval(RELOAD_GAME_DATA); return true; });
187
189 [this](auto&&...) { hotkey_callback_select_tests(); return true; });
190
192 [this](auto&&...) { button_callback_cores(); return true; });
193
195 [](auto&&...) { dialogs::end_credits::display(); return true; });
196
198 [](auto&&...) { show_lua_console(); return true; });
199
200 /** @todo: should eventually become part of global hotkey handling. */
202 [](auto&&...) { make_screenshot(); return true; });
203
204 //
205 // Background and logo images
206 //
208 // game works just fine if just one of the background images are missing
209 ERR_CF << "No titlescreen background defined in game config";
210 }
211
214
215 find_widget<image>("logo-bg").set_image(game_config::images::game_logo_background);
216 find_widget<image>("logo").set_image(game_config::images::game_logo);
217
218 //
219 // Tip-of-the-day browser
220 //
221 if(auto tip_pages = find_widget<multi_page>("tips", false, false)) {
223 tip_pages->add_page({
224 { "tip", {
225 { "use_markup", "true" },
226 { "label", tip.text }
227 }},
228 { "source", {
229 { "use_markup", "true" },
230 { "label", tip.source }
231 }}
232 });
233 }
234
235 update_tip(true);
236 }
237
239 std::bind(&title_screen::update_tip, this, true));
240
242 std::bind(&title_screen::update_tip, this, false));
243
244 // Tip panel visiblity and close button
245 panel& tip_panel = find_widget<panel>("tip_panel");
246
247 tip_panel.set_visible(prefs::get().show_tips()
250
251 if(auto toggle_tips = find_widget<button>("toggle_tip_panel", false, false)) {
252 connect_signal_mouse_left_click(*toggle_tips, [&tip_panel](auto&&...) {
253 const bool currently_hidden = tip_panel.get_visible() == widget::visibility::hidden;
254
255 tip_panel.set_visible(currently_hidden
258
259 // If previously hidden, will now be visible, so we can reuse the same value
260 prefs::get().set_show_tips(currently_hidden);
261 });
262 }
263
264 //
265 // Help
266 //
268
269 //
270 // About
271 //
272 register_button("about", hotkey::HOTKEY_NULL, [] { game_version::display(); });
273
274 //
275 // Campaign
276 //
277 register_button("campaign", hotkey::TITLE_SCREEN__CAMPAIGN, [this]() {
278 try{
279 if(game_.new_campaign()) {
280 // Suspend drawing of the title screen,
281 // so it doesn't flicker in between loading screens.
282 hide();
283 set_retval(LAUNCH_GAME);
284 }
285 } catch (const config::error& e) {
287 }
288 });
289
290 //
291 // Multiplayer
292 //
295
296 //
297 // Load game
298 //
299 register_button("load", hotkey::HOTKEY_LOAD_GAME, [this]() {
300 if(game_.load_game_prompt()) {
301 // Suspend drawing of the title screen,
302 // so it doesn't flicker in between loading screens.
303 hide();
304 set_retval(LOAD_GAME);
305 }
306 });
307
308 //
309 // Addons
310 //
312 if(manage_addons()) {
314 }
315 });
316
317 //
318 // Editor
319 //
321
322 //
323 // Language
324 //
325 register_button("language", hotkey::HOTKEY_LANGUAGE, [this]() {
326 try {
327 if(game_.change_language()) {
328 on_resize();
329 update_static_labels();
330 }
331 } catch(const std::runtime_error& e) {
333 }
334 });
335
336 //
337 // Preferences
338 //
340 std::bind(&title_screen::show_preferences, this));
341
342 //
343 // Achievements
344 //
346 [] { dialogs::achievements_dialog::display(); });
347
348 //
349 // Community
350 //
352 [] { dialogs::game_version::display(4); }); // shows the 5th tab, community
353
354 //
355 // Quit
356 //
357#ifdef __ANDROID__
359 [this]() { set_retval(QUIT_GAME); },
360 [this]() { quit_confirmation().quit_to_desktop(); });
361#else
363#endif
364 // A sanity check, exit immediately if the .cfg file didn't have a "quit" button.
365 find_widget<button>("quit", false, true);
366
367 //
368 // Debug clock
369 //
371 std::bind(&title_screen::show_debug_clock_window, this));
372
373 auto clock = find_widget<button>("clock", false, false);
374 if(clock) {
375 clock->set_visible(show_debug_clock_button);
376 }
377
378 //
379 // GUI Test and Debug Window
380 //
382 [] { dialogs::gui_test_dialog::display(); });
383
384 auto test_dialog = find_widget<button>("test_dialog", false, false);
385 if(test_dialog) {
386 test_dialog->set_visible(show_debug_clock_button);
387 }
388
389 //
390 // Static labels (version and language)
391 //
393}
394
396{
397 //
398 // Version menu label
399 //
400 const std::string& version_string = VGETTEXT("Version $version", {{ "version", game_config::revision }});
401
402 if(label* version_label = find_widget<label>("revision_number", false, false)) {
403 version_label->set_label(version_string);
404 }
405
406 get_canvas(1).set_variable("revision_number", wfl::variant(version_string));
407
408 //
409 // Language menu label
410 //
411 if(auto* lang_button = find_widget<button>("language", false, false); lang_button) {
412 const auto& locale = translation::get_effective_locale_info();
413 // Just assume everything is UTF-8 (it should be as long as we're called Wesnoth)
414 // and strip the charset from the Boost locale identifier.
415 const auto& locale_id = boost::algorithm::erase_first_copy(locale.name(), ".UTF-8");
416
417 if(locale_id == "c" || locale_id == "C") {
418 // If you fork Wesnoth and change the neutral language to something other than US English,
419 // you want to change this too.
420 lang_button->set_label("English (US)");
421 } else {
422 std::string lname = get_translation_name(locale_id);
423 if(!lname.empty()) {
424 lang_button->set_label(lname);
425 } else {
426 // If somehow the locale doesn't match a known locale,
427 // use the locale identifier as a last resort
428 lang_button->set_label(locale_id);
429 }
430 }
431 }
432}
433
438
439void title_screen::update_tip(const bool previous)
440{
441 multi_page* tip_pages = find_widget<multi_page>("tips", false, false);
442 if(tip_pages == nullptr) {
443 return;
444 }
445 if(tip_pages->get_page_count() == 0) {
446 return;
447 }
448
449 int page = tip_pages->get_selected_page();
450 if(previous) {
451 if(page <= 0) {
452 page = tip_pages->get_page_count();
453 }
454 --page;
455 } else {
456 ++page;
457 if(static_cast<unsigned>(page) >= tip_pages->get_page_count()) {
458 page = 0;
459 }
460 }
461
462 tip_pages->select_page(page);
463}
464
466{
468
469 if(debug_clock_) {
470 debug_clock_.reset(nullptr);
471 } else {
472 debug_clock_.reset(new debug_clock());
473 debug_clock_->show(true);
474 }
475}
476
478{
480
481 std::vector<std::string> options;
482 for(const config &sc : game_config_manager::get()->game_config().child_range("test")) {
483 if(!sc["is_unit_test"].to_bool(false)) {
484 options.emplace_back(sc["id"]);
485 }
486 }
487
488 std::sort(options.begin(), options.end());
489
490 gui2::dialogs::simple_item_selector dlg(_("Choose Test Scenario"), "", options);
491 dlg.show();
492
493 int choice = dlg.selected_index();
494 if(choice >= 0) {
495 game_.set_test(options[choice]);
497 }
498}
499
501{
503 pref_dlg.show();
504 if (pref_dlg.get_retval() == RELOAD_UI) {
506 }
507
508 // Currently blurred windows don't capture well if there is something
509 // on top of them at the time of blur. Resizing the game window in
510 // preferences will cause the title screen tip and menu panels to
511 // capture the prefs dialog in their blur. This workaround simply
512 // forces them to re-capture the blur after the dialog closes.
513 panel* tip_panel = find_widget<panel>("tip_panel", false, false);
514 if(tip_panel != nullptr) {
515 tip_panel->get_canvas(tip_panel->get_state()).queue_reblur();
516 tip_panel->queue_redraw();
517 }
518 panel* menu_panel = find_widget<panel>("menu_panel", false, false);
519 if(menu_panel != nullptr) {
520 menu_panel->get_canvas(menu_panel->get_state()).queue_reblur();
521 menu_panel->queue_redraw();
522 }
523}
524
526{
527 while(true) {
529 dlg.show();
530
531 if(dlg.get_retval() != gui2::retval::OK) {
532 return;
533 }
534
535 const auto res = dlg.get_choice();
536
537 if(res == decltype(dlg)::choice::HOST && !gui2::dialogs::mp_host_game_prompt::get_do_not_show_again()) {
538 if(!gui2::dialogs::mp_host_game_prompt::execute()) {
539 continue;
540 }
541 }
542
543 switch(res) {
544 case decltype(dlg)::choice::JOIN:
545 game_.select_mp_server(prefs::get().builtin_servers_list().front().address);
547 break;
548 case decltype(dlg)::choice::CONNECT:
551 break;
552 case decltype(dlg)::choice::HOST:
553 game_.select_mp_server("localhost");
555 break;
556 case decltype(dlg)::choice::LOCAL:
558 break;
559 }
560
561 return;
562 }
563}
564
566{
567 int current = 0;
568
569 std::vector<config> cores;
570 for(const config& core : game_config_manager::get()->game_config().child_range("core")) {
571 cores.push_back(core);
572
573 if(core["id"] == prefs::get().core()) {
574 current = cores.size() - 1;
575 }
576 }
577
578 gui2::dialogs::core_selection core_dlg(cores, current);
579 if(core_dlg.show()) {
580 const std::string& core_id = cores[core_dlg.get_choice()]["id"];
581
582 prefs::get().set_core(core_id);
584 }
585}
586
587} // namespace dialogs
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
static game_config_manager * get()
void load_game_config_for_create(bool is_mp, bool is_test=false)
const game_config_view & game_config() const
config_array_view child_range(std::string_view key) const
void select_mp_server(const std::string &server)
void set_test(const std::string &id)
Simple push button.
Definition button.hpp:36
void set_variable(const std::string &key, wfl::variant &&value)
Definition canvas.hpp:165
void queue_reblur()
Clear the cached blur texture, forcing it to regenerate.
Definition canvas.cpp:794
static void display(lua_kernel_base *lk)
Display a new console, using given video and lua kernel.
Abstract base class for all modal dialogs.
bool show(const unsigned auto_close_time=0)
Shows the window.
static bool get_do_not_show_again()
The execute function.
int selected_index() const
Returns the selected item index after displaying.
This class implements the title screen.
std::unique_ptr< modeless_dialog > debug_clock_
Holds the debug clock dialog.
void update_tip(const bool previous)
Updates the tip of day widget.
void update_static_labels()
Updates UI labels that are not t_string after a language change.
void show_debug_clock_window()
Shows the debug clock.
void show_preferences()
Shows the preferences dialog.
void register_button(const std::string &id, hotkey::HOTKEY_COMMAND hk, const std::function< void()> &callback_btn, const std::function< void()> &callback_hotkey)
void register_hotkey(const hotkey::HOTKEY_COMMAND id, Func &&function)
Registers a hotkey.
unsigned get_page_count() const
Returns the number of pages.
int get_selected_page() const
Returns the selected page.
void select_page(const unsigned page, const bool select=true)
Selects a page.
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition panel.cpp:61
canvas & get_canvas(const unsigned index)
void set_visible(const visibility visible)
Definition widget.cpp:479
void queue_redraw()
Indicates that this widget should be redrawn.
Definition widget.cpp:464
visibility get_visible() const
Definition widget.cpp:506
@ visible
The user sets the widget visible, that means:
@ hidden
The user sets the widget hidden, that means:
void set_enter_disabled(const bool enter_disabled)
Disable the enter key.
Definition window.hpp:313
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition window.hpp:387
void set_escape_disabled(const bool escape_disabled)
Disable the escape key.
Definition window.hpp:326
void set_click_dismiss(const bool click_dismiss)
Definition window.hpp:399
int get_retval()
Definition window.hpp:394
static prefs & get()
Implements a quit confirmation dialog.
static void quit_to_desktop()
#define ERR_CF
Definition config.cpp:37
Declarations for File-IO.
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
static std::string _(const char *str)
Definition gettext.hpp:100
#define ERR_GUI_P
Definition log.hpp:69
This file contains the window object, this object is a top level container which has the event manage...
std::string get_translation_name(const std::string &locale_id)
Definition language.cpp:178
Standard logging facilities (interface).
bool manage_addons(const utils::optional< ADDON_TYPE > initial_type_filter)
Shows the add-ons server connection dialog, for access to the various management front-ends.
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
std::string get_screenshot_dir()
std::string get_next_filename(const std::string &name, const std::string &extension)
Get the next free filename using "name + number (3 digits) + extension" maximum 1000 files then start...
std::string game_title_background
std::string game_logo_background
const std::string revision
bool show_debug_clock_button
Do we wish to show the button for the debug clock.
std::vector< game_tip > tips
Definition settings.cpp:57
std::vector< game_tip > shuffle(const std::vector< game_tip > &tips)
Shuffles the tips.
Definition tips.cpp:43
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition message.cpp:201
@ OK
Dialog was closed with the OK button.
Definition retval.hpp:35
void show_help(const std::string &show_topic)
Open the help browser.
Definition help.cpp:84
@ HOTKEY_ACHIEVEMENTS
@ TITLE_SCREEN__MULTIPLAYER
@ TITLE_SCREEN__CREDITS
@ HOTKEY_QUIT_TO_DESKTOP
@ TITLE_SCREEN__PREVIOUS_TIP
@ TITLE_SCREEN__CAMPAIGN
@ TITLE_SCREEN__RELOAD_WML
@ TITLE_SCREEN__NEXT_TIP
const boost::locale::info & get_effective_locale_info()
A facet that holds general information about the effective locale.
Definition gettext.cpp:572
surface read_pixels(rect *r)
Copy back a portion of the render target that is already drawn.
Definition video.cpp:625
This file contains the settings handling of the widget library.
std::string filename
Filename.
Holds a 2D point.
Definition point.hpp:25
Helper class, don't construct this directly.
static lg::log_domain log_config("config")
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define i
#define e