progress_dialog.cpp 9.4 KB

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