viewport_container.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "viewport_container.h"
  2. #include "scene/main/viewport.h"
  3. Size2 ViewportContainer::get_minimum_size() const {
  4. if (stretch)
  5. return Size2();
  6. Size2 ms;
  7. for(int i=0;i<get_child_count();i++) {
  8. Viewport *c = get_child(i)->cast_to<Viewport>();
  9. if (!c)
  10. continue;
  11. Size2 minsize = c->get_size();
  12. ms.width = MAX(ms.width , minsize.width);
  13. ms.height = MAX(ms.height , minsize.height);
  14. }
  15. return ms;
  16. }
  17. void ViewportContainer::set_stretch(bool p_enable) {
  18. stretch=p_enable;
  19. queue_sort();
  20. update();
  21. }
  22. bool ViewportContainer::is_stretch_enabled() const {
  23. return stretch;
  24. }
  25. void ViewportContainer::_notification(int p_what) {
  26. if (p_what==NOTIFICATION_RESIZED) {
  27. if (!stretch)
  28. return;
  29. for(int i=0;i<get_child_count();i++) {
  30. Viewport *c = get_child(i)->cast_to<Viewport>();
  31. if (!c)
  32. continue;
  33. c->set_size(get_size());
  34. }
  35. }
  36. if (p_what==NOTIFICATION_ENTER_TREE || p_what==NOTIFICATION_VISIBILITY_CHANGED) {
  37. for(int i=0;i<get_child_count();i++) {
  38. Viewport *c = get_child(i)->cast_to<Viewport>();
  39. if (!c)
  40. continue;
  41. if (is_visible())
  42. c->set_update_mode(Viewport::UPDATE_ALWAYS);
  43. else
  44. c->set_update_mode(Viewport::UPDATE_DISABLED);
  45. }
  46. }
  47. if (p_what==NOTIFICATION_DRAW) {
  48. for(int i=0;i<get_child_count();i++) {
  49. Viewport *c = get_child(i)->cast_to<Viewport>();
  50. if (!c)
  51. continue;
  52. if (stretch)
  53. draw_texture_rect(c->get_texture(),Rect2(Vector2(),get_size()*Size2(1,-1)));
  54. else
  55. draw_texture_rect(c->get_texture(),Rect2(Vector2(),c->get_size()*Size2(1,-1)));
  56. }
  57. }
  58. }
  59. void ViewportContainer::_bind_methods() {
  60. ObjectTypeDB::bind_method(_MD("set_stretch","enable"),&ViewportContainer::set_stretch);
  61. ObjectTypeDB::bind_method(_MD("is_stretch_enabled"),&ViewportContainer::is_stretch_enabled);
  62. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"stretch"),_SCS("set_stretch"),_SCS("is_stretch_enabled"));
  63. }
  64. ViewportContainer::ViewportContainer() {
  65. stretch=false;
  66. }