From 0c6851ae53c1c7b7aff743d05c658cb28e1af576 Mon Sep 17 00:00:00 2001 From: Michael Alexsander Date: Wed, 1 Apr 2020 15:28:09 -0300 Subject: Rename ViewportContainer to SubViewportContainer --- doc/classes/SubViewportContainer.xml | 25 ++++ doc/classes/Viewport.xml | 2 +- doc/classes/ViewportContainer.xml | 25 ---- editor/icons/SubViewportContainer.svg | 1 + editor/icons/ViewportContainer.svg | 1 - editor/plugins/canvas_item_editor_plugin.cpp | 4 +- editor/plugins/material_editor_plugin.cpp | 4 +- editor/plugins/material_editor_plugin.h | 4 +- editor/plugins/mesh_editor_plugin.h | 6 +- editor/plugins/node_3d_editor_plugin.cpp | 24 +-- editor/plugins/node_3d_editor_plugin.h | 4 +- scene/gui/subviewport_container.cpp | 215 +++++++++++++++++++++++++++ scene/gui/subviewport_container.h | 61 ++++++++ scene/gui/viewport_container.cpp | 215 --------------------------- scene/gui/viewport_container.h | 61 -------- scene/register_scene_types.cpp | 5 +- 16 files changed, 329 insertions(+), 328 deletions(-) create mode 100644 doc/classes/SubViewportContainer.xml delete mode 100644 doc/classes/ViewportContainer.xml create mode 100644 editor/icons/SubViewportContainer.svg delete mode 100644 editor/icons/ViewportContainer.svg create mode 100644 scene/gui/subviewport_container.cpp create mode 100644 scene/gui/subviewport_container.h delete mode 100644 scene/gui/viewport_container.cpp delete mode 100644 scene/gui/viewport_container.h diff --git a/doc/classes/SubViewportContainer.xml b/doc/classes/SubViewportContainer.xml new file mode 100644 index 0000000000..e6a0bd4866 --- /dev/null +++ b/doc/classes/SubViewportContainer.xml @@ -0,0 +1,25 @@ + + + + Control for holding [SubViewport]s. + + + A [Container] node that holds a [SubViewport], automatically setting its size. + + + + + + + + If [code]true[/code], the sub-viewport will be scaled to the control's size. + + + Divides the sub-viewport's effective resolution by this value while preserving its scale. This can be used to speed up rendering. + For example, a 1280×720 sub-viewport with [member stretch_shrink] set to [code]2[/code] will be rendered at 640×360 while occupying the same size in the container. + [b]Note:[/b] [member stretch] must be [code]true[/code] for this property to work. + + + + + diff --git a/doc/classes/Viewport.xml b/doc/classes/Viewport.xml index 517eb3b24c..5826822c6e 100644 --- a/doc/classes/Viewport.xml +++ b/doc/classes/Viewport.xml @@ -6,7 +6,7 @@ A Viewport creates a different view into the screen, or a sub-view inside another viewport. Children 2D Nodes will display on it, and children Camera3D 3D nodes will render on it too. Optionally, a viewport can have its own 2D or 3D world, so they don't share what they draw with other viewports. - If a viewport is a child of a [ViewportContainer], it will automatically take up its size, otherwise it must be set manually. + If a viewport is a child of a [SubViewportContainer], it will automatically take up its size, otherwise it must be set manually. Viewports can also choose to be audio listeners, so they generate positional audio depending on a 2D or 3D camera child of it. Also, viewports can be assigned to different screens in case the devices have multiple screens. Finally, viewports can also behave as render targets, in which case they will not be visible unless the associated texture is used to draw. diff --git a/doc/classes/ViewportContainer.xml b/doc/classes/ViewportContainer.xml deleted file mode 100644 index d2fbb85305..0000000000 --- a/doc/classes/ViewportContainer.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - Control for holding [Viewport]s. - - - A [Container] node that holds a [Viewport], automatically setting its size. - - - - - - - - If [code]true[/code], the viewport will be scaled to the control's size. - - - Divides the viewport's effective resolution by this value while preserving its scale. This can be used to speed up rendering. - For example, a 1280×720 viewport with [member stretch_shrink] set to [code]2[/code] will be rendered at 640×360 while occupying the same size in the container. - [b]Note:[/b] [member stretch] must be [code]true[/code] for this property to work. - - - - - diff --git a/editor/icons/SubViewportContainer.svg b/editor/icons/SubViewportContainer.svg new file mode 100644 index 0000000000..18dcddc15f --- /dev/null +++ b/editor/icons/SubViewportContainer.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/editor/icons/ViewportContainer.svg b/editor/icons/ViewportContainer.svg deleted file mode 100644 index 18dcddc15f..0000000000 --- a/editor/icons/ViewportContainer.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index c7ddcd5e46..7d45a28403 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -48,7 +48,7 @@ #include "scene/2d/touch_screen_button.h" #include "scene/gui/grid_container.h" #include "scene/gui/nine_patch_rect.h" -#include "scene/gui/viewport_container.h" +#include "scene/gui/subviewport_container.h" #include "scene/main/canvas_layer.h" #include "scene/main/window.h" #include "scene/resources/packed_scene.h" @@ -5434,7 +5434,7 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { viewport_scrollable->set_h_size_flags(Control::SIZE_EXPAND_FILL); viewport_scrollable->connect("draw", callable_mp(this, &CanvasItemEditor::_update_scrollbars)); - ViewportContainer *scene_tree = memnew(ViewportContainer); + SubViewportContainer *scene_tree = memnew(SubViewportContainer); viewport_scrollable->add_child(scene_tree); scene_tree->set_stretch(true); scene_tree->set_anchors_and_margins_preset(Control::PRESET_WIDE); diff --git a/editor/plugins/material_editor_plugin.cpp b/editor/plugins/material_editor_plugin.cpp index 5623805201..00e8a05e4e 100644 --- a/editor/plugins/material_editor_plugin.cpp +++ b/editor/plugins/material_editor_plugin.cpp @@ -31,7 +31,7 @@ #include "material_editor_plugin.h" #include "editor/editor_scale.h" -#include "scene/gui/viewport_container.h" +#include "scene/gui/subviewport_container.h" #include "scene/resources/particles_material.h" #include "scene/resources/sky_material.h" @@ -110,7 +110,7 @@ void MaterialEditor::_bind_methods() { MaterialEditor::MaterialEditor() { - vc = memnew(ViewportContainer); + vc = memnew(SubViewportContainer); vc->set_stretch(true); add_child(vc); vc->set_anchors_and_margins_preset(PRESET_WIDE); diff --git a/editor/plugins/material_editor_plugin.h b/editor/plugins/material_editor_plugin.h index 0938e79ca2..50036e4f72 100644 --- a/editor/plugins/material_editor_plugin.h +++ b/editor/plugins/material_editor_plugin.h @@ -41,13 +41,13 @@ #include "scene/3d/mesh_instance_3d.h" #include "scene/resources/material.h" -class ViewportContainer; +class SubViewportContainer; class MaterialEditor : public Control { GDCLASS(MaterialEditor, Control); - ViewportContainer *vc; + SubViewportContainer *vc; SubViewport *viewport; MeshInstance3D *sphere_instance; MeshInstance3D *box_instance; diff --git a/editor/plugins/mesh_editor_plugin.h b/editor/plugins/mesh_editor_plugin.h index 59810517d9..072e21f260 100644 --- a/editor/plugins/mesh_editor_plugin.h +++ b/editor/plugins/mesh_editor_plugin.h @@ -36,12 +36,12 @@ #include "scene/3d/camera_3d.h" #include "scene/3d/light_3d.h" #include "scene/3d/mesh_instance_3d.h" -#include "scene/gui/viewport_container.h" +#include "scene/gui/subviewport_container.h" #include "scene/resources/material.h" -class MeshEditor : public ViewportContainer { +class MeshEditor : public SubViewportContainer { - GDCLASS(MeshEditor, ViewportContainer); + GDCLASS(MeshEditor, SubViewportContainer); float rot_x; float rot_y; diff --git a/editor/plugins/node_3d_editor_plugin.cpp b/editor/plugins/node_3d_editor_plugin.cpp index 01868ba77e..71ce17ef3e 100644 --- a/editor/plugins/node_3d_editor_plugin.cpp +++ b/editor/plugins/node_3d_editor_plugin.cpp @@ -48,7 +48,7 @@ #include "scene/3d/mesh_instance_3d.h" #include "scene/3d/physics_body_3d.h" #include "scene/3d/visual_instance_3d.h" -#include "scene/gui/viewport_container.h" +#include "scene/gui/subviewport_container.h" #include "scene/resources/packed_scene.h" #include "scene/resources/surface_tool.h" #include "servers/display_server.h" @@ -418,12 +418,12 @@ Vector3 Node3DEditorViewport::_get_camera_position() const { Point2 Node3DEditorViewport::_point_to_screen(const Vector3 &p_point) { - return camera->unproject_position(p_point) * viewport_container->get_stretch_shrink(); + return camera->unproject_position(p_point) * subviewport_container->get_stretch_shrink(); } Vector3 Node3DEditorViewport::_get_ray_pos(const Vector2 &p_pos) const { - return camera->project_ray_origin(p_pos / viewport_container->get_stretch_shrink()); + return camera->project_ray_origin(p_pos / subviewport_container->get_stretch_shrink()); } Vector3 Node3DEditorViewport::_get_camera_normal() const { @@ -433,7 +433,7 @@ Vector3 Node3DEditorViewport::_get_camera_normal() const { Vector3 Node3DEditorViewport::_get_ray(const Vector2 &p_pos) const { - return camera->project_ray_normal(p_pos / viewport_container->get_stretch_shrink()); + return camera->project_ray_normal(p_pos / subviewport_container->get_stretch_shrink()); } void Node3DEditorViewport::_clear_selected() { @@ -494,7 +494,7 @@ ObjectID Node3DEditorViewport::_select_ray(const Point2 &p_pos, bool p_append, b Vector3 ray = _get_ray(p_pos); Vector3 pos = _get_ray_pos(p_pos); - Vector2 shrinked_pos = p_pos / viewport_container->get_stretch_shrink(); + Vector2 shrinked_pos = p_pos / subviewport_container->get_stretch_shrink(); Vector instances = RenderingServer::get_singleton()->instances_cull_ray(pos, ray, get_tree()->get_root()->get_world()->get_scenario()); Set> found_gizmos; @@ -2472,8 +2472,8 @@ void Node3DEditorViewport::_notification(int p_what) { bool shrink = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION)); - if (shrink != (viewport_container->get_stretch_shrink() > 1)) { - viewport_container->set_stretch_shrink(shrink ? 2 : 1); + if (shrink != (subviewport_container->get_stretch_shrink() > 1)) { + subviewport_container->set_stretch_shrink(shrink ? 2 : 1); } //update msaa if changed @@ -3237,8 +3237,8 @@ void Node3DEditorViewport::update_transform_gizmo_view() { const int viewport_base_height = 400 * MAX(1, EDSCALE); gizmo_scale = (gizmo_size / Math::abs(dd)) * MAX(1, EDSCALE) * - MIN(viewport_base_height, viewport_container->get_size().height) / viewport_base_height / - viewport_container->get_stretch_shrink(); + MIN(viewport_base_height, subviewport_container->get_size().height) / viewport_base_height / + subviewport_container->get_stretch_shrink(); Vector3 scale = Vector3(1, 1, 1) * gizmo_scale; xform.basis.scale(scale); @@ -3398,7 +3398,7 @@ Dictionary Node3DEditorViewport::get_state() const { d["gizmos"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS)); d["information"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION)); d["fps"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_FPS)); - d["half_res"] = viewport_container->get_stretch_shrink() > 1; + d["half_res"] = subviewport_container->get_stretch_shrink() > 1; d["cinematic_preview"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW)); if (previewing) d["previewing"] = EditorNode::get_singleton()->get_edited_scene()->get_path_to(previewing); @@ -3829,8 +3829,8 @@ Node3DEditorViewport::Node3DEditorViewport(Node3DEditor *p_spatial_editor, Edito zoom_indicator_delay = 0.0; spatial_editor = p_spatial_editor; - ViewportContainer *c = memnew(ViewportContainer); - viewport_container = c; + SubViewportContainer *c = memnew(SubViewportContainer); + subviewport_container = c; c->set_stretch(true); add_child(c); c->set_anchors_and_margins_preset(Control::PRESET_WIDE); diff --git a/editor/plugins/node_3d_editor_plugin.h b/editor/plugins/node_3d_editor_plugin.h index 4b9f5a605b..340f05276c 100644 --- a/editor/plugins/node_3d_editor_plugin.h +++ b/editor/plugins/node_3d_editor_plugin.h @@ -43,7 +43,7 @@ class Camera3D; class Node3DEditor; class EditorNode3DGizmoPlugin; class Node3DEditorViewport; -class ViewportContainer; +class SubViewportContainer; class EditorNode3DGizmo : public Node3DGizmo { @@ -256,7 +256,7 @@ private: UndoRedo *undo_redo; CheckBox *preview_camera; - ViewportContainer *viewport_container; + SubViewportContainer *subviewport_container; MenuButton *view_menu; PopupMenu *display_submenu; diff --git a/scene/gui/subviewport_container.cpp b/scene/gui/subviewport_container.cpp new file mode 100644 index 0000000000..50f468741d --- /dev/null +++ b/scene/gui/subviewport_container.cpp @@ -0,0 +1,215 @@ +/*************************************************************************/ +/* subviewport_container.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "subviewport_container.h" + +#include "core/engine.h" +#include "scene/main/viewport.h" + +Size2 SubViewportContainer::get_minimum_size() const { + + if (stretch) + return Size2(); + Size2 ms; + for (int i = 0; i < get_child_count(); i++) { + + SubViewport *c = Object::cast_to(get_child(i)); + if (!c) + continue; + + Size2 minsize = c->get_size(); + ms.width = MAX(ms.width, minsize.width); + ms.height = MAX(ms.height, minsize.height); + } + + return ms; +} + +void SubViewportContainer::set_stretch(bool p_enable) { + + stretch = p_enable; + queue_sort(); + update(); +} + +bool SubViewportContainer::is_stretch_enabled() const { + + return stretch; +} + +void SubViewportContainer::set_stretch_shrink(int p_shrink) { + + ERR_FAIL_COND(p_shrink < 1); + if (shrink == p_shrink) + return; + + shrink = p_shrink; + + if (!stretch) + return; + + for (int i = 0; i < get_child_count(); i++) { + + SubViewport *c = Object::cast_to(get_child(i)); + if (!c) + continue; + + c->set_size(get_size() / shrink); + } + + update(); +} + +int SubViewportContainer::get_stretch_shrink() const { + + return shrink; +} + +void SubViewportContainer::_notification(int p_what) { + + if (p_what == NOTIFICATION_RESIZED) { + + if (!stretch) + return; + + for (int i = 0; i < get_child_count(); i++) { + + SubViewport *c = Object::cast_to(get_child(i)); + if (!c) + continue; + + c->set_size(get_size() / shrink); + } + } + + if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_VISIBILITY_CHANGED) { + + for (int i = 0; i < get_child_count(); i++) { + + SubViewport *c = Object::cast_to(get_child(i)); + if (!c) + continue; + + if (is_visible_in_tree()) + c->set_update_mode(SubViewport::UPDATE_ALWAYS); + else + c->set_update_mode(SubViewport::UPDATE_DISABLED); + + c->set_handle_input_locally(false); //do not handle input locally here + } + } + + if (p_what == NOTIFICATION_DRAW) { + + for (int i = 0; i < get_child_count(); i++) { + + SubViewport *c = Object::cast_to(get_child(i)); + if (!c) + continue; + + if (stretch) + draw_texture_rect(c->get_texture(), Rect2(Vector2(), get_size())); + else + draw_texture_rect(c->get_texture(), Rect2(Vector2(), c->get_size())); + } + } +} + +void SubViewportContainer::_input(const Ref &p_event) { + + if (Engine::get_singleton()->is_editor_hint()) + return; + + Transform2D xform = get_global_transform(); + + if (stretch) { + Transform2D scale_xf; + scale_xf.scale(Vector2(shrink, shrink)); + xform *= scale_xf; + } + + Ref ev = p_event->xformed_by(xform.affine_inverse()); + + for (int i = 0; i < get_child_count(); i++) { + + SubViewport *c = Object::cast_to(get_child(i)); + if (!c || c->is_input_disabled()) + continue; + + c->input(ev); + } +} + +void SubViewportContainer::_unhandled_input(const Ref &p_event) { + + if (Engine::get_singleton()->is_editor_hint()) + return; + + Transform2D xform = get_global_transform(); + + if (stretch) { + Transform2D scale_xf; + scale_xf.scale(Vector2(shrink, shrink)); + xform *= scale_xf; + } + + Ref ev = p_event->xformed_by(xform.affine_inverse()); + + for (int i = 0; i < get_child_count(); i++) { + + SubViewport *c = Object::cast_to(get_child(i)); + if (!c || c->is_input_disabled()) + continue; + + c->unhandled_input(ev); + } +} + +void SubViewportContainer::_bind_methods() { + + ClassDB::bind_method(D_METHOD("_unhandled_input", "event"), &SubViewportContainer::_unhandled_input); + ClassDB::bind_method(D_METHOD("_input", "event"), &SubViewportContainer::_input); + ClassDB::bind_method(D_METHOD("set_stretch", "enable"), &SubViewportContainer::set_stretch); + ClassDB::bind_method(D_METHOD("is_stretch_enabled"), &SubViewportContainer::is_stretch_enabled); + + ClassDB::bind_method(D_METHOD("set_stretch_shrink", "amount"), &SubViewportContainer::set_stretch_shrink); + ClassDB::bind_method(D_METHOD("get_stretch_shrink"), &SubViewportContainer::get_stretch_shrink); + + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch"), "set_stretch", "is_stretch_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_shrink"), "set_stretch_shrink", "get_stretch_shrink"); +} + +SubViewportContainer::SubViewportContainer() { + + stretch = false; + shrink = 1; + set_process_input(true); + set_process_unhandled_input(true); +} diff --git a/scene/gui/subviewport_container.h b/scene/gui/subviewport_container.h new file mode 100644 index 0000000000..6ff3d188e2 --- /dev/null +++ b/scene/gui/subviewport_container.h @@ -0,0 +1,61 @@ +/*************************************************************************/ +/* subviewport_container.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef VIEWPORTCONTAINER_H +#define VIEWPORTCONTAINER_H + +#include "scene/gui/container.h" + +class SubViewportContainer : public Container { + + GDCLASS(SubViewportContainer, Container); + + bool stretch; + int shrink; + +protected: + void _notification(int p_what); + static void _bind_methods(); + +public: + void set_stretch(bool p_enable); + bool is_stretch_enabled() const; + + void _input(const Ref &p_event); + void _unhandled_input(const Ref &p_event); + void set_stretch_shrink(int p_shrink); + int get_stretch_shrink() const; + + virtual Size2 get_minimum_size() const; + + SubViewportContainer(); +}; + +#endif // VIEWPORTCONTAINER_H diff --git a/scene/gui/viewport_container.cpp b/scene/gui/viewport_container.cpp deleted file mode 100644 index 7ce1d9e551..0000000000 --- a/scene/gui/viewport_container.cpp +++ /dev/null @@ -1,215 +0,0 @@ -/*************************************************************************/ -/* viewport_container.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "viewport_container.h" - -#include "core/engine.h" -#include "scene/main/viewport.h" - -Size2 ViewportContainer::get_minimum_size() const { - - if (stretch) - return Size2(); - Size2 ms; - for (int i = 0; i < get_child_count(); i++) { - - SubViewport *c = Object::cast_to(get_child(i)); - if (!c) - continue; - - Size2 minsize = c->get_size(); - ms.width = MAX(ms.width, minsize.width); - ms.height = MAX(ms.height, minsize.height); - } - - return ms; -} - -void ViewportContainer::set_stretch(bool p_enable) { - - stretch = p_enable; - queue_sort(); - update(); -} - -bool ViewportContainer::is_stretch_enabled() const { - - return stretch; -} - -void ViewportContainer::set_stretch_shrink(int p_shrink) { - - ERR_FAIL_COND(p_shrink < 1); - if (shrink == p_shrink) - return; - - shrink = p_shrink; - - if (!stretch) - return; - - for (int i = 0; i < get_child_count(); i++) { - - SubViewport *c = Object::cast_to(get_child(i)); - if (!c) - continue; - - c->set_size(get_size() / shrink); - } - - update(); -} - -int ViewportContainer::get_stretch_shrink() const { - - return shrink; -} - -void ViewportContainer::_notification(int p_what) { - - if (p_what == NOTIFICATION_RESIZED) { - - if (!stretch) - return; - - for (int i = 0; i < get_child_count(); i++) { - - SubViewport *c = Object::cast_to(get_child(i)); - if (!c) - continue; - - c->set_size(get_size() / shrink); - } - } - - if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_VISIBILITY_CHANGED) { - - for (int i = 0; i < get_child_count(); i++) { - - SubViewport *c = Object::cast_to(get_child(i)); - if (!c) - continue; - - if (is_visible_in_tree()) - c->set_update_mode(SubViewport::UPDATE_ALWAYS); - else - c->set_update_mode(SubViewport::UPDATE_DISABLED); - - c->set_handle_input_locally(false); //do not handle input locally here - } - } - - if (p_what == NOTIFICATION_DRAW) { - - for (int i = 0; i < get_child_count(); i++) { - - SubViewport *c = Object::cast_to(get_child(i)); - if (!c) - continue; - - if (stretch) - draw_texture_rect(c->get_texture(), Rect2(Vector2(), get_size())); - else - draw_texture_rect(c->get_texture(), Rect2(Vector2(), c->get_size())); - } - } -} - -void ViewportContainer::_input(const Ref &p_event) { - - if (Engine::get_singleton()->is_editor_hint()) - return; - - Transform2D xform = get_global_transform(); - - if (stretch) { - Transform2D scale_xf; - scale_xf.scale(Vector2(shrink, shrink)); - xform *= scale_xf; - } - - Ref ev = p_event->xformed_by(xform.affine_inverse()); - - for (int i = 0; i < get_child_count(); i++) { - - SubViewport *c = Object::cast_to(get_child(i)); - if (!c || c->is_input_disabled()) - continue; - - c->input(ev); - } -} - -void ViewportContainer::_unhandled_input(const Ref &p_event) { - - if (Engine::get_singleton()->is_editor_hint()) - return; - - Transform2D xform = get_global_transform(); - - if (stretch) { - Transform2D scale_xf; - scale_xf.scale(Vector2(shrink, shrink)); - xform *= scale_xf; - } - - Ref ev = p_event->xformed_by(xform.affine_inverse()); - - for (int i = 0; i < get_child_count(); i++) { - - SubViewport *c = Object::cast_to(get_child(i)); - if (!c || c->is_input_disabled()) - continue; - - c->unhandled_input(ev); - } -} - -void ViewportContainer::_bind_methods() { - - ClassDB::bind_method(D_METHOD("_unhandled_input", "event"), &ViewportContainer::_unhandled_input); - ClassDB::bind_method(D_METHOD("_input", "event"), &ViewportContainer::_input); - ClassDB::bind_method(D_METHOD("set_stretch", "enable"), &ViewportContainer::set_stretch); - ClassDB::bind_method(D_METHOD("is_stretch_enabled"), &ViewportContainer::is_stretch_enabled); - - ClassDB::bind_method(D_METHOD("set_stretch_shrink", "amount"), &ViewportContainer::set_stretch_shrink); - ClassDB::bind_method(D_METHOD("get_stretch_shrink"), &ViewportContainer::get_stretch_shrink); - - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch"), "set_stretch", "is_stretch_enabled"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_shrink"), "set_stretch_shrink", "get_stretch_shrink"); -} - -ViewportContainer::ViewportContainer() { - - stretch = false; - shrink = 1; - set_process_input(true); - set_process_unhandled_input(true); -} diff --git a/scene/gui/viewport_container.h b/scene/gui/viewport_container.h deleted file mode 100644 index 8597444426..0000000000 --- a/scene/gui/viewport_container.h +++ /dev/null @@ -1,61 +0,0 @@ -/*************************************************************************/ -/* viewport_container.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef VIEWPORTCONTAINER_H -#define VIEWPORTCONTAINER_H - -#include "scene/gui/container.h" - -class ViewportContainer : public Container { - - GDCLASS(ViewportContainer, Container); - - bool stretch; - int shrink; - -protected: - void _notification(int p_what); - static void _bind_methods(); - -public: - void set_stretch(bool p_enable); - bool is_stretch_enabled() const; - - void _input(const Ref &p_event); - void _unhandled_input(const Ref &p_event); - void set_stretch_shrink(int p_shrink); - int get_stretch_shrink() const; - - virtual Size2 get_minimum_size() const; - - ViewportContainer(); -}; - -#endif // VIEWPORTCONTAINER_H diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index 22c000fae7..76fbc9dbf8 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -110,6 +110,7 @@ #include "scene/gui/slider.h" #include "scene/gui/spin_box.h" #include "scene/gui/split_container.h" +#include "scene/gui/subviewport_container.h" #include "scene/gui/tab_container.h" #include "scene/gui/tabs.h" #include "scene/gui/text_edit.h" @@ -119,7 +120,6 @@ #include "scene/gui/tool_button.h" #include "scene/gui/tree.h" #include "scene/gui/video_player.h" -#include "scene/gui/viewport_container.h" #include "scene/main/canvas_item.h" #include "scene/main/canvas_layer.h" #include "scene/main/http_request.h" @@ -357,7 +357,7 @@ void register_scene_types() { ClassDB::register_class(); ClassDB::register_class(); - ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_virtual_class(); ClassDB::register_class(); ClassDB::register_class(); @@ -842,6 +842,7 @@ void register_scene_types() { ClassDB::add_compatibility_class("StaticBody", "StaticBody3D"); ClassDB::add_compatibility_class("VehicleBody", "VehicleBody3D"); ClassDB::add_compatibility_class("VehicleWheel", "VehicleWheel3D"); + ClassDB::add_compatibility_class("ViewportContainer", "SubViewportContainer"); ClassDB::add_compatibility_class("VisibilityEnabler", "VisibilityEnabler3D"); ClassDB::add_compatibility_class("VisibilityNotifier", "VisibilityNotifier3D"); ClassDB::add_compatibility_class("VisualServer", "RenderingServer"); -- cgit v1.2.3