progress_dialog.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/message_queue.h"
  32. #include "core/os/os.h"
  33. #include "editor_scale.h"
  34. #include "main/main.h"
  35. void BackgroundProgress::_add_task(const String &p_task, const String &p_label, int p_steps) {
  36. _THREAD_SAFE_METHOD_
  37. ERR_FAIL_COND_MSG(tasks.has(p_task), "Task '" + p_task + "' already exists.");
  38. BackgroundProgress::Task t;
  39. t.hb = memnew(HBoxContainer);
  40. Label *l = memnew(Label);
  41. l->set_text(p_label + " ");
  42. t.hb->add_child(l);
  43. t.progress = memnew(ProgressBar);
  44. t.progress->set_max(p_steps);
  45. t.progress->set_value(p_steps);
  46. Control *ec = memnew(Control);
  47. ec->set_h_size_flags(SIZE_EXPAND_FILL);
  48. ec->set_v_size_flags(SIZE_EXPAND_FILL);
  49. t.progress->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  50. ec->add_child(t.progress);
  51. ec->set_custom_minimum_size(Size2(80, 5) * EDSCALE);
  52. t.hb->add_child(ec);
  53. add_child(t.hb);
  54. tasks[p_task] = t;
  55. }
  56. void BackgroundProgress::_update() {
  57. _THREAD_SAFE_METHOD_
  58. for (Map<String, int>::Element *E = updates.front(); E; E = E->next()) {
  59. if (tasks.has(E->key())) {
  60. _task_step(E->key(), E->get());
  61. }
  62. }
  63. updates.clear();
  64. }
  65. void BackgroundProgress::_task_step(const String &p_task, int p_step) {
  66. _THREAD_SAFE_METHOD_
  67. ERR_FAIL_COND(!tasks.has(p_task));
  68. Task &t = tasks[p_task];
  69. if (p_step < 0) {
  70. t.progress->set_value(t.progress->get_value() + 1);
  71. } else {
  72. t.progress->set_value(p_step);
  73. }
  74. }
  75. void BackgroundProgress::_end_task(const String &p_task) {
  76. _THREAD_SAFE_METHOD_
  77. ERR_FAIL_COND(!tasks.has(p_task));
  78. Task &t = tasks[p_task];
  79. memdelete(t.hb);
  80. tasks.erase(p_task);
  81. }
  82. void BackgroundProgress::_bind_methods() {
  83. ClassDB::bind_method("_add_task", &BackgroundProgress::_add_task);
  84. ClassDB::bind_method("_task_step", &BackgroundProgress::_task_step);
  85. ClassDB::bind_method("_end_task", &BackgroundProgress::_end_task);
  86. ClassDB::bind_method("_update", &BackgroundProgress::_update);
  87. }
  88. void BackgroundProgress::add_task(const String &p_task, const String &p_label, int p_steps) {
  89. MessageQueue::get_singleton()->push_call(this, "_add_task", p_task, p_label, p_steps);
  90. }
  91. void BackgroundProgress::task_step(const String &p_task, int p_step) {
  92. //this code is weird, but it prevents deadlock.
  93. bool no_updates = true;
  94. {
  95. _THREAD_SAFE_METHOD_
  96. no_updates = updates.empty();
  97. }
  98. if (no_updates) {
  99. MessageQueue::get_singleton()->push_call(this, "_update");
  100. }
  101. {
  102. _THREAD_SAFE_METHOD_
  103. updates[p_task] = p_step;
  104. }
  105. }
  106. void BackgroundProgress::end_task(const String &p_task) {
  107. MessageQueue::get_singleton()->push_call(this, "_end_task", p_task);
  108. }
  109. ////////////////////////////////////////////////
  110. ProgressDialog *ProgressDialog::singleton = nullptr;
  111. void ProgressDialog::_notification(int p_what) {
  112. switch (p_what) {
  113. case NOTIFICATION_DRAW: {
  114. Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
  115. draw_style_box(style, Rect2(Point2(), get_size()));
  116. } break;
  117. }
  118. }
  119. void ProgressDialog::_popup() {
  120. Size2 ms = main->get_combined_minimum_size();
  121. ms.width = MAX(500 * EDSCALE, ms.width);
  122. Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
  123. ms += style->get_minimum_size();
  124. main->set_margin(MARGIN_LEFT, style->get_margin(MARGIN_LEFT));
  125. main->set_margin(MARGIN_RIGHT, -style->get_margin(MARGIN_RIGHT));
  126. main->set_margin(MARGIN_TOP, style->get_margin(MARGIN_TOP));
  127. main->set_margin(MARGIN_BOTTOM, -style->get_margin(MARGIN_BOTTOM));
  128. raise();
  129. popup_centered(ms);
  130. }
  131. void ProgressDialog::add_task(const String &p_task, const String &p_label, int p_steps, bool p_can_cancel) {
  132. if (MessageQueue::get_singleton()->is_flushing()) {
  133. ERR_PRINT("Do not use progress dialog (task) while flushing the message queue or using call_deferred()!");
  134. return;
  135. }
  136. ERR_FAIL_COND_MSG(tasks.has(p_task), "Task '" + p_task + "' already exists.");
  137. ProgressDialog::Task t;
  138. t.vb = memnew(VBoxContainer);
  139. VBoxContainer *vb2 = memnew(VBoxContainer);
  140. t.vb->add_margin_child(p_label, vb2);
  141. t.progress = memnew(ProgressBar);
  142. t.progress->set_max(p_steps);
  143. t.progress->set_value(p_steps);
  144. t.last_progress_tick = 0;
  145. vb2->add_child(t.progress);
  146. t.state = memnew(Label);
  147. t.state->set_clip_text(true);
  148. vb2->add_child(t.state);
  149. main->add_child(t.vb);
  150. tasks[p_task] = t;
  151. if (p_can_cancel) {
  152. cancel_hb->show();
  153. } else {
  154. cancel_hb->hide();
  155. }
  156. cancel_hb->raise();
  157. cancelled = false;
  158. _popup();
  159. if (p_can_cancel) {
  160. cancel->grab_focus();
  161. }
  162. }
  163. bool ProgressDialog::task_step(const String &p_task, const String &p_state, int p_step, bool p_force_redraw) {
  164. ERR_FAIL_COND_V(!tasks.has(p_task), cancelled);
  165. Task &t = tasks[p_task];
  166. if (!p_force_redraw) {
  167. uint64_t tus = OS::get_singleton()->get_ticks_usec();
  168. if (tus - t.last_progress_tick < 200000) { //200ms
  169. return cancelled;
  170. }
  171. }
  172. if (p_step < 0) {
  173. t.progress->set_value(t.progress->get_value() + 1);
  174. } else {
  175. t.progress->set_value(p_step);
  176. }
  177. t.state->set_text(p_state);
  178. t.last_progress_tick = OS::get_singleton()->get_ticks_usec();
  179. if (cancel_hb->is_visible()) {
  180. OS::get_singleton()->force_process_input();
  181. }
  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 cancelled;
  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.empty()) {
  193. hide();
  194. } else {
  195. _popup();
  196. }
  197. }
  198. void ProgressDialog::_cancel_pressed() {
  199. cancelled = true;
  200. }
  201. void ProgressDialog::_bind_methods() {
  202. ClassDB::bind_method("_cancel_pressed", &ProgressDialog::_cancel_pressed);
  203. }
  204. ProgressDialog::ProgressDialog() {
  205. main = memnew(VBoxContainer);
  206. add_child(main);
  207. main->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  208. set_exclusive(true);
  209. singleton = this;
  210. cancel_hb = memnew(HBoxContainer);
  211. main->add_child(cancel_hb);
  212. cancel_hb->hide();
  213. cancel = memnew(Button);
  214. cancel_hb->add_spacer();
  215. cancel_hb->add_child(cancel);
  216. cancel->set_text(TTR("Cancel"));
  217. cancel_hb->add_spacer();
  218. cancel->connect("pressed", this, "_cancel_pressed");
  219. }