2017-03-05 15:47:28 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* viewport_container.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2007-2017 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. */
|
|
|
|
/*************************************************************************/
|
2016-10-05 06:26:35 +02:00
|
|
|
#include "viewport_container.h"
|
2017-03-05 15:47:28 +01:00
|
|
|
|
2016-10-05 06:26:35 +02:00
|
|
|
#include "scene/main/viewport.h"
|
|
|
|
|
2017-03-05 15:47:28 +01:00
|
|
|
Size2 ViewportContainer::get_minimum_size() const {
|
2016-10-05 06:26:35 +02:00
|
|
|
|
|
|
|
if (stretch)
|
|
|
|
return Size2();
|
|
|
|
Size2 ms;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2016-10-05 06:26:35 +02:00
|
|
|
|
|
|
|
Viewport *c = get_child(i)->cast_to<Viewport>();
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Size2 minsize = c->get_size();
|
2017-03-05 16:44:50 +01:00
|
|
|
ms.width = MAX(ms.width, minsize.width);
|
|
|
|
ms.height = MAX(ms.height, minsize.height);
|
2016-10-05 06:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewportContainer::set_stretch(bool p_enable) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stretch = p_enable;
|
2016-10-05 06:26:35 +02:00
|
|
|
queue_sort();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ViewportContainer::is_stretch_enabled() const {
|
|
|
|
|
|
|
|
return stretch;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewportContainer::_notification(int p_what) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_what == NOTIFICATION_RESIZED) {
|
2016-10-05 06:26:35 +02:00
|
|
|
|
|
|
|
if (!stretch)
|
|
|
|
return;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2016-10-05 06:26:35 +02:00
|
|
|
|
|
|
|
Viewport *c = get_child(i)->cast_to<Viewport>();
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
c->set_size(get_size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_VISIBILITY_CHANGED) {
|
2016-10-05 06:26:35 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2016-10-05 06:26:35 +02:00
|
|
|
|
|
|
|
Viewport *c = get_child(i)->cast_to<Viewport>();
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
|
2017-01-13 14:45:50 +01:00
|
|
|
if (is_visible_in_tree())
|
2016-10-05 06:26:35 +02:00
|
|
|
c->set_update_mode(Viewport::UPDATE_ALWAYS);
|
|
|
|
else
|
|
|
|
c->set_update_mode(Viewport::UPDATE_DISABLED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_what == NOTIFICATION_DRAW) {
|
2016-10-05 06:26:35 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2016-10-05 06:26:35 +02:00
|
|
|
|
|
|
|
Viewport *c = get_child(i)->cast_to<Viewport>();
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (stretch)
|
2017-03-05 16:44:50 +01:00
|
|
|
draw_texture_rect(c->get_texture(), Rect2(Vector2(), get_size() * Size2(1, -1)));
|
2016-10-05 06:26:35 +02:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
draw_texture_rect(c->get_texture(), Rect2(Vector2(), c->get_size() * Size2(1, -1)));
|
2016-10-05 06:26:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewportContainer::_bind_methods() {
|
|
|
|
|
2017-03-05 16:44:50 +01: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 06:26:35 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch"), "set_stretch", "is_stretch_enabled");
|
2016-10-05 06:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewportContainer::ViewportContainer() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stretch = false;
|
2016-10-05 06:26:35 +02:00
|
|
|
}
|