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 http://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"
32 #include "gui/dialogs/message.hpp"
36 #include "log.hpp"
37 #include "preferences/game.hpp"
38 //#define DEBUG_TOOLTIP
39 #ifdef DEBUG_TOOLTIP
40 #include "gui/dialogs/tooltip.hpp"
41 #endif
42 #include "gui/widgets/button.hpp"
43 #include "gui/widgets/image.hpp"
44 #include "gui/widgets/label.hpp"
46 #include "gui/widgets/settings.hpp"
47 #include "gui/widgets/window.hpp"
48 #include "help/help.hpp"
50 #include "utils/functional.hpp"
51 
52 #include <algorithm>
53 
54 static lg::log_domain log_config("config");
55 #define ERR_CF LOG_STREAM(err, log_config)
56 #define WRN_CF LOG_STREAM(warn, log_config)
57 
58 namespace gui2
59 {
60 namespace dialogs
61 {
62 
63 /*WIKI
64  * @page = GUIWindowDefinitionWML
65  * @order = 2_title_screen
66  *
67  * == Title screen ==
68  *
69  * This shows the title screen.
70  *
71  * @begin{table}{dialog_widgets}
72  * tutorial & & button & m &
73  * The button to start the tutorial. $
74  *
75  * campaign & & button & m &
76  * The button to start a campaign. $
77  *
78  * multiplayer & & button & m &
79  * The button to start multiplayer mode. $
80  *
81  * load & & button & m &
82  * The button to load a saved game. $
83  *
84  * editor & & button & m &
85  * The button to start the editor. $
86  *
87  * addons & & button & m &
88  * The button to start managing the addons. $
89  *
90  * cores & & button & m &
91  * The button to start managing the cores. $
92  *
93  * language & & button & m &
94  * The button to select the game language. $
95  *
96  * credits & & button & m &
97  * The button to show Wesnoth's contributors. $
98  *
99  * quit & & button & m &
100  * The button to quit Wesnoth. $
101  *
102  * tips & & multi_page & m &
103  * A multi_page to hold all tips, when this widget is used the area of
104  * the tips doesn't need to be resized when the next or previous button
105  * is pressed. $
106  *
107  * -tip & & label & o &
108  * Shows the text of the current tip. $
109  *
110  * -source & & label & o &
111  * The source (the one who's quoted or the book referenced) of the
112  * current tip. $
113  *
114  * next_tip & & button & m &
115  * The button show the next tip of the day. $
116  *
117  * previous_tip & & button & m &
118  * The button show the previous tip of the day. $
119  *
120  * logo & & image & o &
121  * The Wesnoth logo. $
122  *
123  * revision_number & & styled_widget & o &
124  * A widget to show the version number when the version number is
125  * known. $
126  *
127  * @end{table}
128  */
129 
130 REGISTER_DIALOG(title_screen)
131 
133 
135  : debug_clock_()
136  , game_(game)
137 {
138  // Need to set this in the constructor, pre_show() / post_build() is too late
139  set_allow_plugin_skip(false);
140 }
141 
143 {
144 }
145 
146 using btn_callback = std::function<void()>;
147 
148 static void register_button(window& win, const std::string& id, hotkey::HOTKEY_COMMAND hk, btn_callback callback)
149 {
150  if(hk != hotkey::HOTKEY_NULL) {
151  win.register_hotkey(hk, std::bind(callback));
152  }
153 
154  connect_signal_mouse_left_click(find_widget<button>(&win, id, false), std::bind(callback));
155 }
156 
157 static void launch_lua_console()
158 {
160 }
161 
162 #ifdef DEBUG_TOOLTIP
163 /*
164  * This function is used to test the tooltip placement algorithms as
165  * described in the »Tooltip placement« section in the GUI2 design
166  * document.
167  *
168  * Use a 1024 x 768 screen size, set the maximum loop iteration to:
169  * - 0 to test with a normal tooltip placement.
170  * - 30 to test with a larger normal tooltip placement.
171  * - 60 to test with a huge tooltip placement.
172  * - 150 to test with a borderline to insanely huge tooltip placement.
173  * - 180 to test with an insanely huge tooltip placement.
174  */
175 static void debug_tooltip(window& /*window*/, bool& handled, const point& coordinate)
176 {
177  std::string message = "Hello world.";
178 
179  for(int i = 0; i < 0; ++i) {
180  message += " More greetings.";
181  }
182 
184  gui2::tip::show("tooltip", message, coordinate);
185 
186  handled = true;
187 }
188 #endif
189 
191 {
192  win.set_click_dismiss(false);
193  win.set_enter_disabled(true);
194  win.set_escape_disabled(true);
195 
196 #ifdef DEBUG_TOOLTIP
198  std::bind(debug_tooltip, std::ref(win), _3, _5),
200 #endif
201 
202  win.connect_signal<event::SDL_VIDEO_RESIZE>(std::bind(&title_screen::on_resize, this, std::ref(win)));
203 
204  //
205  // General hotkeys
206  //
208  std::bind(&gui2::window::set_retval, std::ref(win), RELOAD_GAME_DATA, true));
209 
211  std::bind(&title_screen::hotkey_callback_select_tests, this, std::ref(win)));
212 
213  // A wrapper is needed here since the relevant display function is overloaded, and
214  // since the wrapper's signature doesn't exactly match what register_hotkey expects.
216 
217  //
218  // Background and logo images
219  //
220  if(game_config::images::game_title.empty()) {
221  ERR_CF << "No title image defined" << std::endl;
222  }
223 
225 
227  ERR_CF << "No title background image defined" << std::endl;
228  }
229 
231 
232  find_widget<image>(&win, "logo-bg", false).set_image(game_config::images::game_logo_background);
233  find_widget<image>(&win, "logo", false).set_image(game_config::images::game_logo);
234 
235  //
236  // Version string
237  //
238  const std::string& version_string = VGETTEXT("Version $version", {{ "version", game_config::revision }});
239 
240  if(label* version_label = find_widget<label>(&win, "revision_number", false, false)) {
241  version_label->set_label(version_string);
242  }
243 
244  win.get_canvas(0).set_variable("revision_number", wfl::variant(version_string));
245 
246  //
247  // Tip-of-the-day browser
248  //
249  multi_page& tip_pages = find_widget<multi_page>(&win, "tips", false);
250 
251  std::vector<game_tip> tips = tip_of_the_day::shuffle(settings::tips);
252  if(tips.empty()) {
253  WRN_CF << "There are no tips of day available." << std::endl;
254  }
255 
256  for(const auto& tip : tips) {
258  std::map<std::string, string_map> page;
259 
260  widget["use_markup"] = "true";
261 
262  widget["label"] = tip.text();
263  page.emplace("tip", widget);
264 
265  widget["label"] = tip.source();
266  page.emplace("source", widget);
267 
268  tip_pages.add_page(page);
269  }
270 
271  update_tip(win, true);
272 
274  std::bind(&title_screen::update_tip, this, std::ref(win), true));
275 
277  std::bind(&title_screen::update_tip, this, std::ref(win), false));
278 
279  //
280  // Help
281  //
282  register_button(win, "help", hotkey::HOTKEY_HELP, []() {
283  help::show_help();
284  });
285 
286  //
287  // About
288  //
289  register_button(win, "about", hotkey::HOTKEY_NULL, std::bind(&game_version::display<>));
290 
291  //
292  // Tutorial
293  //
294  register_button(win, "tutorial", hotkey::TITLE_SCREEN__TUTORIAL, [this, &win]() {
296  win.set_retval(LAUNCH_GAME);
297  });
298 
299  //
300  // Campaign
301  //
302  register_button(win, "campaign", hotkey::TITLE_SCREEN__CAMPAIGN, [this, &win]() {
303  try{
304  if(game_.new_campaign()) {
305  win.set_retval(LAUNCH_GAME);
306  }
307  } catch (const config::error& e) {
309  }
310  });
311 
312  //
313  // Multiplayer
314  //
316  std::bind(&title_screen::button_callback_multiplayer, this, std::ref(win)));
317 
318  //
319  // Load game
320  //
321  register_button(win, "load", hotkey::HOTKEY_LOAD_GAME, [this, &win]() {
322  if(game_.load_game()) {
323  win.set_retval(LAUNCH_GAME);
324  } else {
326  }
327  });
328 
329  //
330  // Addons
331  //
332  register_button(win, "addons", hotkey::TITLE_SCREEN__ADDONS, []() {
333  if(manage_addons()) {
335  }
336  });
337 
338  //
339  // Editor
340  //
341  register_button(win, "editor", hotkey::TITLE_SCREEN__EDITOR, [&win]() { win.set_retval(MAP_EDITOR); });
342 
343  //
344  // Cores
345  //
347  std::bind(&title_screen::button_callback_cores, this));
348 
349  if(game_config_manager::get()->game_config().child_range("core").size() <= 1) {
350  find_widget<button>(&win, "cores", false).set_visible(widget::visibility::invisible);
351  }
352 
353  //
354  // Language
355  //
356  register_button(win, "language", hotkey::HOTKEY_LANGUAGE, [this, &win]() {
357  try {
358  if(game_.change_language()) {
361  on_resize(win);
362  }
363  } catch(const std::runtime_error& e) {
364  gui2::show_error_message(e.what());
365  }
366  });
367 
368  //
369  // Preferences
370  //
371  register_button(win, "preferences", hotkey::HOTKEY_PREFERENCES, [this]() { game_.show_preferences(); });
372 
373  //
374  // Credits
375  //
376  register_button(win, "credits", hotkey::TITLE_SCREEN__CREDITS, [&win]() { win.set_retval(SHOW_ABOUT); });
377 
378  //
379  // Quit
380  //
381  register_button(win, "quit", hotkey::HOTKEY_QUIT_TO_DESKTOP, [&win]() { win.set_retval(QUIT_GAME); });
382 
383  //
384  // Debug clock
385  //
386  register_button(win, "clock", hotkey::HOTKEY_NULL,
387  std::bind(&title_screen::show_debug_clock_window, this));
388 
389  find_widget<button>(&win, "clock", false).set_visible(show_debug_clock_button
392 }
393 
395 {
397 }
398 
399 void title_screen::update_tip(window& win, const bool previous)
400 {
401  multi_page& tips = find_widget<multi_page>(&win, "tips", false);
402  if(tips.get_page_count() == 0) {
403  return;
404  }
405 
406  int page = tips.get_selected_page();
407  if(previous) {
408  if(page <= 0) {
409  page = tips.get_page_count();
410  }
411  --page;
412  } else {
413  ++page;
414  if(static_cast<unsigned>(page) >= tips.get_page_count()) {
415  page = 0;
416  }
417  }
418 
419  tips.select_page(page);
420 }
421 
423 {
424  assert(show_debug_clock_button);
425 
426  if(debug_clock_) {
427  debug_clock_.reset(nullptr);
428  } else {
429  debug_clock_.reset(new debug_clock());
430  debug_clock_->show(true);
431  }
432 }
433 
435 {
437 
438  std::vector<std::string> options;
439  for(const config &sc : game_config_manager::get()->game_config().child_range("test")) {
440  if(!sc["is_unit_test"].to_bool(false)) {
441  options.emplace_back(sc["id"]);
442  }
443  }
444 
445  std::sort(options.begin(), options.end());
446 
447  gui2::dialogs::simple_item_selector dlg(_("Choose Test"), "", options);
448  dlg.show();
449 
450  int choice = dlg.selected_index();
451  if(choice >= 0) {
452  game_.set_test(options[choice]);
453  window.set_retval(LAUNCH_GAME);
454  }
455 }
456 
458 {
459  while(true) {
461  dlg.show();
462 
463  if(dlg.get_retval() != gui2::retval::OK) {
464  return;
465  }
466 
467  const int res = dlg.get_choice();
468 
469  if(res == 2 && preferences::mp_server_warning_disabled() < 2) {
470  if(!gui2::dialogs::mp_host_game_prompt::execute()) {
471  continue;
472  }
473  }
474 
475  switch(res) {
476  case 0:
478  window.set_retval(MP_CONNECT);
479  break;
480  case 1:
482  window.set_retval(MP_CONNECT);
483  break;
484  case 2:
485  game_.select_mp_server("localhost");
486  window.set_retval(MP_HOST);
487  break;
488  case 3:
489  window.set_retval(MP_LOCAL);
490  break;
491  }
492 
493  return;
494  }
495 }
496 
498 {
499  int current = 0;
500 
501  std::vector<config> cores;
502  for(const config& core : game_config_manager::get()->game_config().child_range("core")) {
503  cores.push_back(core);
504 
505  if(core["id"] == preferences::core_id()) {
506  current = cores.size() - 1;
507  }
508  }
509 
510  gui2::dialogs::core_selection core_dlg(cores, current);
511  if(core_dlg.show()) {
512  const std::string& core_id = cores[core_dlg.get_choice()]["id"];
513 
514  preferences::set_core_id(core_id);
516  }
517 }
518 
519 } // namespace dialogs
520 } // namespace gui2
void select_mp_server(std::string server)
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
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:134
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:159
std::string game_title
An SDL resize request, coordinate is the new window size.
Definition: handler.hpp:51
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:363
void set_escape_disabled(const bool escape_disabled)
Disable the escape key.
Definition: window.hpp:282
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
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: image.cpp:250
Generic file dialog.
Definition: field-fwd.hpp:22
const wesnothd::game & game_
Definition: game.cpp:1651
const config & options()
Definition: game.cpp:569
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:164
std::string game_title_background
const std::vector< game_config::server_info > & server_list()
Definition: game.cpp:373
void set_test(const std::string &id)
void set_core_id(const std::string &core_id)
Definition: general.cpp:321
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:502
void show_help(const std::string &show_topic)
Open the help browser, show topic with id show_topic.
Definition: help.cpp:36
std::size_t i
Definition: function.cpp:933
std::enable_if_t< is_general_event(E)> connect_signal(const signal_function &signal, const queue_position position=back_child)
Connect a signal for callback in set_event.
Definition: dispatcher.hpp:483
const std::string revision
Definition: version.cpp:42
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:46
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:346
static int sort(lua_State *L)
Definition: ltablib.cpp:411
#define VGETTEXT(msgid,...)
std::string core_id()
Definition: general.cpp:315
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:203
#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:68
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:53
#define ERR_CF
void set_enter_disabled(const bool enter_disabled)
Disable the enter key.
Definition: window.hpp:269