subviewport_container.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. 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_force(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_force(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. if (_is_propagated_in_gui_input(p_event)) {
  153. return;
  154. }
  155. _send_event_to_viewports(p_event);
  156. }
  157. void SubViewportContainer::gui_input(const Ref<InputEvent> &p_event) {
  158. ERR_FAIL_COND(p_event.is_null());
  159. if (Engine::get_singleton()->is_editor_hint()) {
  160. return;
  161. }
  162. if (!_is_propagated_in_gui_input(p_event)) {
  163. return;
  164. }
  165. if (stretch && shrink > 1) {
  166. Transform2D xform;
  167. xform.scale(Vector2(1, 1) / shrink);
  168. _send_event_to_viewports(p_event->xformed_by(xform));
  169. } else {
  170. _send_event_to_viewports(p_event);
  171. }
  172. }
  173. void SubViewportContainer::_send_event_to_viewports(const Ref<InputEvent> &p_event) {
  174. for (int i = 0; i < get_child_count(); i++) {
  175. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  176. if (!c || c->is_input_disabled()) {
  177. continue;
  178. }
  179. c->push_input(p_event);
  180. }
  181. }
  182. bool SubViewportContainer::_is_propagated_in_gui_input(const Ref<InputEvent> &p_event) {
  183. // Propagation of events with a position property happen in gui_input
  184. // Propagation of other events happen in input
  185. 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)) {
  186. return true;
  187. }
  188. return false;
  189. }
  190. void SubViewportContainer::unhandled_input(const Ref<InputEvent> &p_event) {
  191. ERR_FAIL_COND(p_event.is_null());
  192. if (Engine::get_singleton()->is_editor_hint()) {
  193. return;
  194. }
  195. Transform2D xform = get_global_transform_with_canvas();
  196. if (stretch) {
  197. Transform2D scale_xf;
  198. scale_xf.scale(Vector2(shrink, shrink));
  199. xform *= scale_xf;
  200. }
  201. Ref<InputEvent> ev = p_event->xformed_by(xform.affine_inverse());
  202. for (int i = 0; i < get_child_count(); i++) {
  203. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  204. if (!c || c->is_input_disabled()) {
  205. continue;
  206. }
  207. c->push_unhandled_input(ev);
  208. }
  209. }
  210. void SubViewportContainer::add_child_notify(Node *p_child) {
  211. if (Object::cast_to<SubViewport>(p_child)) {
  212. queue_redraw();
  213. }
  214. }
  215. void SubViewportContainer::remove_child_notify(Node *p_child) {
  216. if (Object::cast_to<SubViewport>(p_child)) {
  217. queue_redraw();
  218. }
  219. }
  220. PackedStringArray SubViewportContainer::get_configuration_warnings() const {
  221. PackedStringArray warnings = Node::get_configuration_warnings();
  222. bool has_viewport = false;
  223. for (int i = 0; i < get_child_count(); i++) {
  224. if (Object::cast_to<SubViewport>(get_child(i))) {
  225. has_viewport = true;
  226. break;
  227. }
  228. }
  229. if (!has_viewport) {
  230. 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."));
  231. }
  232. return warnings;
  233. }
  234. void SubViewportContainer::_bind_methods() {
  235. ClassDB::bind_method(D_METHOD("set_stretch", "enable"), &SubViewportContainer::set_stretch);
  236. ClassDB::bind_method(D_METHOD("is_stretch_enabled"), &SubViewportContainer::is_stretch_enabled);
  237. ClassDB::bind_method(D_METHOD("set_stretch_shrink", "amount"), &SubViewportContainer::set_stretch_shrink);
  238. ClassDB::bind_method(D_METHOD("get_stretch_shrink"), &SubViewportContainer::get_stretch_shrink);
  239. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch"), "set_stretch", "is_stretch_enabled");
  240. ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_shrink"), "set_stretch_shrink", "get_stretch_shrink");
  241. }
  242. SubViewportContainer::SubViewportContainer() {
  243. set_process_input(true);
  244. set_process_unhandled_input(true);
  245. }