diff options
Diffstat (limited to 'scene/2d')
-rw-r--r-- | scene/2d/canvas_item.cpp | 35 | ||||
-rw-r--r-- | scene/2d/canvas_item.h | 4 | ||||
-rw-r--r-- | scene/2d/sprite.cpp | 201 | ||||
-rw-r--r-- | scene/2d/sprite.h | 40 |
4 files changed, 255 insertions, 25 deletions
diff --git a/scene/2d/canvas_item.cpp b/scene/2d/canvas_item.cpp index 2e10ae2edb..8eb5c9dfc8 100644 --- a/scene/2d/canvas_item.cpp +++ b/scene/2d/canvas_item.cpp @@ -255,7 +255,7 @@ void CanvasItem::_enter_canvas() { if ((!get_parent() || !get_parent()->cast_to<CanvasItem>()) || toplevel) { Node *n = this; - viewport=NULL; + Viewport *viewport=NULL; canvas_layer=NULL; while(n) { @@ -288,15 +288,10 @@ void CanvasItem::_enter_canvas() { } else { CanvasItem *parent = get_parent_item(); - viewport=parent->viewport; VisualServer::get_singleton()->canvas_item_set_parent(canvas_item,parent->get_canvas_item()); parent->_queue_sort_children(); } - if (!viewport) { - - print_line("no viewport wtf!"); - } pending_update=false; update(); @@ -308,7 +303,6 @@ void CanvasItem::_exit_canvas() { notification(NOTIFICATION_EXIT_CANVAS,true); //reverse the notification VisualServer::get_singleton()->canvas_item_set_parent(canvas_item,RID()); - viewport=NULL; canvas_layer=NULL; group=""; @@ -655,7 +649,7 @@ void CanvasItem::_notify_transform(CanvasItem *p_node) { Rect2 CanvasItem::get_viewport_rect() const { ERR_FAIL_COND_V(!is_inside_scene(),Rect2()); - return viewport->get_visible_rect(); + return get_viewport()->get_visible_rect(); } RID CanvasItem::get_canvas() const { @@ -665,7 +659,7 @@ RID CanvasItem::get_canvas() const { if (canvas_layer) return canvas_layer->get_world_2d()->get_canvas(); else - return viewport->find_world_2d()->get_canvas(); + return get_viewport()->find_world_2d()->get_canvas(); } @@ -680,11 +674,6 @@ CanvasItem *CanvasItem::get_toplevel() const { return ci; } -Viewport *CanvasItem::get_viewport() const { - - return viewport; -} - Ref<World2D> CanvasItem::get_world_2d() const { @@ -694,8 +683,8 @@ Ref<World2D> CanvasItem::get_world_2d() const { if (tl->canvas_layer) { return tl->canvas_layer->get_world_2d(); - } else if (tl->viewport) { - return tl->viewport->find_world_2d(); + } else if (tl->get_viewport()) { + return tl->get_viewport()->find_world_2d(); } else { return Ref<World2D>(); } @@ -705,7 +694,7 @@ Ref<World2D> CanvasItem::get_world_2d() const { RID CanvasItem::get_viewport_rid() const { ERR_FAIL_COND_V(!is_inside_scene(),RID()); - return viewport->get_viewport(); + return get_viewport()->get_viewport(); } void CanvasItem::set_block_transform_notify(bool p_enable) { @@ -795,7 +784,7 @@ void CanvasItem::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_viewport_rect"),&CanvasItem::get_viewport_rect); ObjectTypeDB::bind_method(_MD("get_canvas"),&CanvasItem::get_canvas); ObjectTypeDB::bind_method(_MD("get_world_2d"),&CanvasItem::get_world_2d); - ObjectTypeDB::bind_method(_MD("get_viewport"),&CanvasItem::get_viewport); + //ObjectTypeDB::bind_method(_MD("get_viewport"),&CanvasItem::get_viewport); BIND_VMETHOD(MethodInfo("_draw")); @@ -838,14 +827,14 @@ Matrix32 CanvasItem::get_viewport_transform() const { if (canvas_layer) { - if (viewport) { - return viewport->get_final_transform() * canvas_layer->get_transform(); + if (get_viewport()) { + return get_viewport()->get_final_transform() * canvas_layer->get_transform(); } else { return canvas_layer->get_transform(); } - } else if (viewport) { - return viewport->get_final_transform() * viewport->get_canvas_transform(); + } else if (get_viewport()) { + return get_viewport()->get_final_transform() * get_viewport()->get_canvas_transform(); } return Matrix32(); @@ -868,7 +857,7 @@ CanvasItem::CanvasItem() : xform_change(this) { drawing=false; behind=false; block_transform_notify=false; - viewport=NULL; +// viewport=NULL; canvas_layer=NULL; global_invalid=true; diff --git a/scene/2d/canvas_item.h b/scene/2d/canvas_item.h index 5489e105d9..397b206677 100644 --- a/scene/2d/canvas_item.h +++ b/scene/2d/canvas_item.h @@ -59,7 +59,7 @@ private: RID canvas_item; String group; - Viewport *viewport; + CanvasLayer *canvas_layer; float opacity; @@ -200,7 +200,7 @@ public: RID get_viewport_rid() const; RID get_canvas() const; Ref<World2D> get_world_2d() const; - Viewport *get_viewport() const; + CanvasItem(); diff --git a/scene/2d/sprite.cpp b/scene/2d/sprite.cpp index ad9a76ee35..7d033ed87f 100644 --- a/scene/2d/sprite.cpp +++ b/scene/2d/sprite.cpp @@ -29,6 +29,8 @@ #include "sprite.h" #include "core/core_string_names.h" #include "scene/scene_string_names.h" +#include "scene/main/viewport.h" + void Sprite::edit_set_pivot(const Point2& p_pivot) { set_offset(p_pivot); @@ -335,3 +337,202 @@ Sprite::Sprite() { } + + + + +//////////////////////////// VPSPRITE +/// +/// +/// + + +void ViewportSprite::edit_set_pivot(const Point2& p_pivot) { + + set_offset(p_pivot); +} + +Point2 ViewportSprite::edit_get_pivot() const { + + return get_offset(); +} +bool ViewportSprite::edit_has_pivot() const { + + return true; +} + +void ViewportSprite::_notification(int p_what) { + + switch(p_what) { + + case NOTIFICATION_ENTER_SCENE: { + + if (!viewport_path.is_empty()) { + + Node *n = get_node(viewport_path); + ERR_FAIL_COND(!n); + Viewport *vp=n->cast_to<Viewport>(); + ERR_FAIL_COND(!vp); + + Ref<RenderTargetTexture> rtt = vp->get_render_target_texture(); + texture=rtt; + texture->connect("changed",this,"update"); + item_rect_changed(); + } + } break; + case NOTIFICATION_EXIT_SCENE: { + + if (texture.is_valid()) { + + texture->disconnect("changed",this,"update"); + texture=Ref<Texture>(); + } + } break; + case NOTIFICATION_DRAW: { + + if (texture.is_null()) + return; + + RID ci = get_canvas_item(); + + /* + texture->draw(ci,Point2()); + break; + */ + + Size2i s; + Rect2i src_rect; + + s = texture->get_size(); + + src_rect.size=s; + + Point2i ofs=offset; + if (centered) + ofs-=s/2; + + Rect2i dst_rect(ofs,s); + texture->draw_rect_region(ci,dst_rect,src_rect,modulate); + + } break; + } +} + +void ViewportSprite::set_viewport_path(const NodePath& p_viewport) { + + viewport_path=p_viewport; + update(); + if (!is_inside_scene()) + return; + + if (texture.is_valid()) { + texture->disconnect("changed",this,"update"); + texture=Ref<Texture>(); + } + + if (viewport_path.is_empty()) + return; + + + Node *n = get_node(viewport_path); + ERR_FAIL_COND(!n); + Viewport *vp=n->cast_to<Viewport>(); + ERR_FAIL_COND(!vp); + + Ref<RenderTargetTexture> rtt = vp->get_render_target_texture(); + texture=rtt; + + if (texture.is_valid()) { + texture->connect("changed",this,"update"); + } + + item_rect_changed(); + +} + +NodePath ViewportSprite::get_viewport_path() const { + + return viewport_path; +} + +void ViewportSprite::set_centered(bool p_center) { + + centered=p_center; + update(); + item_rect_changed(); +} + +bool ViewportSprite::is_centered() const { + + return centered; +} + +void ViewportSprite::set_offset(const Point2& p_offset) { + + offset=p_offset; + update(); + item_rect_changed(); +} +Point2 ViewportSprite::get_offset() const { + + return offset; +} +void ViewportSprite::set_modulate(const Color& p_color) { + + modulate=p_color; + update(); +} + +Color ViewportSprite::get_modulate() const{ + + return modulate; +} + + +Rect2 ViewportSprite::get_item_rect() const { + + if (texture.is_null()) + return Rect2(0,0,1,1); + //if (texture.is_null()) + // return CanvasItem::get_item_rect(); + + Size2i s; + + s = texture->get_size(); + Point2i ofs=offset; + if (centered) + ofs-=s/2; + + if (s==Size2(0,0)) + s=Size2(1,1); + + return Rect2(ofs,s); +} + + +void ViewportSprite::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("set_viewport_path","path"),&ViewportSprite::set_viewport_path); + ObjectTypeDB::bind_method(_MD("get_viewport_path"),&ViewportSprite::get_viewport_path); + + ObjectTypeDB::bind_method(_MD("set_centered","centered"),&ViewportSprite::set_centered); + ObjectTypeDB::bind_method(_MD("is_centered"),&ViewportSprite::is_centered); + + ObjectTypeDB::bind_method(_MD("set_offset","offset"),&ViewportSprite::set_offset); + ObjectTypeDB::bind_method(_MD("get_offset"),&ViewportSprite::get_offset); + + ObjectTypeDB::bind_method(_MD("set_modulate","modulate"),&ViewportSprite::set_modulate); + ObjectTypeDB::bind_method(_MD("get_modulate"),&ViewportSprite::get_modulate); + + ADD_PROPERTY( PropertyInfo( Variant::NODE_PATH, "viewport"), _SCS("set_viewport_path"),_SCS("get_viewport_path")); + ADD_PROPERTY( PropertyInfo( Variant::BOOL, "centered"), _SCS("set_centered"),_SCS("is_centered")); + ADD_PROPERTY( PropertyInfo( Variant::VECTOR2, "offset"), _SCS("set_offset"),_SCS("get_offset")); + ADD_PROPERTY( PropertyInfo( Variant::COLOR, "modulate"), _SCS("set_modulate"),_SCS("get_modulate")); + +} + +ViewportSprite::ViewportSprite() { + + centered=true; + modulate=Color(1,1,1,1); +} diff --git a/scene/2d/sprite.h b/scene/2d/sprite.h index 42f737fa91..f26852fea6 100644 --- a/scene/2d/sprite.h +++ b/scene/2d/sprite.h @@ -105,4 +105,44 @@ public: Sprite(); }; +class ViewportSprite : public Node2D { + + OBJ_TYPE( ViewportSprite, Node2D ); + + Ref<Texture> texture; + NodePath viewport_path; + + bool centered; + Point2 offset; + Color modulate; + +protected: + + void _notification(int p_what); + + static void _bind_methods();; + +public: + + virtual void edit_set_pivot(const Point2& p_pivot); + virtual Point2 edit_get_pivot() const; + virtual bool edit_has_pivot() const; + + void set_viewport_path(const NodePath& p_viewport); + NodePath get_viewport_path() const; + + void set_centered(bool p_center); + bool is_centered() const; + + void set_offset(const Point2& p_offset); + Point2 get_offset() const; + + void set_modulate(const Color& p_color); + Color get_modulate() const; + + virtual Rect2 get_item_rect() const; + + ViewportSprite(); +}; + #endif // SPRITE_H |