viewport_container.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*************************************************************************/
  2. /* viewport_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "viewport_container.h"
  31. #include "scene/main/viewport.h"
  32. Size2 ViewportContainer::get_minimum_size() const {
  33. if (stretch)
  34. return Size2();
  35. Size2 ms;
  36. for (int i = 0; i < get_child_count(); i++) {
  37. Viewport *c = Object::cast_to<Viewport>(get_child(i));
  38. if (!c)
  39. continue;
  40. Size2 minsize = c->get_size();
  41. ms.width = MAX(ms.width, minsize.width);
  42. ms.height = MAX(ms.height, minsize.height);
  43. }
  44. return ms;
  45. }
  46. void ViewportContainer::set_stretch(bool p_enable) {
  47. stretch = p_enable;
  48. queue_sort();
  49. update();
  50. }
  51. bool ViewportContainer::is_stretch_enabled() const {
  52. return stretch;
  53. }
  54. void ViewportContainer::set_stretch_shrink(int p_shrink) {
  55. ERR_FAIL_COND(p_shrink < 1);
  56. if (shrink == p_shrink)
  57. return;
  58. shrink = p_shrink;
  59. if (!stretch)
  60. return;
  61. for (int i = 0; i < get_child_count(); i++) {
  62. Viewport *c = Object::cast_to<Viewport>(get_child(i));
  63. if (!c)
  64. continue;
  65. c->set_size(get_size() / shrink);
  66. }
  67. update();
  68. }
  69. int ViewportContainer::get_stretch_shrink() const {
  70. return shrink;
  71. }
  72. void ViewportContainer::_notification(int p_what) {
  73. if (p_what == NOTIFICATION_RESIZED) {
  74. if (!stretch)
  75. return;
  76. for (int i = 0; i < get_child_count(); i++) {
  77. Viewport *c = Object::cast_to<Viewport>(get_child(i));
  78. if (!c)
  79. continue;
  80. c->set_size(get_size() / shrink);
  81. }
  82. }
  83. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  84. for (int i = 0; i < get_child_count(); i++) {
  85. Viewport *c = Object::cast_to<Viewport>(get_child(i));
  86. if (!c)
  87. continue;
  88. if (is_visible_in_tree())
  89. c->set_update_mode(Viewport::UPDATE_ALWAYS);
  90. else
  91. c->set_update_mode(Viewport::UPDATE_DISABLED);
  92. }
  93. }
  94. if (p_what == NOTIFICATION_DRAW) {
  95. for (int i = 0; i < get_child_count(); i++) {
  96. Viewport *c = Object::cast_to<Viewport>(get_child(i));
  97. if (!c)
  98. continue;
  99. if (stretch)
  100. draw_texture_rect(c->get_texture(), Rect2(Vector2(), get_size() * Size2(1, -1)));
  101. else
  102. draw_texture_rect(c->get_texture(), Rect2(Vector2(), c->get_size() * Size2(1, -1)));
  103. }
  104. }
  105. }
  106. void ViewportContainer::_bind_methods() {
  107. ClassDB::bind_method(D_METHOD("set_stretch", "enable"), &ViewportContainer::set_stretch);
  108. ClassDB::bind_method(D_METHOD("is_stretch_enabled"), &ViewportContainer::is_stretch_enabled);
  109. ClassDB::bind_method(D_METHOD("set_stretch_shrink", "amount"), &ViewportContainer::set_stretch_shrink);
  110. ClassDB::bind_method(D_METHOD("get_stretch_shrink"), &ViewportContainer::get_stretch_shrink);
  111. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch"), "set_stretch", "is_stretch_enabled");
  112. ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_shrink"), "set_stretch_shrink", "get_stretch_shrink");
  113. }
  114. ViewportContainer::ViewportContainer() {
  115. stretch = false;
  116. shrink = 1;
  117. }