subviewport_container.cpp 9.1 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. 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_FOCUS_ENTER: {
  127. // If focused, send InputEvent to the SubViewport before the Gui-Input stage.
  128. set_process_input(true);
  129. set_process_unhandled_input(false);
  130. } break;
  131. case NOTIFICATION_FOCUS_EXIT: {
  132. // A different Control has focus and should receive Gui-Input before the InputEvent is sent to the SubViewport.
  133. set_process_input(false);
  134. set_process_unhandled_input(true);
  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. _propagate_nonpositional_event(p_event);
  149. }
  150. void SubViewportContainer::unhandled_input(const Ref<InputEvent> &p_event) {
  151. _propagate_nonpositional_event(p_event);
  152. }
  153. void SubViewportContainer::_propagate_nonpositional_event(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. bool send;
  162. if (GDVIRTUAL_CALL(_propagate_input_event, p_event, send)) {
  163. if (!send) {
  164. return;
  165. }
  166. }
  167. _send_event_to_viewports(p_event);
  168. }
  169. void SubViewportContainer::gui_input(const Ref<InputEvent> &p_event) {
  170. ERR_FAIL_COND(p_event.is_null());
  171. if (Engine::get_singleton()->is_editor_hint()) {
  172. return;
  173. }
  174. if (!_is_propagated_in_gui_input(p_event)) {
  175. return;
  176. }
  177. bool send;
  178. if (GDVIRTUAL_CALL(_propagate_input_event, p_event, send)) {
  179. if (!send) {
  180. return;
  181. }
  182. }
  183. if (stretch && shrink > 1) {
  184. Transform2D xform;
  185. xform.scale(Vector2(1, 1) / shrink);
  186. _send_event_to_viewports(p_event->xformed_by(xform));
  187. } else {
  188. _send_event_to_viewports(p_event);
  189. }
  190. }
  191. void SubViewportContainer::_send_event_to_viewports(const Ref<InputEvent> &p_event) {
  192. for (int i = 0; i < get_child_count(); i++) {
  193. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  194. if (!c || c->is_input_disabled()) {
  195. continue;
  196. }
  197. c->push_input(p_event);
  198. }
  199. }
  200. bool SubViewportContainer::_is_propagated_in_gui_input(const Ref<InputEvent> &p_event) {
  201. // Propagation of events with a position property happen in gui_input
  202. // Propagation of other events happen in input
  203. 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)) {
  204. return true;
  205. }
  206. return false;
  207. }
  208. void SubViewportContainer::add_child_notify(Node *p_child) {
  209. if (Object::cast_to<SubViewport>(p_child)) {
  210. queue_redraw();
  211. }
  212. }
  213. void SubViewportContainer::remove_child_notify(Node *p_child) {
  214. if (Object::cast_to<SubViewport>(p_child)) {
  215. queue_redraw();
  216. }
  217. }
  218. PackedStringArray SubViewportContainer::get_configuration_warnings() const {
  219. PackedStringArray warnings = Node::get_configuration_warnings();
  220. bool has_viewport = false;
  221. for (int i = 0; i < get_child_count(); i++) {
  222. if (Object::cast_to<SubViewport>(get_child(i))) {
  223. has_viewport = true;
  224. break;
  225. }
  226. }
  227. if (!has_viewport) {
  228. 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."));
  229. }
  230. if (get_default_cursor_shape() != Control::CURSOR_ARROW) {
  231. warnings.push_back(RTR("The default mouse cursor shape of SubViewportContainer has no effect.\nConsider leaving it at its initial value `CURSOR_ARROW`."));
  232. }
  233. return warnings;
  234. }
  235. void SubViewportContainer::_bind_methods() {
  236. ClassDB::bind_method(D_METHOD("set_stretch", "enable"), &SubViewportContainer::set_stretch);
  237. ClassDB::bind_method(D_METHOD("is_stretch_enabled"), &SubViewportContainer::is_stretch_enabled);
  238. ClassDB::bind_method(D_METHOD("set_stretch_shrink", "amount"), &SubViewportContainer::set_stretch_shrink);
  239. ClassDB::bind_method(D_METHOD("get_stretch_shrink"), &SubViewportContainer::get_stretch_shrink);
  240. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch"), "set_stretch", "is_stretch_enabled");
  241. ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_shrink"), "set_stretch_shrink", "get_stretch_shrink");
  242. GDVIRTUAL_BIND(_propagate_input_event, "event");
  243. }
  244. SubViewportContainer::SubViewportContainer() {
  245. set_process_unhandled_input(true);
  246. set_focus_mode(FOCUS_CLICK);
  247. }