progress_dialog.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #include "progress_dialog.h"
  31. #include "core/os/os.h"
  32. #include "editor/editor_interface.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/themes/editor_scale.h"
  35. #include "main/main.h"
  36. #include "scene/gui/panel_container.h"
  37. #include "scene/main/window.h"
  38. #include "servers/display/display_server.h"
  39. void BackgroundProgress::_add_task(const String &p_task, const String &p_label, int p_steps) {
  40. _THREAD_SAFE_METHOD_
  41. ERR_FAIL_COND_MSG(tasks.has(p_task), "Task '" + p_task + "' already exists.");
  42. BackgroundProgress::Task t;
  43. t.hb = memnew(HBoxContainer);
  44. Label *l = memnew(Label);
  45. l->set_text(p_label + " ");
  46. t.hb->add_child(l);
  47. t.progress = memnew(ProgressBar);
  48. t.progress->set_theme_type_variation("PopupProgressBar");
  49. t.progress->set_max(p_steps);
  50. t.progress->set_value(p_steps);
  51. Control *ec = memnew(Control);
  52. ec->set_h_size_flags(SIZE_EXPAND_FILL);
  53. ec->set_v_size_flags(SIZE_EXPAND_FILL);
  54. t.progress->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  55. ec->add_child(t.progress);
  56. ec->set_custom_minimum_size(Size2(80, 5) * EDSCALE);
  57. t.hb->add_child(ec);
  58. add_child(t.hb);
  59. tasks[p_task] = t;
  60. }
  61. void BackgroundProgress::_update() {
  62. _THREAD_SAFE_METHOD_
  63. for (const KeyValue<String, int> &E : updates) {
  64. if (tasks.has(E.key)) {
  65. _task_step(E.key, E.value);
  66. }
  67. }
  68. updates.clear();
  69. }
  70. void BackgroundProgress::_task_step(const String &p_task, int p_step) {
  71. _THREAD_SAFE_METHOD_
  72. ERR_FAIL_COND(!tasks.has(p_task));
  73. Task &t = tasks[p_task];
  74. if (p_step < 0) {
  75. t.progress->set_value(t.progress->get_value() + 1);
  76. } else {
  77. t.progress->set_value(p_step);
  78. }
  79. }
  80. void BackgroundProgress::_end_task(const String &p_task) {
  81. _THREAD_SAFE_METHOD_
  82. ERR_FAIL_COND(!tasks.has(p_task));
  83. Task &t = tasks[p_task];
  84. memdelete(t.hb);
  85. tasks.erase(p_task);
  86. }
  87. void BackgroundProgress::add_task(const String &p_task, const String &p_label, int p_steps) {
  88. callable_mp(this, &BackgroundProgress::_add_task).call_deferred(p_task, p_label, p_steps);
  89. }
  90. void BackgroundProgress::task_step(const String &p_task, int p_step) {
  91. //this code is weird, but it prevents deadlock.
  92. bool no_updates = true;
  93. {
  94. _THREAD_SAFE_METHOD_
  95. no_updates = updates.is_empty();
  96. }
  97. if (no_updates) {
  98. callable_mp(this, &BackgroundProgress::_update).call_deferred();
  99. }
  100. {
  101. _THREAD_SAFE_METHOD_
  102. updates[p_task] = p_step;
  103. }
  104. }
  105. void BackgroundProgress::end_task(const String &p_task) {
  106. callable_mp(this, &BackgroundProgress::_end_task).call_deferred(p_task);
  107. }
  108. ////////////////////////////////////////////////
  109. ProgressDialog *ProgressDialog::singleton = nullptr;
  110. void ProgressDialog::_notification(int p_what) {
  111. switch (p_what) {
  112. case NOTIFICATION_THEME_CHANGED: {
  113. Ref<StyleBox> style = main->get_theme_stylebox(SceneStringName(panel), SNAME("PopupMenu"));
  114. main_border_size = style->get_minimum_size();
  115. main->set_offset(SIDE_LEFT, style->get_margin(SIDE_LEFT));
  116. main->set_offset(SIDE_RIGHT, -style->get_margin(SIDE_RIGHT));
  117. main->set_offset(SIDE_TOP, style->get_margin(SIDE_TOP));
  118. main->set_offset(SIDE_BOTTOM, -style->get_margin(SIDE_BOTTOM));
  119. center_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), "PopupPanel"));
  120. } break;
  121. }
  122. }
  123. void ProgressDialog::_update_ui() {
  124. // Run main loop for two frames.
  125. if (is_inside_tree()) {
  126. DisplayServer::get_singleton()->process_events();
  127. Main::iteration();
  128. }
  129. }
  130. void ProgressDialog::_popup() {
  131. // Activate processing of all inputs in EditorNode, and the EditorNode::input method
  132. // will discard every key input.
  133. EditorNode::get_singleton()->set_process_input(true);
  134. // Disable all other windows to prevent interaction with them.
  135. for (ObjectID wid : host_windows) {
  136. Window *w = ObjectDB::get_instance<Window>(wid);
  137. if (w) {
  138. w->set_process_mode(PROCESS_MODE_DISABLED);
  139. }
  140. }
  141. Size2 ms = main->get_combined_minimum_size();
  142. ms.width = MAX(500 * EDSCALE, ms.width);
  143. ms += main_border_size;
  144. center_panel->set_custom_minimum_size(ms);
  145. if (is_ready()) {
  146. _reparent_and_show();
  147. } else {
  148. callable_mp(this, &ProgressDialog::_reparent_and_show).call_deferred();
  149. }
  150. }
  151. void ProgressDialog::_reparent_and_show() {
  152. Window *current_window = SceneTree::get_singleton()->get_root()->get_last_exclusive_window();
  153. ERR_FAIL_NULL(current_window);
  154. reparent(current_window);
  155. // Ensures that events are properly released before the dialog blocks input.
  156. bool window_is_input_disabled = current_window->is_input_disabled();
  157. current_window->set_disable_input(!window_is_input_disabled);
  158. current_window->set_disable_input(window_is_input_disabled);
  159. show();
  160. }
  161. void ProgressDialog::add_task(const String &p_task, const String &p_label, int p_steps, bool p_can_cancel) {
  162. if (MessageQueue::get_singleton()->is_flushing()) {
  163. ERR_PRINT("Do not use progress dialog (task) while flushing the message queue or using call_deferred()!");
  164. return;
  165. }
  166. ERR_FAIL_COND_MSG(tasks.has(p_task), "Task '" + p_task + "' already exists.");
  167. ProgressDialog::Task t;
  168. t.vb = memnew(VBoxContainer);
  169. VBoxContainer *vb2 = memnew(VBoxContainer);
  170. t.vb->add_margin_child(p_label, vb2);
  171. t.progress = memnew(ProgressBar);
  172. t.progress->set_theme_type_variation("PopupProgressBar");
  173. t.progress->set_max(p_steps);
  174. t.progress->set_value(p_steps);
  175. vb2->add_child(t.progress);
  176. t.state = memnew(Label);
  177. t.state->set_clip_text(true);
  178. vb2->add_child(t.state);
  179. main->add_child(t.vb);
  180. tasks[p_task] = t;
  181. if (p_can_cancel) {
  182. cancel_hb->show();
  183. } else {
  184. cancel_hb->hide();
  185. }
  186. cancel_hb->move_to_front();
  187. canceled = false;
  188. _popup();
  189. if (p_can_cancel) {
  190. cancel->grab_focus();
  191. }
  192. _update_ui();
  193. }
  194. bool ProgressDialog::task_step(const String &p_task, const String &p_state, int p_step, bool p_force_redraw) {
  195. ERR_FAIL_COND_V(!tasks.has(p_task), canceled);
  196. Task &t = tasks[p_task];
  197. if (!p_force_redraw) {
  198. uint64_t tus = OS::get_singleton()->get_ticks_usec();
  199. if (tus - t.last_progress_tick < 200000) { //200ms
  200. return canceled;
  201. }
  202. }
  203. if (p_step < 0) {
  204. t.progress->set_value(t.progress->get_value() + 1);
  205. } else {
  206. t.progress->set_value(p_step);
  207. }
  208. t.state->set_text(p_state);
  209. t.last_progress_tick = OS::get_singleton()->get_ticks_usec();
  210. _update_ui();
  211. return canceled;
  212. }
  213. void ProgressDialog::end_task(const String &p_task) {
  214. ERR_FAIL_COND(!tasks.has(p_task));
  215. Task &t = tasks[p_task];
  216. memdelete(t.vb);
  217. tasks.erase(p_task);
  218. if (tasks.is_empty()) {
  219. hide();
  220. EditorNode::get_singleton()->set_process_input(false);
  221. for (ObjectID wid : host_windows) {
  222. Window *w = ObjectDB::get_instance<Window>(wid);
  223. if (w) {
  224. w->set_process_mode(PROCESS_MODE_INHERIT);
  225. }
  226. }
  227. } else {
  228. _popup();
  229. }
  230. }
  231. void ProgressDialog::add_host_window(ObjectID p_window) {
  232. host_windows.push_back(p_window);
  233. }
  234. void ProgressDialog::remove_host_window(ObjectID p_window) {
  235. host_windows.erase(p_window);
  236. }
  237. void ProgressDialog::_cancel_pressed() {
  238. canceled = true;
  239. }
  240. ProgressDialog::ProgressDialog() {
  241. // We want to cover the entire screen to prevent the user from interacting with the Editor.
  242. set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  243. // Be sure it's the top most component.
  244. set_z_index(RS::CANVAS_ITEM_Z_MAX);
  245. singleton = this;
  246. hide();
  247. center_panel = memnew(PanelContainer);
  248. add_child(center_panel);
  249. center_panel->set_h_size_flags(SIZE_SHRINK_BEGIN);
  250. center_panel->set_v_size_flags(SIZE_SHRINK_BEGIN);
  251. main = memnew(VBoxContainer);
  252. center_panel->add_child(main);
  253. cancel_hb = memnew(HBoxContainer);
  254. main->add_child(cancel_hb);
  255. cancel_hb->hide();
  256. cancel = memnew(Button);
  257. cancel_hb->add_spacer();
  258. cancel_hb->add_child(cancel);
  259. cancel->set_text(TTR("Cancel"));
  260. cancel_hb->add_spacer();
  261. cancel->connect(SceneStringName(pressed), callable_mp(this, &ProgressDialog::_cancel_pressed));
  262. }
  263. ProgressDialog::~ProgressDialog() {
  264. singleton = nullptr;
  265. }