The Battle for Wesnoth  1.15.12+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"
40 #include "log.hpp"
41 #include "preferences/game.hpp"
42 //#define DEBUG_TOOLTIP
43 #ifdef DEBUG_TOOLTIP
44 #include "gui/dialogs/tooltip.hpp"
45 #endif
46 #include "gui/widgets/button.hpp"
47 #include "gui/widgets/image.hpp"
48 #include "gui/widgets/label.hpp"
50 #include "gui/widgets/settings.hpp"
51 #include "gui/widgets/window.hpp"
52 #include "help/help.hpp"
54 #include "sdl/surface.hpp"
55 #include "sdl/utils.hpp"
56 #include "video.hpp"
57 
58 #include <algorithm>
59 #include <functional>
60 
61 static lg::log_domain log_config("config");
62 #define ERR_CF LOG_STREAM(err, log_config)
63 #define WRN_CF LOG_STREAM(warn, log_config)
64 
65 namespace gui2::dialogs
66 {
67 
68 REGISTER_DIALOG(title_screen)
69 
71 
73  : debug_clock_()
74  , game_(game)
75 {
76  set_restore(false);
77 
78  // Need to set this in the constructor, pre_show() / post_build() is too late
79  set_allow_plugin_skip(false);
80 }
81 
83 {
84 }
85 
86 using btn_callback = std::function<void()>;
87 
88 static void register_button(window& win, const std::string& id, hotkey::HOTKEY_COMMAND hk, btn_callback callback)
89 {
90  if(hk != hotkey::HOTKEY_NULL) {
91  win.register_hotkey(hk, std::bind(callback));
92  }
93 
94  auto b = find_widget<button>(&win, id, false, false);
95  if(b != nullptr)
96  {
97  connect_signal_mouse_left_click(*b, std::bind(callback));
98  }
99 }
100 
101 static void launch_lua_console()
102 {
104 }
105 
106 static void make_screenshot()
107 {
108  surface screenshot = CVideo::get_singleton().getSurface().clone();
109  if(screenshot) {
110  std::string filename = filesystem::get_screenshot_dir() + "/" + _("Screenshot") + "_";
111  filename = filesystem::get_next_filename(filename, ".jpg");
112  gui2::dialogs::screenshot_notification::display(filename, screenshot);
113  }
114 }
115 
116 #ifdef DEBUG_TOOLTIP
117 /*
118  * This function is used to test the tooltip placement algorithms as
119  * described in the »Tooltip placement« section in the GUI2 design
120  * document.
121  *
122  * Use a 1024 x 768 screen size, set the maximum loop iteration to:
123  * - 0 to test with a normal tooltip placement.
124  * - 30 to test with a larger normal tooltip placement.
125  * - 60 to test with a huge tooltip placement.
126  * - 150 to test with a borderline to insanely huge tooltip placement.
127  * - 180 to test with an insanely huge tooltip placement.
128  */
129 static void debug_tooltip(window& /*window*/, bool& handled, const point& coordinate)
130 {
131  std::string message = "Hello world.";
132 
133  for(int i = 0; i < 0; ++i) {
134  message += " More greetings.";
135  }
136 
138  gui2::tip::show("tooltip", message, coordinate);
139 
140  handled = true;
141 }
142 #endif
143 
145 {
146  win.set_click_dismiss(false);
147  win.set_enter_disabled(true);
148  win.set_escape_disabled(true);
149 
150 #ifdef DEBUG_TOOLTIP
152  std::bind(debug_tooltip, std::ref(win), std::placeholders::_3, std::placeholders::_5),
154 #endif
155 
157 
158  //
159  // General hotkeys
160  //
162  std::bind(&gui2::window::set_retval, std::ref(win), RELOAD_GAME_DATA, true));
163 
166 
167  // A wrapper is needed here since the relevant display function is overloaded, and
168  // since the wrapper's signature doesn't exactly match what register_hotkey expects.
170 
172 
173  //
174  // Background and logo images
175  //
176  if(game_config::images::game_title.empty()) {
177  ERR_CF << "No title image defined" << std::endl;
178  }
179 
181 
183  ERR_CF << "No title background image defined" << std::endl;
184  }
185 
187 
188  find_widget<image>(&win, "logo-bg", false).set_image(game_config::images::game_logo_background);
189  find_widget<image>(&win, "logo", false).set_image(game_config::images::game_logo);
190 
191  //
192  // Version string
193  //
194  const std::string& version_string = VGETTEXT("Version $version", {{ "version", game_config::revision }});
195 
196  if(label* version_label = find_widget<label>(&win, "revision_number", false, false)) {
197  version_label->set_label(version_string);
198  }
199 
200  win.get_canvas(0).set_variable("revision_number", wfl::variant(version_string));
201 
202  //
203  // Tip-of-the-day browser
204  //
205  multi_page* tip_pages = find_widget<multi_page>(&win, "tips", false, false);
206 
207  if(tip_pages != nullptr) {
208  std::vector<game_tip> tips = tip_of_the_day::shuffle(settings::tips);
209  if(tips.empty()) {
210  WRN_CF << "There are no tips of day available." << std::endl;
211  }
212  for(const auto& tip : tips) {
214  std::map<std::string, string_map> page;
215 
216  widget["use_markup"] = "true";
217 
218  widget["label"] = tip.text();
219  page.emplace("tip", widget);
220 
221  widget["label"] = tip.source();
222  page.emplace("source", widget);
223 
224  tip_pages->add_page(page);
225  }
226 
227  update_tip(true);
228  }
229 
231  std::bind(&title_screen::update_tip, this, true));
232 
234  std::bind(&title_screen::update_tip, this, false));
235 
236  //
237  // Help
238  //
239  register_button(win, "help", hotkey::HOTKEY_HELP, []() {
240  if(gui2::new_widgets) {
241  gui2::dialogs::help_browser::display();
242  }
243 
245  help::show_help();
246  });
247 
248  //
249  // About
250  //
251  register_button(win, "about", hotkey::HOTKEY_NULL, std::bind(&game_version::display<>));
252 
253  //
254  // Campaign
255  //
256  register_button(win, "campaign", hotkey::TITLE_SCREEN__CAMPAIGN, [this, &win]() {
257  try{
258  if(game_.new_campaign()) {
259  win.set_retval(LAUNCH_GAME);
260  }
261  } catch (const config::error& e) {
263  }
264  });
265 
266  //
267  // Multiplayer
268  //
271 
272  //
273  // Load game
274  //
275  register_button(win, "load", hotkey::HOTKEY_LOAD_GAME, [this, &win]() {
276  if(game_.load_game()) {
277  win.set_retval(LAUNCH_GAME);
278  }
279  });
280 
281  //
282  // Addons
283  //
284  register_button(win, "addons", hotkey::TITLE_SCREEN__ADDONS, [&win]() {
285  // NOTE: we need the help_manager to get access to the Add-ons section in the game help!
287 
288  if(manage_addons()) {
289  win.set_retval(RELOAD_GAME_DATA);
290  }
291  });
292 
293  //
294  // Editor
295  //
296  register_button(win, "editor", hotkey::TITLE_SCREEN__EDITOR, [&win]() { win.set_retval(MAP_EDITOR); });
297 
298  //
299  // Cores
300  //
301  win.register_hotkey(hotkey::TITLE_SCREEN__CORES,
302  std::bind(&title_screen::button_callback_cores, this));
303 
304  //
305  // Language
306  //
307  register_button(win, "language", hotkey::HOTKEY_LANGUAGE, [this, &win]() {
308  try {
309  if(game_.change_language()) {
310  on_resize();
311  }
312  } catch(const std::runtime_error& e) {
313  gui2::show_error_message(e.what());
314  }
315  });
316 
317  //
318  // Preferences
319  //
320  register_button(win, "preferences", hotkey::HOTKEY_PREFERENCES, []() {
321  gui2::dialogs::preferences_dialog::display();
322  });
323 
324  //
325  // Credits
326  //
327  register_button(win, "credits", hotkey::TITLE_SCREEN__CREDITS, [&win]() { win.set_retval(SHOW_ABOUT); });
328 
329  //
330  // Quit
331  //
332  register_button(win, "quit", hotkey::HOTKEY_QUIT_TO_DESKTOP, [&win]() { win.set_retval(QUIT_GAME); });
333  // A sanity check, exit immediately if the .cfg file didn't have a "quit" button.
334  find_widget<button>(&win, "quit", false, true);
335 
336  //
337  // Debug clock
338  //
339  register_button(win, "clock", hotkey::HOTKEY_NULL,
340  std::bind(&title_screen::show_debug_clock_window, this));
341 
342  auto clock = find_widget<button>(&win, "clock", false, false);
343  if(clock) {
345  }
346 }
347 
349 {
351 }
352 
353 void title_screen::update_tip(const bool previous)
354 {
355  multi_page* tip_pages = find_widget<multi_page>(get_window(), "tips", false, false);
356  if(tip_pages == nullptr) {
357  return;
358  }
359  if(tip_pages->get_page_count() == 0) {
360  return;
361  }
362 
363  int page = tip_pages->get_selected_page();
364  if(previous) {
365  if(page <= 0) {
366  page = tip_pages->get_page_count();
367  }
368  --page;
369  } else {
370  ++page;
371  if(static_cast<unsigned>(page) >= tip_pages->get_page_count()) {
372  page = 0;
373  }
374  }
375 
376  tip_pages->select_page(page);
377 
378  /**
379  * @todo Look for a proper fix.
380  *
381  * This dirtying is required to avoid the blurring to be rendered wrong.
382  * Not entirely sure why, but since we plan to move to SDL2 that change
383  * will probably fix this issue automatically.
384  */
385  get_window()->set_is_dirty(true);
386 }
387 
389 {
390  assert(show_debug_clock_button);
391 
392  if(debug_clock_) {
393  debug_clock_.reset(nullptr);
394  } else {
395  debug_clock_.reset(new debug_clock());
396  debug_clock_->show(true);
397  }
398 }
399 
401 {
403 
404  std::vector<std::string> options;
405  for(const config &sc : game_config_manager::get()->game_config().child_range("test")) {
406  if(!sc["is_unit_test"].to_bool(false)) {
407  options.emplace_back(sc["id"]);
408  }
409  }
410 
411  std::sort(options.begin(), options.end());
412 
413  gui2::dialogs::simple_item_selector dlg(_("Choose Test"), "", options);
414  dlg.show();
415 
416  int choice = dlg.selected_index();
417  if(choice >= 0) {
418  game_.set_test(options[choice]);
420  }
421 }
422 
424 {
425  while(true) {
427  dlg.show();
428 
429  if(dlg.get_retval() != gui2::retval::OK) {
430  return;
431  }
432 
433  const auto res = dlg.get_choice();
434 
435  if(res == decltype(dlg)::choice::HOST && preferences::mp_server_warning_disabled() < 2) {
436  if(!gui2::dialogs::mp_host_game_prompt::execute()) {
437  continue;
438  }
439  }
440 
441  switch(res) {
442  case decltype(dlg)::choice::JOIN:
445  break;
446  case decltype(dlg)::choice::CONNECT:
449  break;
450  case decltype(dlg)::choice::HOST:
451  game_.select_mp_server("localhost");
453  break;
454  case decltype(dlg)::choice::LOCAL:
456  break;
457  }
458 
459  return;
460  }
461 }
462 
464 {
465  int current = 0;
466 
467  std::vector<config> cores;
468  for(const config& core : game_config_manager::get()->game_config().child_range("core")) {
469  cores.push_back(core);
470 
471  if(core["id"] == preferences::core_id()) {
472  current = cores.size() - 1;
473  }
474  }
475 
476  gui2::dialogs::core_selection core_dlg(cores, current);
477  if(core_dlg.show()) {
478  const std::string& core_id = cores[core_dlg.get_choice()]["id"];
479 
480  preferences::set_core_id(core_id);
482  }
483 }
484 
485 } // namespace dialogs
bool new_widgets
Do we wish to use the new library or not.
Definition: settings.cpp:24
void remove()
Removes a tip.
Definition: tooltip.cpp:174
void show_debug_clock_window()
Shows the debug clock.
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:114
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:490
std::vector< game_tip > tips
Definition: settings.cpp:49
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:141
grid & add_page(const string_map &item)
Adds single page to the grid.
Definition: multi_page.cpp:43
#define WRN_CF
void set_variable(const std::string &key, const wfl::variant &value)
Definition: canvas.hpp:167
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:49
This class implements the title screen.
void set_click_dismiss(const bool click_dismiss)
Definition: window.hpp:374
std::string get_screenshot_dir()
void set_escape_disabled(const bool escape_disabled)
Disable the escape key.
Definition: window.hpp:298
static CVideo & get_singleton()
Definition: video.hpp:48
window * get_window() const
Returns a pointer to the dialog&#39;s window.
bool change_language()
A label displays a text, the text can be wrapped but no scrollbars are provided.
Definition: label.hpp:56
int get_selected_page() const
Returns the selected page.
Definition: multi_page.cpp:115
static std::string _(const char *str)
Definition: gettext.hpp:92
bool show(const unsigned auto_close_time=0)
Shows the window.
static void launch_lua_console()
surface clone() const
Makes a copy of this surface.
Definition: surface.cpp:62
surface & getSurface()
Returns a reference to the framebuffer.
Definition: video.cpp:482
static void display(lua_kernel_base *lk)
Display a new console, using given video and lua kernel.
#define b
const config & options()
Definition: game.cpp:563
static game_config_manager * get()
const std::vector< game_config::server_info > & builtin_servers_list()
Definition: game.cpp:364
A simple one-column listbox with OK and Cancel buttons.
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:171
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:465
std::string game_title_background
void set_test(const std::string &id)
void set_core_id(const std::string &core_id)
Definition: general.cpp:323
void select_mp_server(const std::string &server)
static lg::log_domain log_config("config")
std::string game_logo_background
const char * what() const noexcept
Definition: exceptions.hpp:35
static tooltip & tip()
Definition: tooltip.cpp:129
bool manage_addons()
Shows the add-ons server connection dialog, for access to the various management front-ends.
Definition: manager_ui.cpp:228
int mp_server_warning_disabled()
Definition: game.cpp:499
std::size_t i
Definition: function.cpp:940
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:505
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:48
Game configuration data as global variables.
Definition: build_info.cpp:58
The user set the widget invisible, that means:
void select_page(const unsigned page, const bool select=true)
Selects a page.
Definition: multi_page.cpp:106
Contains the gui2 timer routines.
std::map< std::string, t_string > string_map
Definition: widget.hpp:26
Holds a 2D point.
Definition: point.hpp:23
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:357
static int sort(lua_State *L)
Definition: ltablib.cpp:397
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
std::string core_id()
Definition: general.cpp:317
unsigned get_page_count() const
Returns the number of pages.
Definition: multi_page.cpp:100
int get_retval() const
Returns the cached window exit code.
The user sets the widget visible, that means:
This shows the dialog to select the kind of MP game the user wants to play.
A multi page is a control that contains several &#39;pages&#39; of which only one is visible.
Definition: multi_page.hpp:48
void update_tip(const bool previous)
Updates the tip of day widget.
Standard logging facilities (interface).
std::string game_logo
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
This shows the dialog which allows the user to choose which core to play.
#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
Actions to be taken before showing the window.
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:59
canvas & get_canvas(const unsigned index)
static void make_screenshot()
void set_retval(int retval)
Convenience wrapper to set the window&#39;s exit code.
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
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:139
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:285