progress_dialog.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*************************************************************************/
  2. /* progress_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/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_margins_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. 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. _THREAD_SAFE_METHOD_
  102. updates[p_task] = p_step;
  103. }
  104. }
  105. void BackgroundProgress::end_task(const String &p_task) {
  106. MessageQueue::get_singleton()->push_call(this, "_end_task", p_task);
  107. }
  108. ////////////////////////////////////////////////
  109. ProgressDialog *ProgressDialog::singleton = NULL;
  110. void ProgressDialog::_notification(int p_what) {
  111. switch (p_what) {
  112. case NOTIFICATION_DRAW: {
  113. Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
  114. draw_style_box(style, Rect2(Point2(), get_size()));
  115. } break;
  116. }
  117. }
  118. void ProgressDialog::_popup() {
  119. Size2 ms = main->get_combined_minimum_size();
  120. ms.width = MAX(500 * EDSCALE, ms.width);
  121. Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
  122. ms += style->get_minimum_size();
  123. main->set_margin(MARGIN_LEFT, style->get_margin(MARGIN_LEFT));
  124. main->set_margin(MARGIN_RIGHT, -style->get_margin(MARGIN_RIGHT));
  125. main->set_margin(MARGIN_TOP, style->get_margin(MARGIN_TOP));
  126. main->set_margin(MARGIN_BOTTOM, -style->get_margin(MARGIN_BOTTOM));
  127. raise();
  128. popup_centered(ms);
  129. }
  130. void ProgressDialog::add_task(const String &p_task, const String &p_label, int p_steps, bool p_can_cancel) {
  131. if (MessageQueue::get_singleton()->is_flushing()) {
  132. ERR_PRINT("Do not use progress dialog (task) while flushing the message queue or using call_deferred()!");
  133. return;
  134. }
  135. ERR_FAIL_COND_MSG(tasks.has(p_task), "Task '" + p_task + "' already exists.");
  136. ProgressDialog::Task t;
  137. t.vb = memnew(VBoxContainer);
  138. VBoxContainer *vb2 = memnew(VBoxContainer);
  139. t.vb->add_margin_child(p_label, vb2);
  140. t.progress = memnew(ProgressBar);
  141. t.progress->set_max(p_steps);
  142. t.progress->set_value(p_steps);
  143. vb2->add_child(t.progress);
  144. t.state = memnew(Label);
  145. t.state->set_clip_text(true);
  146. vb2->add_child(t.state);
  147. main->add_child(t.vb);
  148. tasks[p_task] = t;
  149. if (p_can_cancel) {
  150. cancel_hb->show();
  151. } else {
  152. cancel_hb->hide();
  153. }
  154. cancel_hb->raise();
  155. cancelled = false;
  156. _popup();
  157. if (p_can_cancel) {
  158. cancel->grab_focus();
  159. }
  160. }
  161. bool ProgressDialog::task_step(const String &p_task, const String &p_state, int p_step, bool p_force_redraw) {
  162. ERR_FAIL_COND_V(!tasks.has(p_task), cancelled);
  163. if (!p_force_redraw) {
  164. uint64_t tus = OS::get_singleton()->get_ticks_usec();
  165. if (tus - last_progress_tick < 200000) //200ms
  166. return cancelled;
  167. }
  168. Task &t = tasks[p_task];
  169. if (p_step < 0)
  170. t.progress->set_value(t.progress->get_value() + 1);
  171. else
  172. t.progress->set_value(p_step);
  173. t.state->set_text(p_state);
  174. last_progress_tick = OS::get_singleton()->get_ticks_usec();
  175. if (cancel_hb->is_visible()) {
  176. DisplayServer::get_singleton()->process_events();
  177. }
  178. Main::iteration(); // this will not work on a lot of platforms, so it's only meant for the editor
  179. return cancelled;
  180. }
  181. void ProgressDialog::end_task(const String &p_task) {
  182. ERR_FAIL_COND(!tasks.has(p_task));
  183. Task &t = tasks[p_task];
  184. memdelete(t.vb);
  185. tasks.erase(p_task);
  186. if (tasks.empty())
  187. hide();
  188. else
  189. _popup();
  190. }
  191. void ProgressDialog::_cancel_pressed() {
  192. cancelled = true;
  193. }
  194. void ProgressDialog::_bind_methods() {
  195. }
  196. ProgressDialog::ProgressDialog() {
  197. main = memnew(VBoxContainer);
  198. add_child(main);
  199. main->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  200. set_exclusive(true);
  201. last_progress_tick = 0;
  202. singleton = this;
  203. cancel_hb = memnew(HBoxContainer);
  204. main->add_child(cancel_hb);
  205. cancel_hb->hide();
  206. cancel = memnew(Button);
  207. cancel_hb->add_spacer();
  208. cancel_hb->add_child(cancel);
  209. cancel->set_text(TTR("Cancel"));
  210. cancel_hb->add_spacer();
  211. cancel->connect("pressed", callable_mp(this, &ProgressDialog::_cancel_pressed));
  212. }