Browse Source

Restore viewport set_world_2d functionality

(cherry picked from commit 97cf3eba56f7f89b9a6b275c70e54e52303e25a7)
Fabio Alessandrelli 9 years ago
parent
commit
b61d7e6261
2 changed files with 34 additions and 22 deletions
  1. 32 22
      scene/main/viewport.cpp
  2. 2 0
      scene/main/viewport.h

+ 32 - 22
scene/main/viewport.cpp

@@ -359,13 +359,7 @@ void Viewport::_notification(int p_what) {
 			_update_listener_2d();
 			_update_rect();
 
-			if (world_2d.is_valid()) {
-				find_world_2d()->_register_viewport(this,Rect2());
-//best to defer this and not do it here, as it can annoy a lot of setup logic if user
-//adds a node and then moves it, will get enter/exit screen/viewport notifications
-//unnecesarily
-//				update_worlds();
-			}
+			find_world_2d()->_register_viewport(this,Rect2());
 
 			add_to_group("_viewports");
 			if (get_tree()->is_debugging_collisions_hint()) {
@@ -1001,19 +995,34 @@ bool Viewport::has_transparent_background() const {
 	return transparent_bg;
 }
 
-#if 0
 void Viewport::set_world_2d(const Ref<World2D>& p_world_2d) {
+	if (world_2d==p_world_2d)
+		return;
+
+	if (parent && parent->find_world_2d()==p_world_2d) {
+		WARN_PRINT("Unable to use parent world as world_2d");
+		return;
+	}
+
+	if (is_inside_tree()) {
+		find_world_2d()->_remove_viewport(this);
+		VisualServer::get_singleton()->viewport_remove_canvas(viewport,current_canvas);
+	}
+
+	if (p_world_2d.is_valid())
+		world_2d=p_world_2d;
+	else {
+		WARN_PRINT("Invalid world");
+		world_2d=Ref<World2D>( memnew( World2D ));
+	}
 
-	world_2d=p_world_2d;
 	_update_listener_2d();
 
-	if (is_inside_scene()) {
-		if (current_canvas.is_valid())
-			VisualServer::get_singleton()->viewport_remove_canvas(viewport,current_canvas);
+	if (is_inside_tree()) {
 		current_canvas=find_world_2d()->get_canvas();
 		VisualServer::get_singleton()->viewport_attach_canvas(viewport,current_canvas);
+		find_world_2d()->_register_viewport(this,Rect2());
 	}
-
 }
 
 Ref<World2D> Viewport::find_world_2d() const{
@@ -1025,13 +1034,6 @@ Ref<World2D> Viewport::find_world_2d() const{
 	else
 		return Ref<World2D>();
 }
-#endif
-
-Ref<World2D> Viewport::find_world_2d() const{
-
-	return world_2d;
-}
-
 
 void Viewport::_propagate_enter_world(Node *p_node) {
 
@@ -1141,6 +1143,11 @@ Ref<World> Viewport::get_world() const{
 	return world;
 }
 
+Ref<World2D> Viewport::get_world_2d() const{
+
+	return world_2d;
+}
+
 Ref<World> Viewport::find_world() const{
 
 	if (own_world.is_valid())
@@ -1303,6 +1310,9 @@ void Viewport::render_target_clear() {
 
 void Viewport::set_render_target_filter(bool p_enable) {
 
+	if(!render_target)
+		return;
+
 	render_target_texture->set_flags(p_enable?int(Texture::FLAG_FILTER):int(0));
 
 }
@@ -2590,8 +2600,8 @@ void Viewport::_bind_methods() {
 
 	ObjectTypeDB::bind_method(_MD("set_rect","rect"), &Viewport::set_rect);
 	ObjectTypeDB::bind_method(_MD("get_rect"), &Viewport::get_rect);
-	//ObjectTypeDB::bind_method(_MD("set_world_2d","world_2d:World2D"), &Viewport::set_world_2d);
-	//ObjectTypeDB::bind_method(_MD("get_world_2d:World2D"), &Viewport::get_world_2d);
+	ObjectTypeDB::bind_method(_MD("set_world_2d","world_2d:World2D"), &Viewport::set_world_2d);
+	ObjectTypeDB::bind_method(_MD("get_world_2d:World2D"), &Viewport::get_world_2d);
 	ObjectTypeDB::bind_method(_MD("find_world_2d:World2D"), &Viewport::find_world_2d);
 	ObjectTypeDB::bind_method(_MD("set_world","world:World"), &Viewport::set_world);
 	ObjectTypeDB::bind_method(_MD("get_world:World"), &Viewport::get_world);

+ 2 - 0
scene/main/viewport.h

@@ -305,9 +305,11 @@ public:
 	RID get_viewport() const;
 
 	void set_world(const Ref<World>& p_world);
+	void set_world_2d(const Ref<World2D>& p_world_2d);
 	Ref<World> get_world() const;
 	Ref<World> find_world() const;
 
+	Ref<World2D> get_world_2d() const;
 	Ref<World2D> find_world_2d() const;