| 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 | |
| 31 | #include "subviewport_container.h" |
| 32 | |
| 33 | #include "core/config/engine.h" |
| 34 | #include "scene/main/viewport.h" |
| 35 | |
| 36 | Size2 SubViewportContainer::get_minimum_size() const { |
| 37 | if (stretch) { |
| 38 | return Size2(); |
| 39 | } |
| 40 | Size2 ms; |
| 41 | for (int i = 0; i < get_child_count(); i++) { |
| 42 | SubViewport *c = Object::cast_to<SubViewport>(get_child(i)); |
| 43 | if (!c) { |
| 44 | continue; |
| 45 | } |
| 46 | |
| 47 | Size2 minsize = c->get_size(); |
| 48 | ms = ms.max(minsize); |
| 49 | } |
| 50 | |
| 51 | return ms; |
| 52 | } |
| 53 | |
| 54 | void SubViewportContainer::set_stretch(bool p_enable) { |
| 55 | if (stretch == p_enable) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | stretch = p_enable; |
| 60 | recalc_force_viewport_sizes(); |
| 61 | update_minimum_size(); |
| 62 | queue_sort(); |
| 63 | queue_redraw(); |
| 64 | } |
| 65 | |
| 66 | bool SubViewportContainer::is_stretch_enabled() const { |
| 67 | return stretch; |
| 68 | } |
| 69 | |
| 70 | void SubViewportContainer::set_stretch_shrink(int p_shrink) { |
| 71 | ERR_FAIL_COND(p_shrink < 1); |
| 72 | if (shrink == p_shrink) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | shrink = p_shrink; |
| 77 | |
| 78 | recalc_force_viewport_sizes(); |
| 79 | queue_redraw(); |
| 80 | } |
| 81 | |
| 82 | void SubViewportContainer::recalc_force_viewport_sizes() { |
| 83 | if (!stretch) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | // If stretch is enabled, make sure that all child SubViwewports have the correct size. |
| 88 | for (int i = 0; i < get_child_count(); i++) { |
| 89 | SubViewport *c = Object::cast_to<SubViewport>(get_child(i)); |
| 90 | if (!c) { |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | c->set_size_force(get_size() / shrink); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | int SubViewportContainer::get_stretch_shrink() const { |
| 99 | return shrink; |
| 100 | } |
| 101 | |
| 102 | Vector<int> SubViewportContainer::get_allowed_size_flags_horizontal() const { |
| 103 | return Vector<int>(); |
| 104 | } |
| 105 | |
| 106 | Vector<int> SubViewportContainer::get_allowed_size_flags_vertical() const { |
| 107 | return Vector<int>(); |
| 108 | } |
| 109 | |
| 110 | void SubViewportContainer::_notification(int p_what) { |
| 111 | switch (p_what) { |
| 112 | case NOTIFICATION_RESIZED: { |
| 113 | recalc_force_viewport_sizes(); |
| 114 | } break; |
| 115 | |
| 116 | case NOTIFICATION_ENTER_TREE: |
| 117 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 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 | |
| 124 | if (is_visible_in_tree()) { |
| 125 | c->set_update_mode(SubViewport::UPDATE_ALWAYS); |
| 126 | } else { |
| 127 | c->set_update_mode(SubViewport::UPDATE_DISABLED); |
| 128 | } |
| 129 | |
| 130 | c->set_handle_input_locally(false); //do not handle input locally here |
| 131 | } |
| 132 | } break; |
| 133 | |
| 134 | case NOTIFICATION_DRAW: { |
| 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 | |
| 141 | if (stretch) { |
| 142 | draw_texture_rect(c->get_texture(), Rect2(Vector2(), get_size())); |
| 143 | } else { |
| 144 | draw_texture_rect(c->get_texture(), Rect2(Vector2(), c->get_size())); |
| 145 | } |
| 146 | } |
| 147 | } break; |
| 148 | |
| 149 | case NOTIFICATION_FOCUS_ENTER: { |
| 150 | // If focused, send InputEvent to the SubViewport before the Gui-Input stage. |
| 151 | set_process_input(true); |
| 152 | set_process_unhandled_input(false); |
| 153 | } break; |
| 154 | |
| 155 | case NOTIFICATION_FOCUS_EXIT: { |
| 156 | // A different Control has focus and should receive Gui-Input before the InputEvent is sent to the SubViewport. |
| 157 | set_process_input(false); |
| 158 | set_process_unhandled_input(true); |
| 159 | } break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void SubViewportContainer::_notify_viewports(int p_notification) { |
| 164 | for (int i = 0; i < get_child_count(); i++) { |
| 165 | SubViewport *c = Object::cast_to<SubViewport>(get_child(i)); |
| 166 | if (!c) { |
| 167 | continue; |
| 168 | } |
| 169 | c->notification(p_notification); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void SubViewportContainer::input(const Ref<InputEvent> &p_event) { |
| 174 | _propagate_nonpositional_event(p_event); |
| 175 | } |
| 176 | |
| 177 | void SubViewportContainer::unhandled_input(const Ref<InputEvent> &p_event) { |
| 178 | _propagate_nonpositional_event(p_event); |
| 179 | } |
| 180 | |
| 181 | void SubViewportContainer::_propagate_nonpositional_event(const Ref<InputEvent> &p_event) { |
| 182 | ERR_FAIL_COND(p_event.is_null()); |
| 183 | |
| 184 | if (Engine::get_singleton()->is_editor_hint()) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | if (_is_propagated_in_gui_input(p_event)) { |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | bool send; |
| 193 | if (GDVIRTUAL_CALL(_propagate_input_event, p_event, send)) { |
| 194 | if (!send) { |
| 195 | return; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | _send_event_to_viewports(p_event); |
| 200 | } |
| 201 | |
| 202 | void SubViewportContainer::gui_input(const Ref<InputEvent> &p_event) { |
| 203 | ERR_FAIL_COND(p_event.is_null()); |
| 204 | |
| 205 | if (Engine::get_singleton()->is_editor_hint()) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | if (!_is_propagated_in_gui_input(p_event)) { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | bool send; |
| 214 | if (GDVIRTUAL_CALL(_propagate_input_event, p_event, send)) { |
| 215 | if (!send) { |
| 216 | return; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (stretch && shrink > 1) { |
| 221 | Transform2D xform; |
| 222 | xform.scale(Vector2(1, 1) / shrink); |
| 223 | _send_event_to_viewports(p_event->xformed_by(xform)); |
| 224 | } else { |
| 225 | _send_event_to_viewports(p_event); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void SubViewportContainer::_send_event_to_viewports(const Ref<InputEvent> &p_event) { |
| 230 | for (int i = 0; i < get_child_count(); i++) { |
| 231 | SubViewport *c = Object::cast_to<SubViewport>(get_child(i)); |
| 232 | if (!c || c->is_input_disabled()) { |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | c->push_input(p_event); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | bool SubViewportContainer::_is_propagated_in_gui_input(const Ref<InputEvent> &p_event) { |
| 241 | // Propagation of events with a position property happen in gui_input |
| 242 | // Propagation of other events happen in input |
| 243 | 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)) { |
| 244 | return true; |
| 245 | } |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | void SubViewportContainer::set_mouse_target(bool p_enable) { |
| 250 | mouse_target = p_enable; |
| 251 | } |
| 252 | |
| 253 | bool SubViewportContainer::is_mouse_target_enabled() { |
| 254 | return mouse_target; |
| 255 | } |
| 256 | |
| 257 | void SubViewportContainer::add_child_notify(Node *p_child) { |
| 258 | if (Object::cast_to<SubViewport>(p_child)) { |
| 259 | queue_redraw(); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | void SubViewportContainer::remove_child_notify(Node *p_child) { |
| 264 | if (Object::cast_to<SubViewport>(p_child)) { |
| 265 | queue_redraw(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | PackedStringArray SubViewportContainer::get_configuration_warnings() const { |
| 270 | PackedStringArray warnings = Container::get_configuration_warnings(); |
| 271 | |
| 272 | bool has_viewport = false; |
| 273 | for (int i = 0; i < get_child_count(); i++) { |
| 274 | if (Object::cast_to<SubViewport>(get_child(i))) { |
| 275 | has_viewport = true; |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | if (!has_viewport) { |
| 280 | 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.")); |
| 281 | } |
| 282 | |
| 283 | if (get_default_cursor_shape() != Control::CURSOR_ARROW) { |
| 284 | warnings.push_back(RTR("The default mouse cursor shape of SubViewportContainer has no effect.\nConsider leaving it at its initial value `CURSOR_ARROW`.")); |
| 285 | } |
| 286 | |
| 287 | return warnings; |
| 288 | } |
| 289 | |
| 290 | void SubViewportContainer::_bind_methods() { |
| 291 | ClassDB::bind_method(D_METHOD("set_stretch", "enable"), &SubViewportContainer::set_stretch); |
| 292 | ClassDB::bind_method(D_METHOD("is_stretch_enabled"), &SubViewportContainer::is_stretch_enabled); |
| 293 | |
| 294 | ClassDB::bind_method(D_METHOD("set_stretch_shrink", "amount"), &SubViewportContainer::set_stretch_shrink); |
| 295 | ClassDB::bind_method(D_METHOD("get_stretch_shrink"), &SubViewportContainer::get_stretch_shrink); |
| 296 | |
| 297 | ClassDB::bind_method(D_METHOD("set_mouse_target", "amount"), &SubViewportContainer::set_mouse_target); |
| 298 | ClassDB::bind_method(D_METHOD("is_mouse_target_enabled"), &SubViewportContainer::is_mouse_target_enabled); |
| 299 | |
| 300 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch"), "set_stretch", "is_stretch_enabled"); |
| 301 | ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_shrink", PROPERTY_HINT_RANGE, "1,32,1,or_greater"), "set_stretch_shrink", "get_stretch_shrink"); |
| 302 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mouse_target"), "set_mouse_target", "is_mouse_target_enabled"); |
| 303 | |
| 304 | GDVIRTUAL_BIND(_propagate_input_event, "event"); |
| 305 | } |
| 306 | |
| 307 | SubViewportContainer::SubViewportContainer() { |
| 308 | set_process_unhandled_input(true); |
| 309 | set_focus_mode(FOCUS_CLICK); |
| 310 | } |