progress_dialog.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. 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::_cancel_pressed() {
  192. canceled = true;
  193. }
  194. void ProgressDialog::_bind_methods() {
  195. }
  196. ProgressDialog::ProgressDialog() {
  197. main = memnew(VBoxContainer);
  198. add_child(main);
  199. main->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  200. set_exclusive(true);
  201. set_flag(Window::FLAG_POPUP, false);
  202. last_progress_tick = 0;
  203. singleton = this;
  204. cancel_hb = memnew(HBoxContainer);
  205. main->add_child(cancel_hb);
  206. cancel_hb->hide();
  207. cancel = memnew(Button);
  208. cancel_hb->add_spacer();
  209. cancel_hb->add_child(cancel);
  210. cancel->set_text(TTR("Cancel"));
  211. cancel_hb->add_spacer();
  212. cancel->connect("pressed", callable_mp(this, &ProgressDialog::_cancel_pressed));
  213. }