progress_dialog.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "editor_scale.h"
  32. #include "main/main.h"
  33. #include "message_queue.h"
  34. #include "os/os.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(tasks.has(p_task));
  38. 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_val(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_area_as_parent_rect();
  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_val(t.progress->get_val() + 1);
  71. else
  72. t.progress->set_val(p_step);
  73. }
  74. void BackgroundProgress::_end_task(const String &p_task) {
  75. _THREAD_SAFE_METHOD_
  76. ERR_FAIL_COND(!tasks.has(p_task));
  77. Task &t = tasks[p_task];
  78. memdelete(t.hb);
  79. tasks.erase(p_task);
  80. }
  81. void BackgroundProgress::_bind_methods() {
  82. ObjectTypeDB::bind_method("_add_task", &BackgroundProgress::_add_task);
  83. ObjectTypeDB::bind_method("_task_step", &BackgroundProgress::_task_step);
  84. ObjectTypeDB::bind_method("_end_task", &BackgroundProgress::_end_task);
  85. ObjectTypeDB::bind_method("_update", &BackgroundProgress::_update);
  86. }
  87. void BackgroundProgress::add_task(const String &p_task, const String &p_label, int p_steps) {
  88. MessageQueue::get_singleton()->push_call(this, "_add_task", p_task, p_label, p_steps);
  89. }
  90. void BackgroundProgress::task_step(const String &p_task, int p_step) {
  91. //this code is weird, but it prevents deadlock.
  92. bool no_updates;
  93. {
  94. _THREAD_SAFE_METHOD_
  95. no_updates = updates.empty();
  96. }
  97. if (no_updates)
  98. MessageQueue::get_singleton()->push_call(this, "_update");
  99. {
  100. _THREAD_SAFE_METHOD_
  101. updates[p_task] = p_step;
  102. }
  103. }
  104. void BackgroundProgress::end_task(const String &p_task) {
  105. MessageQueue::get_singleton()->push_call(this, "_end_task", p_task);
  106. }
  107. ////////////////////////////////////////////////
  108. ProgressDialog *ProgressDialog::singleton = NULL;
  109. void ProgressDialog::_notification(int p_what) {
  110. switch (p_what) {
  111. case NOTIFICATION_DRAW: {
  112. Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
  113. draw_style_box(style, Rect2(Point2(), get_size()));
  114. } break;
  115. }
  116. }
  117. void ProgressDialog::_popup() {
  118. Size2 ms = main->get_combined_minimum_size();
  119. ms.width = MAX(500 * EDSCALE, ms.width);
  120. Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
  121. ms += style->get_minimum_size();
  122. for (int i = 0; i < 4; i++) {
  123. main->set_margin(Margin(i), style->get_margin(Margin(i)));
  124. }
  125. popup_centered(ms);
  126. }
  127. void ProgressDialog::add_task(const String &p_task, const String &p_label, int p_steps) {
  128. ERR_FAIL_COND(tasks.has(p_task));
  129. Task t;
  130. t.vb = memnew(VBoxContainer);
  131. VBoxContainer *vb2 = memnew(VBoxContainer);
  132. t.vb->add_margin_child(p_label, vb2);
  133. t.progress = memnew(ProgressBar);
  134. t.progress->set_max(p_steps);
  135. t.progress->set_val(p_steps);
  136. vb2->add_child(t.progress);
  137. t.state = memnew(Label);
  138. t.state->set_clip_text(true);
  139. vb2->add_child(t.state);
  140. main->add_child(t.vb);
  141. tasks[p_task] = t;
  142. _popup();
  143. }
  144. void ProgressDialog::task_step(const String &p_task, const String &p_state, int p_step, bool p_force_redraw) {
  145. ERR_FAIL_COND(!tasks.has(p_task));
  146. if (!p_force_redraw) {
  147. uint64_t tus = OS::get_singleton()->get_ticks_usec();
  148. if (tus - last_progress_tick < 50000) //50ms
  149. return;
  150. }
  151. Task &t = tasks[p_task];
  152. if (p_step < 0)
  153. t.progress->set_val(t.progress->get_val() + 1);
  154. else
  155. t.progress->set_val(p_step);
  156. t.state->set_text(p_state);
  157. last_progress_tick = OS::get_singleton()->get_ticks_usec();
  158. Main::iteration(); // this will not work on a lot of platforms, so it's only meant for the editor
  159. }
  160. void ProgressDialog::end_task(const String &p_task) {
  161. ERR_FAIL_COND(!tasks.has(p_task));
  162. Task &t = tasks[p_task];
  163. memdelete(t.vb);
  164. tasks.erase(p_task);
  165. if (tasks.empty())
  166. hide();
  167. else
  168. _popup();
  169. }
  170. ProgressDialog::ProgressDialog() {
  171. main = memnew(VBoxContainer);
  172. add_child(main);
  173. main->set_area_as_parent_rect();
  174. set_exclusive(true);
  175. last_progress_tick = 0;
  176. singleton = this;
  177. }