| 1 | /**************************************************************************/ |
| 2 | /* progress_dialog.cpp */ |
| 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 | #include "progress_dialog.h" |
| 32 | |
| 33 | #include "core/object/message_queue.h" |
| 34 | #include "core/os/os.h" |
| 35 | #include "editor/editor_interface.h" |
| 36 | #include "editor/editor_node.h" |
| 37 | #include "editor/editor_scale.h" |
| 38 | #include "main/main.h" |
| 39 | #include "servers/display_server.h" |
| 40 | |
| 41 | void BackgroundProgress::_add_task(const String &p_task, const String &p_label, int p_steps) { |
| 42 | _THREAD_SAFE_METHOD_ |
| 43 | ERR_FAIL_COND_MSG(tasks.has(p_task), "Task '" + p_task + "' already exists." ); |
| 44 | BackgroundProgress::Task t; |
| 45 | t.hb = memnew(HBoxContainer); |
| 46 | Label *l = memnew(Label); |
| 47 | l->set_text(p_label + " " ); |
| 48 | t.hb->add_child(l); |
| 49 | t.progress = memnew(ProgressBar); |
| 50 | t.progress->set_max(p_steps); |
| 51 | t.progress->set_value(p_steps); |
| 52 | Control *ec = memnew(Control); |
| 53 | ec->set_h_size_flags(SIZE_EXPAND_FILL); |
| 54 | ec->set_v_size_flags(SIZE_EXPAND_FILL); |
| 55 | t.progress->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 56 | ec->add_child(t.progress); |
| 57 | ec->set_custom_minimum_size(Size2(80, 5) * EDSCALE); |
| 58 | t.hb->add_child(ec); |
| 59 | |
| 60 | add_child(t.hb); |
| 61 | |
| 62 | tasks[p_task] = t; |
| 63 | } |
| 64 | |
| 65 | void BackgroundProgress::_update() { |
| 66 | _THREAD_SAFE_METHOD_ |
| 67 | |
| 68 | for (const KeyValue<String, int> &E : updates) { |
| 69 | if (tasks.has(E.key)) { |
| 70 | _task_step(E.key, E.value); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | updates.clear(); |
| 75 | } |
| 76 | |
| 77 | void BackgroundProgress::_task_step(const String &p_task, int p_step) { |
| 78 | _THREAD_SAFE_METHOD_ |
| 79 | |
| 80 | ERR_FAIL_COND(!tasks.has(p_task)); |
| 81 | |
| 82 | Task &t = tasks[p_task]; |
| 83 | if (p_step < 0) { |
| 84 | t.progress->set_value(t.progress->get_value() + 1); |
| 85 | } else { |
| 86 | t.progress->set_value(p_step); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void BackgroundProgress::_end_task(const String &p_task) { |
| 91 | _THREAD_SAFE_METHOD_ |
| 92 | |
| 93 | ERR_FAIL_COND(!tasks.has(p_task)); |
| 94 | Task &t = tasks[p_task]; |
| 95 | |
| 96 | memdelete(t.hb); |
| 97 | tasks.erase(p_task); |
| 98 | } |
| 99 | |
| 100 | void BackgroundProgress::_bind_methods() { |
| 101 | ClassDB::bind_method("_add_task" , &BackgroundProgress::_add_task); |
| 102 | ClassDB::bind_method("_task_step" , &BackgroundProgress::_task_step); |
| 103 | ClassDB::bind_method("_end_task" , &BackgroundProgress::_end_task); |
| 104 | ClassDB::bind_method("_update" , &BackgroundProgress::_update); |
| 105 | } |
| 106 | |
| 107 | void BackgroundProgress::add_task(const String &p_task, const String &p_label, int p_steps) { |
| 108 | MessageQueue::get_singleton()->push_call(this, "_add_task" , p_task, p_label, p_steps); |
| 109 | } |
| 110 | |
| 111 | void BackgroundProgress::task_step(const String &p_task, int p_step) { |
| 112 | //this code is weird, but it prevents deadlock. |
| 113 | bool no_updates = true; |
| 114 | { |
| 115 | _THREAD_SAFE_METHOD_ |
| 116 | no_updates = updates.is_empty(); |
| 117 | } |
| 118 | |
| 119 | if (no_updates) { |
| 120 | MessageQueue::get_singleton()->push_call(this, "_update" ); |
| 121 | } |
| 122 | |
| 123 | { |
| 124 | _THREAD_SAFE_METHOD_ |
| 125 | updates[p_task] = p_step; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void BackgroundProgress::end_task(const String &p_task) { |
| 130 | MessageQueue::get_singleton()->push_call(this, "_end_task" , p_task); |
| 131 | } |
| 132 | |
| 133 | //////////////////////////////////////////////// |
| 134 | |
| 135 | ProgressDialog *ProgressDialog::singleton = nullptr; |
| 136 | |
| 137 | void ProgressDialog::() { |
| 138 | Size2 ms = main->get_combined_minimum_size(); |
| 139 | ms.width = MAX(500 * EDSCALE, ms.width); |
| 140 | |
| 141 | Ref<StyleBox> style = main->get_theme_stylebox(SNAME("panel" ), SNAME("PopupMenu" )); |
| 142 | ms += style->get_minimum_size(); |
| 143 | |
| 144 | main->set_offset(SIDE_LEFT, style->get_margin(SIDE_LEFT)); |
| 145 | main->set_offset(SIDE_RIGHT, -style->get_margin(SIDE_RIGHT)); |
| 146 | main->set_offset(SIDE_TOP, style->get_margin(SIDE_TOP)); |
| 147 | main->set_offset(SIDE_BOTTOM, -style->get_margin(SIDE_BOTTOM)); |
| 148 | |
| 149 | if (!is_inside_tree()) { |
| 150 | EditorInterface::get_singleton()->popup_dialog_centered(this, ms); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void ProgressDialog::add_task(const String &p_task, const String &p_label, int p_steps, bool p_can_cancel) { |
| 155 | if (MessageQueue::get_singleton()->is_flushing()) { |
| 156 | ERR_PRINT("Do not use progress dialog (task) while flushing the message queue or using call_deferred()!" ); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | ERR_FAIL_COND_MSG(tasks.has(p_task), "Task '" + p_task + "' already exists." ); |
| 161 | ProgressDialog::Task t; |
| 162 | t.vb = memnew(VBoxContainer); |
| 163 | VBoxContainer *vb2 = memnew(VBoxContainer); |
| 164 | t.vb->add_margin_child(p_label, vb2); |
| 165 | t.progress = memnew(ProgressBar); |
| 166 | t.progress->set_max(p_steps); |
| 167 | t.progress->set_value(p_steps); |
| 168 | vb2->add_child(t.progress); |
| 169 | t.state = memnew(Label); |
| 170 | t.state->set_clip_text(true); |
| 171 | vb2->add_child(t.state); |
| 172 | main->add_child(t.vb); |
| 173 | |
| 174 | tasks[p_task] = t; |
| 175 | if (p_can_cancel) { |
| 176 | cancel_hb->show(); |
| 177 | } else { |
| 178 | cancel_hb->hide(); |
| 179 | } |
| 180 | cancel_hb->move_to_front(); |
| 181 | canceled = false; |
| 182 | _popup(); |
| 183 | if (p_can_cancel) { |
| 184 | cancel->grab_focus(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | bool ProgressDialog::task_step(const String &p_task, const String &p_state, int p_step, bool p_force_redraw) { |
| 189 | ERR_FAIL_COND_V(!tasks.has(p_task), canceled); |
| 190 | |
| 191 | if (!p_force_redraw) { |
| 192 | uint64_t tus = OS::get_singleton()->get_ticks_usec(); |
| 193 | if (tus - last_progress_tick < 200000) { //200ms |
| 194 | return canceled; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | Task &t = tasks[p_task]; |
| 199 | if (p_step < 0) { |
| 200 | t.progress->set_value(t.progress->get_value() + 1); |
| 201 | } else { |
| 202 | t.progress->set_value(p_step); |
| 203 | } |
| 204 | |
| 205 | t.state->set_text(p_state); |
| 206 | last_progress_tick = OS::get_singleton()->get_ticks_usec(); |
| 207 | DisplayServer::get_singleton()->process_events(); |
| 208 | |
| 209 | #ifndef ANDROID_ENABLED |
| 210 | Main::iteration(); // this will not work on a lot of platforms, so it's only meant for the editor |
| 211 | #endif |
| 212 | return canceled; |
| 213 | } |
| 214 | |
| 215 | void ProgressDialog::end_task(const String &p_task) { |
| 216 | ERR_FAIL_COND(!tasks.has(p_task)); |
| 217 | Task &t = tasks[p_task]; |
| 218 | |
| 219 | memdelete(t.vb); |
| 220 | tasks.erase(p_task); |
| 221 | |
| 222 | if (tasks.is_empty()) { |
| 223 | hide(); |
| 224 | } else { |
| 225 | _popup(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void ProgressDialog::_cancel_pressed() { |
| 230 | canceled = true; |
| 231 | } |
| 232 | |
| 233 | void ProgressDialog::_bind_methods() { |
| 234 | } |
| 235 | |
| 236 | ProgressDialog::ProgressDialog() { |
| 237 | main = memnew(VBoxContainer); |
| 238 | add_child(main); |
| 239 | main->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 240 | set_exclusive(true); |
| 241 | set_flag(Window::FLAG_POPUP, false); |
| 242 | last_progress_tick = 0; |
| 243 | singleton = this; |
| 244 | cancel_hb = memnew(HBoxContainer); |
| 245 | main->add_child(cancel_hb); |
| 246 | cancel_hb->hide(); |
| 247 | cancel = memnew(Button); |
| 248 | cancel_hb->add_spacer(); |
| 249 | cancel_hb->add_child(cancel); |
| 250 | cancel->set_text(TTR("Cancel" )); |
| 251 | cancel_hb->add_spacer(); |
| 252 | cancel->connect("pressed" , callable_mp(this, &ProgressDialog::_cancel_pressed)); |
| 253 | } |
| 254 | |