progress_dialog.cpp 8.3 KB

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