subviewport_container.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 = ms.max(minsize);
  45. }
  46. return ms;
  47. }
  48. void SubViewportContainer::set_stretch(bool p_enable) {
  49. if (stretch == p_enable) {
  50. return;
  51. }
  52. stretch = p_enable;
  53. recalc_force_viewport_sizes();
  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. recalc_force_viewport_sizes();
  68. queue_redraw();
  69. }
  70. void SubViewportContainer::recalc_force_viewport_sizes() {
  71. if (!stretch) {
  72. return;
  73. }
  74. // If stretch is enabled, make sure that all child SubViwewports have the correct size.
  75. for (int i = 0; i < get_child_count(); i++) {
  76. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  77. if (!c) {
  78. continue;
  79. }
  80. c->set_size_force(get_size() / shrink);
  81. }
  82. }
  83. int SubViewportContainer::get_stretch_shrink() const {
  84. return shrink;
  85. }
  86. Vector<int> SubViewportContainer::get_allowed_size_flags_horizontal() const {
  87. return Vector<int>();
  88. }
  89. Vector<int> SubViewportContainer::get_allowed_size_flags_vertical() const {
  90. return Vector<int>();
  91. }
  92. void SubViewportContainer::_notification(int p_what) {
  93. switch (p_what) {
  94. case NOTIFICATION_RESIZED: {
  95. recalc_force_viewport_sizes();
  96. } break;
  97. case NOTIFICATION_ENTER_TREE:
  98. case NOTIFICATION_VISIBILITY_CHANGED: {
  99. for (int i = 0; i < get_child_count(); i++) {
  100. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  101. if (!c) {
  102. continue;
  103. }
  104. if (is_visible_in_tree()) {
  105. c->set_update_mode(SubViewport::UPDATE_ALWAYS);
  106. } else {
  107. c->set_update_mode(SubViewport::UPDATE_DISABLED);
  108. }
  109. c->set_handle_input_locally(false); //do not handle input locally here
  110. }
  111. } break;
  112. case NOTIFICATION_DRAW: {
  113. for (int i = 0; i < get_child_count(); i++) {
  114. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  115. if (!c) {
  116. continue;
  117. }
  118. if (stretch) {
  119. draw_texture_rect(c->get_texture(), Rect2(Vector2(), get_size()));
  120. } else {
  121. draw_texture_rect(c->get_texture(), Rect2(Vector2(), c->get_size()));
  122. }
  123. }
  124. } break;
  125. case NOTIFICATION_FOCUS_ENTER: {
  126. // If focused, send InputEvent to the SubViewport before the Gui-Input stage.
  127. set_process_input(true);
  128. set_process_unhandled_input(false);
  129. } break;
  130. case NOTIFICATION_FOCUS_EXIT: {
  131. // A different Control has focus and should receive Gui-Input before the InputEvent is sent to the SubViewport.
  132. set_process_input(false);
  133. set_process_unhandled_input(true);
  134. } break;
  135. }
  136. }
  137. void SubViewportContainer::_notify_viewports(int p_notification) {
  138. for (int i = 0; i < get_child_count(); i++) {
  139. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  140. if (!c) {
  141. continue;
  142. }
  143. c->notification(p_notification);
  144. }
  145. }
  146. void SubViewportContainer::input(const Ref<InputEvent> &p_event) {
  147. _propagate_nonpositional_event(p_event);
  148. }
  149. void SubViewportContainer::unhandled_input(const Ref<InputEvent> &p_event) {
  150. _propagate_nonpositional_event(p_event);
  151. }
  152. void SubViewportContainer::_propagate_nonpositional_event(const Ref<InputEvent> &p_event) {
  153. ERR_FAIL_COND(p_event.is_null());
  154. if (Engine::get_singleton()->is_editor_hint()) {
  155. return;
  156. }
  157. if (_is_propagated_in_gui_input(p_event)) {
  158. return;
  159. }
  160. bool send;
  161. if (GDVIRTUAL_CALL(_propagate_input_event, p_event, send)) {
  162. if (!send) {
  163. return;
  164. }
  165. }
  166. _send_event_to_viewports(p_event);
  167. }
  168. void SubViewportContainer::gui_input(const Ref<InputEvent> &p_event) {
  169. ERR_FAIL_COND(p_event.is_null());
  170. if (Engine::get_singleton()->is_editor_hint()) {
  171. return;
  172. }
  173. if (!_is_propagated_in_gui_input(p_event)) {
  174. return;
  175. }
  176. bool send;
  177. if (GDVIRTUAL_CALL(_propagate_input_event, p_event, send)) {
  178. if (!send) {
  179. return;
  180. }
  181. }
  182. if (stretch && shrink > 1) {
  183. Transform2D xform;
  184. xform.scale(Vector2(1, 1) / shrink);
  185. _send_event_to_viewports(p_event->xformed_by(xform));
  186. } else {
  187. _send_event_to_viewports(p_event);
  188. }
  189. }
  190. void SubViewportContainer::_send_event_to_viewports(const Ref<InputEvent> &p_event) {
  191. for (int i = 0; i < get_child_count(); i++) {
  192. SubViewport *c = Object::cast_to<SubViewport>(get_child(i));
  193. if (!c || c->is_input_disabled()) {
  194. continue;
  195. }
  196. c->push_input(p_event);
  197. }
  198. }
  199. bool SubViewportContainer::_is_propagated_in_gui_input(const Ref<InputEvent> &p_event) {
  200. // Propagation of events with a position property happen in gui_input
  201. // Propagation of other events happen in input
  202. 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)) {
  203. return true;
  204. }
  205. return false;
  206. }
  207. void SubViewportContainer::set_consume_drag_and_drop(bool p_enable) {
  208. consume_drag_and_drop = p_enable;
  209. }
  210. bool SubViewportContainer::is_consume_drag_and_drop_enabled() {
  211. return consume_drag_and_drop;
  212. }
  213. void SubViewportContainer::add_child_notify(Node *p_child) {
  214. if (Object::cast_to<SubViewport>(p_child)) {
  215. queue_redraw();
  216. }
  217. }
  218. void SubViewportContainer::remove_child_notify(Node *p_child) {
  219. if (Object::cast_to<SubViewport>(p_child)) {
  220. queue_redraw();
  221. }
  222. }
  223. PackedStringArray SubViewportContainer::get_configuration_warnings() const {
  224. PackedStringArray warnings = Container::get_configuration_warnings();
  225. bool has_viewport = false;
  226. for (int i = 0; i < get_child_count(); i++) {
  227. if (Object::cast_to<SubViewport>(get_child(i))) {
  228. has_viewport = true;
  229. break;
  230. }
  231. }
  232. if (!has_viewport) {
  233. 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."));
  234. }
  235. if (get_default_cursor_shape() != Control::CURSOR_ARROW) {
  236. warnings.push_back(RTR("The default mouse cursor shape of SubViewportContainer has no effect.\nConsider leaving it at its initial value `CURSOR_ARROW`."));
  237. }
  238. return warnings;
  239. }
  240. void SubViewportContainer::_bind_methods() {
  241. ClassDB::bind_method(D_METHOD("set_stretch", "enable"), &SubViewportContainer::set_stretch);
  242. ClassDB::bind_method(D_METHOD("is_stretch_enabled"), &SubViewportContainer::is_stretch_enabled);
  243. ClassDB::bind_method(D_METHOD("set_stretch_shrink", "amount"), &SubViewportContainer::set_stretch_shrink);
  244. ClassDB::bind_method(D_METHOD("get_stretch_shrink"), &SubViewportContainer::get_stretch_shrink);
  245. ClassDB::bind_method(D_METHOD("set_consume_drag_and_drop", "amount"), &SubViewportContainer::set_consume_drag_and_drop);
  246. ClassDB::bind_method(D_METHOD("is_consume_drag_and_drop_enabled"), &SubViewportContainer::is_consume_drag_and_drop_enabled);
  247. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch"), "set_stretch", "is_stretch_enabled");
  248. ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_shrink", PROPERTY_HINT_RANGE, "1,32,1,or_greater"), "set_stretch_shrink", "get_stretch_shrink");
  249. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "consume_drag_and_drop"), "set_consume_drag_and_drop", "is_consume_drag_and_drop_enabled");
  250. GDVIRTUAL_BIND(_propagate_input_event, "event");
  251. }
  252. SubViewportContainer::SubViewportContainer() {
  253. set_process_unhandled_input(true);
  254. set_focus_mode(FOCUS_CLICK);
  255. }