viewport_container.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*************************************************************************/
  2. /* viewport_container.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 "viewport_container.h"
  31. #include "core/engine.h"
  32. #include "scene/main/viewport.h"
  33. Size2 ViewportContainer::get_minimum_size() const {
  34. if (stretch)
  35. return Size2();
  36. Size2 ms;
  37. for (int i = 0; i < get_child_count(); i++) {
  38. Viewport *c = Object::cast_to<Viewport>(get_child(i));
  39. if (!c)
  40. continue;
  41. Size2 minsize = c->get_size();
  42. ms.width = MAX(ms.width, minsize.width);
  43. ms.height = MAX(ms.height, minsize.height);
  44. }
  45. return ms;
  46. }
  47. void ViewportContainer::set_stretch(bool p_enable) {
  48. stretch = p_enable;
  49. queue_sort();
  50. update();
  51. }
  52. bool ViewportContainer::is_stretch_enabled() const {
  53. return stretch;
  54. }
  55. void ViewportContainer::set_stretch_shrink(int p_shrink) {
  56. ERR_FAIL_COND(p_shrink < 1);
  57. if (shrink == p_shrink)
  58. return;
  59. shrink = p_shrink;
  60. if (!stretch)
  61. return;
  62. for (int i = 0; i < get_child_count(); i++) {
  63. Viewport *c = Object::cast_to<Viewport>(get_child(i));
  64. if (!c)
  65. continue;
  66. c->set_size(get_size() / shrink);
  67. }
  68. update();
  69. }
  70. int ViewportContainer::get_stretch_shrink() const {
  71. return shrink;
  72. }
  73. void ViewportContainer::_notification(int p_what) {
  74. if (p_what == NOTIFICATION_RESIZED) {
  75. if (!stretch)
  76. return;
  77. for (int i = 0; i < get_child_count(); i++) {
  78. Viewport *c = Object::cast_to<Viewport>(get_child(i));
  79. if (!c)
  80. continue;
  81. c->set_size(get_size() / shrink);
  82. }
  83. }
  84. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  85. for (int i = 0; i < get_child_count(); i++) {
  86. Viewport *c = Object::cast_to<Viewport>(get_child(i));
  87. if (!c)
  88. continue;
  89. if (is_visible_in_tree())
  90. c->set_update_mode(Viewport::UPDATE_ALWAYS);
  91. else
  92. c->set_update_mode(Viewport::UPDATE_DISABLED);
  93. c->set_handle_input_locally(false); //do not handle input locally here
  94. }
  95. }
  96. if (p_what == NOTIFICATION_DRAW) {
  97. for (int i = 0; i < get_child_count(); i++) {
  98. Viewport *c = Object::cast_to<Viewport>(get_child(i));
  99. if (!c)
  100. continue;
  101. if (stretch)
  102. draw_texture_rect(c->get_texture(), Rect2(Vector2(), get_size() * Size2(1, -1)));
  103. else
  104. draw_texture_rect(c->get_texture(), Rect2(Vector2(), c->get_size() * Size2(1, -1)));
  105. }
  106. }
  107. }
  108. void ViewportContainer::_input(const Ref<InputEvent> &p_event) {
  109. if (Engine::get_singleton()->is_editor_hint())
  110. return;
  111. Transform2D xform = get_global_transform();
  112. if (stretch) {
  113. Transform2D scale_xf;
  114. scale_xf.scale(Vector2(shrink, shrink));
  115. xform *= scale_xf;
  116. }
  117. Ref<InputEvent> ev = p_event->xformed_by(xform.affine_inverse());
  118. for (int i = 0; i < get_child_count(); i++) {
  119. Viewport *c = Object::cast_to<Viewport>(get_child(i));
  120. if (!c || c->is_input_disabled())
  121. continue;
  122. c->input(ev);
  123. }
  124. }
  125. void ViewportContainer::_unhandled_input(const Ref<InputEvent> &p_event) {
  126. if (Engine::get_singleton()->is_editor_hint())
  127. return;
  128. Transform2D xform = get_global_transform();
  129. if (stretch) {
  130. Transform2D scale_xf;
  131. scale_xf.scale(Vector2(shrink, shrink));
  132. xform *= scale_xf;
  133. }
  134. Ref<InputEvent> ev = p_event->xformed_by(xform.affine_inverse());
  135. for (int i = 0; i < get_child_count(); i++) {
  136. Viewport *c = Object::cast_to<Viewport>(get_child(i));
  137. if (!c || c->is_input_disabled())
  138. continue;
  139. c->unhandled_input(ev);
  140. }
  141. }
  142. void ViewportContainer::_bind_methods() {
  143. ClassDB::bind_method(D_METHOD("_unhandled_input", "event"), &ViewportContainer::_unhandled_input);
  144. ClassDB::bind_method(D_METHOD("_input", "event"), &ViewportContainer::_input);
  145. ClassDB::bind_method(D_METHOD("set_stretch", "enable"), &ViewportContainer::set_stretch);
  146. ClassDB::bind_method(D_METHOD("is_stretch_enabled"), &ViewportContainer::is_stretch_enabled);
  147. ClassDB::bind_method(D_METHOD("set_stretch_shrink", "amount"), &ViewportContainer::set_stretch_shrink);
  148. ClassDB::bind_method(D_METHOD("get_stretch_shrink"), &ViewportContainer::get_stretch_shrink);
  149. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch"), "set_stretch", "is_stretch_enabled");
  150. ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_shrink"), "set_stretch_shrink", "get_stretch_shrink");
  151. }
  152. ViewportContainer::ViewportContainer() {
  153. stretch = false;
  154. shrink = 1;
  155. set_process_input(true);
  156. }