The Battle for Wesnoth  1.17.17+dev
loading_screen.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2023
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 #pragma once
16 
19 
20 #include "events.hpp"
21 #include "tstring.hpp"
22 
23 #include <atomic>
24 #include <chrono>
25 #include <future>
26 #include <map>
27 #include <optional>
28 #include <vector>
29 
30 namespace cursor
31 {
32  struct setter;
33 }
34 
35 /**
36  * Loading screen stage IDs.
37  * When adding new entries here, don't forget to update the stage_names
38  * map with an appropriate description.
39  */
40 enum class loading_stage
41 {
45  init_fonts,
46  init_teams,
47  init_theme,
49  load_data,
50  load_level,
51  init_lua,
54  load_units,
56  start_game,
60  waiting,
61  redirect,
65  none,
66 };
67 
68 namespace gui2
69 {
70 class drawing;
71 class label;
72 class window;
73 
74 namespace dialogs
75 {
77 {
78 public:
79  loading_screen(std::function<void()> f);
80 
82 
83  static void display(std::function<void()> f);
84  static bool displaying() { return singleton_ != nullptr; }
85 
86  /**
87  * Report what is being loaded to the loading screen.
88  *
89  * Also processes any pending events and draw calls.
90  *
91  * This should be called before commencing each loading stage.
92  *
93  * @param stage Which loading stage the caller is about to perform.
94  */
95  static void progress(loading_stage stage = loading_stage::none);
96 
97  /**
98  * Indicate to the player that loading is progressing.
99  *
100  * Calling this function is necessary to allow loading screen animations
101  * to run, and input events to be processed. It should be placed
102  * inside any loading loops that may take significant time.
103  *
104  * There is an internal guard against acting too frequently, so there
105  * should be little need to limit calls to this function.
106  *
107  * If a loading screen is not currently being shown, this function does
108  * nothing.
109  */
110  static void spin();
111 
112  /**
113  * Raise the loading screen to the top of the draw stack.
114  *
115  * This can be called if another TLD has been created during loading,
116  * such as happens with the game display.
117  */
118  static void raise();
119 
120 private:
121  virtual const std::string& window_id() const override;
122 
123  virtual void pre_show(window& window) override;
124 
125  virtual void post_show(window& window) override;
126 
127  /** Inherited from events::pump_monitor. */
128  virtual void process(events::pump_info&) override;
129 
130  /** Called by draw_manager to assign concrete layout. */
131  virtual void layout() override;
132 
134 
135  std::vector<std::function<void()>> load_funcs_;
136  std::future<void> worker_result_;
137  std::unique_ptr<cursor::setter> cursor_setter_;
138 
141 
142  std::optional<decltype(std::chrono::steady_clock::now())> animation_start_;
143 
144  std::atomic<loading_stage> current_stage_;
145 
146  using stage_map = std::map<loading_stage, t_string>;
148  stage_map::const_iterator current_visible_stage_;
149 
150  bool running_;
151 };
152 
153 } // namespace dialogs
154 } // namespace gui2
std::future< void > worker_result_
static void progress(loading_stage stage=loading_stage::none)
Report what is being loaded to the loading screen.
std::optional< decltype(std::chrono::steady_clock::now())> animation_start_
virtual const std::string & window_id() const override
The ID of the window to build.
virtual void layout() override
Called by draw_manager to assign concrete layout.
std::atomic< loading_stage > current_stage_
static loading_screen * singleton_
virtual void process(events::pump_info &) override
Inherited from events::pump_monitor.
static void display(std::function< void()> f)
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
std::vector< std::function< void()> > load_funcs_
std::unique_ptr< cursor::setter > cursor_setter_
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
stage_map::const_iterator current_visible_stage_
std::map< loading_stage, t_string > stage_map
loading_screen(std::function< void()> f)
static void spin()
Indicate to the player that loading is progressing.
Abstract base class for all modal dialogs.
A drawing is widget with a fixed size and gives access to the canvas of the widget in the window inst...
Definition: drawing.hpp:53
A label displays a text, the text can be wrapped but no scrollbars are provided.
Definition: label.hpp:58
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:67
std::string label
What to show in the filter's drop-down list.
Definition: manager.cpp:217
loading_stage
Loading screen stage IDs.
Various uncategorised dialogs.
Generic file dialog.
#define f