progress_dialog.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 "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::add_task(const String &p_task, const String &p_label, int p_steps) {
  85. callable_mp(this, &BackgroundProgress::_add_task).call_deferred(p_task, p_label, p_steps);
  86. }
  87. void BackgroundProgress::task_step(const String &p_task, int p_step) {
  88. //this code is weird, but it prevents deadlock.
  89. bool no_updates = true;
  90. {
  91. _THREAD_SAFE_METHOD_
  92. no_updates = updates.is_empty();
  93. }
  94. if (no_updates) {
  95. callable_mp(this, &BackgroundProgress::_update).call_deferred();
  96. }
  97. {
  98. _THREAD_SAFE_METHOD_
  99. updates[p_task] = p_step;
  100. }
  101. }
  102. void BackgroundProgress::end_task(const String &p_task) {
  103. callable_mp(this, &BackgroundProgress::_end_task).call_deferred(p_task);
  104. }
  105. ////////////////////////////////////////////////
  106. ProgressDialog *ProgressDialog::singleton = nullptr;
  107. void ProgressDialog::_update_ui() {
  108. // Run main loop for two frames.
  109. if (is_inside_tree()) {
  110. DisplayServer::get_singleton()->process_events();
  111. Main::iteration();
  112. }
  113. }
  114. void ProgressDialog::_popup() {
  115. Size2 ms = main->get_combined_minimum_size();
  116. ms.width = MAX(500 * EDSCALE, ms.width);
  117. Ref<StyleBox> style = main->get_theme_stylebox(SceneStringName(panel), SNAME("PopupMenu"));
  118. ms += style->get_minimum_size();
  119. main->set_offset(SIDE_LEFT, style->get_margin(SIDE_LEFT));
  120. main->set_offset(SIDE_RIGHT, -style->get_margin(SIDE_RIGHT));
  121. main->set_offset(SIDE_TOP, style->get_margin(SIDE_TOP));
  122. main->set_offset(SIDE_BOTTOM, -style->get_margin(SIDE_BOTTOM));
  123. if (is_inside_tree()) {
  124. Rect2i adjust = _popup_adjust_rect();
  125. if (adjust != Rect2i()) {
  126. set_position(adjust.position);
  127. set_size(adjust.size);
  128. }
  129. } else {
  130. for (Window *window : host_windows) {
  131. if (window->has_focus()) {
  132. popup_exclusive_centered(window, ms);
  133. return;
  134. }
  135. }
  136. // No host window found, use main window.
  137. EditorInterface::get_singleton()->popup_dialog_centered(this, ms);
  138. }
  139. }
  140. void ProgressDialog::add_task(const String &p_task, const String &p_label, int p_steps, bool p_can_cancel) {
  141. if (MessageQueue::get_singleton()->is_flushing()) {
  142. ERR_PRINT("Do not use progress dialog (task) while flushing the message queue or using call_deferred()!");
  143. return;
  144. }
  145. ERR_FAIL_COND_MSG(tasks.has(p_task), "Task '" + p_task + "' already exists.");
  146. ProgressDialog::Task t;
  147. t.vb = memnew(VBoxContainer);
  148. VBoxContainer *vb2 = memnew(VBoxContainer);
  149. t.vb->add_margin_child(p_label, vb2);
  150. t.progress = memnew(ProgressBar);
  151. t.progress->set_max(p_steps);
  152. t.progress->set_value(p_steps);
  153. vb2->add_child(t.progress);
  154. t.state = memnew(Label);
  155. t.state->set_clip_text(true);
  156. vb2->add_child(t.state);
  157. main->add_child(t.vb);
  158. tasks[p_task] = t;
  159. if (p_can_cancel) {
  160. cancel_hb->show();
  161. } else {
  162. cancel_hb->hide();
  163. }
  164. cancel_hb->move_to_front();
  165. canceled = false;
  166. _popup();
  167. if (p_can_cancel) {
  168. cancel->grab_focus();
  169. }
  170. _update_ui();
  171. }
  172. bool ProgressDialog::task_step(const String &p_task, const String &p_state, int p_step, bool p_force_redraw) {
  173. ERR_FAIL_COND_V(!tasks.has(p_task), canceled);
  174. Task &t = tasks[p_task];
  175. if (!p_force_redraw) {
  176. uint64_t tus = OS::get_singleton()->get_ticks_usec();
  177. if (tus - t.last_progress_tick < 200000) { //200ms
  178. return canceled;
  179. }
  180. }
  181. if (p_step < 0) {
  182. t.progress->set_value(t.progress->get_value() + 1);
  183. } else {
  184. t.progress->set_value(p_step);
  185. }
  186. t.state->set_text(p_state);
  187. t.last_progress_tick = OS::get_singleton()->get_ticks_usec();
  188. _update_ui();
  189. return canceled;
  190. }
  191. void ProgressDialog::end_task(const String &p_task) {
  192. ERR_FAIL_COND(!tasks.has(p_task));
  193. Task &t = tasks[p_task];
  194. memdelete(t.vb);
  195. tasks.erase(p_task);
  196. if (tasks.is_empty()) {
  197. hide();
  198. } else {
  199. _popup();
  200. }
  201. }
  202. void ProgressDialog::add_host_window(Window *p_window) {
  203. ERR_FAIL_NULL(p_window);
  204. host_windows.push_back(p_window);
  205. }
  206. void ProgressDialog::_cancel_pressed() {
  207. canceled = true;
  208. }
  209. ProgressDialog::ProgressDialog() {
  210. main = memnew(VBoxContainer);
  211. add_child(main);
  212. main->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  213. set_exclusive(true);
  214. set_flag(Window::FLAG_POPUP, false);
  215. singleton = this;
  216. cancel_hb = memnew(HBoxContainer);
  217. main->add_child(cancel_hb);
  218. cancel_hb->hide();
  219. cancel = memnew(Button);
  220. cancel_hb->add_spacer();
  221. cancel_hb->add_child(cancel);
  222. cancel->set_text(TTR("Cancel"));
  223. cancel_hb->add_spacer();
  224. cancel->connect(SceneStringName(pressed), callable_mp(this, &ProgressDialog::_cancel_pressed));
  225. }