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