2023-01-10 14:26:54 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* viewport_container.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2016-10-05 04:26:35 +00:00
|
|
|
#include "viewport_container.h"
|
2017-03-05 14:47:28 +00:00
|
|
|
|
2018-01-14 23:36:57 +00:00
|
|
|
#include "core/engine.h"
|
2016-10-05 04:26:35 +00:00
|
|
|
#include "scene/main/viewport.h"
|
|
|
|
|
2017-03-05 14:47:28 +00:00
|
|
|
Size2 ViewportContainer::get_minimum_size() const {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (stretch) {
|
2016-10-05 04:26:35 +00:00
|
|
|
return Size2();
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-10-05 04:26:35 +00:00
|
|
|
Size2 ms;
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2017-08-24 20:58:51 +00:00
|
|
|
Viewport *c = Object::cast_to<Viewport>(get_child(i));
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!c) {
|
2016-10-05 04:26:35 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-10-05 04:26:35 +00:00
|
|
|
|
|
|
|
Size2 minsize = c->get_size();
|
2017-03-05 15:44:50 +00:00
|
|
|
ms.width = MAX(ms.width, minsize.width);
|
|
|
|
ms.height = MAX(ms.height, minsize.height);
|
2016-10-05 04:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewportContainer::set_stretch(bool p_enable) {
|
2017-03-05 15:44:50 +00:00
|
|
|
stretch = p_enable;
|
2022-02-02 12:21:03 +00:00
|
|
|
minimum_size_changed();
|
2016-10-05 04:26:35 +00:00
|
|
|
queue_sort();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ViewportContainer::is_stretch_enabled() const {
|
|
|
|
return stretch;
|
|
|
|
}
|
|
|
|
|
2017-10-31 17:23:25 +00:00
|
|
|
void ViewportContainer::set_stretch_shrink(int p_shrink) {
|
|
|
|
ERR_FAIL_COND(p_shrink < 1);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (shrink == p_shrink) {
|
2017-10-31 17:23:25 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-10-31 17:23:25 +00:00
|
|
|
|
|
|
|
shrink = p_shrink;
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!stretch) {
|
2017-10-31 17:23:25 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-10-31 17:23:25 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
|
|
|
Viewport *c = Object::cast_to<Viewport>(get_child(i));
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!c) {
|
2017-10-31 17:23:25 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-10-31 17:23:25 +00:00
|
|
|
|
|
|
|
c->set_size(get_size() / shrink);
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ViewportContainer::get_stretch_shrink() const {
|
|
|
|
return shrink;
|
|
|
|
}
|
|
|
|
|
2016-10-05 04:26:35 +00:00
|
|
|
void ViewportContainer::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_RESIZED) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!stretch) {
|
2016-10-05 04:26:35 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-10-05 04:26:35 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2017-08-24 20:58:51 +00:00
|
|
|
Viewport *c = Object::cast_to<Viewport>(get_child(i));
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!c) {
|
2016-10-05 04:26:35 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-10-05 04:26:35 +00:00
|
|
|
|
2017-10-31 17:23:25 +00:00
|
|
|
c->set_size(get_size() / shrink);
|
2016-10-05 04:26:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_VISIBILITY_CHANGED) {
|
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2017-08-24 20:58:51 +00:00
|
|
|
Viewport *c = Object::cast_to<Viewport>(get_child(i));
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!c) {
|
2016-10-05 04:26:35 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-10-05 04:26:35 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (is_visible_in_tree()) {
|
2016-10-05 04:26:35 +00:00
|
|
|
c->set_update_mode(Viewport::UPDATE_ALWAYS);
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2016-10-05 04:26:35 +00:00
|
|
|
c->set_update_mode(Viewport::UPDATE_DISABLED);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-11-15 16:54:26 +00:00
|
|
|
|
|
|
|
c->set_handle_input_locally(false); //do not handle input locally here
|
2016-10-05 04:26:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_DRAW) {
|
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2017-08-24 20:58:51 +00:00
|
|
|
Viewport *c = Object::cast_to<Viewport>(get_child(i));
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!c) {
|
2016-10-05 04:26:35 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-10-05 04:26:35 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (stretch) {
|
2017-03-05 15:44:50 +00:00
|
|
|
draw_texture_rect(c->get_texture(), Rect2(Vector2(), get_size() * Size2(1, -1)));
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
draw_texture_rect(c->get_texture(), Rect2(Vector2(), c->get_size() * Size2(1, -1)));
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-10-05 04:26:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 23:36:57 +00:00
|
|
|
void ViewportContainer::_input(const Ref<InputEvent> &p_event) {
|
2021-04-27 14:51:29 +00:00
|
|
|
ERR_FAIL_COND(p_event.is_null());
|
2018-01-14 23:36:57 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2018-01-14 23:36:57 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-01-14 23:36:57 +00:00
|
|
|
|
2022-04-04 20:28:06 +00:00
|
|
|
Transform2D xform = get_global_transform_with_canvas();
|
2018-01-14 23:36:57 +00:00
|
|
|
|
|
|
|
if (stretch) {
|
|
|
|
Transform2D scale_xf;
|
|
|
|
scale_xf.scale(Vector2(shrink, shrink));
|
|
|
|
xform *= scale_xf;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<InputEvent> ev = p_event->xformed_by(xform.affine_inverse());
|
|
|
|
|
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
|
|
|
Viewport *c = Object::cast_to<Viewport>(get_child(i));
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!c || c->is_input_disabled()) {
|
2018-01-14 23:36:57 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-01-14 23:36:57 +00:00
|
|
|
|
|
|
|
c->input(ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 20:29:55 +00:00
|
|
|
void ViewportContainer::_unhandled_input(const Ref<InputEvent> &p_event) {
|
2021-04-27 14:51:29 +00:00
|
|
|
ERR_FAIL_COND(p_event.is_null());
|
2018-11-15 20:29:55 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2018-11-15 20:29:55 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-11-15 20:29:55 +00:00
|
|
|
|
2022-04-04 20:28:06 +00:00
|
|
|
Transform2D xform = get_global_transform_with_canvas();
|
2018-11-15 20:29:55 +00:00
|
|
|
|
|
|
|
if (stretch) {
|
|
|
|
Transform2D scale_xf;
|
|
|
|
scale_xf.scale(Vector2(shrink, shrink));
|
|
|
|
xform *= scale_xf;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<InputEvent> ev = p_event->xformed_by(xform.affine_inverse());
|
|
|
|
|
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
|
|
|
Viewport *c = Object::cast_to<Viewport>(get_child(i));
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!c || c->is_input_disabled()) {
|
2018-11-15 20:29:55 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-11-15 20:29:55 +00:00
|
|
|
|
|
|
|
c->unhandled_input(ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 04:26:35 +00:00
|
|
|
void ViewportContainer::_bind_methods() {
|
2018-11-15 20:29:55 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_unhandled_input", "event"), &ViewportContainer::_unhandled_input);
|
2018-01-14 23:36:57 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_input", "event"), &ViewportContainer::_input);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_stretch", "enable"), &ViewportContainer::set_stretch);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_stretch_enabled"), &ViewportContainer::is_stretch_enabled);
|
2016-10-05 04:26:35 +00:00
|
|
|
|
2017-10-31 17:23:25 +00:00
|
|
|
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);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch"), "set_stretch", "is_stretch_enabled");
|
2017-10-31 17:23:25 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_shrink"), "set_stretch_shrink", "get_stretch_shrink");
|
2016-10-05 04:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewportContainer::ViewportContainer() {
|
2017-03-05 15:44:50 +00:00
|
|
|
stretch = false;
|
2017-10-31 17:23:25 +00:00
|
|
|
shrink = 1;
|
2018-01-14 23:36:57 +00:00
|
|
|
set_process_input(true);
|
2019-08-31 12:51:16 +00:00
|
|
|
set_process_unhandled_input(true);
|
2016-10-05 04:26:35 +00:00
|
|
|
}
|