1 /*
2    Copyright (C) 2016 - 2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY.
10 
11    See the COPYING file for more details.
12 */
13 
14 #pragma once
15 
16 #include "gui/dialogs/modal_dialog.hpp"
17 #include "tstring.hpp"
18 
19 #include <map>
20 #include <vector>
21 #include <atomic>
22 
23 namespace boost
24 {
25 	class thread;
26 }
27 namespace cursor
28 {
29 	struct setter;
30 }
31 
32 /**
33  * Loading screen stage IDs.
34  * When adding new entries here, don't forget to update the stage_names
35  * map with an appropriate description.
36  */
37 enum class loading_stage
38 {
39 	build_terrain,
40 	create_cache,
41 	init_display,
42 	init_fonts,
43 	init_teams,
44 	init_theme,
45 	load_config,
46 	load_data,
47 	load_level,
48 	init_lua,
49 	init_whiteboard,
50 	load_unit_types,
51 	load_units,
52 	refresh_addons,
53 	start_game,
54 	verify_cache,
55 	connect_to_server,
56 	login_response,
57 	waiting,
58 	redirect,
59 	next_scenario,
60 	download_level_data,
61 	download_lobby_data,
62 	none,
63 };
64 
65 namespace gui2
66 {
67 
68 class label;
69 class window;
70 
71 namespace dialogs
72 {
73 class loading_screen : public modal_dialog
74 {
75 public:
76 	loading_screen(std::function<void()> f);
77 
78 	~loading_screen();
79 
80 	static void display(std::function<void()> f);
displaying()81 	static bool displaying() { return current_load != nullptr; }
82 
83 	static void progress(loading_stage stage = loading_stage::none);
84 
85 private:
86 	size_t timer_id_;
87 	int animation_counter_;
88 	std::function<void()> work_;
89 	std::unique_ptr<boost::thread> worker_;
90 	std::unique_ptr<cursor::setter> cursor_setter_;
91 	std::exception_ptr exception_;
92 	void clear_timer();
93 
94 	virtual const std::string& window_id() const override;
95 
96 	void timer_callback(window& window);
97 
98 	/** Inherited from modal_dialog. */
99 	virtual void pre_show(window& window) override;
100 
101 	/** Inherited from modal_dialog. */
102 	virtual void post_show(window& window) override;
103 
104 	label* progress_stage_label_;
105 	label* animation_label_;
106 	static loading_screen* current_load;
107 
108 	std::atomic<loading_stage> current_stage_;
109 
110 	using stage_map = std::map<loading_stage, t_string>;
111 
112 	stage_map visible_stages_;
113 	std::vector<t_string> animation_stages_;
114 	stage_map::const_iterator current_visible_stage_;
115 
116 	bool is_worker_running_;
117 };
118 
119 } // namespace dialogs
120 } // namespace gui2
121