From a93e33f5c8afc8ce2e9c1674718ff103bfae49b0 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Sun, 22 Mar 2015 10:33:58 -0300 Subject: added spatial and node2d helper methods to perform operations such as translaiton, rotation, etc directly on nodes. --- scene/2d/light_2d.cpp | 10 +++++----- scene/2d/light_2d.h | 2 +- scene/2d/node_2d.cpp | 49 ++++++++++++++++++++++++++++++++++--------------- scene/2d/node_2d.h | 8 ++++++-- 4 files changed, 46 insertions(+), 23 deletions(-) (limited to 'scene/2d') diff --git a/scene/2d/light_2d.cpp b/scene/2d/light_2d.cpp index 8f6907798f..949c952b39 100644 --- a/scene/2d/light_2d.cpp +++ b/scene/2d/light_2d.cpp @@ -23,7 +23,7 @@ Rect2 Light2D::get_item_rect() const { Size2i s; - s = texture->get_size()*scale; + s = texture->get_size()*_scale; Point2i ofs=texture_offset; ofs-=s/2; @@ -98,8 +98,8 @@ float Light2D::get_height() const { void Light2D::set_scale( float p_scale) { - scale=p_scale; - VS::get_singleton()->canvas_light_set_scale(canvas_light,scale); + _scale=p_scale; + VS::get_singleton()->canvas_light_set_scale(canvas_light,_scale); item_rect_changed(); } @@ -107,7 +107,7 @@ void Light2D::set_scale( float p_scale) { float Light2D::get_scale() const { - return scale; + return _scale; } void Light2D::set_z_range_min( int p_min_z) { @@ -322,7 +322,7 @@ Light2D::Light2D() { shadow=false; color=Color(1,1,1); height=0; - scale=1.0; + _scale=1.0; z_min=-1024; z_max=1024; layer_min=0; diff --git a/scene/2d/light_2d.h b/scene/2d/light_2d.h index 6eb6ad5237..95196af5e0 100644 --- a/scene/2d/light_2d.h +++ b/scene/2d/light_2d.h @@ -12,7 +12,7 @@ private: bool shadow; Color color; float height; - float scale; + float _scale; int z_min; int z_max; int layer_min; diff --git a/scene/2d/node_2d.cpp b/scene/2d/node_2d.cpp index 36b6b220b3..0b098f0cad 100644 --- a/scene/2d/node_2d.cpp +++ b/scene/2d/node_2d.cpp @@ -65,7 +65,7 @@ void Node2D::edit_set_state(const Variant& p_state) { pos = state[0]; angle = state[1]; - scale = state[2]; + _scale = state[2]; _update_transform(); _change_notify("transform/rot"); _change_notify("transform/scale"); @@ -93,11 +93,11 @@ void Node2D::edit_set_rect(const Rect2& p_edit_rect) { Point2 new_pos = p_edit_rect.pos + p_edit_rect.size*zero_offset;//p_edit_rect.pos - r.pos; Matrix32 postxf; - postxf.set_rotation_and_scale(angle,scale); + postxf.set_rotation_and_scale(angle,_scale); new_pos = postxf.xform(new_pos); pos+=new_pos; - scale*=new_scale; + _scale*=new_scale; _update_transform(); _change_notify("transform/scale"); @@ -118,14 +118,14 @@ void Node2D::_update_xform_values() { pos=_mat.elements[2]; angle=_mat.get_rotation(); - scale=_mat.get_scale(); + _scale=_mat.get_scale(); _xform_dirty=false; } void Node2D::_update_transform() { Matrix32 mat(angle,pos); - _mat.set_rotation_and_scale(angle,scale); + _mat.set_rotation_and_scale(angle,_scale); _mat.elements[2]=pos; VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(),_mat); @@ -161,11 +161,11 @@ void Node2D::set_scale(const Size2& p_scale) { if (_xform_dirty) ((Node2D*)this)->_update_xform_values(); - scale=p_scale; - if (scale.x==0) - scale.x=CMP_EPSILON; - if (scale.y==0) - scale.y=CMP_EPSILON; + _scale=p_scale; + if (_scale.x==0) + _scale.x=CMP_EPSILON; + if (_scale.y==0) + _scale.y=CMP_EPSILON; _update_transform(); _change_notify("transform/scale"); @@ -187,7 +187,7 @@ Size2 Node2D::get_scale() const { if (_xform_dirty) ((Node2D*)this)->_update_xform_values(); - return scale; + return _scale; } void Node2D::_set_rotd(float p_angle) { @@ -224,11 +224,27 @@ Rect2 Node2D::get_item_rect() const { return Rect2(Point2(-32,-32),Size2(64,64)); } -void Node2D::rotate(float p_degrees) { +void Node2D::rotate(float p_radians) { - set_rot( get_rot() + p_degrees); + set_rot( get_rot() + p_radians); } +void Node2D::translate(const Vector2& p_amount) { + + set_pos( get_pos() + p_amount ); +} + +void Node2D::global_translate(const Vector2& p_amount) { + + set_global_pos( get_global_pos() + p_amount ); +} + +void Node2D::scale(const Vector2& p_amount) { + + set_scale( get_scale() * p_amount ); +} + + void Node2D::move_x(float p_delta,bool p_scaled){ Matrix32 t = get_transform(); @@ -345,9 +361,12 @@ void Node2D::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_rot"),&Node2D::get_rot); ObjectTypeDB::bind_method(_MD("get_scale"),&Node2D::get_scale); - ObjectTypeDB::bind_method(_MD("rotate","degrees"),&Node2D::rotate); + ObjectTypeDB::bind_method(_MD("rotate","radians"),&Node2D::rotate); ObjectTypeDB::bind_method(_MD("move_local_x","delta","scaled"),&Node2D::move_x,DEFVAL(false)); ObjectTypeDB::bind_method(_MD("move_local_y","delta","scaled"),&Node2D::move_y,DEFVAL(false)); + ObjectTypeDB::bind_method(_MD("translate","offset"),&Node2D::translate); + ObjectTypeDB::bind_method(_MD("global_translate","offset"),&Node2D::global_translate); + ObjectTypeDB::bind_method(_MD("scale","ratio"),&Node2D::scale); ObjectTypeDB::bind_method(_MD("set_global_pos","pos"),&Node2D::set_global_pos); ObjectTypeDB::bind_method(_MD("get_global_pos"),&Node2D::get_global_pos); @@ -379,7 +398,7 @@ Node2D::Node2D() { angle=0; - scale=Vector2(1,1); + _scale=Vector2(1,1); _xform_dirty=false; z=0; z_relative=true; diff --git a/scene/2d/node_2d.h b/scene/2d/node_2d.h index 7b059008c2..39a1061195 100644 --- a/scene/2d/node_2d.h +++ b/scene/2d/node_2d.h @@ -37,7 +37,7 @@ class Node2D : public CanvasItem { Point2 pos; float angle; - Size2 scale; + Size2 _scale; int z; bool z_relative; @@ -72,9 +72,12 @@ public: void set_rot(float p_angle); void set_scale(const Size2& p_scale); - void rotate(float p_degrees); + void rotate(float p_radians); void move_x(float p_delta,bool p_scaled=false); void move_y(float p_delta,bool p_scaled=false); + void translate(const Vector2& p_amount); + void global_translate(const Vector2& p_amount); + void scale(const Vector2& p_amount); Point2 get_pos() const; float get_rot() const; @@ -96,6 +99,7 @@ public: Matrix32 get_relative_transform(const Node *p_parent) const; + Matrix32 get_transform() const; Node2D(); -- cgit v1.2.3