The Battle for Wesnoth  1.19.8+dev
title_screen.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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"
23 #include "formula/string_utils.hpp"
24 #include "game_config.hpp"
25 #include "game_config_manager.hpp"
26 #include "game_launcher.hpp"
27 #include "gui/auxiliary/tips.hpp"
33 #include "gui/dialogs/message.hpp"
41 #include "language.hpp"
42 #include "log.hpp"
44 //#define DEBUG_TOOLTIP
45 #ifdef DEBUG_TOOLTIP
46 #include "gui/dialogs/tooltip.hpp"
47 #endif
48 #include "gui/widgets/button.hpp"
49 #include "gui/widgets/image.hpp"
50 #include "gui/widgets/label.hpp"
52 #include "gui/widgets/settings.hpp"
53 #include "gui/widgets/window.hpp"
54 #include "help/help.hpp"
55 #include "sdl/surface.hpp"
57 #include "video.hpp"
58 #include "wml_exception.hpp"
59 
60 #include <algorithm>
61 #include <functional>
62 
63 #include <boost/algorithm/string/erase.hpp>
64 
65 static lg::log_domain log_config("config");
66 #define ERR_CF LOG_STREAM(err, log_config)
67 #define WRN_CF LOG_STREAM(warn, log_config)
68 
69 namespace gui2::dialogs
70 {
71 
72 REGISTER_DIALOG(title_screen)
73 
75 
77  : modal_dialog(window_id())
78  , debug_clock_()
79  , game_(game)
80 {
81  set_allow_plugin_skip(false);
82  init_callbacks();
83 }
84 
86 {
87 }
88 
89 void title_screen::register_button(const std::string& id, hotkey::HOTKEY_COMMAND hk, const std::function<void()>& callback)
90 {
91  if(hk != hotkey::HOTKEY_NULL) {
92  register_hotkey(hk, std::bind(callback));
93  }
94 
95  try {
96  button& btn = find_widget<button>(id);
97  connect_signal_mouse_left_click(btn, std::bind(callback));
98  } catch(const wml_exception& e) {
99  ERR_GUI_P << e.user_message;
100  prefs::get().set_gui2_theme("default");
102  }
103 }
104 
105 static void launch_lua_console()
106 {
108 }
109 
110 static void make_screenshot()
111 {
112  surface screenshot = video::read_pixels();
113  if(screenshot) {
114  std::string filename = filesystem::get_screenshot_dir() + "/" + _("Screenshot") + "_";
116  gui2::dialogs::screenshot_notification::display(filename, screenshot);
117  }
118 }
119 
120 #ifdef DEBUG_TOOLTIP
121 /*
122  * This function is used to test the tooltip placement algorithms as
123  * described in the »Tooltip placement« section in the GUI2 design
124  * document.
125  *
126  * Use a 1024 x 768 screen size, set the maximum loop iteration to:
127  * - 0 to test with a normal tooltip placement.
128  * - 30 to test with a larger normal tooltip placement.
129  * - 60 to test with a huge tooltip placement.
130  * - 150 to test with a borderline to insanely huge tooltip placement.
131  * - 180 to test with an insanely huge tooltip placement.
132  */
133 static void debug_tooltip(window& /*window*/, bool& handled, const point& coordinate)
134 {
135  std::string message = "Hello world.";
136 
137  for(int i = 0; i < 0; ++i) {
138  message += " More greetings.";
139  }
140 
142  gui2::tip::show("tooltip", message, coordinate);
143 
144  handled = true;
145 }
146 #endif
147 
149 {
150  set_click_dismiss(false);
151  set_enter_disabled(true);
152  set_escape_disabled(true);
153 
154 #ifdef DEBUG_TOOLTIP
155  connect_signal<event::SDL_MOUSE_MOTION>(
156  std::bind(debug_tooltip, std::ref(*this), std::placeholders::_3, std::placeholders::_5),
158 #endif
159 
160  connect_signal<event::SDL_VIDEO_RESIZE>(std::bind(&title_screen::on_resize, this));
161 
162  //
163  // General hotkeys
164  //
166  std::bind(&gui2::window::set_retval, std::ref(*this), RELOAD_GAME_DATA, true));
167 
169  std::bind(&title_screen::show_achievements, this));
170 
173 
174  // A wrapper is needed here since the relevant display function is overloaded, and
175  // since the wrapper's signature doesn't exactly match what register_hotkey expects.
177 
179 
180  //
181  // Background and logo images
182  //
184  // game works just fine if just one of the background images are missing
185  ERR_CF << "No titlescreen background defined in game config";
186  }
187 
190 
191  find_widget<image>("logo-bg").set_image(game_config::images::game_logo_background);
192  find_widget<image>("logo").set_image(game_config::images::game_logo);
193 
194  //
195  // Tip-of-the-day browser
196  //
197  multi_page* tip_pages = find_widget<multi_page>("tips", false, false);
198 
199  if(tip_pages != nullptr) {
200  std::vector<game_tip> tips = tip_of_the_day::shuffle(settings::tips);
201  if(tips.empty()) {
202  WRN_CF << "There are no tips of day available.";
203  }
204  for(const auto& tip : tips) {
206  widget_data page;
207 
208  widget["use_markup"] = "true";
209 
210  // Use pango markup to insert drop cap
211  // Example: Lawful units -> <span ...>L</span>awful units
212  // If tip starts with a tag, we need to insert the <span> after it
213  // then insert the </span> tag after the first character of the text
214  // after markup. Assumes that the tags themselves don't
215  // contain non-ASCII characters.
216  // Example: <i>Lawful</i> units -> <i><span ...>L</span>awful</i> units
217  const std::string& script_font = font::get_font_families(font::FONT_SCRIPT);
218  std::string tip_text = tip.text().str();
219  std::size_t pos = 0;
220  while (pos < tip_text.size() && tip_text.at(pos) == '<') {
221  pos = tip_text.find_first_of(">", pos) + 1;
222  }
223  utf8::insert(tip_text, pos+1, "</span>");
224  utf8::insert(tip_text, pos, "<span font_family='" + script_font + "' font_size='xx-large'>");
225 
226  widget["label"] = tip_text;
227 
228  page.emplace("tip", widget);
229 
230  widget["label"] = tip.source();
231  page.emplace("source", widget);
232 
233  tip_pages->add_page(page);
234  }
235 
236  update_tip(true);
237  }
238 
240  std::bind(&title_screen::update_tip, this, true));
241 
243  std::bind(&title_screen::update_tip, this, false));
244 
245  // Tip panel visiblity and close button
246  panel& tip_panel = find_widget<panel>("tip_panel");
247  if (!prefs::get().show_tips()) {
248  tip_panel.set_visible(false);
249  } else {
250  auto close = find_widget<button>("close", false, false);
251  if (close) {
252  connect_signal_mouse_left_click(*close, [&](auto&&...) {
253  prefs::get().set_show_tips(false);
254  tip_panel.set_visible(false);
255  });
256  }
257  }
258 
259  //
260  // Help
261  //
262  register_button("help", hotkey::HOTKEY_HELP, []() {
264  help::show_help();
265  });
266 
267  //
268  // About
269  //
270  register_button("about", hotkey::HOTKEY_NULL, std::bind(&game_version::display<>));
271 
272  //
273  // Campaign
274  //
275  register_button("campaign", hotkey::TITLE_SCREEN__CAMPAIGN, [this]() {
276  try{
277  if(game_.new_campaign()) {
278  // Suspend drawing of the title screen,
279  // so it doesn't flicker in between loading screens.
280  hide();
281  set_retval(LAUNCH_GAME);
282  }
283  } catch (const config::error& e) {
284  gui2::show_error_message(e.what());
285  }
286  });
287 
288  //
289  // Multiplayer
290  //
293 
294  //
295  // Load game
296  //
297  register_button("load", hotkey::HOTKEY_LOAD_GAME, [this]() {
298  if(game_.load_game()) {
299  // Suspend drawing of the title screen,
300  // so it doesn't flicker in between loading screens.
301  hide();
302  set_retval(LAUNCH_GAME);
303  }
304  });
305 
306  //
307  // Addons
308  //
309  register_button("addons", hotkey::TITLE_SCREEN__ADDONS, [this]() {
310  if(manage_addons()) {
312  }
313  });
314 
315  //
316  // Editor
317  //
319 
320  //
321  // Cores
322  //
324  std::bind(&title_screen::button_callback_cores, this));
325 
326  //
327  // Language
328  //
329  register_button("language", hotkey::HOTKEY_LANGUAGE, [this]() {
330  try {
331  if(game_.change_language()) {
332  on_resize();
333  update_static_labels();
334  }
335  } catch(const std::runtime_error& e) {
336  gui2::show_error_message(e.what());
337  }
338  });
339 
340  //
341  // Preferences
342  //
344  std::bind(&title_screen::show_preferences, this));
345 
346  //
347  // Achievements
348  //
350  std::bind(&title_screen::show_achievements, this));
351 
352  //
353  // Community
354  //
356  std::bind(&title_screen::show_community, this));
357 
358  //
359  // Quit
360  //
362  // A sanity check, exit immediately if the .cfg file didn't have a "quit" button.
363  find_widget<button>("quit", false, true);
364 
365  //
366  // Debug clock
367  //
369  std::bind(&title_screen::show_debug_clock_window, this));
370 
371  auto clock = find_widget<button>("clock", false, false);
372  if(clock) {
373  clock->set_visible(show_debug_clock_button);
374  }
375 
376  //
377  // GUI Test and Debug Window
378  //
379  register_button("test_dialog", hotkey::HOTKEY_NULL,
380  std::bind(&title_screen::show_gui_test_dialog, this));
381 
382  auto test_dialog = find_widget<button>("test_dialog", false, false);
383  if(test_dialog) {
384  test_dialog->set_visible(show_debug_clock_button);
385  }
386 
387  //
388  // Static labels (version and language)
389  //
391 }
392 
394 {
395  //
396  // Version menu label
397  //
398  const std::string& version_string = VGETTEXT("Version $version", {{ "version", game_config::revision }});
399 
400  if(label* version_label = find_widget<label>("revision_number", false, false)) {
401  version_label->set_label(version_string);
402  }
403 
404  get_canvas(0).set_variable("revision_number", wfl::variant(version_string));
405 
406  //
407  // Language menu label
408  //
409  if(auto* lang_button = find_widget<button>("language", false, false); lang_button) {
410  const auto& locale = translation::get_effective_locale_info();
411  // Just assume everything is UTF-8 (it should be as long as we're called Wesnoth)
412  // and strip the charset from the Boost locale identifier.
413  const auto& boost_name = boost::algorithm::erase_first_copy(locale.name(), ".UTF-8");
414  const auto& langs = get_languages(true);
415 
416  auto lang_def = std::find_if(langs.begin(), langs.end(), [&](language_def const& lang) {
417  return lang.localename == boost_name;
418  });
419 
420  if(lang_def != langs.end()) {
421  lang_button->set_label(lang_def->language.str());
422  } else if(boost_name == "c" || boost_name == "C") {
423  // HACK: sometimes System Default doesn't match anything on the list. If you fork
424  // Wesnoth and change the neutral language to something other than US English, you
425  // want to change this too.
426  lang_button->set_label("English (US)");
427  } else {
428  // If somehow the locale doesn't match a known translation, use the
429  // locale identifier as a last resort
430  lang_button->set_label(boost_name);
431  }
432  }
433 }
434 
436 {
438 }
439 
440 void title_screen::update_tip(const bool previous)
441 {
442  multi_page* tip_pages = find_widget<multi_page>("tips", false, false);
443  if(tip_pages == nullptr) {
444  return;
445  }
446  if(tip_pages->get_page_count() == 0) {
447  return;
448  }
449 
450  int page = tip_pages->get_selected_page();
451  if(previous) {
452  if(page <= 0) {
453  page = tip_pages->get_page_count();
454  }
455  --page;
456  } else {
457  ++page;
458  if(static_cast<unsigned>(page) >= tip_pages->get_page_count()) {
459  page = 0;
460  }
461  }
462 
463  tip_pages->select_page(page);
464 }
465 
467 {
468  assert(show_debug_clock_button);
469 
470  if(debug_clock_) {
471  debug_clock_.reset(nullptr);
472  } else {
473  debug_clock_.reset(new debug_clock());
474  debug_clock_->show(true);
475  }
476 }
477 
479 {
481 
482  std::vector<std::string> options;
483  for(const config &sc : game_config_manager::get()->game_config().child_range("test")) {
484  if(!sc["is_unit_test"].to_bool(false)) {
485  options.emplace_back(sc["id"]);
486  }
487  }
488 
489  std::sort(options.begin(), options.end());
490 
491  gui2::dialogs::simple_item_selector dlg(_("Choose Test Scenario"), "", options);
492  dlg.show();
493 
494  int choice = dlg.selected_index();
495  if(choice >= 0) {
496  game_.set_test(options[choice]);
498  }
499 }
500 
502 {
504  ach.show();
505 }
506 
508 {
509  game_version dlg;
510  // shows the 5th tab, community, when the dialog is shown
511  dlg.display(4);
512 }
513 
515 {
516  gui2::dialogs::gui_test_dialog::execute();
517 }
518 
520 {
522  pref_dlg.show();
523  if (pref_dlg.get_retval() == RELOAD_UI) {
525  }
526 
527  // Currently blurred windows don't capture well if there is something
528  // on top of them at the time of blur. Resizing the game window in
529  // preferences will cause the title screen tip and menu panels to
530  // capture the prefs dialog in their blur. This workaround simply
531  // forces them to re-capture the blur after the dialog closes.
532  panel* tip_panel = find_widget<panel>("tip_panel", false, false);
533  if(tip_panel != nullptr) {
534  tip_panel->get_canvas(tip_panel->get_state()).queue_reblur();
535  tip_panel->queue_redraw();
536  }
537  panel* menu_panel = find_widget<panel>("menu_panel", false, false);
538  if(menu_panel != nullptr) {
539  menu_panel->get_canvas(menu_panel->get_state()).queue_reblur();
540  menu_panel->queue_redraw();
541  }
542 }
543 
545 {
546  while(true) {
548  dlg.show();
549 
550  if(dlg.get_retval() != gui2::retval::OK) {
551  return;
552  }
553 
554  const auto res = dlg.get_choice();
555 
556  if(res == decltype(dlg)::choice::HOST && prefs::get().mp_server_warning_disabled() < 2) {
557  if(!gui2::dialogs::mp_host_game_prompt::execute()) {
558  continue;
559  }
560  }
561 
562  switch(res) {
563  case decltype(dlg)::choice::JOIN:
564  game_.select_mp_server(prefs::get().builtin_servers_list().front().address);
566  break;
567  case decltype(dlg)::choice::CONNECT:
570  break;
571  case decltype(dlg)::choice::HOST:
572  game_.select_mp_server("localhost");
574  break;
575  case decltype(dlg)::choice::LOCAL:
577  break;
578  }
579 
580  return;
581  }
582 }
583 
585 {
586  int current = 0;
587 
588  std::vector<config> cores;
589  for(const config& core : game_config_manager::get()->game_config().child_range("core")) {
590  cores.push_back(core);
591 
592  if(core["id"] == prefs::get().core()) {
593  current = cores.size() - 1;
594  }
595  }
596 
597  gui2::dialogs::core_selection core_dlg(cores, current);
598  if(core_dlg.show()) {
599  const std::string& core_id = cores[core_dlg.get_choice()]["id"];
600 
601  prefs::get().set_core(core_id);
603  }
604 }
605 
606 } // namespace dialogs
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
static game_config_manager * get()
void load_game_config_for_create(bool is_mp, bool is_test=false)
bool change_language()
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:154
void queue_reblur()
Clear the cached blur texture, forcing it to regenerate.
Definition: canvas.cpp:629
static void display(lua_kernel_base *lk)
Display a new console, using given video and lua kernel.
Main class to show messages to the user.
Definition: message.hpp:36
Abstract base class for all modal dialogs.
bool show(const unsigned auto_close_time=0)
Shows the window.
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 show_gui_test_dialog()
Shows the gui test window.
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)
void register_hotkey(const hotkey::HOTKEY_COMMAND id, const hotkey_function &function)
Registers a hotkey.
Definition: dispatcher.cpp:147
grid & add_page(const widget_item &item)
Adds single page to the grid.
Definition: multi_page.cpp:58
unsigned get_page_count() const
Returns the number of pages.
Definition: multi_page.cpp:113
int get_selected_page() const
Returns the selected page.
Definition: multi_page.cpp:128
void select_page(const unsigned page, const bool select=true)
Selects a page.
Definition: multi_page.cpp:119
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: panel.cpp:61
canvas & get_canvas(const unsigned index)
Base class for all widgets.
Definition: widget.hpp:55
void set_visible(const visibility visible)
Definition: widget.cpp:479
void queue_redraw()
Indicates that this widget should be redrawn.
Definition: widget.cpp:464
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:61
void set_enter_disabled(const bool enter_disabled)
Disable the enter key.
Definition: window.hpp:321
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:395
void close()
Requests to close the window.
Definition: window.hpp:218
void set_escape_disabled(const bool escape_disabled)
Disable the escape key.
Definition: window.hpp:334
void set_click_dismiss(const bool click_dismiss)
Definition: window.hpp:412
int get_retval()
Definition: window.hpp:402
static prefs & get()
Declarations for File-IO.
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
std::size_t i
Definition: function.cpp:1029
static std::string _(const char *str)
Definition: gettext.hpp:93
#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...
language_list get_languages(bool all)
Return a list of available translations.
Definition: language.cpp:126
Standard logging facilities (interface).
bool manage_addons()
Shows the add-ons server connection dialog, for access to the various management front-ends.
Definition: manager_ui.cpp:228
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...
Definition: filesystem.cpp:587
@ FONT_SCRIPT
const t_string & get_font_families(family_class fclass)
Returns the currently defined fonts.
std::string game_title_background
std::string game_title
std::string game_logo
std::string game_logo_background
Game configuration data as global variables.
Definition: build_info.cpp:61
const std::string revision
void show(const std::string &window_id, const t_string &message, const point &mouse, const SDL_Rect &source_rect)
Shows a tip.
Definition: tooltip.cpp:64
void remove()
Removes a tip.
Definition: tooltip.cpp:94
static std::unique_ptr< tooltip > tip
Definition: tooltip.cpp:62
static void launch_lua_console()
static void make_screenshot()
bool show_debug_clock_button
Do we wish to show the button for the debug clock.
REGISTER_DIALOG(editor_edit_unit)
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177
std::vector< game_tip > tips
Definition: settings.cpp:47
std::vector< game_tip > shuffle(const std::vector< game_tip > &tips)
Shuffles the tips.
Definition: tips.cpp:48
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:36
std::map< std::string, t_string > widget_item
Definition: widget.hpp:33
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, show topic with id show_topic.
Definition: help.cpp:140
@ HOTKEY_SCREENSHOT
@ HOTKEY_ACHIEVEMENTS
@ TITLE_SCREEN__MULTIPLAYER
@ TITLE_SCREEN__ADDONS
@ TITLE_SCREEN__EDITOR
@ HOTKEY_QUIT_TO_DESKTOP
@ HOTKEY_LOAD_GAME
@ TITLE_SCREEN__TEST
@ TITLE_SCREEN__PREVIOUS_TIP
@ HOTKEY_PREFERENCES
@ TITLE_SCREEN__CORES
@ TITLE_SCREEN__CAMPAIGN
@ TITLE_SCREEN__RELOAD_WML
@ TITLE_SCREEN__NEXT_TIP
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
const boost::locale::info & get_effective_locale_info()
A facet that holds general information about the effective locale.
Definition: gettext.cpp:576
std::string & insert(std::string &str, const std::size_t pos, const std::string &insert)
Insert a UTF-8 string at the specified position.
Definition: unicode.cpp:98
surface read_pixels(SDL_Rect *r)
Copy back a portion of the render target that is already drawn.
Definition: video.cpp:593
This file contains the settings handling of the widget library.
std::string filename
Filename.
The help implementation caches data parsed from the game_config.
Definition: help.hpp:39
Holds a 2D point.
Definition: point.hpp:25
Helper class, don't construct this directly.
#define ERR_CF
#define WRN_CF
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 e