progress_dialog.cpp 8.4 KB

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