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