diff options
Diffstat (limited to 'scene/main')
-rw-r--r-- | scene/main/canvas_layer.cpp | 60 | ||||
-rw-r--r-- | scene/main/canvas_layer.h | 8 |
2 files changed, 56 insertions, 12 deletions
diff --git a/scene/main/canvas_layer.cpp b/scene/main/canvas_layer.cpp index c31a57f819..8e238c7d77 100644 --- a/scene/main/canvas_layer.cpp +++ b/scene/main/canvas_layer.cpp @@ -168,20 +168,13 @@ void CanvasLayer::_notification(int p_what) { case NOTIFICATION_ENTER_TREE: { - Node *n = this; - vp=NULL; + if (custom_viewport && ObjectDB::get_instance(custom_viewport_id)) { - while(n) { + vp=custom_viewport; + } else { + vp=Node::get_viewport(); - if (n->cast_to<Viewport>()) { - - vp = n->cast_to<Viewport>(); - break; - } - n=n->get_parent(); } - - ERR_FAIL_COND(!vp); viewport=vp->get_viewport(); @@ -205,6 +198,7 @@ Size2 CanvasLayer::get_viewport_size() const { if (!is_inside_tree()) return Size2(1,1); + Rect2 r = vp->get_visible_rect(); return r.size; } @@ -215,6 +209,43 @@ RID CanvasLayer::get_viewport() const { return viewport; } +void CanvasLayer::set_custom_viewport(Node *p_viewport) { + ERR_FAIL_NULL(p_viewport); + if (is_inside_tree()) { + VisualServer::get_singleton()->viewport_remove_canvas(viewport,canvas->get_canvas()); + viewport=RID(); + } + + custom_viewport=p_viewport->cast_to<Viewport>(); + + if (custom_viewport) { + custom_viewport_id=custom_viewport->get_instance_ID(); + } else { + custom_viewport_id=0; + } + + if (is_inside_tree()) { + + + if (custom_viewport) + vp=custom_viewport; + else + vp=Node::get_viewport(); + + viewport = vp->get_viewport(); + + VisualServer::get_singleton()->viewport_attach_canvas(viewport,canvas->get_canvas()); + VisualServer::get_singleton()->viewport_set_canvas_layer(viewport,canvas->get_canvas(),layer); + VisualServer::get_singleton()->viewport_set_canvas_transform(viewport,canvas->get_canvas(),transform); + } + +} + +Node* CanvasLayer::get_custom_viewport() const { + + return custom_viewport; +} + void CanvasLayer::_bind_methods() { @@ -241,8 +272,11 @@ void CanvasLayer::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_scale","scale"),&CanvasLayer::set_scale); ObjectTypeDB::bind_method(_MD("get_scale"),&CanvasLayer::get_scale); + ObjectTypeDB::bind_method(_MD("set_custom_viewport","viewport:Viewport"),&CanvasLayer::set_custom_viewport); + ObjectTypeDB::bind_method(_MD("get_custom_viewport:Viewport"),&CanvasLayer::get_custom_viewport); + ObjectTypeDB::bind_method(_MD("get_world_2d:World2D"),&CanvasLayer::get_world_2d); - ObjectTypeDB::bind_method(_MD("get_viewport"),&CanvasLayer::get_viewport); +// ObjectTypeDB::bind_method(_MD("get_viewport"),&CanvasLayer::get_viewport); ADD_PROPERTY( PropertyInfo(Variant::INT,"layer",PROPERTY_HINT_RANGE,"-128,128,1"),_SCS("set_layer"),_SCS("get_layer") ); //ADD_PROPERTY( PropertyInfo(Variant::MATRIX32,"transform",PROPERTY_HINT_RANGE),_SCS("set_transform"),_SCS("get_transform") ); @@ -260,4 +294,6 @@ CanvasLayer::CanvasLayer() { locrotscale_dirty=false; layer=1; canvas = Ref<World2D>( memnew(World2D) ); + custom_viewport=NULL; + custom_viewport_id=0; } diff --git a/scene/main/canvas_layer.h b/scene/main/canvas_layer.h index a3e8211a43..6aad90a09d 100644 --- a/scene/main/canvas_layer.h +++ b/scene/main/canvas_layer.h @@ -45,6 +45,10 @@ class CanvasLayer : public Node { int layer; Matrix32 transform; Ref<World2D> canvas; + + ObjectID custom_viewport_id; // to check validity + Viewport *custom_viewport; + RID viewport; Viewport *vp; @@ -55,6 +59,7 @@ class CanvasLayer : public Node { void _update_xform(); void _update_locrotscale(); + protected: void _notification(int p_what); @@ -85,6 +90,9 @@ public: RID get_viewport() const; + void set_custom_viewport(Node *p_viewport); + Node* get_custom_viewport() const; + CanvasLayer(); }; |