progress_dialog.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*************************************************************************/
  2. /* progress_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. 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. 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. 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. ClassDB::bind_method("_add_task", &BackgroundProgress::_add_task);
  83. ClassDB::bind_method("_task_step", &BackgroundProgress::_task_step);
  84. ClassDB::bind_method("_end_task", &BackgroundProgress::_end_task);
  85. ClassDB::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 = true;
  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. main->set_margin(MARGIN_LEFT, style->get_margin(MARGIN_LEFT));
  123. main->set_margin(MARGIN_RIGHT, -style->get_margin(MARGIN_RIGHT));
  124. main->set_margin(MARGIN_TOP, style->get_margin(MARGIN_TOP));
  125. main->set_margin(MARGIN_BOTTOM, -style->get_margin(MARGIN_BOTTOM));
  126. popup_centered(ms);
  127. }
  128. void ProgressDialog::add_task(const String &p_task, const String &p_label, int p_steps, bool p_can_cancel) {
  129. ERR_FAIL_COND(tasks.has(p_task));
  130. ProgressDialog::Task t;
  131. t.vb = memnew(VBoxContainer);
  132. VBoxContainer *vb2 = memnew(VBoxContainer);
  133. t.vb->add_margin_child(p_label, vb2);
  134. t.progress = memnew(ProgressBar);
  135. t.progress->set_max(p_steps);
  136. t.progress->set_value(p_steps);
  137. vb2->add_child(t.progress);
  138. t.state = memnew(Label);
  139. t.state->set_clip_text(true);
  140. vb2->add_child(t.state);
  141. main->add_child(t.vb);
  142. tasks[p_task] = t;
  143. if (p_can_cancel) {
  144. cancel_hb->show();
  145. } else {
  146. cancel_hb->hide();
  147. }
  148. cancel_hb->raise();
  149. cancelled = false;
  150. _popup();
  151. if (p_can_cancel) {
  152. cancel->grab_focus();
  153. }
  154. }
  155. bool ProgressDialog::task_step(const String &p_task, const String &p_state, int p_step, bool p_force_redraw) {
  156. ERR_FAIL_COND_V(!tasks.has(p_task), cancelled);
  157. if (!p_force_redraw) {
  158. uint64_t tus = OS::get_singleton()->get_ticks_usec();
  159. if (tus - last_progress_tick < 200000) //200ms
  160. return cancelled;
  161. }
  162. Task &t = tasks[p_task];
  163. if (p_step < 0)
  164. t.progress->set_value(t.progress->get_value() + 1);
  165. else
  166. t.progress->set_value(p_step);
  167. t.state->set_text(p_state);
  168. last_progress_tick = OS::get_singleton()->get_ticks_usec();
  169. if (cancel_hb->is_visible()) {
  170. OS::get_singleton()->force_process_input();
  171. }
  172. Main::iteration(); // this will not work on a lot of platforms, so it's only meant for the editor
  173. return cancelled;
  174. }
  175. void ProgressDialog::end_task(const String &p_task) {
  176. ERR_FAIL_COND(!tasks.has(p_task));
  177. Task &t = tasks[p_task];
  178. memdelete(t.vb);
  179. tasks.erase(p_task);
  180. if (tasks.empty())
  181. hide();
  182. else
  183. _popup();
  184. }
  185. void ProgressDialog::_cancel_pressed() {
  186. cancelled = true;
  187. }
  188. void ProgressDialog::_bind_methods() {
  189. ClassDB::bind_method("_cancel_pressed", &ProgressDialog::_cancel_pressed);
  190. }
  191. ProgressDialog::ProgressDialog() {
  192. main = memnew(VBoxContainer);
  193. add_child(main);
  194. main->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  195. set_exclusive(true);
  196. last_progress_tick = 0;
  197. singleton = this;
  198. cancel_hb = memnew(HBoxContainer);
  199. main->add_child(cancel_hb);
  200. cancel_hb->hide();
  201. cancel = memnew(Button);
  202. cancel_hb->add_spacer();
  203. cancel_hb->add_child(cancel);
  204. cancel->set_text(TTR("Cancel"));
  205. cancel_hb->add_spacer();
  206. cancel->connect("pressed", this, "_cancel_pressed");
  207. }