1 /*************************************************************************/
2 /* viewport_container.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
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 "viewport_container.h"
32
33 #include "core/engine.h"
34 #include "scene/main/viewport.h"
35
get_minimum_size() const36 Size2 ViewportContainer::get_minimum_size() const {
37
38 if (stretch)
39 return Size2();
40 Size2 ms;
41 for (int i = 0; i < get_child_count(); i++) {
42
43 Viewport *c = Object::cast_to<Viewport>(get_child(i));
44 if (!c)
45 continue;
46
47 Size2 minsize = c->get_size();
48 ms.width = MAX(ms.width, minsize.width);
49 ms.height = MAX(ms.height, minsize.height);
50 }
51
52 return ms;
53 }
54
set_stretch(bool p_enable)55 void ViewportContainer::set_stretch(bool p_enable) {
56
57 stretch = p_enable;
58 queue_sort();
59 update();
60 }
61
is_stretch_enabled() const62 bool ViewportContainer::is_stretch_enabled() const {
63
64 return stretch;
65 }
66
set_stretch_shrink(int p_shrink)67 void ViewportContainer::set_stretch_shrink(int p_shrink) {
68
69 ERR_FAIL_COND(p_shrink < 1);
70 if (shrink == p_shrink)
71 return;
72
73 shrink = p_shrink;
74
75 if (!stretch)
76 return;
77
78 for (int i = 0; i < get_child_count(); i++) {
79
80 Viewport *c = Object::cast_to<Viewport>(get_child(i));
81 if (!c)
82 continue;
83
84 c->set_size(get_size() / shrink);
85 }
86
87 update();
88 }
89
get_stretch_shrink() const90 int ViewportContainer::get_stretch_shrink() const {
91
92 return shrink;
93 }
94
_notification(int p_what)95 void ViewportContainer::_notification(int p_what) {
96
97 if (p_what == NOTIFICATION_RESIZED) {
98
99 if (!stretch)
100 return;
101
102 for (int i = 0; i < get_child_count(); i++) {
103
104 Viewport *c = Object::cast_to<Viewport>(get_child(i));
105 if (!c)
106 continue;
107
108 c->set_size(get_size() / shrink);
109 }
110 }
111
112 if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_VISIBILITY_CHANGED) {
113
114 for (int i = 0; i < get_child_count(); i++) {
115
116 Viewport *c = Object::cast_to<Viewport>(get_child(i));
117 if (!c)
118 continue;
119
120 if (is_visible_in_tree())
121 c->set_update_mode(Viewport::UPDATE_ALWAYS);
122 else
123 c->set_update_mode(Viewport::UPDATE_DISABLED);
124
125 c->set_handle_input_locally(false); //do not handle input locally here
126 }
127 }
128
129 if (p_what == NOTIFICATION_DRAW) {
130
131 for (int i = 0; i < get_child_count(); i++) {
132
133 Viewport *c = Object::cast_to<Viewport>(get_child(i));
134 if (!c)
135 continue;
136
137 if (stretch)
138 draw_texture_rect(c->get_texture(), Rect2(Vector2(), get_size() * Size2(1, -1)));
139 else
140 draw_texture_rect(c->get_texture(), Rect2(Vector2(), c->get_size() * Size2(1, -1)));
141 }
142 }
143 }
144
_input(const Ref<InputEvent> & p_event)145 void ViewportContainer::_input(const Ref<InputEvent> &p_event) {
146
147 if (Engine::get_singleton()->is_editor_hint())
148 return;
149
150 Transform2D xform = get_global_transform();
151
152 if (stretch) {
153 Transform2D scale_xf;
154 scale_xf.scale(Vector2(shrink, shrink));
155 xform *= scale_xf;
156 }
157
158 Ref<InputEvent> ev = p_event->xformed_by(xform.affine_inverse());
159
160 for (int i = 0; i < get_child_count(); i++) {
161
162 Viewport *c = Object::cast_to<Viewport>(get_child(i));
163 if (!c || c->is_input_disabled())
164 continue;
165
166 c->input(ev);
167 }
168 }
169
_unhandled_input(const Ref<InputEvent> & p_event)170 void ViewportContainer::_unhandled_input(const Ref<InputEvent> &p_event) {
171
172 if (Engine::get_singleton()->is_editor_hint())
173 return;
174
175 Transform2D xform = get_global_transform();
176
177 if (stretch) {
178 Transform2D scale_xf;
179 scale_xf.scale(Vector2(shrink, shrink));
180 xform *= scale_xf;
181 }
182
183 Ref<InputEvent> ev = p_event->xformed_by(xform.affine_inverse());
184
185 for (int i = 0; i < get_child_count(); i++) {
186
187 Viewport *c = Object::cast_to<Viewport>(get_child(i));
188 if (!c || c->is_input_disabled())
189 continue;
190
191 c->unhandled_input(ev);
192 }
193 }
194
_bind_methods()195 void ViewportContainer::_bind_methods() {
196
197 ClassDB::bind_method(D_METHOD("_unhandled_input", "event"), &ViewportContainer::_unhandled_input);
198 ClassDB::bind_method(D_METHOD("_input", "event"), &ViewportContainer::_input);
199 ClassDB::bind_method(D_METHOD("set_stretch", "enable"), &ViewportContainer::set_stretch);
200 ClassDB::bind_method(D_METHOD("is_stretch_enabled"), &ViewportContainer::is_stretch_enabled);
201
202 ClassDB::bind_method(D_METHOD("set_stretch_shrink", "amount"), &ViewportContainer::set_stretch_shrink);
203 ClassDB::bind_method(D_METHOD("get_stretch_shrink"), &ViewportContainer::get_stretch_shrink);
204
205 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch"), "set_stretch", "is_stretch_enabled");
206 ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_shrink"), "set_stretch_shrink", "get_stretch_shrink");
207 }
208
ViewportContainer()209 ViewportContainer::ViewportContainer() {
210
211 stretch = false;
212 shrink = 1;
213 set_process_input(true);
214 set_process_unhandled_input(true);
215 }
216