progress_dialog.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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::_popup() {
  108. Size2 ms = main->get_combined_minimum_size();
  109. ms.width = MAX(500 * EDSCALE, ms.width);
  110. Ref<StyleBox> style = main->get_theme_stylebox(SNAME("panel"), SNAME("PopupMenu"));
  111. ms += style->get_minimum_size();
  112. main->set_offset(SIDE_LEFT, style->get_margin(SIDE_LEFT));
  113. main->set_offset(SIDE_RIGHT, -style->get_margin(SIDE_RIGHT));
  114. main->set_offset(SIDE_TOP, style->get_margin(SIDE_TOP));
  115. main->set_offset(SIDE_BOTTOM, -style->get_margin(SIDE_BOTTOM));
  116. if (!is_inside_tree()) {
  117. for (Window *window : host_windows) {
  118. if (window->has_focus()) {
  119. popup_exclusive_centered(window, ms);
  120. return;
  121. }
  122. }
  123. // No host window found, use main window.
  124. EditorInterface::get_singleton()->popup_dialog_centered(this, ms);
  125. }
  126. }
  127. void ProgressDialog::add_task(const String &p_task, const String &p_label, int p_steps, bool p_can_cancel) {
  128. if (MessageQueue::get_singleton()->is_flushing()) {
  129. ERR_PRINT("Do not use progress dialog (task) while flushing the message queue or using call_deferred()!");
  130. return;
  131. }
  132. ERR_FAIL_COND_MSG(tasks.has(p_task), "Task '" + p_task + "' already exists.");
  133. ProgressDialog::Task t;
  134. t.vb = memnew(VBoxContainer);
  135. VBoxContainer *vb2 = memnew(VBoxContainer);
  136. t.vb->add_margin_child(p_label, vb2);
  137. t.progress = memnew(ProgressBar);
  138. t.progress->set_max(p_steps);
  139. t.progress->set_value(p_steps);
  140. vb2->add_child(t.progress);
  141. t.state = memnew(Label);
  142. t.state->set_clip_text(true);
  143. vb2->add_child(t.state);
  144. main->add_child(t.vb);
  145. tasks[p_task] = t;
  146. if (p_can_cancel) {
  147. cancel_hb->show();
  148. } else {
  149. cancel_hb->hide();
  150. }
  151. cancel_hb->move_to_front();
  152. canceled = false;
  153. _popup();
  154. if (p_can_cancel) {
  155. cancel->grab_focus();
  156. }
  157. }
  158. bool ProgressDialog::task_step(const String &p_task, const String &p_state, int p_step, bool p_force_redraw) {
  159. ERR_FAIL_COND_V(!tasks.has(p_task), canceled);
  160. if (!p_force_redraw) {
  161. uint64_t tus = OS::get_singleton()->get_ticks_usec();
  162. if (tus - last_progress_tick < 200000) { //200ms
  163. return canceled;
  164. }
  165. }
  166. Task &t = tasks[p_task];
  167. if (p_step < 0) {
  168. t.progress->set_value(t.progress->get_value() + 1);
  169. } else {
  170. t.progress->set_value(p_step);
  171. }
  172. t.state->set_text(p_state);
  173. last_progress_tick = OS::get_singleton()->get_ticks_usec();
  174. DisplayServer::get_singleton()->process_events();
  175. #ifndef ANDROID_ENABLED
  176. Main::iteration(); // this will not work on a lot of platforms, so it's only meant for the editor
  177. #endif
  178. return canceled;
  179. }
  180. void ProgressDialog::end_task(const String &p_task) {
  181. ERR_FAIL_COND(!tasks.has(p_task));
  182. Task &t = tasks[p_task];
  183. memdelete(t.vb);
  184. tasks.erase(p_task);
  185. if (tasks.is_empty()) {
  186. hide();
  187. } else {
  188. _popup();
  189. }
  190. }
  191. void ProgressDialog::add_host_window(Window *p_window) {
  192. ERR_FAIL_NULL(p_window);
  193. host_windows.push_back(p_window);
  194. }
  195. void ProgressDialog::_cancel_pressed() {
  196. canceled = true;
  197. }
  198. ProgressDialog::ProgressDialog() {
  199. main = memnew(VBoxContainer);
  200. add_child(main);
  201. main->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  202. set_exclusive(true);
  203. set_flag(Window::FLAG_POPUP, false);
  204. last_progress_tick = 0;
  205. singleton = this;
  206. cancel_hb = memnew(HBoxContainer);
  207. main->add_child(cancel_hb);
  208. cancel_hb->hide();
  209. cancel = memnew(Button);
  210. cancel_hb->add_spacer();
  211. cancel_hb->add_child(cancel);
  212. cancel->set_text(TTR("Cancel"));
  213. cancel_hb->add_spacer();
  214. cancel->connect(SceneStringName(pressed), callable_mp(this, &ProgressDialog::_cancel_pressed));
  215. }