2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* visibility_notifier_2d.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. */
|
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_2d.h"
|
|
|
|
|
|
|
|
#include "scene/scene_string_names.h"
|
|
|
|
#include "scene/2d/physics_body_2d.h"
|
2016-05-15 02:48:23 +00:00
|
|
|
#include "scene/2d/animated_sprite.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "scene/animation/animation_player.h"
|
|
|
|
#include "scene/scene_string_names.h"
|
2015-03-09 05:34:56 +00:00
|
|
|
#include "particles_2d.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void VisibilityNotifier2D::_enter_viewport(Viewport* p_viewport) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(viewports.has(p_viewport));
|
|
|
|
viewports.insert(p_viewport);
|
2014-03-14 01:57:24 +00:00
|
|
|
|
2016-08-08 22:04:12 +00:00
|
|
|
if (is_inside_tree() && get_tree()->is_editor_hint())
|
|
|
|
return;
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (viewports.size()==1) {
|
2017-01-12 03:51:08 +00:00
|
|
|
emit_signal(SceneStringNames::get_singleton()->screen_entered);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-03-14 01:57:24 +00:00
|
|
|
_screen_enter();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-01-12 03:51:08 +00:00
|
|
|
emit_signal(SceneStringNames::get_singleton()->viewport_entered,p_viewport);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisibilityNotifier2D::_exit_viewport(Viewport* p_viewport){
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!viewports.has(p_viewport));
|
|
|
|
viewports.erase(p_viewport);
|
|
|
|
|
2016-08-08 22:04:12 +00:00
|
|
|
if (is_inside_tree() && get_tree()->is_editor_hint())
|
|
|
|
return;
|
|
|
|
|
2017-01-12 03:51:08 +00:00
|
|
|
emit_signal(SceneStringNames::get_singleton()->viewport_exited,p_viewport);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (viewports.size()==0) {
|
2017-01-12 03:51:08 +00:00
|
|
|
emit_signal(SceneStringNames::get_singleton()->screen_exited);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_screen_exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VisibilityNotifier2D::set_rect(const Rect2& p_rect){
|
|
|
|
|
|
|
|
rect=p_rect;
|
2015-04-02 10:22:17 +00:00
|
|
|
if (is_inside_tree()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
get_world_2d()->_update_notifier(this,get_global_transform().xform(rect));
|
2015-04-02 10:22:17 +00:00
|
|
|
if (get_tree()->is_editor_hint()) {
|
|
|
|
update();
|
|
|
|
item_rect_changed();
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_change_notify("rect");
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect2 VisibilityNotifier2D::get_item_rect() const {
|
|
|
|
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect2 VisibilityNotifier2D::get_rect() const{
|
|
|
|
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VisibilityNotifier2D::_notification(int p_what) {
|
|
|
|
|
|
|
|
|
|
|
|
switch(p_what) {
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//get_world_2d()->
|
|
|
|
get_world_2d()->_register_notifier(this,get_global_transform().xform(rect));
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_TRANSFORM_CHANGED: {
|
|
|
|
|
|
|
|
//get_world_2d()->
|
|
|
|
get_world_2d()->_update_notifier(this,get_global_transform().xform(rect));
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
if (get_tree()->is_editor_hint()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
draw_rect(rect,Color(1,0.5,1,0.2));
|
|
|
|
}
|
|
|
|
} break;
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
get_world_2d()->_remove_notifier(this);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisibilityNotifier2D::is_on_screen() const {
|
|
|
|
|
|
|
|
return viewports.size()>0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisibilityNotifier2D::_bind_methods(){
|
|
|
|
|
2017-01-03 02:03:46 +00:00
|
|
|
ClassDB::bind_method(_MD("set_rect","rect"),&VisibilityNotifier2D::set_rect);
|
|
|
|
ClassDB::bind_method(_MD("get_rect"),&VisibilityNotifier2D::get_rect);
|
|
|
|
ClassDB::bind_method(_MD("is_on_screen"),&VisibilityNotifier2D::is_on_screen);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ADD_PROPERTY( PropertyInfo(Variant::RECT2,"rect"),_SCS("set_rect"),_SCS("get_rect"));
|
|
|
|
|
2017-01-12 03:51:08 +00:00
|
|
|
ADD_SIGNAL( MethodInfo("viewport_entered",PropertyInfo(Variant::OBJECT,"viewport",PROPERTY_HINT_RESOURCE_TYPE,"Viewport")) );
|
|
|
|
ADD_SIGNAL( MethodInfo("viewport_exited",PropertyInfo(Variant::OBJECT,"viewport",PROPERTY_HINT_RESOURCE_TYPE,"Viewport")) );
|
|
|
|
ADD_SIGNAL( MethodInfo("screen_entered"));
|
|
|
|
ADD_SIGNAL( MethodInfo("screen_exited"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VisibilityNotifier2D::VisibilityNotifier2D() {
|
|
|
|
|
|
|
|
rect=Rect2(-10,-10,20,20);
|
2017-01-12 23:35:46 +00:00
|
|
|
set_notify_transform(true);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
void VisibilityEnabler2D::_screen_enter() {
|
|
|
|
|
|
|
|
|
|
|
|
for(Map<Node*,Variant>::Element *E=nodes.front();E;E=E->next()) {
|
|
|
|
|
|
|
|
_change_node_state(E->key(),true);
|
|
|
|
}
|
|
|
|
|
2015-06-22 18:43:13 +00:00
|
|
|
if (enabler[ENABLER_PARENT_FIXED_PROCESS] && get_parent())
|
|
|
|
get_parent()->set_fixed_process(true);
|
|
|
|
if (enabler[ENABLER_PARENT_PROCESS] && get_parent())
|
|
|
|
get_parent()->set_process(true);
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
visible=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisibilityEnabler2D::_screen_exit(){
|
|
|
|
|
|
|
|
for(Map<Node*,Variant>::Element *E=nodes.front();E;E=E->next()) {
|
|
|
|
|
|
|
|
_change_node_state(E->key(),false);
|
|
|
|
}
|
|
|
|
|
2015-06-22 18:43:13 +00:00
|
|
|
if (enabler[ENABLER_PARENT_FIXED_PROCESS] && get_parent())
|
|
|
|
get_parent()->set_fixed_process(false);
|
|
|
|
if (enabler[ENABLER_PARENT_PROCESS] && get_parent())
|
|
|
|
get_parent()->set_process(false);
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
visible=false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisibilityEnabler2D::_find_nodes(Node* p_node) {
|
|
|
|
|
|
|
|
|
|
|
|
bool add=false;
|
|
|
|
Variant meta;
|
|
|
|
|
|
|
|
if (enabler[ENABLER_FREEZE_BODIES]) {
|
|
|
|
|
|
|
|
RigidBody2D *rb2d = p_node->cast_to<RigidBody2D>();
|
|
|
|
if (rb2d && ((rb2d->get_mode()==RigidBody2D::MODE_CHARACTER || (rb2d->get_mode()==RigidBody2D::MODE_RIGID && !rb2d->is_able_to_sleep())))) {
|
|
|
|
|
|
|
|
|
|
|
|
add=true;
|
|
|
|
meta=rb2d->get_mode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabler[ENABLER_PAUSE_ANIMATIONS]) {
|
|
|
|
|
|
|
|
AnimationPlayer *ap = p_node->cast_to<AnimationPlayer>();
|
|
|
|
if (ap) {
|
|
|
|
add=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-15 02:48:23 +00:00
|
|
|
if (enabler[ENABLER_PAUSE_ANIMATED_SPRITES]) {
|
|
|
|
|
|
|
|
AnimatedSprite *as = p_node->cast_to<AnimatedSprite>();
|
|
|
|
if (as) {
|
|
|
|
add=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-09 05:34:56 +00:00
|
|
|
if (enabler[ENABLER_PAUSE_PARTICLES]) {
|
|
|
|
|
|
|
|
Particles2D *ps = p_node->cast_to<Particles2D>();
|
|
|
|
if (ps) {
|
|
|
|
add=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (add) {
|
|
|
|
|
2017-01-12 03:51:08 +00:00
|
|
|
p_node->connect(SceneStringNames::get_singleton()->tree_exited,this,"_node_removed",varray(p_node),CONNECT_ONESHOT);
|
2014-02-10 01:10:30 +00:00
|
|
|
nodes[p_node]=meta;
|
|
|
|
_change_node_state(p_node,false);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i=0;i<p_node->get_child_count();i++) {
|
|
|
|
Node *c = p_node->get_child(i);
|
|
|
|
if (c->get_filename()!=String())
|
|
|
|
continue; //skip, instance
|
|
|
|
|
|
|
|
_find_nodes(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisibilityEnabler2D::_notification(int p_what){
|
|
|
|
|
2014-11-06 00:20:42 +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
|
|
|
|
while(from->get_parent() && from->get_filename()==String())
|
|
|
|
from=from->get_parent();
|
|
|
|
|
|
|
|
_find_nodes(from);
|
|
|
|
|
2015-06-22 18:43:13 +00:00
|
|
|
if (enabler[ENABLER_PARENT_FIXED_PROCESS] && get_parent())
|
|
|
|
get_parent()->set_fixed_process(false);
|
|
|
|
if (enabler[ENABLER_PARENT_PROCESS] && get_parent())
|
|
|
|
get_parent()->set_process(false);
|
|
|
|
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2014-11-06 00:20:42 +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;
|
|
|
|
|
|
|
|
|
|
|
|
for (Map<Node*,Variant>::Element *E=nodes.front();E;E=E->next()) {
|
|
|
|
|
|
|
|
if (!visible)
|
|
|
|
_change_node_state(E->key(),true);
|
2017-01-12 03:51:08 +00:00
|
|
|
E->key()->disconnect(SceneStringNames::get_singleton()->tree_exited,this,"_node_removed");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nodes.clear();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisibilityEnabler2D::_change_node_state(Node* p_node,bool p_enabled) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!nodes.has(p_node));
|
|
|
|
|
|
|
|
{
|
|
|
|
RigidBody2D *rb = p_node->cast_to<RigidBody2D>();
|
|
|
|
if (rb) {
|
|
|
|
|
2016-07-07 22:32:28 +00:00
|
|
|
rb->set_sleeping(!p_enabled);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
AnimationPlayer *ap=p_node->cast_to<AnimationPlayer>();
|
|
|
|
|
|
|
|
if (ap) {
|
|
|
|
|
|
|
|
ap->set_active(p_enabled);
|
|
|
|
}
|
|
|
|
}
|
2016-05-15 02:48:23 +00:00
|
|
|
{
|
|
|
|
AnimatedSprite *as=p_node->cast_to<AnimatedSprite>();
|
|
|
|
|
|
|
|
if (as) {
|
|
|
|
|
|
|
|
if (p_enabled)
|
|
|
|
as->play();
|
|
|
|
else
|
|
|
|
as->stop();
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-03-09 05:34:56 +00:00
|
|
|
{
|
|
|
|
Particles2D *ps=p_node->cast_to<Particles2D>();
|
|
|
|
|
|
|
|
if (ps) {
|
|
|
|
|
|
|
|
ps->set_emitting(p_enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VisibilityEnabler2D::_node_removed(Node* p_node) {
|
|
|
|
|
|
|
|
if (!visible)
|
|
|
|
_change_node_state(p_node,true);
|
|
|
|
//changed to one shot, not needed
|
|
|
|
//p_node->disconnect(SceneStringNames::get_singleton()->exit_scene,this,"_node_removed");
|
|
|
|
nodes.erase(p_node);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-17 21:27:15 +00:00
|
|
|
String VisibilityEnabler2D::get_configuration_warning() const {
|
2016-05-18 05:29:53 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2016-05-17 21:27:15 +00:00
|
|
|
if (is_inside_tree() && get_parent() && (get_parent()->get_filename()==String() && get_parent()!=get_tree()->get_edited_scene_root())) {
|
|
|
|
return TTR("VisibilityEnable2D works best when used with the edited scene root directly as parent.");
|
|
|
|
}
|
2016-05-18 05:29:53 +00:00
|
|
|
#endif
|
2016-05-17 21:27:15 +00:00
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void VisibilityEnabler2D::_bind_methods(){
|
|
|
|
|
2017-01-03 02:03:46 +00:00
|
|
|
ClassDB::bind_method(_MD("set_enabler","enabler","enabled"),&VisibilityEnabler2D::set_enabler);
|
|
|
|
ClassDB::bind_method(_MD("is_enabler_enabled","enabler"),&VisibilityEnabler2D::is_enabler_enabled);
|
|
|
|
ClassDB::bind_method(_MD("_node_removed"),&VisibilityEnabler2D::_node_removed);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-04 04:16:14 +00:00
|
|
|
ADD_PROPERTYI( PropertyInfo(Variant::BOOL,"pause_animations"),_SCS("set_enabler"),_SCS("is_enabler_enabled"), ENABLER_PAUSE_ANIMATIONS );
|
|
|
|
ADD_PROPERTYI( PropertyInfo(Variant::BOOL,"freeze_bodies"),_SCS("set_enabler"),_SCS("is_enabler_enabled"), ENABLER_FREEZE_BODIES);
|
|
|
|
ADD_PROPERTYI( PropertyInfo(Variant::BOOL,"pause_particles"),_SCS("set_enabler"),_SCS("is_enabler_enabled"), ENABLER_PAUSE_PARTICLES);
|
|
|
|
ADD_PROPERTYI( PropertyInfo(Variant::BOOL,"pause_animated_sprites"),_SCS("set_enabler"),_SCS("is_enabler_enabled"), ENABLER_PAUSE_ANIMATED_SPRITES);
|
|
|
|
ADD_PROPERTYI( PropertyInfo(Variant::BOOL,"process_parent"),_SCS("set_enabler"),_SCS("is_enabler_enabled"), ENABLER_PARENT_PROCESS);
|
|
|
|
ADD_PROPERTYI( PropertyInfo(Variant::BOOL,"fixed_process_parent"),_SCS("set_enabler"),_SCS("is_enabler_enabled"), ENABLER_PARENT_FIXED_PROCESS);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
BIND_CONSTANT( ENABLER_FREEZE_BODIES );
|
|
|
|
BIND_CONSTANT( ENABLER_PAUSE_ANIMATIONS );
|
2015-03-09 05:34:56 +00:00
|
|
|
BIND_CONSTANT( ENABLER_PAUSE_PARTICLES );
|
2016-05-15 02:48:23 +00:00
|
|
|
BIND_CONSTANT( ENABLER_PAUSE_ANIMATED_SPRITES );
|
2015-06-22 18:43:13 +00:00
|
|
|
BIND_CONSTANT( ENABLER_PARENT_PROCESS );
|
|
|
|
BIND_CONSTANT( ENABLER_PARENT_FIXED_PROCESS );
|
2014-02-10 01:10:30 +00:00
|
|
|
BIND_CONSTANT( ENABLER_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisibilityEnabler2D::set_enabler(Enabler p_enabler,bool p_enable){
|
|
|
|
|
|
|
|
ERR_FAIL_INDEX(p_enabler,ENABLER_MAX);
|
|
|
|
enabler[p_enabler]=p_enable;
|
|
|
|
|
|
|
|
}
|
|
|
|
bool VisibilityEnabler2D::is_enabler_enabled(Enabler p_enabler) const{
|
|
|
|
|
|
|
|
ERR_FAIL_INDEX_V(p_enabler,ENABLER_MAX,false);
|
|
|
|
return enabler[p_enabler];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
VisibilityEnabler2D::VisibilityEnabler2D() {
|
|
|
|
|
|
|
|
for(int i=0;i<ENABLER_MAX;i++)
|
|
|
|
enabler[i]=true;
|
2015-06-22 18:43:13 +00:00
|
|
|
enabler[ENABLER_PARENT_PROCESS]=false;
|
|
|
|
enabler[ENABLER_PARENT_FIXED_PROCESS]=false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
visible=false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-09 05:34:56 +00:00
|
|
|
|