progress_dialog.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*************************************************************************/
  2. /* progress_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_scale.h"
  34. #include "main/main.h"
  35. #include "servers/display_server.h"
  36. void BackgroundProgress::_add_task(const String &p_task, const String &p_label, int p_steps) {
  37. _THREAD_SAFE_METHOD_
  38. ERR_FAIL_COND_MSG(tasks.has(p_task), "Task '" + p_task + "' already exists.");
  39. BackgroundProgress::Task t;
  40. t.hb = memnew(HBoxContainer);
  41. Label *l = memnew(Label);
  42. l->set_text(p_label + " ");
  43. t.hb->add_child(l);
  44. t.progress = memnew(ProgressBar);
  45. t.progress->set_max(p_steps);
  46. t.progress->set_value(p_steps);
  47. Control *ec = memnew(Control);
  48. ec->set_h_size_flags(SIZE_EXPAND_FILL);
  49. ec->set_v_size_flags(SIZE_EXPAND_FILL);
  50. t.progress->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  51. ec->add_child(t.progress);
  52. ec->set_custom_minimum_size(Size2(80, 5) * EDSCALE);
  53. t.hb->add_child(ec);
  54. add_child(t.hb);
  55. tasks[p_task] = t;
  56. }
  57. void BackgroundProgress::_update() {
  58. _THREAD_SAFE_METHOD_
  59. for (Map<String, int>::Element *E = updates.front(); E; E = E->next()) {
  60. if (tasks.has(E->key())) {
  61. _task_step(E->key(), E->get());
  62. }
  63. }
  64. updates.clear();
  65. }
  66. void BackgroundProgress::_task_step(const String &p_task, int p_step) {
  67. _THREAD_SAFE_METHOD_
  68. ERR_FAIL_COND(!tasks.has(p_task));
  69. Task &t = tasks[p_task];
  70. if (p_step < 0) {
  71. t.progress->set_value(t.progress->get_value() + 1);
  72. } else {
  73. t.progress->set_value(p_step);
  74. }
  75. }
  76. void BackgroundProgress::_end_task(const String &p_task) {
  77. _THREAD_SAFE_METHOD_
  78. ERR_FAIL_COND(!tasks.has(p_task));
  79. Task &t = tasks[p_task];
  80. memdelete(t.hb);
  81. tasks.erase(p_task);
  82. }
  83. void BackgroundProgress::_bind_methods() {
  84. ClassDB::bind_method("_add_task", &BackgroundProgress::_add_task);
  85. ClassDB::bind_method("_task_step", &BackgroundProgress::_task_step);
  86. ClassDB::bind_method("_end_task", &BackgroundProgress::_end_task);
  87. ClassDB::bind_method("_update", &BackgroundProgress::_update);
  88. }
  89. void BackgroundProgress::add_task(const String &p_task, const String &p_label, int p_steps) {
  90. MessageQueue::get_singleton()->push_call(this, "_add_task", p_task, p_label, p_steps);
  91. }
  92. void BackgroundProgress::task_step(const String &p_task, int p_step) {
  93. //this code is weird, but it prevents deadlock.
  94. bool no_updates = true;
  95. {
  96. _THREAD_SAFE_METHOD_
  97. no_updates = updates.is_empty();
  98. }
  99. if (no_updates) {
  100. MessageQueue::get_singleton()->push_call(this, "_update");
  101. }
  102. {
  103. _THREAD_SAFE_METHOD_
  104. updates[p_task] = p_step;
  105. }
  106. }
  107. void BackgroundProgress::end_task(const String &p_task) {
  108. MessageQueue::get_singleton()->push_call(this, "_end_task", p_task);
  109. }
  110. ////////////////////////////////////////////////
  111. ProgressDialog *ProgressDialog::singleton = nullptr;
  112. void ProgressDialog::_notification(int p_what) {
  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("panel", "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. //raise();
  124. popup_centered(ms);
  125. }
  126. void ProgressDialog::add_task(const String &p_task, const String &p_label, int p_steps, bool p_can_cancel) {
  127. if (MessageQueue::get_singleton()->is_flushing()) {
  128. ERR_PRINT("Do not use progress dialog (task) while flushing the message queue or using call_deferred()!");
  129. return;
  130. }
  131. ERR_FAIL_COND_MSG(tasks.has(p_task), "Task '" + p_task + "' already exists.");
  132. ProgressDialog::Task t;
  133. t.vb = memnew(VBoxContainer);
  134. VBoxContainer *vb2 = memnew(VBoxContainer);
  135. t.vb->add_margin_child(p_label, vb2);
  136. t.progress = memnew(ProgressBar);
  137. t.progress->set_max(p_steps);
  138. t.progress->set_value(p_steps);
  139. vb2->add_child(t.progress);
  140. t.state = memnew(Label);
  141. t.state->set_clip_text(true);
  142. vb2->add_child(t.state);
  143. main->add_child(t.vb);
  144. tasks[p_task] = t;
  145. if (p_can_cancel) {
  146. cancel_hb->show();
  147. } else {
  148. cancel_hb->hide();
  149. }
  150. cancel_hb->raise();
  151. cancelled = false;
  152. _popup();
  153. if (p_can_cancel) {
  154. cancel->grab_focus();
  155. }
  156. }
  157. bool ProgressDialog::task_step(const String &p_task, const String &p_state, int p_step, bool p_force_redraw) {
  158. ERR_FAIL_COND_V(!tasks.has(p_task), cancelled);
  159. if (!p_force_redraw) {
  160. uint64_t tus = OS::get_singleton()->get_ticks_usec();
  161. if (tus - last_progress_tick < 200000) { //200ms
  162. return cancelled;
  163. }
  164. }
  165. Task &t = tasks[p_task];
  166. if (p_step < 0) {
  167. t.progress->set_value(t.progress->get_value() + 1);
  168. } else {
  169. t.progress->set_value(p_step);
  170. }
  171. t.state->set_text(p_state);
  172. last_progress_tick = OS::get_singleton()->get_ticks_usec();
  173. if (cancel_hb->is_visible()) {
  174. DisplayServer::get_singleton()->process_events();
  175. }
  176. Main::iteration(); // this will not work on a lot of platforms, so it's only meant for the editor
  177. return cancelled;
  178. }
  179. void ProgressDialog::end_task(const String &p_task) {
  180. ERR_FAIL_COND(!tasks.has(p_task));
  181. Task &t = tasks[p_task];
  182. memdelete(t.vb);
  183. tasks.erase(p_task);
  184. if (tasks.is_empty()) {
  185. hide();
  186. } else {
  187. _popup();
  188. }
  189. }
  190. void ProgressDialog::_cancel_pressed() {
  191. cancelled = true;
  192. }
  193. void ProgressDialog::_bind_methods() {
  194. }
  195. ProgressDialog::ProgressDialog() {
  196. main = memnew(VBoxContainer);
  197. add_child(main);
  198. main->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  199. set_exclusive(true);
  200. last_progress_tick = 0;
  201. singleton = this;
  202. cancel_hb = memnew(HBoxContainer);
  203. main->add_child(cancel_hb);
  204. cancel_hb->hide();
  205. cancel = memnew(Button);
  206. cancel_hb->add_spacer();
  207. cancel_hb->add_child(cancel);
  208. cancel->set_text(TTR("Cancel"));
  209. cancel_hb->add_spacer();
  210. cancel->connect("pressed", callable_mp(this, &ProgressDialog::_cancel_pressed));
  211. }