subviewport_container.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**************************************************************************/
  2. /* subviewport_container.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. recalc_force_viewport_sizes();
  55. update_minimum_size();
  56. queue_sort();
  57. queue_redraw();
  58. }
  59. bool SubViewportContainer::is_stretch_enabled() const {
  60. return stretch;
  61. }
  62. void SubViewportContainer::set_stretch_shrink(int p_shrink) {
  63. ERR_FAIL_COND(p_shrink < 1);
  64. if (shrink == p_shrink) {
  65. return;
  66. }
  67. shrink = p_shrink;
  68. recalc_force_viewport_sizes();
  69. queue_redraw();
  70. }
  71. void SubViewportContainer::recalc_force_viewport_sizes() {
  72. if (!stretch) {
  73. return;
  74. }
  75. // If stretch is enabled, make sure that all child SubViwewports have the correct size.
  76. for (int i = 0; i < get_child_count(); i++) {
  77. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  78. if (!c) {
  79. continue;
  80. }
  81. c->set_size_force(get_size() / shrink);
  82. }
  83. }
  84. int SubViewportContainer::get_stretch_shrink() const {
  85. return shrink;
  86. }
  87. Vector<int> SubViewportContainer::get_allowed_size_flags_horizontal() const {
  88. return Vector<int>();
  89. }
  90. Vector<int> SubViewportContainer::get_allowed_size_flags_vertical() const {
  91. return Vector<int>();
  92. }
  93. void SubViewportContainer::_notification(int p_what) {
  94. switch (p_what) {
  95. case NOTIFICATION_RESIZED: {
  96. recalc_force_viewport_sizes();
  97. } break;
  98. case NOTIFICATION_ENTER_TREE:
  99. case NOTIFICATION_VISIBILITY_CHANGED: {
  100. for (int i = 0; i < get_child_count(); i++) {
  101. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  102. if (!c) {
  103. continue;
  104. }
  105. if (is_visible_in_tree()) {
  106. c->set_update_mode(SubViewport::UPDATE_ALWAYS);
  107. } else {
  108. c->set_update_mode(SubViewport::UPDATE_DISABLED);
  109. }
  110. c->set_handle_input_locally(false); //do not handle input locally here
  111. }
  112. } break;
  113. case NOTIFICATION_DRAW: {
  114. for (int i = 0; i < get_child_count(); i++) {
  115. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  116. if (!c) {
  117. continue;
  118. }
  119. if (stretch) {
  120. draw_texture_rect(c->get_texture(), Rect2(Vector2(), get_size()));
  121. } else {
  122. draw_texture_rect(c->get_texture(), Rect2(Vector2(), c->get_size()));
  123. }
  124. }
  125. } break;
  126. case NOTIFICATION_MOUSE_ENTER: {
  127. _notify_viewports(NOTIFICATION_VP_MOUSE_ENTER);
  128. } break;
  129. case NOTIFICATION_MOUSE_EXIT: {
  130. _notify_viewports(NOTIFICATION_VP_MOUSE_EXIT);
  131. } break;
  132. }
  133. }
  134. void SubViewportContainer::_notify_viewports(int p_notification) {
  135. for (int i = 0; i < get_child_count(); i++) {
  136. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  137. if (!c) {
  138. continue;
  139. }
  140. c->notification(p_notification);
  141. }
  142. }
  143. void SubViewportContainer::input(const Ref<InputEvent> &p_event) {
  144. ERR_FAIL_COND(p_event.is_null());
  145. if (Engine::get_singleton()->is_editor_hint()) {
  146. return;
  147. }
  148. if (_is_propagated_in_gui_input(p_event)) {
  149. return;
  150. }
  151. _send_event_to_viewports(p_event);
  152. }
  153. void SubViewportContainer::gui_input(const Ref<InputEvent> &p_event) {
  154. ERR_FAIL_COND(p_event.is_null());
  155. if (Engine::get_singleton()->is_editor_hint()) {
  156. return;
  157. }
  158. if (!_is_propagated_in_gui_input(p_event)) {
  159. return;
  160. }
  161. if (stretch && shrink > 1) {
  162. Transform2D xform;
  163. xform.scale(Vector2(1, 1) / shrink);
  164. _send_event_to_viewports(p_event->xformed_by(xform));
  165. } else {
  166. _send_event_to_viewports(p_event);
  167. }
  168. }
  169. void SubViewportContainer::_send_event_to_viewports(const Ref<InputEvent> &p_event) {
  170. for (int i = 0; i < get_child_count(); i++) {
  171. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  172. if (!c || c->is_input_disabled()) {
  173. continue;
  174. }
  175. c->push_input(p_event);
  176. }
  177. }
  178. bool SubViewportContainer::_is_propagated_in_gui_input(const Ref<InputEvent> &p_event) {
  179. // Propagation of events with a position property happen in gui_input
  180. // Propagation of other events happen in input
  181. if (Object::cast_to<InputEventMouse>(*p_event) || Object::cast_to<InputEventScreenDrag>(*p_event) || Object::cast_to<InputEventScreenTouch>(*p_event) || Object::cast_to<InputEventGesture>(*p_event)) {
  182. return true;
  183. }
  184. return false;
  185. }
  186. void SubViewportContainer::unhandled_input(const Ref<InputEvent> &p_event) {
  187. ERR_FAIL_COND(p_event.is_null());
  188. if (Engine::get_singleton()->is_editor_hint()) {
  189. return;
  190. }
  191. Transform2D xform = get_global_transform_with_canvas();
  192. if (stretch) {
  193. Transform2D scale_xf;
  194. scale_xf.scale(Vector2(shrink, shrink));
  195. xform *= scale_xf;
  196. }
  197. Ref<InputEvent> ev = p_event->xformed_by(xform.affine_inverse());
  198. for (int i = 0; i < get_child_count(); i++) {
  199. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  200. if (!c || c->is_input_disabled()) {
  201. continue;
  202. }
  203. c->push_unhandled_input(ev);
  204. }
  205. }
  206. void SubViewportContainer::add_child_notify(Node *p_child) {
  207. if (Object::cast_to<SubViewport>(p_child)) {
  208. queue_redraw();
  209. }
  210. }
  211. void SubViewportContainer::remove_child_notify(Node *p_child) {
  212. if (Object::cast_to<SubViewport>(p_child)) {
  213. queue_redraw();
  214. }
  215. }
  216. PackedStringArray SubViewportContainer::get_configuration_warnings() const {
  217. PackedStringArray warnings = Node::get_configuration_warnings();
  218. bool has_viewport = false;
  219. for (int i = 0; i < get_child_count(); i++) {
  220. if (Object::cast_to<SubViewport>(get_child(i))) {
  221. has_viewport = true;
  222. break;
  223. }
  224. }
  225. if (!has_viewport) {
  226. 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."));
  227. }
  228. return warnings;
  229. }
  230. void SubViewportContainer::_bind_methods() {
  231. ClassDB::bind_method(D_METHOD("set_stretch", "enable"), &SubViewportContainer::set_stretch);
  232. ClassDB::bind_method(D_METHOD("is_stretch_enabled"), &SubViewportContainer::is_stretch_enabled);
  233. ClassDB::bind_method(D_METHOD("set_stretch_shrink", "amount"), &SubViewportContainer::set_stretch_shrink);
  234. ClassDB::bind_method(D_METHOD("get_stretch_shrink"), &SubViewportContainer::get_stretch_shrink);
  235. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch"), "set_stretch", "is_stretch_enabled");
  236. ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_shrink"), "set_stretch_shrink", "get_stretch_shrink");
  237. }
  238. SubViewportContainer::SubViewportContainer() {
  239. set_process_input(true);
  240. set_process_unhandled_input(true);
  241. }