The Battle for Wesnoth  1.17.12+dev
loading_screen.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2022
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,
48  load_config,
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 
81  ~loading_screen();
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
stage_map::const_iterator current_visible_stage_
std::atomic< loading_stage > current_stage_
std::vector< std::function< void()> > load_funcs_
A drawing is widget with a fixed size and gives access to the canvas of the widget in the window inst...
Definition: drawing.hpp:52
A label displays a text, the text can be wrapped but no scrollbars are provided.
Definition: label.hpp:57
static void layout()
Generic file dialog.
loading_stage
Loading screen stage IDs.
void process(int mousex, int mousey)
Definition: tooltips.cpp:278
std::string label
What to show in the filter&#39;s drop-down list.
Definition: manager.cpp:217
std::future< void > worker_result_
Various uncategorised dialogs.
std::optional< decltype(std::chrono::steady_clock::now())> animation_start_
std::map< loading_stage, t_string > stage_map
std::unique_ptr< cursor::setter > cursor_setter_
static loading_screen * singleton_
#define f
Abstract base class for all modal dialogs.
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:66