subviewport_container.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*************************************************************************/
  2. /* subviewport_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "subviewport_container.h"
  31. #include "core/config/engine.h"
  32. #include "scene/main/viewport.h"
  33. Size2 SubViewportContainer::get_minimum_size() const {
  34. if (stretch) {
  35. return Size2();
  36. }
  37. Size2 ms;
  38. for (int i = 0; i < get_child_count(); i++) {
  39. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  40. if (!c) {
  41. continue;
  42. }
  43. Size2 minsize = c->get_size();
  44. ms.width = MAX(ms.width, minsize.width);
  45. ms.height = MAX(ms.height, minsize.height);
  46. }
  47. return ms;
  48. }
  49. void SubViewportContainer::set_stretch(bool p_enable) {
  50. if (stretch == p_enable) {
  51. return;
  52. }
  53. stretch = p_enable;
  54. update_minimum_size();
  55. queue_sort();
  56. queue_redraw();
  57. }
  58. bool SubViewportContainer::is_stretch_enabled() const {
  59. return stretch;
  60. }
  61. void SubViewportContainer::set_stretch_shrink(int p_shrink) {
  62. ERR_FAIL_COND(p_shrink < 1);
  63. if (shrink == p_shrink) {
  64. return;
  65. }
  66. shrink = p_shrink;
  67. if (!stretch) {
  68. return;
  69. }
  70. for (int i = 0; i < get_child_count(); i++) {
  71. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  72. if (!c) {
  73. continue;
  74. }
  75. c->set_size(get_size() / shrink);
  76. }
  77. queue_redraw();
  78. }
  79. int SubViewportContainer::get_stretch_shrink() const {
  80. return shrink;
  81. }
  82. Vector<int> SubViewportContainer::get_allowed_size_flags_horizontal() const {
  83. return Vector<int>();
  84. }
  85. Vector<int> SubViewportContainer::get_allowed_size_flags_vertical() const {
  86. return Vector<int>();
  87. }
  88. void SubViewportContainer::_notification(int p_what) {
  89. switch (p_what) {
  90. case NOTIFICATION_RESIZED: {
  91. if (!stretch) {
  92. return;
  93. }
  94. for (int i = 0; i < get_child_count(); i++) {
  95. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  96. if (!c) {
  97. continue;
  98. }
  99. c->set_size(get_size() / shrink);
  100. }
  101. } break;
  102. case NOTIFICATION_ENTER_TREE:
  103. case NOTIFICATION_VISIBILITY_CHANGED: {
  104. for (int i = 0; i < get_child_count(); i++) {
  105. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  106. if (!c) {
  107. continue;
  108. }
  109. if (is_visible_in_tree()) {
  110. c->set_update_mode(SubViewport::UPDATE_ALWAYS);
  111. } else {
  112. c->set_update_mode(SubViewport::UPDATE_DISABLED);
  113. }
  114. c->set_handle_input_locally(false); //do not handle input locally here
  115. }
  116. } break;
  117. case NOTIFICATION_DRAW: {
  118. for (int i = 0; i < get_child_count(); i++) {
  119. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  120. if (!c) {
  121. continue;
  122. }
  123. if (stretch) {
  124. draw_texture_rect(c->get_texture(), Rect2(Vector2(), get_size()));
  125. } else {
  126. draw_texture_rect(c->get_texture(), Rect2(Vector2(), c->get_size()));
  127. }
  128. }
  129. } break;
  130. case NOTIFICATION_MOUSE_ENTER: {
  131. _notify_viewports(NOTIFICATION_VP_MOUSE_ENTER);
  132. } break;
  133. case NOTIFICATION_MOUSE_EXIT: {
  134. _notify_viewports(NOTIFICATION_VP_MOUSE_EXIT);
  135. } break;
  136. }
  137. }
  138. void SubViewportContainer::_notify_viewports(int p_notification) {
  139. for (int i = 0; i < get_child_count(); i++) {
  140. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  141. if (!c) {
  142. continue;
  143. }
  144. c->notification(p_notification);
  145. }
  146. }
  147. void SubViewportContainer::input(const Ref<InputEvent> &p_event) {
  148. ERR_FAIL_COND(p_event.is_null());
  149. if (Engine::get_singleton()->is_editor_hint()) {
  150. return;
  151. }
  152. Transform2D xform = get_global_transform_with_canvas();
  153. if (stretch) {
  154. Transform2D scale_xf;
  155. scale_xf.scale(Vector2(shrink, shrink));
  156. xform *= scale_xf;
  157. }
  158. Ref<InputEvent> ev = p_event->xformed_by(xform.affine_inverse());
  159. for (int i = 0; i < get_child_count(); i++) {
  160. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  161. if (!c || c->is_input_disabled()) {
  162. continue;
  163. }
  164. c->push_input(ev);
  165. }
  166. }
  167. void SubViewportContainer::unhandled_input(const Ref<InputEvent> &p_event) {
  168. ERR_FAIL_COND(p_event.is_null());
  169. if (Engine::get_singleton()->is_editor_hint()) {
  170. return;
  171. }
  172. Transform2D xform = get_global_transform_with_canvas();
  173. if (stretch) {
  174. Transform2D scale_xf;
  175. scale_xf.scale(Vector2(shrink, shrink));
  176. xform *= scale_xf;
  177. }
  178. Ref<InputEvent> ev = p_event->xformed_by(xform.affine_inverse());
  179. for (int i = 0; i < get_child_count(); i++) {
  180. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  181. if (!c || c->is_input_disabled()) {
  182. continue;
  183. }
  184. c->push_unhandled_input(ev);
  185. }
  186. }
  187. TypedArray<String> SubViewportContainer::get_configuration_warnings() const {
  188. TypedArray<String> warnings = Node::get_configuration_warnings();
  189. bool has_viewport = false;
  190. for (int i = 0; i < get_child_count(); i++) {
  191. if (Object::cast_to<SubViewport>(get_child(i))) {
  192. has_viewport = true;
  193. break;
  194. }
  195. }
  196. if (!has_viewport) {
  197. warnings.push_back(RTR("This node doesn't have a SubViewport as child, so it can't display its intended content.\nConsider adding a SubViewport as a child to provide something displayable."));
  198. }
  199. return warnings;
  200. }
  201. void SubViewportContainer::_bind_methods() {
  202. ClassDB::bind_method(D_METHOD("set_stretch", "enable"), &SubViewportContainer::set_stretch);
  203. ClassDB::bind_method(D_METHOD("is_stretch_enabled"), &SubViewportContainer::is_stretch_enabled);
  204. ClassDB::bind_method(D_METHOD("set_stretch_shrink", "amount"), &SubViewportContainer::set_stretch_shrink);
  205. ClassDB::bind_method(D_METHOD("get_stretch_shrink"), &SubViewportContainer::get_stretch_shrink);
  206. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch"), "set_stretch", "is_stretch_enabled");
  207. ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_shrink"), "set_stretch_shrink", "get_stretch_shrink");
  208. }
  209. SubViewportContainer::SubViewportContainer() {
  210. set_process_input(true);
  211. set_process_unhandled_input(true);
  212. }