1 | /**************************************************************************/ |
2 | /* progress_dialog.h */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #ifndef PROGRESS_DIALOG_H |
32 | #define PROGRESS_DIALOG_H |
33 | |
34 | #include "scene/gui/box_container.h" |
35 | #include "scene/gui/button.h" |
36 | #include "scene/gui/label.h" |
37 | #include "scene/gui/popup.h" |
38 | #include "scene/gui/progress_bar.h" |
39 | |
40 | class BackgroundProgress : public HBoxContainer { |
41 | GDCLASS(BackgroundProgress, HBoxContainer); |
42 | |
43 | _THREAD_SAFE_CLASS_ |
44 | |
45 | struct Task { |
46 | HBoxContainer *hb = nullptr; |
47 | ProgressBar *progress = nullptr; |
48 | }; |
49 | |
50 | HashMap<String, Task> tasks; |
51 | HashMap<String, int> updates; |
52 | void _update(); |
53 | |
54 | protected: |
55 | void _add_task(const String &p_task, const String &p_label, int p_steps); |
56 | void _task_step(const String &p_task, int p_step = -1); |
57 | void _end_task(const String &p_task); |
58 | |
59 | static void _bind_methods(); |
60 | |
61 | public: |
62 | void add_task(const String &p_task, const String &p_label, int p_steps); |
63 | void task_step(const String &p_task, int p_step = -1); |
64 | void end_task(const String &p_task); |
65 | |
66 | BackgroundProgress() {} |
67 | }; |
68 | |
69 | class ProgressDialog : public PopupPanel { |
70 | GDCLASS(ProgressDialog, PopupPanel); |
71 | struct Task { |
72 | String task; |
73 | VBoxContainer *vb = nullptr; |
74 | ProgressBar *progress = nullptr; |
75 | Label *state = nullptr; |
76 | }; |
77 | HBoxContainer *cancel_hb = nullptr; |
78 | Button *cancel = nullptr; |
79 | |
80 | HashMap<String, Task> tasks; |
81 | VBoxContainer *main = nullptr; |
82 | uint64_t last_progress_tick; |
83 | |
84 | static ProgressDialog *singleton; |
85 | void (); |
86 | |
87 | void _cancel_pressed(); |
88 | bool canceled = false; |
89 | |
90 | protected: |
91 | static void _bind_methods(); |
92 | |
93 | public: |
94 | static ProgressDialog *get_singleton() { return singleton; } |
95 | void add_task(const String &p_task, const String &p_label, int p_steps, bool p_can_cancel = false); |
96 | bool task_step(const String &p_task, const String &p_state, int p_step = -1, bool p_force_redraw = true); |
97 | void end_task(const String &p_task); |
98 | |
99 | ProgressDialog(); |
100 | }; |
101 | |
102 | #endif // PROGRESS_DIALOG_H |
103 | |