diff options
Diffstat (limited to 'scene')
-rw-r--r-- | scene/main/scene_main_loop.cpp | 76 | ||||
-rw-r--r-- | scene/main/scene_main_loop.h | 15 |
2 files changed, 88 insertions, 3 deletions
diff --git a/scene/main/scene_main_loop.cpp b/scene/main/scene_main_loop.cpp index 9f2fadcb4d..4113fd1998 100644 --- a/scene/main/scene_main_loop.cpp +++ b/scene/main/scene_main_loop.cpp @@ -41,7 +41,7 @@ #include "scene/scene_string_names.h" #include "io/resource_loader.h" #include "viewport.h" - +#include "scene/resources/packed_scene.h" void SceneTree::tree_changed() { @@ -51,6 +51,9 @@ void SceneTree::tree_changed() { void SceneTree::node_removed(Node *p_node) { + if (current_scene==p_node) { + current_scene=NULL; + } emit_signal(node_removed_name,p_node); if (call_lock>0) call_skip.insert(p_node); @@ -984,6 +987,63 @@ Node *SceneTree::get_edited_scene_root() const { } #endif +void SceneTree::set_current_scene(Node* p_scene) { + + ERR_FAIL_COND(p_scene && p_scene->get_parent()!=root); + current_scene=p_scene; +} + +Node* SceneTree::get_current_scene() const{ + + return current_scene; +} + +void SceneTree::_change_scene(Node* p_to) { + + if (current_scene) { + memdelete( current_scene ); + current_scene=NULL; + } + + if (p_to) { + current_scene=p_to; + root->add_child(p_to); + } +} + +Error SceneTree::change_scene(const String& p_path){ + + Ref<PackedScene> new_scene = ResourceLoader::load(p_path); + if (new_scene.is_null()) + return ERR_CANT_OPEN; + + return change_scene_to(new_scene); + +} +Error SceneTree::change_scene_to(const Ref<PackedScene>& p_scene){ + + Node *new_scene=NULL; + if (p_scene.is_valid()) { + new_scene = p_scene->instance(); + ERR_FAIL_COND_V(!new_scene,ERR_CANT_CREATE); + } + + call_deferred("_change_scene",new_scene); + return OK; + +} +Error SceneTree::reload_current_scene() { + + ERR_FAIL_COND_V(!current_scene,ERR_UNCONFIGURED); + String fname = current_scene->get_filename(); + return change_scene(fname); +} + +void SceneTree::add_current_scene(Node * p_current) { + + current_scene=p_current; + root->add_child(p_current); +} void SceneTree::_bind_methods() { @@ -1016,10 +1076,11 @@ void SceneTree::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_screen_stretch","mode","aspect","minsize"),&SceneTree::set_screen_stretch); - ObjectTypeDB::bind_method(_MD("queue_delete","obj"),&SceneTree::queue_delete); + + MethodInfo mi; mi.name="call_group"; mi.arguments.push_back( PropertyInfo( Variant::INT, "flags")); @@ -1033,6 +1094,16 @@ void SceneTree::_bind_methods() { ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"call_group",&SceneTree::_call_group,mi,defargs); + ObjectTypeDB::bind_method(_MD("set_current_scene","child_node"),&SceneTree::set_current_scene); + ObjectTypeDB::bind_method(_MD("get_current_scene"),&SceneTree::get_current_scene); + + ObjectTypeDB::bind_method(_MD("change_scene","path"),&SceneTree::change_scene); + ObjectTypeDB::bind_method(_MD("change_scene_to","packed_scene"),&SceneTree::change_scene_to); + + ObjectTypeDB::bind_method(_MD("reload_current_scene"),&SceneTree::reload_current_scene); + + ObjectTypeDB::bind_method(_MD("_change_scene"),&SceneTree::_change_scene); + ADD_SIGNAL( MethodInfo("tree_changed") ); ADD_SIGNAL( MethodInfo("node_removed",PropertyInfo( Variant::OBJECT, "node") ) ); ADD_SIGNAL( MethodInfo("screen_resized") ); @@ -1077,6 +1148,7 @@ SceneTree::SceneTree() { //root->set_world_2d( Ref<World2D>( memnew( World2D ))); root->set_as_audio_listener(true); root->set_as_audio_listener_2d(true); + current_scene=NULL; stretch_mode=STRETCH_MODE_DISABLED; stretch_aspect=STRETCH_ASPECT_IGNORE; diff --git a/scene/main/scene_main_loop.h b/scene/main/scene_main_loop.h index fec4dd3908..e49c150fbf 100644 --- a/scene/main/scene_main_loop.h +++ b/scene/main/scene_main_loop.h @@ -42,7 +42,7 @@ class SceneTree; - +class PackedScene; class Node; class Viewport; @@ -136,7 +136,9 @@ private: Array _get_nodes_in_group(const StringName& p_group); + Node *current_scene; + void _change_scene(Node* p_to); //void _call_group(uint32_t p_call_flags,const StringName& p_group,const StringName& p_function,const Variant& p_arg1,const Variant& p_arg2); friend class Node; @@ -234,6 +236,17 @@ public: Node *get_edited_scene_root() const; #endif + void set_current_scene(Node* p_scene); + Node* get_current_scene() const; + Error change_scene(const String& p_path); + Error change_scene_to(const Ref<PackedScene>& p_scene); + Error reload_current_scene(); + + //used by Main::start, don't use otherwise + void add_current_scene(Node * p_current); + + + SceneTree(); ~SceneTree(); |