2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* visibility_notifier.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:45:00 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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 "visibility_notifier.h"
|
|
|
|
|
|
|
|
#include "scene/3d/physics_body.h"
|
|
|
|
#include "scene/animation/animation_player.h"
|
|
|
|
#include "scene/scene_string_names.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "scene/scene_string_names.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void VisibilityNotifier::_enter_camera(Camera *p_camera) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(cameras.has(p_camera));
|
|
|
|
cameras.insert(p_camera);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (cameras.size() == 1) {
|
2014-02-10 01:10:30 +00:00
|
|
|
emit_signal(SceneStringNames::get_singleton()->enter_screen);
|
|
|
|
_screen_enter();
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
emit_signal(SceneStringNames::get_singleton()->enter_camera, p_camera);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void VisibilityNotifier::_exit_camera(Camera *p_camera) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!cameras.has(p_camera));
|
|
|
|
cameras.erase(p_camera);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
emit_signal(SceneStringNames::get_singleton()->exit_camera, p_camera);
|
|
|
|
if (cameras.size() == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
emit_signal(SceneStringNames::get_singleton()->exit_screen);
|
|
|
|
|
|
|
|
_screen_exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void VisibilityNotifier::set_aabb(const AABB &p_aabb) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (aabb == p_aabb)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2017-03-18 23:36:26 +00:00
|
|
|
aabb = p_aabb;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (is_inside_world()) {
|
2017-03-18 23:36:26 +00:00
|
|
|
get_world()->_update_notifier(this, get_global_transform().xform(aabb));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_change_notify("aabb");
|
|
|
|
update_gizmo();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
AABB VisibilityNotifier::get_aabb() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return aabb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisibilityNotifier::_notification(int p_what) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
switch (p_what) {
|
2014-02-10 01:10:30 +00:00
|
|
|
case NOTIFICATION_ENTER_WORLD: {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
get_world()->_register_notifier(this, get_global_transform().xform(aabb));
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_TRANSFORM_CHANGED: {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
get_world()->_update_notifier(this, get_global_transform().xform(aabb));
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_EXIT_WORLD: {
|
|
|
|
|
|
|
|
get_world()->_remove_notifier(this);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisibilityNotifier::is_on_screen() const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return cameras.size() != 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void VisibilityNotifier::_bind_methods() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_aabb", "rect"), &VisibilityNotifier::set_aabb);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_aabb"), &VisibilityNotifier::get_aabb);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_on_screen"), &VisibilityNotifier::is_on_screen);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::_AABB, "aabb"), _SCS("set_aabb"), _SCS("get_aabb"));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("enter_camera", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera")));
|
|
|
|
ADD_SIGNAL(MethodInfo("exit_camera", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera")));
|
|
|
|
ADD_SIGNAL(MethodInfo("enter_screen"));
|
|
|
|
ADD_SIGNAL(MethodInfo("exit_screen"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VisibilityNotifier::VisibilityNotifier() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
aabb = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////
|
|
|
|
|
|
|
|
void VisibilityEnabler::_screen_enter() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
_change_node_state(E->key(), true);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
visible = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisibilityEnabler::_screen_exit() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
_change_node_state(E->key(), false);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
visible = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void VisibilityEnabler::_find_nodes(Node *p_node) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool add = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
Variant meta;
|
|
|
|
|
|
|
|
if (enabler[ENABLER_FREEZE_BODIES]) {
|
|
|
|
|
|
|
|
RigidBody *rb = p_node->cast_to<RigidBody>();
|
2017-03-18 23:36:26 +00:00
|
|
|
if (rb && ((rb->get_mode() == RigidBody::MODE_CHARACTER || (rb->get_mode() == RigidBody::MODE_RIGID && !rb->is_able_to_sleep())))) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
add = true;
|
|
|
|
meta = rb->get_mode();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabler[ENABLER_PAUSE_ANIMATIONS]) {
|
|
|
|
|
|
|
|
AnimationPlayer *ap = p_node->cast_to<AnimationPlayer>();
|
|
|
|
if (ap) {
|
2017-03-18 23:36:26 +00:00
|
|
|
add = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (add) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
p_node->connect(SceneStringNames::get_singleton()->exit_tree, this, "_node_removed", varray(p_node), CONNECT_ONESHOT);
|
|
|
|
nodes[p_node] = meta;
|
|
|
|
_change_node_state(p_node, false);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p_node->get_child_count(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
Node *c = p_node->get_child(i);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (c->get_filename() != String())
|
2014-02-10 01:10:30 +00:00
|
|
|
continue; //skip, instance
|
|
|
|
|
|
|
|
_find_nodes(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void VisibilityEnabler::_notification(int p_what) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
if (get_tree()->is_editor_hint())
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
Node *from = this;
|
|
|
|
//find where current scene starts
|
2017-03-18 23:36:26 +00:00
|
|
|
while (from->get_parent() && from->get_filename() == String())
|
|
|
|
from = from->get_parent();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_find_nodes(from);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_what == NOTIFICATION_EXIT_TREE) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
if (get_tree()->is_editor_hint())
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!visible)
|
2017-03-18 23:36:26 +00:00
|
|
|
_change_node_state(E->key(), true);
|
|
|
|
E->key()->disconnect(SceneStringNames::get_singleton()->exit_tree, this, "_node_removed");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nodes.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void VisibilityEnabler::_change_node_state(Node *p_node, bool p_enabled) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!nodes.has(p_node));
|
|
|
|
|
|
|
|
{
|
|
|
|
RigidBody *rb = p_node->cast_to<RigidBody>();
|
2016-07-07 22:32:28 +00:00
|
|
|
if (rb)
|
|
|
|
|
|
|
|
rb->set_sleeping(!p_enabled);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-03-18 23:36:26 +00:00
|
|
|
AnimationPlayer *ap = p_node->cast_to<AnimationPlayer>();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (ap) {
|
|
|
|
|
|
|
|
ap->set_active(p_enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void VisibilityEnabler::_node_removed(Node *p_node) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!visible)
|
2017-03-18 23:36:26 +00:00
|
|
|
_change_node_state(p_node, true);
|
|
|
|
p_node->disconnect(SceneStringNames::get_singleton()->exit_tree, this, "_node_removed");
|
2014-02-10 01:10:30 +00:00
|
|
|
nodes.erase(p_node);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void VisibilityEnabler::_bind_methods() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_enabler", "enabler", "enabled"), &VisibilityEnabler::set_enabler);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_enabler_enabled", "enabler"), &VisibilityEnabler::is_enabler_enabled);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_node_removed"), &VisibilityEnabler::_node_removed);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "enabler/pause_animations"), _SCS("set_enabler"), _SCS("is_enabler_enabled"), ENABLER_PAUSE_ANIMATIONS);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "enabler/freeze_bodies"), _SCS("set_enabler"), _SCS("is_enabler_enabled"), ENABLER_FREEZE_BODIES);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
BIND_CONSTANT(ENABLER_FREEZE_BODIES);
|
|
|
|
BIND_CONSTANT(ENABLER_PAUSE_ANIMATIONS);
|
|
|
|
BIND_CONSTANT(ENABLER_MAX);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void VisibilityEnabler::set_enabler(Enabler p_enabler, bool p_enable) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX(p_enabler, ENABLER_MAX);
|
|
|
|
enabler[p_enabler] = p_enable;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
bool VisibilityEnabler::is_enabler_enabled(Enabler p_enabler) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_enabler, ENABLER_MAX, false);
|
2014-02-10 01:10:30 +00:00
|
|
|
return enabler[p_enabler];
|
|
|
|
}
|
|
|
|
|
|
|
|
VisibilityEnabler::VisibilityEnabler() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < ENABLER_MAX; i++)
|
|
|
|
enabler[i] = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
visible = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|