The Battle for Wesnoth  1.13.11+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 "formatter.hpp"
21 #include "game_config.hpp"
22 #include "game_config_manager.hpp"
23 #include "game_launcher.hpp"
24 #include "gettext.hpp"
26 #include "gui/auxiliary/tips.hpp"
27 #include "gui/core/timer.hpp"
34 #include "gui/dialogs/message.hpp"
38 #include "log.hpp"
39 #include "preferences/game.hpp"
40 //#define DEBUG_TOOLTIP
41 #ifdef DEBUG_TOOLTIP
42 #include "gui/dialogs/tooltip.hpp"
43 #endif
44 #include "gui/widgets/button.hpp"
45 #include "gui/widgets/image.hpp"
46 #include "gui/widgets/label.hpp"
48 #include "gui/widgets/settings.hpp"
49 #include "gui/widgets/window.hpp"
50 #include "help/help.hpp"
52 #include "video.hpp"
53 #include "utils/functional.hpp"
54 
55 #include <algorithm>
56 
57 static lg::log_domain log_config("config");
58 #define ERR_CF LOG_STREAM(err, log_config)
59 #define WRN_CF LOG_STREAM(warn, log_config)
60 
61 namespace gui2
62 {
63 namespace dialogs
64 {
65 
66 /*WIKI
67  * @page = GUIWindowDefinitionWML
68  * @order = 2_title_screen
69  *
70  * == Title screen ==
71  *
72  * This shows the title screen.
73  *
74  * @begin{table}{dialog_widgets}
75  * tutorial & & button & m &
76  * The button to start the tutorial. $
77  *
78  * campaign & & button & m &
79  * The button to start a campaign. $
80  *
81  * multiplayer & & button & m &
82  * The button to start multiplayer mode. $
83  *
84  * load & & button & m &
85  * The button to load a saved game. $
86  *
87  * editor & & button & m &
88  * The button to start the editor. $
89  *
90  * addons & & button & m &
91  * The button to start managing the addons. $
92  *
93  * cores & & button & m &
94  * The button to start managing the cores. $
95  *
96  * language & & button & m &
97  * The button to select the game language. $
98  *
99  * credits & & button & m &
100  * The button to show Wesnoth's contributors. $
101  *
102  * quit & & button & m &
103  * The button to quit Wesnoth. $
104  *
105  * tips & & multi_page & m &
106  * A multi_page to hold all tips, when this widget is used the area of
107  * the tips doesn't need to be resized when the next or previous button
108  * is pressed. $
109  *
110  * -tip & & label & o &
111  * Shows the text of the current tip. $
112  *
113  * -source & & label & o &
114  * The source (the one who's quoted or the book referenced) of the
115  * current tip. $
116  *
117  * next_tip & & button & m &
118  * The button show the next tip of the day. $
119  *
120  * previous_tip & & button & m &
121  * The button show the previous tip of the day. $
122  *
123  * logo & & image & o &
124  * The Wesnoth logo. $
125  *
126  * revision_number & & styled_widget & o &
127  * A widget to show the version number when the version number is
128  * known. $
129  *
130  * @end{table}
131  */
132 
133 REGISTER_DIALOG(title_screen)
134 
136 
138  : debug_clock_()
139  , game_(game)
140 {
141  set_restore(false);
142 
143  // Need to set this in the constructor, pre_show() / post_build() is too late
144  set_allow_plugin_skip(false);
145 }
146 
148 {
149 }
150 
151 using btn_callback = std::function<void()>;
152 
153 static void register_button(window& win, const std::string& id, hotkey::HOTKEY_COMMAND hk, btn_callback callback)
154 {
155  if(hk != hotkey::HOTKEY_NULL) {
156  win.register_hotkey(hk, std::bind(callback));
157  }
158 
159  connect_signal_mouse_left_click(find_widget<button>(&win, id, false), std::bind(callback));
160 }
161 
162 static void launch_lua_console()
163 {
165 }
166 
167 #ifdef DEBUG_TOOLTIP
168 /*
169  * This function is used to test the tooltip placement algorithms as
170  * described in the »Tooltip placement« section in the GUI2 design
171  * document.
172  *
173  * Use a 1024 x 768 screen size, set the maximum loop iteration to:
174  * - 0 to test with a normal tooltip placement.
175  * - 30 to test with a larger normal tooltip placement.
176  * - 60 to test with a huge tooltip placement.
177  * - 150 to test with a borderline to insanely huge tooltip placement.
178  * - 180 to test with an insanely huge tooltip placement.
179  */
180 static void debug_tooltip(window& /*window*/, bool& handled, const point& coordinate)
181 {
182  std::string message = "Hello world.";
183 
184  for(int i = 0; i < 0; ++i) {
185  message += " More greetings.";
186  }
187 
189  gui2::tip::show("tooltip", message, coordinate);
190 
191  handled = true;
192 }
193 #endif
194 
196 {
197  win.set_click_dismiss(false);
198  win.set_enter_disabled(true);
199  win.set_escape_disabled(true);
200 
201 #ifdef DEBUG_TOOLTIP
203  std::bind(debug_tooltip, std::ref(win), _3, _5),
205 #endif
206 
207  win.connect_signal<event::SDL_VIDEO_RESIZE>(std::bind(&title_screen::on_resize, this, std::ref(win)));
208 
209  //
210  // General hotkeys
211  //
213  std::bind(&gui2::window::set_retval, std::ref(win), RELOAD_GAME_DATA, true));
214 
216  std::bind(&title_screen::hotkey_callback_select_tests, this, std::ref(win)));
217 
219  std::bind(&CVideo::toggle_fullscreen, std::ref(win.video())));
220 
221  // A wrapper is needed here since the relevant display function is overloaded, and
222  // since the wrapper's signature doesn't exactly match what register_hotkey expects.
224 
225  //
226  // Background and logo images
227  //
228  if(game_config::images::game_title.empty()) {
229  ERR_CF << "No title image defined" << std::endl;
230  }
231 
233 
235  ERR_CF << "No title background image defined" << std::endl;
236  }
237 
239 
240  find_widget<image>(&win, "logo-bg", false).set_image(game_config::images::game_logo_background);
241  find_widget<image>(&win, "logo", false).set_image(game_config::images::game_logo);
242 
243  //
244  // Version string
245  //
246  const std::string version_string = formatter() << ("Version") << " " << game_config::revision;
247 
248  if(label* version_label = find_widget<label>(&win, "revision_number", false, false)) {
249  version_label->set_label(version_string);
250  }
251 
252  win.get_canvas(0).set_variable("revision_number", wfl::variant(version_string));
253 
254  //
255  // Tip-of-the-day browser
256  //
257  multi_page& tip_pages = find_widget<multi_page>(&win, "tips", false);
258 
259  std::vector<game_tip> tips = tip_of_the_day::shuffle(settings::tips);
260  if(tips.empty()) {
261  WRN_CF << "There are no tips of day available." << std::endl;
262  }
263 
264  for(const auto& tip : tips) {
266  std::map<std::string, string_map> page;
267 
268  widget["use_markup"] = "true";
269 
270  widget["label"] = tip.text();
271  page.emplace("tip", widget);
272 
273  widget["label"] = tip.source();
274  page.emplace("source", widget);
275 
276  tip_pages.add_page(page);
277  }
278 
279  update_tip(win, true);
280 
282  std::bind(&title_screen::update_tip, this, std::ref(win), true));
283 
285  std::bind(&title_screen::update_tip, this, std::ref(win), false));
286 
287  //
288  // Help
289  //
290  register_button(win, "help", hotkey::HOTKEY_HELP, []() {
291  if(gui2::new_widgets) {
292  gui2::dialogs::help_browser::display();
293  }
294 
296  help::show_help();
297  });
298 
299  //
300  // About
301  //
302  register_button(win, "about", hotkey::HOTKEY_NULL, std::bind(&game_version::display<>));
303 
304  //
305  // Tutorial
306  //
307  register_button(win, "tutorial", hotkey::TITLE_SCREEN__TUTORIAL, [this, &win]() {
309  win.set_retval(LAUNCH_GAME);
310  });
311 
312  //
313  // Campaign
314  //
315  register_button(win, "campaign", hotkey::TITLE_SCREEN__CAMPAIGN, [this, &win]() {
316  try{
317  if(game_.new_campaign()) {
318  win.set_retval(LAUNCH_GAME);
319  }
320  } catch (const config::error& e) {
322  }
323  });
324 
325  //
326  // Multiplayer
327  //
329  std::bind(&title_screen::button_callback_multiplayer, this, std::ref(win)));
330 
331  //
332  // Load game
333  //
334  register_button(win, "load", hotkey::HOTKEY_LOAD_GAME, [this, &win]() {
335  if(game_.load_game()) {
336  win.set_retval(LAUNCH_GAME);
337  } else {
339  }
340  });
341 
342  //
343  // Addons
344  //
345  register_button(win, "addons", hotkey::TITLE_SCREEN__ADDONS, []() {
346  // NOTE: we need the help_manager to get access to the Add-ons section in the game help!
348 
349  if(manage_addons()) {
351  }
352  });
353 
354  //
355  // Editor
356  //
357  register_button(win, "editor", hotkey::TITLE_SCREEN__EDITOR, [&win]() { win.set_retval(MAP_EDITOR); });
358 
359  //
360  // Cores
361  //
363  std::bind(&title_screen::button_callback_cores, this));
364 
365  if(game_config_manager::get()->game_config().child_range("core").size() <= 1) {
366  find_widget<button>(&win, "cores", false).set_visible(widget::visibility::invisible);
367  }
368 
369  //
370  // Language
371  //
372  register_button(win, "language", hotkey::HOTKEY_LANGUAGE, [this, &win]() {
373  try {
374  if(game_.change_language()) {
377  on_resize(win);
378  }
379  } catch(std::runtime_error& e) {
380  gui2::show_error_message(e.what());
381  }
382  });
383 
384  //
385  // Preferences
386  //
387  register_button(win, "preferences", hotkey::HOTKEY_PREFERENCES, [this]() { game_.show_preferences(); });
388 
389  //
390  // Credits
391  //
392  register_button(win, "credits", hotkey::TITLE_SCREEN__CREDITS, [&win]() { win.set_retval(SHOW_ABOUT); });
393 
394  //
395  // Quit
396  //
397  register_button(win, "quit", hotkey::HOTKEY_QUIT_TO_DESKTOP, [&win]() { win.set_retval(QUIT_GAME); });
398 
399  //
400  // Debug clock
401  //
402  register_button(win, "clock", hotkey::HOTKEY_NULL,
403  std::bind(&title_screen::show_debug_clock_window, this));
404 
405  find_widget<button>(&win, "clock", false).set_visible(show_debug_clock_button
408 }
409 
411 {
413 }
414 
415 void title_screen::update_tip(window& win, const bool previous)
416 {
417  multi_page& tips = find_widget<multi_page>(&win, "tips", false);
418  if(tips.get_page_count() == 0) {
419  return;
420  }
421 
422  int page = tips.get_selected_page();
423  if(previous) {
424  if(page <= 0) {
425  page = tips.get_page_count();
426  }
427  --page;
428  } else {
429  ++page;
430  if(static_cast<unsigned>(page) >= tips.get_page_count()) {
431  page = 0;
432  }
433  }
434 
435  tips.select_page(page);
436 
437  /**
438  * @todo Look for a proper fix.
439  *
440  * This dirtying is required to avoid the blurring to be rendered wrong.
441  * Not entirely sure why, but since we plan to move to SDL2 that change
442  * will probably fix this issue automatically.
443  */
444  win.set_is_dirty(true);
445 }
446 
448 {
449  assert(show_debug_clock_button);
450 
451  if(debug_clock_) {
452  debug_clock_.reset(nullptr);
453  } else {
454  debug_clock_.reset(new debug_clock());
455  debug_clock_->show(true);
456  }
457 }
458 
460 {
462 
463  std::vector<std::string> options;
464  for(const config &sc : game_config_manager::get()->game_config().child_range("test")) {
465  if(!sc["is_unit_test"].to_bool(false)) {
466  options.emplace_back(sc["id"]);
467  }
468  }
469 
470  std::sort(options.begin(), options.end());
471 
472  gui2::dialogs::simple_item_selector dlg(_("Choose Test"), "", options);
473  dlg.show();
474 
475  int choice = dlg.selected_index();
476  if(choice >= 0) {
477  game_.set_test(options[choice]);
478  window.set_retval(LAUNCH_GAME);
479  }
480 }
481 
483 {
484  while(true) {
486  dlg.show();
487 
488  if(dlg.get_retval() != gui2::window::OK) {
489  return;
490  }
491 
492  const int res = dlg.get_choice();
493 
494  if(res == 2 && preferences::mp_server_warning_disabled() < 2) {
495  if(!gui2::dialogs::mp_host_game_prompt::execute()) {
496  continue;
497  }
498  }
499 
500  switch(res) {
501  case 0:
503  window.set_retval(MP_CONNECT);
504  break;
505  case 1:
507  window.set_retval(MP_CONNECT);
508  break;
509  case 2:
510  game_.select_mp_server("localhost");
511  window.set_retval(MP_HOST);
512  break;
513  case 3:
514  window.set_retval(MP_LOCAL);
515  break;
516  }
517 
518  return;
519  }
520 }
521 
523 {
524  int current = 0;
525 
526  std::vector<config> cores;
527  for(const config& core : game_config_manager::get()->game_config().child_range("core")) {
528  cores.push_back(core);
529 
530  if(core["id"] == preferences::core_id()) {
531  current = cores.size() - 1;
532  }
533  }
534 
535  gui2::dialogs::core_selection core_dlg(cores, current);
536  if(core_dlg.show()) {
537  const std::string& core_id = cores[core_dlg.get_choice()]["id"];
538 
539  preferences::set_core_id(core_id);
541  }
542 }
543 
544 } // namespace dialogs
545 } // 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.
Dialog is closed with ok button.
Definition: window.hpp:101
static void reset_translations()
Definition: tstring.cpp:654
const char * what() const NOEXCEPT
Definition: exceptions.hpp:37
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< char_t > string
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:188
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...
unsigned get_page_count() const
Returns the number of pages.
Definition: multi_page.cpp:100
Base class for all widgets.
Definition: widget.hpp:48
This class implements the title screen.
void set_click_dismiss(const bool click_dismiss)
Definition: window.hpp:409
void set_escape_disabled(const bool escape_disabled)
Disable the escape key.
Definition: window.hpp:328
bool change_language()
Label showing a text.
Definition: label.hpp:32
bool show(const unsigned auto_close_time=0)
Shows the window.
-file util.hpp
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:211
Generic file dialog.
Definition: field-fwd.hpp:22
const wesnothd::game & game_
Definition: game.cpp:1634
const config & options()
Definition: game.cpp:570
static game_config_manager * get()
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
This file contains the settings handling of the widget library.
std::ostringstream wrapper.
Definition: formatter.hpp:38
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:218
std::string game_title_background
const std::vector< game_config::server_info > & server_list()
Definition: game.cpp:369
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
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.
CVideo & video()
Definition: window.hpp:381
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
int mp_server_warning_disabled()
Definition: game.cpp:503
const std::string revision
Definition: game_config.cpp:50
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:53
void toggle_fullscreen()
Definition: video.cpp:552
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
size_t i
Definition: function.cpp:933
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)
int get_selected_page() const
Returns the selected page.
Definition: multi_page.cpp:112
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:392
static int sort(lua_State *L)
Definition: ltablib.cpp:411
std::string core_id()
Definition: general.cpp:315
The user sets the widget visible, that means:
The multi page class.
Definition: multi_page.hpp:35
utils::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:362
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.
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:93
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:62
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:315