added spatial and node2d helper methods

to perform operations such as translaiton, rotation, etc directly on
nodes.
This commit is contained in:
Juan Linietsky 2015-03-22 10:33:58 -03:00
parent dac2017dee
commit a93e33f5c8
8 changed files with 166 additions and 43 deletions

View File

@ -23,7 +23,7 @@ Rect2 Light2D::get_item_rect() const {
Size2i s; Size2i s;
s = texture->get_size()*scale; s = texture->get_size()*_scale;
Point2i ofs=texture_offset; Point2i ofs=texture_offset;
ofs-=s/2; ofs-=s/2;
@ -98,8 +98,8 @@ float Light2D::get_height() const {
void Light2D::set_scale( float p_scale) { void Light2D::set_scale( float p_scale) {
scale=p_scale; _scale=p_scale;
VS::get_singleton()->canvas_light_set_scale(canvas_light,scale); VS::get_singleton()->canvas_light_set_scale(canvas_light,_scale);
item_rect_changed(); item_rect_changed();
} }
@ -107,7 +107,7 @@ void Light2D::set_scale( float p_scale) {
float Light2D::get_scale() const { float Light2D::get_scale() const {
return scale; return _scale;
} }
void Light2D::set_z_range_min( int p_min_z) { void Light2D::set_z_range_min( int p_min_z) {
@ -322,7 +322,7 @@ Light2D::Light2D() {
shadow=false; shadow=false;
color=Color(1,1,1); color=Color(1,1,1);
height=0; height=0;
scale=1.0; _scale=1.0;
z_min=-1024; z_min=-1024;
z_max=1024; z_max=1024;
layer_min=0; layer_min=0;

View File

@ -12,7 +12,7 @@ private:
bool shadow; bool shadow;
Color color; Color color;
float height; float height;
float scale; float _scale;
int z_min; int z_min;
int z_max; int z_max;
int layer_min; int layer_min;

View File

@ -65,7 +65,7 @@ void Node2D::edit_set_state(const Variant& p_state) {
pos = state[0]; pos = state[0];
angle = state[1]; angle = state[1];
scale = state[2]; _scale = state[2];
_update_transform(); _update_transform();
_change_notify("transform/rot"); _change_notify("transform/rot");
_change_notify("transform/scale"); _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; Point2 new_pos = p_edit_rect.pos + p_edit_rect.size*zero_offset;//p_edit_rect.pos - r.pos;
Matrix32 postxf; Matrix32 postxf;
postxf.set_rotation_and_scale(angle,scale); postxf.set_rotation_and_scale(angle,_scale);
new_pos = postxf.xform(new_pos); new_pos = postxf.xform(new_pos);
pos+=new_pos; pos+=new_pos;
scale*=new_scale; _scale*=new_scale;
_update_transform(); _update_transform();
_change_notify("transform/scale"); _change_notify("transform/scale");
@ -118,14 +118,14 @@ void Node2D::_update_xform_values() {
pos=_mat.elements[2]; pos=_mat.elements[2];
angle=_mat.get_rotation(); angle=_mat.get_rotation();
scale=_mat.get_scale(); _scale=_mat.get_scale();
_xform_dirty=false; _xform_dirty=false;
} }
void Node2D::_update_transform() { void Node2D::_update_transform() {
Matrix32 mat(angle,pos); Matrix32 mat(angle,pos);
_mat.set_rotation_and_scale(angle,scale); _mat.set_rotation_and_scale(angle,_scale);
_mat.elements[2]=pos; _mat.elements[2]=pos;
VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(),_mat); 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) if (_xform_dirty)
((Node2D*)this)->_update_xform_values(); ((Node2D*)this)->_update_xform_values();
scale=p_scale; _scale=p_scale;
if (scale.x==0) if (_scale.x==0)
scale.x=CMP_EPSILON; _scale.x=CMP_EPSILON;
if (scale.y==0) if (_scale.y==0)
scale.y=CMP_EPSILON; _scale.y=CMP_EPSILON;
_update_transform(); _update_transform();
_change_notify("transform/scale"); _change_notify("transform/scale");
@ -187,7 +187,7 @@ Size2 Node2D::get_scale() const {
if (_xform_dirty) if (_xform_dirty)
((Node2D*)this)->_update_xform_values(); ((Node2D*)this)->_update_xform_values();
return scale; return _scale;
} }
void Node2D::_set_rotd(float p_angle) { 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)); 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){ void Node2D::move_x(float p_delta,bool p_scaled){
Matrix32 t = get_transform(); 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_rot"),&Node2D::get_rot);
ObjectTypeDB::bind_method(_MD("get_scale"),&Node2D::get_scale); 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_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("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("set_global_pos","pos"),&Node2D::set_global_pos);
ObjectTypeDB::bind_method(_MD("get_global_pos"),&Node2D::get_global_pos); ObjectTypeDB::bind_method(_MD("get_global_pos"),&Node2D::get_global_pos);
@ -379,7 +398,7 @@ Node2D::Node2D() {
angle=0; angle=0;
scale=Vector2(1,1); _scale=Vector2(1,1);
_xform_dirty=false; _xform_dirty=false;
z=0; z=0;
z_relative=true; z_relative=true;

View File

@ -37,7 +37,7 @@ class Node2D : public CanvasItem {
Point2 pos; Point2 pos;
float angle; float angle;
Size2 scale; Size2 _scale;
int z; int z;
bool z_relative; bool z_relative;
@ -72,9 +72,12 @@ public:
void set_rot(float p_angle); void set_rot(float p_angle);
void set_scale(const Size2& p_scale); 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_x(float p_delta,bool p_scaled=false);
void move_y(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; Point2 get_pos() const;
float get_rot() const; float get_rot() const;
@ -96,6 +99,7 @@ public:
Matrix32 get_relative_transform(const Node *p_parent) const; Matrix32 get_relative_transform(const Node *p_parent) const;
Matrix32 get_transform() const; Matrix32 get_transform() const;
Node2D(); Node2D();

View File

@ -680,8 +680,6 @@ void Camera::_bind_methods() {
ObjectTypeDB::bind_method( _MD("get_projection"),&Camera::get_projection ); ObjectTypeDB::bind_method( _MD("get_projection"),&Camera::get_projection );
ObjectTypeDB::bind_method( _MD("set_visible_layers","mask"),&Camera::set_visible_layers ); ObjectTypeDB::bind_method( _MD("set_visible_layers","mask"),&Camera::set_visible_layers );
ObjectTypeDB::bind_method( _MD("get_visible_layers"),&Camera::get_visible_layers ); ObjectTypeDB::bind_method( _MD("get_visible_layers"),&Camera::get_visible_layers );
ObjectTypeDB::bind_method( _MD("look_at","target","up"),&Camera::look_at );
ObjectTypeDB::bind_method( _MD("look_at_from_pos","pos","target","up"),&Camera::look_at_from_pos );
ObjectTypeDB::bind_method(_MD("set_environment","env:Environment"),&Camera::set_environment); ObjectTypeDB::bind_method(_MD("set_environment","env:Environment"),&Camera::set_environment);
ObjectTypeDB::bind_method(_MD("get_environment:Environment"),&Camera::get_environment); ObjectTypeDB::bind_method(_MD("get_environment:Environment"),&Camera::get_environment);
ObjectTypeDB::bind_method(_MD("set_keep_aspect_mode","mode"),&Camera::set_keep_aspect_mode); ObjectTypeDB::bind_method(_MD("set_keep_aspect_mode","mode"),&Camera::set_keep_aspect_mode);
@ -752,22 +750,6 @@ Vector<Plane> Camera::get_frustum() const {
void Camera::look_at(const Vector3& p_target, const Vector3& p_up_normal) {
Transform lookat;
lookat.origin=get_camera_transform().origin;
lookat=lookat.looking_at(p_target,p_up_normal);
set_global_transform(lookat);
}
void Camera::look_at_from_pos(const Vector3& p_pos,const Vector3& p_target, const Vector3& p_up_normal) {
Transform lookat;
lookat.origin=p_pos;
lookat=lookat.looking_at(p_target,p_up_normal);
set_global_transform(lookat);
}
void Camera::set_v_offset(float p_offset) { void Camera::set_v_offset(float p_offset) {

View File

@ -139,8 +139,6 @@ public:
void set_keep_aspect_mode(KeepAspect p_aspect); void set_keep_aspect_mode(KeepAspect p_aspect);
KeepAspect get_keep_aspect_mode() const; KeepAspect get_keep_aspect_mode() const;
void look_at(const Vector3& p_target, const Vector3& p_up_normal);
void look_at_from_pos(const Vector3& p_pos,const Vector3& p_target, const Vector3& p_up_normal);
void set_v_offset(float p_offset); void set_v_offset(float p_offset);
float get_v_offset() const; float get_v_offset() const;

View File

@ -593,6 +593,91 @@ bool Spatial::_is_visible_() const {
return !is_hidden(); return !is_hidden();
} }
void Spatial::rotate(const Vector3& p_normal,float p_radians) {
Transform t =get_transform();
t.basis.rotate(p_normal,p_radians);
set_transform(t);
}
void Spatial::rotate_x(float p_radians) {
Transform t =get_transform();
t.basis.rotate(Vector3(1,0,0),p_radians);
set_transform(t);
}
void Spatial::rotate_y(float p_radians){
Transform t =get_transform();
t.basis.rotate(Vector3(0,1,0),p_radians);
set_transform(t);
}
void Spatial::rotate_z(float p_radians){
Transform t =get_transform();
t.basis.rotate(Vector3(0,0,1),p_radians);
set_transform(t);
}
void Spatial::translate(const Vector3& p_offset){
Transform t =get_transform();
t.origin+=p_offset;
set_transform(t);
}
void Spatial::scale(const Vector3& p_ratio){
Transform t =get_transform();
t.basis.scale(p_ratio);
set_transform(t);
}
void Spatial::global_rotate(const Vector3& p_normal,float p_radians){
Matrix3 rotation(p_normal,p_radians);
Transform t = get_global_transform();
t.basis= rotation * t.basis;
set_global_transform(t);
}
void Spatial::global_translate(const Vector3& p_offset){
Transform t = get_global_transform();
t.origin+=p_offset;
set_global_transform(t);
}
void Spatial::orthonormalize() {
Transform t = get_transform();
t.orthonormalize();
set_transform(t);
}
void Spatial::look_at(const Vector3& p_target, const Vector3& p_up_normal) {
Transform lookat;
lookat.origin=get_global_transform().origin;
lookat=lookat.looking_at(p_target,p_up_normal);
set_global_transform(lookat);
}
void Spatial::look_at_from_pos(const Vector3& p_pos,const Vector3& p_target, const Vector3& p_up_normal) {
Transform lookat;
lookat.origin=p_pos;
lookat=lookat.looking_at(p_target,p_up_normal);
set_global_transform(lookat);
}
void Spatial::_bind_methods() { void Spatial::_bind_methods() {
@ -633,6 +718,27 @@ void Spatial::_bind_methods() {
ObjectTypeDB::bind_method(_MD("_set_visible_"), &Spatial::_set_visible_); ObjectTypeDB::bind_method(_MD("_set_visible_"), &Spatial::_set_visible_);
ObjectTypeDB::bind_method(_MD("_is_visible_"), &Spatial::_is_visible_); ObjectTypeDB::bind_method(_MD("_is_visible_"), &Spatial::_is_visible_);
void rotate(const Vector3& p_normal,float p_radians);
void rotate_x(float p_radians);
void rotate_y(float p_radians);
void rotate_z(float p_radians);
void translate(const Vector3& p_offset);
void scale(const Vector3& p_ratio);
void global_rotate(const Vector3& p_normal,float p_radians);
void global_translate(const Vector3& p_offset);
ObjectTypeDB::bind_method( _MD("rotate","normal","radians"),&Spatial::rotate );
ObjectTypeDB::bind_method( _MD("global_rotate","normal","radians"),&Spatial::global_rotate );
ObjectTypeDB::bind_method( _MD("rotate_x","radians"),&Spatial::rotate_x );
ObjectTypeDB::bind_method( _MD("rotate_y","radians"),&Spatial::rotate_y );
ObjectTypeDB::bind_method( _MD("rotate_z","radians"),&Spatial::rotate_z );
ObjectTypeDB::bind_method( _MD("translate","offset"),&Spatial::translate );
ObjectTypeDB::bind_method( _MD("global_translate","offset"),&Spatial::global_translate );
ObjectTypeDB::bind_method( _MD("orthonormalize"),&Spatial::orthonormalize );
ObjectTypeDB::bind_method( _MD("look_at","target","up"),&Spatial::look_at );
ObjectTypeDB::bind_method( _MD("look_at_from_pos","pos","target","up"),&Spatial::look_at_from_pos );
BIND_CONSTANT( NOTIFICATION_TRANSFORM_CHANGED ); BIND_CONSTANT( NOTIFICATION_TRANSFORM_CHANGED );
BIND_CONSTANT( NOTIFICATION_ENTER_WORLD ); BIND_CONSTANT( NOTIFICATION_ENTER_WORLD );
BIND_CONSTANT( NOTIFICATION_EXIT_WORLD ); BIND_CONSTANT( NOTIFICATION_EXIT_WORLD );

View File

@ -167,6 +167,20 @@ public:
Transform get_relative_transform(const Node *p_parent) const; Transform get_relative_transform(const Node *p_parent) const;
void rotate(const Vector3& p_normal,float p_radians);
void rotate_x(float p_radians);
void rotate_y(float p_radians);
void rotate_z(float p_radians);
void translate(const Vector3& p_offset);
void scale(const Vector3& p_ratio);
void global_rotate(const Vector3& p_normal,float p_radians);
void global_translate(const Vector3& p_offset);
void look_at(const Vector3& p_target, const Vector3& p_up_normal);
void look_at_from_pos(const Vector3& p_pos,const Vector3& p_target, const Vector3& p_up_normal);
void orthonormalize();
void show(); void show();
void hide(); void hide();
bool is_visible() const; bool is_visible() const;