progress_dialog.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*************************************************************************/
  2. /* progress_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "progress_dialog.h"
  30. #include "main/main.h"
  31. #include "message_queue.h"
  32. #include "scene/gui/empty_control.h"
  33. void BackgroundProgress::_add_task(const String& p_task,const String& p_label, int p_steps) {
  34. _THREAD_SAFE_METHOD_
  35. ERR_FAIL_COND(tasks.has(p_task));
  36. Task t;
  37. t.hb = memnew( HBoxContainer );
  38. Label *l=memnew( Label );
  39. l->set_text(p_label+" ");
  40. t.hb->add_child(l);
  41. t.progress = memnew( ProgressBar );
  42. t.progress->set_max(p_steps);
  43. t.progress->set_val(p_steps);
  44. EmptyControl *ec = memnew( EmptyControl );
  45. ec->set_h_size_flags(SIZE_EXPAND_FILL);
  46. ec->set_v_size_flags(SIZE_EXPAND_FILL);
  47. t.progress->set_area_as_parent_rect();
  48. ec->add_child(t.progress);
  49. ec->set_minsize(Size2(80,5));
  50. t.hb->add_child(ec);
  51. add_child(t.hb);
  52. tasks[p_task]=t;
  53. }
  54. void BackgroundProgress::_update() {
  55. _THREAD_SAFE_METHOD_
  56. for (Map<String,int>::Element *E=updates.front();E;E=E->next()) {
  57. if (tasks.has(E->key())) {
  58. _task_step(E->key(),E->get());
  59. }
  60. }
  61. updates.clear();
  62. }
  63. void BackgroundProgress::_task_step(const String& p_task, int p_step){
  64. _THREAD_SAFE_METHOD_
  65. ERR_FAIL_COND(!tasks.has(p_task));
  66. Task &t=tasks[p_task];
  67. if (p_step<0)
  68. t.progress->set_val(t.progress->get_val()+1);
  69. else
  70. t.progress->set_val(p_step);
  71. }
  72. void BackgroundProgress::_end_task(const String& p_task){
  73. _THREAD_SAFE_METHOD_
  74. ERR_FAIL_COND(!tasks.has(p_task));
  75. Task &t=tasks[p_task];
  76. memdelete(t.hb);
  77. tasks.erase(p_task);
  78. }
  79. void BackgroundProgress::_bind_methods(){
  80. ObjectTypeDB::bind_method("_add_task",&BackgroundProgress::_add_task);
  81. ObjectTypeDB::bind_method("_task_step",&BackgroundProgress::_task_step);
  82. ObjectTypeDB::bind_method("_end_task",&BackgroundProgress::_end_task);
  83. ObjectTypeDB::bind_method("_update",&BackgroundProgress::_update);
  84. }
  85. void BackgroundProgress::add_task(const String& p_task,const String& p_label, int p_steps){
  86. MessageQueue::get_singleton()->push_call(this,"_add_task",p_task,p_label,p_steps);
  87. }
  88. void BackgroundProgress::task_step(const String& p_task, int p_step){
  89. //this code is weird, but it prevents deadlock.
  90. bool no_updates;
  91. {
  92. _THREAD_SAFE_METHOD_
  93. no_updates=updates.empty();
  94. }
  95. if (no_updates)
  96. MessageQueue::get_singleton()->push_call(this,"_update");
  97. {
  98. _THREAD_SAFE_METHOD_
  99. updates[p_task]=p_step;
  100. }
  101. }
  102. void BackgroundProgress::end_task(const String& p_task){
  103. MessageQueue::get_singleton()->push_call(this,"_end_task",p_task);
  104. }
  105. ////////////////////////////////////////////////
  106. void ProgressDialog::_notification(int p_what) {
  107. switch(p_what) {
  108. case NOTIFICATION_DRAW: {
  109. Ref<StyleBox> style = get_stylebox("panel","PopupMenu");
  110. draw_style_box(style,Rect2(Point2(),get_size()));
  111. } break;
  112. }
  113. }
  114. void ProgressDialog::_popup() {
  115. Size2 ms = main->get_combined_minimum_size();
  116. ms.width = MAX(500,ms.width);
  117. Ref<StyleBox> style = get_stylebox("panel","PopupMenu");
  118. ms+=style->get_minimum_size();
  119. for(int i=0;i<4;i++) {
  120. main->set_margin(Margin(i),style->get_margin(Margin(i)));
  121. }
  122. popup_centered(ms);
  123. }
  124. void ProgressDialog::add_task(const String& p_task,const String& p_label,int p_steps) {
  125. ERR_FAIL_COND(tasks.has(p_task));
  126. Task t;
  127. t.vb = memnew( VBoxContainer );
  128. VBoxContainer *vb2 = memnew( VBoxContainer );
  129. t.vb->add_margin_child(p_label,vb2);
  130. t.progress = memnew( ProgressBar );
  131. t.progress->set_max(p_steps);
  132. t.progress->set_val(p_steps);
  133. vb2->add_child(t.progress);
  134. t.state=memnew( Label );
  135. vb2->add_child(t.state);
  136. main->add_child(t.vb);
  137. tasks[p_task]=t;
  138. _popup();
  139. }
  140. void ProgressDialog::task_step(const String& p_task, const String& p_state, int p_step){
  141. ERR_FAIL_COND(!tasks.has(p_task));
  142. Task &t=tasks[p_task];
  143. if (p_step<0)
  144. t.progress->set_val(t.progress->get_val()+1);
  145. else
  146. t.progress->set_val(p_step);
  147. t.state->set_text(p_state);
  148. Main::iteration(); // this will not work on a lot of platforms, so it's only meant for the editor
  149. }
  150. void ProgressDialog::end_task(const String& p_task){
  151. ERR_FAIL_COND(!tasks.has(p_task));
  152. Task &t=tasks[p_task];
  153. memdelete(t.vb);
  154. tasks.erase(p_task);
  155. if (tasks.empty())
  156. hide();
  157. else
  158. _popup();
  159. }
  160. ProgressDialog::ProgressDialog() {
  161. main = memnew( VBoxContainer );
  162. add_child(main);
  163. main->set_area_as_parent_rect();
  164. set_exclusive(true);
  165. }