diff options
Diffstat (limited to 'scene/2d/visibility_notifier_2d.cpp')
-rw-r--r-- | scene/2d/visibility_notifier_2d.cpp | 218 |
1 files changed, 93 insertions, 125 deletions
diff --git a/scene/2d/visibility_notifier_2d.cpp b/scene/2d/visibility_notifier_2d.cpp index 64ab333e48..f20a995d5f 100644 --- a/scene/2d/visibility_notifier_2d.cpp +++ b/scene/2d/visibility_notifier_2d.cpp @@ -28,14 +28,14 @@ /*************************************************************************/ #include "visibility_notifier_2d.h" -#include "scene/scene_string_names.h" -#include "scene/2d/physics_body_2d.h" +#include "particles_2d.h" #include "scene/2d/animated_sprite.h" +#include "scene/2d/physics_body_2d.h" #include "scene/animation/animation_player.h" #include "scene/scene_string_names.h" -#include "particles_2d.h" +#include "scene/scene_string_names.h" -void VisibilityNotifier2D::_enter_viewport(Viewport* p_viewport) { +void VisibilityNotifier2D::_enter_viewport(Viewport *p_viewport) { ERR_FAIL_COND(viewports.has(p_viewport)); viewports.insert(p_viewport); @@ -43,16 +43,15 @@ void VisibilityNotifier2D::_enter_viewport(Viewport* p_viewport) { if (is_inside_tree() && get_tree()->is_editor_hint()) return; - if (viewports.size()==1) { + if (viewports.size() == 1) { emit_signal(SceneStringNames::get_singleton()->screen_entered); _screen_enter(); } - emit_signal(SceneStringNames::get_singleton()->viewport_entered,p_viewport); - + emit_signal(SceneStringNames::get_singleton()->viewport_entered, p_viewport); } -void VisibilityNotifier2D::_exit_viewport(Viewport* p_viewport){ +void VisibilityNotifier2D::_exit_viewport(Viewport *p_viewport) { ERR_FAIL_COND(!viewports.has(p_viewport)); viewports.erase(p_viewport); @@ -60,20 +59,19 @@ void VisibilityNotifier2D::_exit_viewport(Viewport* p_viewport){ if (is_inside_tree() && get_tree()->is_editor_hint()) return; - emit_signal(SceneStringNames::get_singleton()->viewport_exited,p_viewport); - if (viewports.size()==0) { + emit_signal(SceneStringNames::get_singleton()->viewport_exited, p_viewport); + if (viewports.size() == 0) { emit_signal(SceneStringNames::get_singleton()->screen_exited); _screen_exit(); } } +void VisibilityNotifier2D::set_rect(const Rect2 &p_rect) { -void VisibilityNotifier2D::set_rect(const Rect2& p_rect){ - - rect=p_rect; + rect = p_rect; if (is_inside_tree()) { - get_world_2d()->_update_notifier(this,get_global_transform().xform(rect)); + get_world_2d()->_update_notifier(this, get_global_transform().xform(rect)); if (get_tree()->is_editor_hint()) { update(); item_rect_changed(); @@ -88,31 +86,29 @@ Rect2 VisibilityNotifier2D::get_item_rect() const { return rect; } -Rect2 VisibilityNotifier2D::get_rect() const{ +Rect2 VisibilityNotifier2D::get_rect() const { return rect; } - void VisibilityNotifier2D::_notification(int p_what) { - - switch(p_what) { + switch (p_what) { case NOTIFICATION_ENTER_TREE: { //get_world_2d()-> - get_world_2d()->_register_notifier(this,get_global_transform().xform(rect)); + 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)); + get_world_2d()->_update_notifier(this, get_global_transform().xform(rect)); } break; case NOTIFICATION_DRAW: { if (get_tree()->is_editor_hint()) { - draw_rect(rect,Color(1,0.5,1,0.2)); + draw_rect(rect, Color(1, 0.5, 1, 0.2)); } } break; case NOTIFICATION_EXIT_TREE: { @@ -124,43 +120,36 @@ void VisibilityNotifier2D::_notification(int p_what) { bool VisibilityNotifier2D::is_on_screen() const { - return viewports.size()>0; + return viewports.size() > 0; } -void VisibilityNotifier2D::_bind_methods(){ +void VisibilityNotifier2D::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_rect","rect"),&VisibilityNotifier2D::set_rect); - ClassDB::bind_method(D_METHOD("get_rect"),&VisibilityNotifier2D::get_rect); - ClassDB::bind_method(D_METHOD("is_on_screen"),&VisibilityNotifier2D::is_on_screen); + ClassDB::bind_method(D_METHOD("set_rect", "rect"), &VisibilityNotifier2D::set_rect); + ClassDB::bind_method(D_METHOD("get_rect"), &VisibilityNotifier2D::get_rect); + ClassDB::bind_method(D_METHOD("is_on_screen"), &VisibilityNotifier2D::is_on_screen); - ADD_PROPERTY( PropertyInfo(Variant::RECT2,"rect"),"set_rect","get_rect"); + ADD_PROPERTY(PropertyInfo(Variant::RECT2, "rect"), "set_rect", "get_rect"); - 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")); + 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")); } - VisibilityNotifier2D::VisibilityNotifier2D() { - rect=Rect2(-10,-10,20,20); + rect = Rect2(-10, -10, 20, 20); set_notify_transform(true); } - - - - ////////////////////////////////////// - void VisibilityEnabler2D::_screen_enter() { + for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) { - for(Map<Node*,Variant>::Element *E=nodes.front();E;E=E->next()) { - - _change_node_state(E->key(),true); + _change_node_state(E->key(), true); } if (enabler[ENABLER_PARENT_FIXED_PROCESS] && get_parent()) @@ -168,14 +157,14 @@ void VisibilityEnabler2D::_screen_enter() { if (enabler[ENABLER_PARENT_PROCESS] && get_parent()) get_parent()->set_process(true); - visible=true; + visible = true; } -void VisibilityEnabler2D::_screen_exit(){ +void VisibilityEnabler2D::_screen_exit() { - for(Map<Node*,Variant>::Element *E=nodes.front();E;E=E->next()) { + for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) { - _change_node_state(E->key(),false); + _change_node_state(E->key(), false); } if (enabler[ENABLER_PARENT_FIXED_PROCESS] && get_parent()) @@ -183,23 +172,21 @@ void VisibilityEnabler2D::_screen_exit(){ if (enabler[ENABLER_PARENT_PROCESS] && get_parent()) get_parent()->set_process(false); - visible=false; + visible = false; } -void VisibilityEnabler2D::_find_nodes(Node* p_node) { +void VisibilityEnabler2D::_find_nodes(Node *p_node) { - - bool add=false; + 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())))) { - + 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(); + add = true; + meta = rb2d->get_mode(); } } @@ -207,59 +194,53 @@ void VisibilityEnabler2D::_find_nodes(Node* p_node) { AnimationPlayer *ap = p_node->cast_to<AnimationPlayer>(); if (ap) { - add=true; + add = true; } - } if (enabler[ENABLER_PAUSE_ANIMATED_SPRITES]) { AnimatedSprite *as = p_node->cast_to<AnimatedSprite>(); if (as) { - add=true; + add = true; } - } - if (enabler[ENABLER_PAUSE_PARTICLES]) { Particles2D *ps = p_node->cast_to<Particles2D>(); if (ps) { - add=true; + add = true; } - } if (add) { - p_node->connect(SceneStringNames::get_singleton()->tree_exited,this,"_node_removed",varray(p_node),CONNECT_ONESHOT); - nodes[p_node]=meta; - _change_node_state(p_node,false); + p_node->connect(SceneStringNames::get_singleton()->tree_exited, this, "_node_removed", varray(p_node), CONNECT_ONESHOT); + nodes[p_node] = meta; + _change_node_state(p_node, false); } - for(int i=0;i<p_node->get_child_count();i++) { + for (int i = 0; i < p_node->get_child_count(); i++) { Node *c = p_node->get_child(i); - if (c->get_filename()!=String()) + if (c->get_filename() != String()) continue; //skip, instance _find_nodes(c); } - } -void VisibilityEnabler2D::_notification(int p_what){ +void VisibilityEnabler2D::_notification(int p_what) { - if (p_what==NOTIFICATION_ENTER_TREE) { + if (p_what == NOTIFICATION_ENTER_TREE) { if (get_tree()->is_editor_hint()) return; - Node *from = this; //find where current scene starts - while(from->get_parent() && from->get_filename()==String()) - from=from->get_parent(); + while (from->get_parent() && from->get_filename() == String()) + from = from->get_parent(); _find_nodes(from); @@ -267,29 +248,25 @@ void VisibilityEnabler2D::_notification(int p_what){ get_parent()->set_fixed_process(false); if (enabler[ENABLER_PARENT_PROCESS] && get_parent()) get_parent()->set_process(false); - - } - if (p_what==NOTIFICATION_EXIT_TREE) { + if (p_what == NOTIFICATION_EXIT_TREE) { if (get_tree()->is_editor_hint()) return; - - for (Map<Node*,Variant>::Element *E=nodes.front();E;E=E->next()) { + for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) { if (!visible) - _change_node_state(E->key(),true); - E->key()->disconnect(SceneStringNames::get_singleton()->tree_exited,this,"_node_removed"); + _change_node_state(E->key(), true); + E->key()->disconnect(SceneStringNames::get_singleton()->tree_exited, this, "_node_removed"); } nodes.clear(); - } } -void VisibilityEnabler2D::_change_node_state(Node* p_node,bool p_enabled) { +void VisibilityEnabler2D::_change_node_state(Node *p_node, bool p_enabled) { ERR_FAIL_COND(!nodes.has(p_node)); @@ -302,7 +279,7 @@ void VisibilityEnabler2D::_change_node_state(Node* p_node,bool p_enabled) { } { - AnimationPlayer *ap=p_node->cast_to<AnimationPlayer>(); + AnimationPlayer *ap = p_node->cast_to<AnimationPlayer>(); if (ap) { @@ -310,7 +287,7 @@ void VisibilityEnabler2D::_change_node_state(Node* p_node,bool p_enabled) { } } { - AnimatedSprite *as=p_node->cast_to<AnimatedSprite>(); + AnimatedSprite *as = p_node->cast_to<AnimatedSprite>(); if (as) { @@ -322,81 +299,72 @@ void VisibilityEnabler2D::_change_node_state(Node* p_node,bool p_enabled) { } { - Particles2D *ps=p_node->cast_to<Particles2D>(); + Particles2D *ps = p_node->cast_to<Particles2D>(); if (ps) { ps->set_emitting(p_enabled); } } - } - -void VisibilityEnabler2D::_node_removed(Node* p_node) { +void VisibilityEnabler2D::_node_removed(Node *p_node) { if (!visible) - _change_node_state(p_node,true); + _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); - } String VisibilityEnabler2D::get_configuration_warning() const { #ifdef TOOLS_ENABLED - if (is_inside_tree() && get_parent() && (get_parent()->get_filename()==String() && get_parent()!=get_tree()->get_edited_scene_root())) { + 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."); } #endif return String(); } - -void VisibilityEnabler2D::_bind_methods(){ - - ClassDB::bind_method(D_METHOD("set_enabler","enabler","enabled"),&VisibilityEnabler2D::set_enabler); - ClassDB::bind_method(D_METHOD("is_enabler_enabled","enabler"),&VisibilityEnabler2D::is_enabler_enabled); - ClassDB::bind_method(D_METHOD("_node_removed"),&VisibilityEnabler2D::_node_removed); - - ADD_PROPERTYI( PropertyInfo(Variant::BOOL,"pause_animations"),"set_enabler","is_enabler_enabled", ENABLER_PAUSE_ANIMATIONS ); - ADD_PROPERTYI( PropertyInfo(Variant::BOOL,"freeze_bodies"),"set_enabler","is_enabler_enabled", ENABLER_FREEZE_BODIES); - ADD_PROPERTYI( PropertyInfo(Variant::BOOL,"pause_particles"),"set_enabler","is_enabler_enabled", ENABLER_PAUSE_PARTICLES); - ADD_PROPERTYI( PropertyInfo(Variant::BOOL,"pause_animated_sprites"),"set_enabler","is_enabler_enabled", ENABLER_PAUSE_ANIMATED_SPRITES); - ADD_PROPERTYI( PropertyInfo(Variant::BOOL,"process_parent"),"set_enabler","is_enabler_enabled", ENABLER_PARENT_PROCESS); - ADD_PROPERTYI( PropertyInfo(Variant::BOOL,"fixed_process_parent"),"set_enabler","is_enabler_enabled", ENABLER_PARENT_FIXED_PROCESS); - - BIND_CONSTANT( ENABLER_FREEZE_BODIES ); - BIND_CONSTANT( ENABLER_PAUSE_ANIMATIONS ); - BIND_CONSTANT( ENABLER_PAUSE_PARTICLES ); - BIND_CONSTANT( ENABLER_PAUSE_ANIMATED_SPRITES ); - BIND_CONSTANT( ENABLER_PARENT_PROCESS ); - BIND_CONSTANT( ENABLER_PARENT_FIXED_PROCESS ); - BIND_CONSTANT( ENABLER_MAX); +void VisibilityEnabler2D::_bind_methods() { + + ClassDB::bind_method(D_METHOD("set_enabler", "enabler", "enabled"), &VisibilityEnabler2D::set_enabler); + ClassDB::bind_method(D_METHOD("is_enabler_enabled", "enabler"), &VisibilityEnabler2D::is_enabler_enabled); + ClassDB::bind_method(D_METHOD("_node_removed"), &VisibilityEnabler2D::_node_removed); + + ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "pause_animations"), "set_enabler", "is_enabler_enabled", ENABLER_PAUSE_ANIMATIONS); + ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "freeze_bodies"), "set_enabler", "is_enabler_enabled", ENABLER_FREEZE_BODIES); + ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "pause_particles"), "set_enabler", "is_enabler_enabled", ENABLER_PAUSE_PARTICLES); + ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "pause_animated_sprites"), "set_enabler", "is_enabler_enabled", ENABLER_PAUSE_ANIMATED_SPRITES); + ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "process_parent"), "set_enabler", "is_enabler_enabled", ENABLER_PARENT_PROCESS); + ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "fixed_process_parent"), "set_enabler", "is_enabler_enabled", ENABLER_PARENT_FIXED_PROCESS); + + BIND_CONSTANT(ENABLER_FREEZE_BODIES); + BIND_CONSTANT(ENABLER_PAUSE_ANIMATIONS); + BIND_CONSTANT(ENABLER_PAUSE_PARTICLES); + BIND_CONSTANT(ENABLER_PAUSE_ANIMATED_SPRITES); + BIND_CONSTANT(ENABLER_PARENT_PROCESS); + BIND_CONSTANT(ENABLER_PARENT_FIXED_PROCESS); + 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; +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{ +bool VisibilityEnabler2D::is_enabler_enabled(Enabler p_enabler) const { - ERR_FAIL_INDEX_V(p_enabler,ENABLER_MAX,false); + 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; - enabler[ENABLER_PARENT_PROCESS]=false; - enabler[ENABLER_PARENT_FIXED_PROCESS]=false; - - visible=false; + for (int i = 0; i < ENABLER_MAX; i++) + enabler[i] = true; + enabler[ENABLER_PARENT_PROCESS] = false; + enabler[ENABLER_PARENT_FIXED_PROCESS] = false; + visible = false; } - - |