The Battle for Wesnoth  1.15.0-dev
title_screen.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "addon/manager_ui.hpp"
20 #include "formula/string_utils.hpp"
21 #include "game_config.hpp"
22 #include "game_config_manager.hpp"
23 #include "game_launcher.hpp"
25 #include "gui/auxiliary/tips.hpp"
26 #include "gui/core/timer.hpp"
33 #include "gui/dialogs/message.hpp"
37 #include "log.hpp"
38 #include "preferences/game.hpp"
39 //#define DEBUG_TOOLTIP
40 #ifdef DEBUG_TOOLTIP
41 #include "gui/dialogs/tooltip.hpp"
42 #endif
43 #include "gui/widgets/button.hpp"
44 #include "gui/widgets/image.hpp"
45 #include "gui/widgets/label.hpp"
47 #include "gui/widgets/settings.hpp"
48 #include "gui/widgets/window.hpp"
49 #include "help/help.hpp"
51 #include "utils/functional.hpp"
52 
53 #include <algorithm>
54 
55 static lg::log_domain log_config("config");
56 #define ERR_CF LOG_STREAM(err, log_config)
57 #define WRN_CF LOG_STREAM(warn, log_config)
58 
59 namespace gui2
60 {
61 namespace dialogs
62 {
63 
64 /*WIKI
65  * @page = GUIWindowDefinitionWML
66  * @order = 2_title_screen
67  *
68  * == Title screen ==
69  *
70  * This shows the title screen.
71  *
72  * @begin{table}{dialog_widgets}
73  * tutorial & & button & m &
74  * The button to start the tutorial. $
75  *
76  * campaign & & button & m &
77  * The button to start a campaign. $
78  *
79  * multiplayer & & button & m &
80  * The button to start multiplayer mode. $
81  *
82  * load & & button & m &
83  * The button to load a saved game. $
84  *
85  * editor & & button & m &
86  * The button to start the editor. $
87  *
88  * addons & & button & m &
89  * The button to start managing the addons. $
90  *
91  * cores & & button & m &
92  * The button to start managing the cores. $
93  *
94  * language & & button & m &
95  * The button to select the game language. $
96  *
97  * credits & & button & m &
98  * The button to show Wesnoth's contributors. $
99  *
100  * quit & & button & m &
101  * The button to quit Wesnoth. $
102  *
103  * tips & & multi_page & m &
104  * A multi_page to hold all tips, when this widget is used the area of
105  * the tips doesn't need to be resized when the next or previous button
106  * is pressed. $
107  *
108  * -tip & & label & o &
109  * Shows the text of the current tip. $
110  *
111  * -source & & label & o &
112  * The source (the one who's quoted or the book referenced) of the
113  * current tip. $
114  *
115  * next_tip & & button & m &
116  * The button show the next tip of the day. $
117  *
118  * previous_tip & & button & m &
119  * The button show the previous tip of the day. $
120  *
121  * logo & & image & o &
122  * The Wesnoth logo. $
123  *
124  * revision_number & & styled_widget & o &
125  * A widget to show the version number when the version number is
126  * known. $
127  *
128  * @end{table}
129  */
130 
131 REGISTER_DIALOG(title_screen)
132 
134 
136  : debug_clock_()
137  , game_(game)
138 {
139  set_restore(false);
140 
141  // Need to set this in the constructor, pre_show() / post_build() is too late
142  set_allow_plugin_skip(false);
143 }
144 
146 {
147 }
148 
149 using btn_callback = std::function<void()>;
150 
151 static void register_button(window& win, const std::string& id, hotkey::HOTKEY_COMMAND hk, btn_callback callback)
152 {
153  if(hk != hotkey::HOTKEY_NULL) {
154  win.register_hotkey(hk, std::bind(callback));
155  }
156 
157  connect_signal_mouse_left_click(find_widget<button>(&win, id, false), std::bind(callback));
158 }
159 
160 static void launch_lua_console()
161 {
163 }
164 
165 #ifdef DEBUG_TOOLTIP
166 /*
167  * This function is used to test the tooltip placement algorithms as
168  * described in the »Tooltip placement« section in the GUI2 design
169  * document.
170  *
171  * Use a 1024 x 768 screen size, set the maximum loop iteration to:
172  * - 0 to test with a normal tooltip placement.
173  * - 30 to test with a larger normal tooltip placement.
174  * - 60 to test with a huge tooltip placement.
175  * - 150 to test with a borderline to insanely huge tooltip placement.
176  * - 180 to test with an insanely huge tooltip placement.
177  */
178 static void debug_tooltip(window& /*window*/, bool& handled, const point& coordinate)
179 {
180  std::string message = "Hello world.";
181 
182  for(int i = 0; i < 0; ++i) {
183  message += " More greetings.";
184  }
185 
187  gui2::tip::show("tooltip", message, coordinate);
188 
189  handled = true;
190 }
191 #endif
192 
194 {
195  win.set_click_dismiss(false);
196  win.set_enter_disabled(true);
197  win.set_escape_disabled(true);
198 
199 #ifdef DEBUG_TOOLTIP
201  std::bind(debug_tooltip, std::ref(win), _3, _5),
203 #endif
204 
205  win.connect_signal<event::SDL_VIDEO_RESIZE>(std::bind(&title_screen::on_resize, this, std::ref(win)));
206 
207  //
208  // General hotkeys
209  //
211  std::bind(&gui2::window::set_retval, std::ref(win), RELOAD_GAME_DATA, true));
212 
214  std::bind(&title_screen::hotkey_callback_select_tests, this, std::ref(win)));
215 
216  // A wrapper is needed here since the relevant display function is overloaded, and
217  // since the wrapper's signature doesn't exactly match what register_hotkey expects.
219 
220  //
221  // Background and logo images
222  //
223  if(game_config::images::game_title.empty()) {
224  ERR_CF << "No title image defined" << std::endl;
225  }
226 
228 
230  ERR_CF << "No title background image defined" << std::endl;
231  }
232 
234 
235  find_widget<image>(&win, "logo-bg", false).set_image(game_config::images::game_logo_background);
236  find_widget<image>(&win, "logo", false).set_image(game_config::images::game_logo);
237 
238  //
239  // Version string
240  //
241  const std::string& version_string = VGETTEXT("Version $version", {{ "version", game_config::revision }});
242 
243  if(label* version_label = find_widget<label>(&win, "revision_number", false, false)) {
244  version_label->set_label(version_string);
245  }
246 
247  win.get_canvas(0).set_variable("revision_number", wfl::variant(version_string));
248 
249  //
250  // Tip-of-the-day browser
251  //
252  multi_page& tip_pages = find_widget<multi_page>(&win, "tips", false);
253 
254  std::vector<game_tip> tips = tip_of_the_day::shuffle(settings::tips);
255  if(tips.empty()) {
256  WRN_CF << "There are no tips of day available." << std::endl;
257  }
258 
259  for(const auto& tip : tips) {
261  std::map<std::string, string_map> page;
262 
263  widget["use_markup"] = "true";
264 
265  widget["label"] = tip.text();
266  page.emplace("tip", widget);
267 
268  widget["label"] = tip.source();
269  page.emplace("source", widget);
270 
271  tip_pages.add_page(page);
272  }
273 
274  update_tip(win, true);
275 
277  std::bind(&title_screen::update_tip, this, std::ref(win), true));
278 
280  std::bind(&title_screen::update_tip, this, std::ref(win), false));
281 
282  //
283  // Help
284  //
285  register_button(win, "help", hotkey::HOTKEY_HELP, []() {
286  if(gui2::new_widgets) {
287  gui2::dialogs::help_browser::display();
288  }
289 
291  help::show_help();
292  });
293 
294  //
295  // About
296  //
297  register_button(win, "about", hotkey::HOTKEY_NULL, std::bind(&game_version::display<>));
298 
299  //
300  // Tutorial
301  //
302  register_button(win, "tutorial", hotkey::TITLE_SCREEN__TUTORIAL, [this, &win]() {
304  win.set_retval(LAUNCH_GAME);
305  });
306 
307  //
308  // Campaign
309  //
310  register_button(win, "campaign", hotkey::TITLE_SCREEN__CAMPAIGN, [this, &win]() {
311  try{
312  if(game_.new_campaign()) {
313  win.set_retval(LAUNCH_GAME);
314  }
315  } catch (const config::error& e) {
317  }
318  });
319 
320  //
321  // Multiplayer
322  //
324  std::bind(&title_screen::button_callback_multiplayer, this, std::ref(win)));
325 
326  //
327  // Load game
328  //
329  register_button(win, "load", hotkey::HOTKEY_LOAD_GAME, [this, &win]() {
330  if(game_.load_game()) {
331  win.set_retval(LAUNCH_GAME);
332  } else {
334  }
335  });
336 
337  //
338  // Addons
339  //
340  register_button(win, "addons", hotkey::TITLE_SCREEN__ADDONS, []() {
341  // NOTE: we need the help_manager to get access to the Add-ons section in the game help!
343 
344  if(manage_addons()) {
346  }
347  });
348 
349  //
350  // Editor
351  //
352  register_button(win, "editor", hotkey::TITLE_SCREEN__EDITOR, [&win]() { win.set_retval(MAP_EDITOR); });
353 
354  //
355  // Cores
356  //
358  std::bind(&title_screen::button_callback_cores, this));
359 
360  if(game_config_manager::get()->game_config().child_range("core").size() <= 1) {
361  find_widget<button>(&win, "cores", false).set_visible(widget::visibility::invisible);
362  }
363 
364  //
365  // Language
366  //
367  register_button(win, "language", hotkey::HOTKEY_LANGUAGE, [this, &win]() {
368  try {
369  if(game_.change_language()) {
372  on_resize(win);
373  }
374  } catch(const std::runtime_error& e) {
375  gui2::show_error_message(e.what());
376  }
377  });
378 
379  //
380  // Preferences
381  //
382  register_button(win, "preferences", hotkey::HOTKEY_PREFERENCES, [this]() { game_.show_preferences(); });
383 
384  //
385  // Credits
386  //
387  register_button(win, "credits", hotkey::TITLE_SCREEN__CREDITS, [&win]() { win.set_retval(SHOW_ABOUT); });
388 
389  //
390  // Quit
391  //
392  register_button(win, "quit", hotkey::HOTKEY_QUIT_TO_DESKTOP, [&win]() { win.set_retval(QUIT_GAME); });
393 
394  //
395  // Debug clock
396  //
397  register_button(win, "clock", hotkey::HOTKEY_NULL,
398  std::bind(&title_screen::show_debug_clock_window, this));
399 
400  find_widget<button>(&win, "clock", false).set_visible(show_debug_clock_button
403 }
404 
406 {
408 }
409 
410 void title_screen::update_tip(window& win, const bool previous)
411 {
412  multi_page& tips = find_widget<multi_page>(&win, "tips", false);
413  if(tips.get_page_count() == 0) {
414  return;
415  }
416 
417  int page = tips.get_selected_page();
418  if(previous) {
419  if(page <= 0) {
420  page = tips.get_page_count();
421  }
422  --page;
423  } else {
424  ++page;
425  if(static_cast<unsigned>(page) >= tips.get_page_count()) {
426  page = 0;
427  }
428  }
429 
430  tips.select_page(page);
431 
432  /**
433  * @todo Look for a proper fix.
434  *
435  * This dirtying is required to avoid the blurring to be rendered wrong.
436  * Not entirely sure why, but since we plan to move to SDL2 that change
437  * will probably fix this issue automatically.
438  */
439  win.set_is_dirty(true);
440 }
441 
443 {
444  assert(show_debug_clock_button);
445 
446  if(debug_clock_) {
447  debug_clock_.reset(nullptr);
448  } else {
449  debug_clock_.reset(new debug_clock());
450  debug_clock_->show(true);
451  }
452 }
453 
455 {
457 
458  std::vector<std::string> options;
459  for(const config &sc : game_config_manager::get()->game_config().child_range("test")) {
460  if(!sc["is_unit_test"].to_bool(false)) {
461  options.emplace_back(sc["id"]);
462  }
463  }
464 
465  std::sort(options.begin(), options.end());
466 
467  gui2::dialogs::simple_item_selector dlg(_("Choose Test"), "", options);
468  dlg.show();
469 
470  int choice = dlg.selected_index();
471  if(choice >= 0) {
472  game_.set_test(options[choice]);
473  window.set_retval(LAUNCH_GAME);
474  }
475 }
476 
478 {
479  while(true) {
481  dlg.show();
482 
483  if(dlg.get_retval() != gui2::retval::OK) {
484  return;
485  }
486 
487  const int res = dlg.get_choice();
488 
489  if(res == 2 && preferences::mp_server_warning_disabled() < 2) {
490  if(!gui2::dialogs::mp_host_game_prompt::execute()) {
491  continue;
492  }
493  }
494 
495  switch(res) {
496  case 0:
498  window.set_retval(MP_CONNECT);
499  break;
500  case 1:
502  window.set_retval(MP_CONNECT);
503  break;
504  case 2:
505  game_.select_mp_server("localhost");
506  window.set_retval(MP_HOST);
507  break;
508  case 3:
509  window.set_retval(MP_LOCAL);
510  break;
511  }
512 
513  return;
514  }
515 }
516 
518 {
519  int current = 0;
520 
521  std::vector<config> cores;
522  for(const config& core : game_config_manager::get()->game_config().child_range("core")) {
523  cores.push_back(core);
524 
525  if(core["id"] == preferences::core_id()) {
526  current = cores.size() - 1;
527  }
528  }
529 
530  gui2::dialogs::core_selection core_dlg(cores, current);
531  if(core_dlg.show()) {
532  const std::string& core_id = cores[core_dlg.get_choice()]["id"];
533 
534  preferences::set_core_id(core_id);
536  }
537 }
538 
539 } // namespace dialogs
540 } // namespace gui2
void select_mp_server(std::string server)
bool new_widgets
Do we wish to use the new library or not.
Definition: settings.cpp:21
void remove()
Removes a tip.
Definition: tooltip.cpp:189
void update_tip(window &window, const bool previous)
Updates the tip of day widget.
void show_debug_clock_window()
Shows the debug clock.
static void reset_translations()
Definition: tstring.cpp:654
void show_help(const std::string &show_topic, int xloc, int yloc)
Open the help browser, show topic with id show_topic.
Definition: help.cpp:116
std::vector< game_tip > tips
Definition: settings.cpp:46
Main class to show messages to the user.
Definition: message.hpp:34
void register_hotkey(const hotkey::HOTKEY_COMMAND id, const hotkey_function &function)
Registers a hotkey.
Definition: dispatcher.cpp:203
grid & add_page(const string_map &item)
Adds single page to the grid.
Definition: multi_page.cpp:43
void show_preferences()
#define WRN_CF
void set_variable(const std::string &key, const wfl::variant &value)
Definition: canvas.hpp:171
std::string game_title
An SDL resize request, coordinate is the new window size.
Definition: handler.hpp:59
This file contains the window object, this object is a top level container which has the event manage...
Base class for all widgets.
Definition: widget.hpp:47
This class implements the title screen.
void set_click_dismiss(const bool click_dismiss)
Definition: window.hpp:381
void set_escape_disabled(const bool escape_disabled)
Disable the escape key.
Definition: window.hpp:300
bool change_language()
Label showing a text.
Definition: label.hpp:32
int get_selected_page() const
Returns the selected page.
Definition: multi_page.cpp:112
std::enable_if_t< has_key< set_event, E >::value > connect_signal(const signal_function &signal, const queue_position position=back_child)
Connect a signal for callback in set_event.
Definition: dispatcher.hpp:393
bool show(const unsigned auto_close_time=0)
Shows the window.
void hotkey_callback_select_tests(window &window)
static void launch_lua_console()
static void display(lua_kernel_base *lk)
Display a new console, using given video and lua kernel.
void flush_cache()
Definition: picture.cpp:234
Generic file dialog.
Definition: field-fwd.hpp:22
const wesnothd::game & game_
Definition: game.cpp:1675
const config & options()
Definition: game.cpp:568
static game_config_manager * get()
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
This file contains the settings handling of the widget library.
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:233
std::string game_title_background
const std::vector< game_config::server_info > & server_list()
Definition: game.cpp:372
void set_test(const std::string &id)
void set_core_id(const std::string &core_id)
Definition: general.cpp:319
static lg::log_domain log_config("config")
std::string game_logo_background
const char * what() const noexcept
Definition: exceptions.hpp:37
static tooltip & tip()
Definition: tooltip.cpp:144
bool manage_addons()
Shows the add-ons server connection dialog, for access to the various management front-ends.
Definition: manager_ui.cpp:230
Various uncategorised dialogs.
int mp_server_warning_disabled()
Definition: game.cpp:501
std::size_t i
Definition: function.cpp:933
const std::string revision
void load_game_config_for_create(bool is_mp, bool is_test=false)
Clock to test the draw events.
Definition: debug_clock.hpp:33
Game configuration data as global variables.
Definition: build_info.cpp:49
The user set the widget invisible, that means:
void select_page(const unsigned page, const bool select=true)
Selectes a page.
Definition: multi_page.cpp:106
Contains the gui2 timer routines.
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
Holds a 2D point.
Definition: point.hpp:23
void on_resize(window &window)
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:364
static int sort(lua_State *L)
Definition: ltablib.cpp:411
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
std::string core_id()
Definition: general.cpp:313
unsigned get_page_count() const
Returns the number of pages.
Definition: multi_page.cpp:100
The user sets the widget visible, that means:
The multi page class.
Definition: multi_page.hpp:35
Standard logging facilities (interface).
void clear_loaded_game()
std::string game_logo
void button_callback_multiplayer(window &window)
std::unique_ptr< modeless_dialog > debug_clock_
Holds the debug clock dialog.
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:205
#define e
std::vector< game_tip > shuffle(const std::vector< game_tip > &tips)
Shuffles the tips.
Definition: tips.cpp:46
virtual void pre_show(window &window) override
Inherited from modal_dialog.
Dialog was closed with the OK button.
Definition: retval.hpp:34
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
canvas & get_canvas(const unsigned index)
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
std::function< void()> btn_callback
static void register_button(window &win, const std::string &id, hotkey::HOTKEY_COMMAND hk, btn_callback callback)
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:154
bool show_debug_clock_button
Do we wish to show the button for the debug clock.
An SDL mouse motion event.
Definition: handler.hpp:61
#define ERR_CF
void set_enter_disabled(const bool enter_disabled)
Disable the enter key.
Definition: window.hpp:287