From 3d68949a1cc41b4865618bddde23122f66764ee1 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Sun, 6 Jul 2014 11:49:27 -0300 Subject: [PATCH] 2D Animation Improvements -=-=-=-=-=-=-=-=-=--=-=-= -Ability to set 2D nodes as bones -Abity to set 2D nodes as IK chains -2D IK Solver -Improvements in the UI for adding keyframes (separate loc,rot,scale buttons) --- core/math/math_2d.cpp | 6 +- core/math/math_2d.h | 3 + .../java/src/com/android/godot/Godot.java | 2 +- platform/android/java_glue.cpp | 5 + scene/2d/node_2d.cpp | 6 +- scene/2d/path_2d.cpp | 270 ++++++- scene/2d/path_2d.h | 59 ++ scene/register_scene_types.cpp | 1 + scene/resources/curve.cpp | 663 ++++++++++++------ scene/resources/curve.h | 75 +- scene/scene_string_names.cpp | 1 + scene/scene_string_names.h | 1 + tools/editor/editor_settings.cpp | 6 + tools/editor/icons/icon_bone.png | Bin 0 -> 2992 bytes tools/editor/icons/icon_curve_curve.png | Bin 0 -> 561 bytes tools/editor/icons/icon_unbone.png | Bin 0 -> 3000 bytes .../plugins/canvas_item_editor_plugin.cpp | 652 +++++++++++++++-- .../plugins/canvas_item_editor_plugin.h | 35 +- .../editor/plugins/path_2d_editor_plugin.cpp | 157 ++++- tools/editor/plugins/path_2d_editor_plugin.h | 21 +- tools/editor/plugins/path_editor_plugin.cpp | 8 +- tools/editor/plugins/path_editor_plugin.h | 142 ++-- 22 files changed, 1725 insertions(+), 388 deletions(-) create mode 100644 tools/editor/icons/icon_bone.png create mode 100644 tools/editor/icons/icon_curve_curve.png create mode 100644 tools/editor/icons/icon_unbone.png diff --git a/core/math/math_2d.cpp b/core/math/math_2d.cpp index acaaa327f3a..6c160abaca8 100644 --- a/core/math/math_2d.cpp +++ b/core/math/math_2d.cpp @@ -74,7 +74,7 @@ float Vector2::distance_squared_to(const Vector2& p_vector2) const { float Vector2::angle_to(const Vector2& p_vector2) const { - return Math::atan2( x-p_vector2.x, y - p_vector2.y ); + return Math::atan2( tangent().dot(p_vector2), dot(p_vector2) ); } float Vector2::dot(const Vector2& p_other) const { @@ -594,6 +594,10 @@ Matrix32 Matrix32::rotated(float p_phi) const { } +float Matrix32::basis_determinant() const { + + return elements[0].x * elements[1].y - elements[0].y * elements[1].x; +} Matrix32 Matrix32::interpolate_with(const Matrix32& p_transform, float p_c) const { diff --git a/core/math/math_2d.h b/core/math/math_2d.h index 2c8749f79d9..3cc5bdc843d 100644 --- a/core/math/math_2d.h +++ b/core/math/math_2d.h @@ -553,6 +553,9 @@ struct Matrix32 { void scale_basis(const Vector2& p_scale); void translate( real_t p_tx, real_t p_ty); void translate( const Vector2& p_translation ); + + float basis_determinant() const; + Vector2 get_scale() const; _FORCE_INLINE_ const Vector2& get_origin() const { return elements[2]; } diff --git a/platform/android/java/src/com/android/godot/Godot.java b/platform/android/java/src/com/android/godot/Godot.java index 11fb8945455..658c272321a 100644 --- a/platform/android/java/src/com/android/godot/Godot.java +++ b/platform/android/java/src/com/android/godot/Godot.java @@ -319,7 +319,7 @@ public class Godot extends Activity implements SensorEventListener //check for apk expansion API - if (true) { + if (!true) { command_line = getCommandLine(); boolean use_apk_expansion=false; String main_pack_md5=null; diff --git a/platform/android/java_glue.cpp b/platform/android/java_glue.cpp index 5c39cdbacc2..e67388b6f58 100644 --- a/platform/android/java_glue.cpp +++ b/platform/android/java_glue.cpp @@ -1573,13 +1573,18 @@ JNIEXPORT void JNICALL Java_com_android_godot_GodotLib_calldeferred(JNIEnv * env int count = env->GetArrayLength(params); Variant args[VARIANT_ARG_MAX]; + print_line("Java->GD call: "+obj->get_type()+"::"+str_method+" argc "+itos(count)); + for (int i=0; iGetObjectArrayElement(params, i); args[i] = _jobject_to_variant(env, obj); + print_line("\targ"+itos(i)+": "+Variant::get_type_name(args[i].get_type())); + }; + obj->call_deferred(str_method, args[0],args[1],args[2],args[3],args[4]); // something }; diff --git a/scene/2d/node_2d.cpp b/scene/2d/node_2d.cpp index 85adfbbbdeb..9c282125298 100644 --- a/scene/2d/node_2d.cpp +++ b/scene/2d/node_2d.cpp @@ -51,9 +51,9 @@ bool Node2D::edit_has_pivot() const { Variant Node2D::edit_get_state() const { Array state; - state.push_back(pos); - state.push_back(angle); - state.push_back(scale); + state.push_back(get_pos()); + state.push_back(get_rot()); + state.push_back(get_scale()); return state; diff --git a/scene/2d/path_2d.cpp b/scene/2d/path_2d.cpp index 22d56609eea..febec54124c 100644 --- a/scene/2d/path_2d.cpp +++ b/scene/2d/path_2d.cpp @@ -27,7 +27,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "path_2d.h" - +#include "scene/scene_string_names.h" void Path2D::_notification(int p_what) { @@ -90,3 +90,271 @@ Path2D::Path2D() { set_curve(Ref( memnew( Curve2D ))); //create one by default } + +///////////////////////////////////////////////////////////////////////////////// + + +void PathFollow2D::_update_transform() { + + + if (!path) + return; + + Ref c =path->get_curve(); + if (!c.is_valid()) + return; + + + float o = offset; + if (loop) + o=Math::fposmod(o,c->get_baked_length()); + + Vector2 pos = c->interpolate_baked(o,cubic); + + if (rotate) { + + Vector2 n = (c->interpolate_baked(o+lookahead,cubic)-pos).normalized(); + Vector2 t = -n.tangent(); + pos+=n*h_offset; + pos+=t*v_offset; + + set_rot(t.atan2()); + + } else { + + pos.x+=h_offset; + pos.y+=v_offset; + } + + set_pos(pos); + +} + +void PathFollow2D::_notification(int p_what) { + + + switch(p_what) { + + case NOTIFICATION_ENTER_SCENE: { + + Node *parent=get_parent(); + if (parent) { + + path=parent->cast_to(); + if (path) { + _update_transform(); + } + } + + } break; + case NOTIFICATION_EXIT_SCENE: { + + + path=NULL; + } break; + } + +} + +void PathFollow2D::set_cubic_interpolation(bool p_enable) { + + cubic=p_enable; +} + +bool PathFollow2D::get_cubic_interpolation() const { + + return cubic; +} + + +bool PathFollow2D::_set(const StringName& p_name, const Variant& p_value) { + + if (p_name==SceneStringNames::get_singleton()->offset) { + set_offset(p_value); + } else if (p_name==SceneStringNames::get_singleton()->unit_offset) { + set_unit_offset(p_value); + } else if (p_name==SceneStringNames::get_singleton()->rotate) { + set_rotate(p_value); + } else if (p_name==SceneStringNames::get_singleton()->v_offset) { + set_v_offset(p_value); + } else if (p_name==SceneStringNames::get_singleton()->h_offset) { + set_h_offset(p_value); + } else if (String(p_name)=="cubic_interp") { + set_cubic_interpolation(p_value); + } else if (String(p_name)=="loop") { + set_loop(p_value); + } else if (String(p_name)=="lookahead") { + set_lookahead(p_value); + } else + return false; + + return true; +} + +bool PathFollow2D::_get(const StringName& p_name,Variant &r_ret) const{ + + if (p_name==SceneStringNames::get_singleton()->offset) { + r_ret=get_offset(); + } else if (p_name==SceneStringNames::get_singleton()->unit_offset) { + r_ret=get_unit_offset(); + } else if (p_name==SceneStringNames::get_singleton()->rotate) { + r_ret=is_rotating(); + } else if (p_name==SceneStringNames::get_singleton()->v_offset) { + r_ret=get_v_offset(); + } else if (p_name==SceneStringNames::get_singleton()->h_offset) { + r_ret=get_h_offset(); + } else if (String(p_name)=="cubic_interp") { + r_ret=cubic; + } else if (String(p_name)=="loop") { + r_ret=loop; + } else if (String(p_name)=="lookahead") { + r_ret=lookahead; + } else + return false; + + return true; + +} +void PathFollow2D::_get_property_list( List *p_list) const{ + + float max=10000; + if (path && path->get_curve().is_valid()) + max=path->get_curve()->get_baked_length(); + p_list->push_back( PropertyInfo( Variant::REAL, "offset", PROPERTY_HINT_RANGE,"0,"+rtos(max)+",0.01")); + p_list->push_back( PropertyInfo( Variant::REAL, "unit_offset", PROPERTY_HINT_RANGE,"0,1,0.0001",PROPERTY_USAGE_EDITOR)); + p_list->push_back( PropertyInfo( Variant::REAL, "h_offset") ); + p_list->push_back( PropertyInfo( Variant::REAL, "v_offset") ); + p_list->push_back( PropertyInfo( Variant::BOOL, "rotate") ); + p_list->push_back( PropertyInfo( Variant::BOOL, "cubic_interp")); + p_list->push_back( PropertyInfo( Variant::BOOL, "loop")); + p_list->push_back( PropertyInfo( Variant::REAL, "lookahead",PROPERTY_HINT_RANGE,"0.001,1024.0,0.001")); +} + + +void PathFollow2D::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("set_offset","offset"),&PathFollow2D::set_offset); + ObjectTypeDB::bind_method(_MD("get_offset"),&PathFollow2D::get_offset); + + ObjectTypeDB::bind_method(_MD("set_h_offset","h_offset"),&PathFollow2D::set_h_offset); + ObjectTypeDB::bind_method(_MD("get_h_offset"),&PathFollow2D::get_h_offset); + + ObjectTypeDB::bind_method(_MD("set_v_offset","v_offset"),&PathFollow2D::set_v_offset); + ObjectTypeDB::bind_method(_MD("get_v_offset"),&PathFollow2D::get_v_offset); + + ObjectTypeDB::bind_method(_MD("set_unit_offset","unit_offset"),&PathFollow2D::set_unit_offset); + ObjectTypeDB::bind_method(_MD("get_unit_offset"),&PathFollow2D::get_unit_offset); + + ObjectTypeDB::bind_method(_MD("set_rotate","enable"),&PathFollow2D::set_rotate); + ObjectTypeDB::bind_method(_MD("is_rotating"),&PathFollow2D::is_rotating); + + ObjectTypeDB::bind_method(_MD("set_cubic_interpolation","enable"),&PathFollow2D::set_cubic_interpolation); + ObjectTypeDB::bind_method(_MD("get_cubic_interpolation"),&PathFollow2D::get_cubic_interpolation); + + ObjectTypeDB::bind_method(_MD("set_loop","loop"),&PathFollow2D::set_loop); + ObjectTypeDB::bind_method(_MD("has_loop"),&PathFollow2D::has_loop); + + +} + +void PathFollow2D::set_offset(float p_offset) { + + offset=p_offset; + if (path) + _update_transform(); + _change_notify("offset"); + _change_notify("unit_offset"); + +} + +void PathFollow2D::set_h_offset(float p_h_offset) { + + h_offset=p_h_offset; + if (path) + _update_transform(); + +} + +float PathFollow2D::get_h_offset() const { + + return h_offset; +} + +void PathFollow2D::set_v_offset(float p_v_offset) { + + v_offset=p_v_offset; + if (path) + _update_transform(); + +} + +float PathFollow2D::get_v_offset() const { + + return v_offset; +} + + +float PathFollow2D::get_offset() const{ + + return offset; +} + +void PathFollow2D::set_unit_offset(float p_unit_offset) { + + if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) + set_offset(p_unit_offset*path->get_curve()->get_baked_length()); + +} + +float PathFollow2D::get_unit_offset() const{ + + if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) + return get_offset()/path->get_curve()->get_baked_length(); + else + return 0; +} + +void PathFollow2D::set_lookahead(float p_lookahead) { + + lookahead=p_lookahead; + +} + +float PathFollow2D::get_lookahead() const{ + + return lookahead; +} + +void PathFollow2D::set_rotate(bool p_rotate) { + + rotate=p_rotate; + _update_transform(); +} + +bool PathFollow2D::is_rotating() const { + + return rotate; +} + +void PathFollow2D::set_loop(bool p_loop) { + + loop=p_loop; +} + +bool PathFollow2D::has_loop() const{ + + return loop; +} + + +PathFollow2D::PathFollow2D() { + + offset=0; + h_offset=0; + v_offset=0; + path=NULL; + rotate=true; + cubic=true; + loop=true; + lookahead=4; +} diff --git a/scene/2d/path_2d.h b/scene/2d/path_2d.h index f401f9da4c5..90f57c8eac8 100644 --- a/scene/2d/path_2d.h +++ b/scene/2d/path_2d.h @@ -54,4 +54,63 @@ public: Path2D(); }; + + +class PathFollow2D : public Node2D { + + OBJ_TYPE(PathFollow2D,Node2D); +public: + + +private: + Path2D *path; + real_t offset; + real_t h_offset; + real_t v_offset; + real_t lookahead; + bool cubic; + bool loop; + bool rotate; + + void _update_transform(); + + +protected: + + bool _set(const StringName& p_name, const Variant& p_value); + bool _get(const StringName& p_name,Variant &r_ret) const; + void _get_property_list( List *p_list) const; + + void _notification(int p_what); + static void _bind_methods(); +public: + + void set_offset(float p_offset); + float get_offset() const; + + void set_h_offset(float p_h_offset); + float get_h_offset() const; + + void set_v_offset(float p_v_offset); + float get_v_offset() const; + + void set_unit_offset(float p_unit_offset); + float get_unit_offset() const; + + void set_lookahead(float p_lookahead); + float get_lookahead() const; + + void set_loop(bool p_loop); + bool has_loop() const; + + void set_rotate(bool p_enabled); + bool is_rotating() const; + + void set_cubic_interpolation(bool p_enable); + bool get_cubic_interpolation() const; + + PathFollow2D(); +}; + + #endif // PATH_2D_H diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index 9a6454e4167..ff8660a3874 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -562,6 +562,7 @@ void register_scene_types() { ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); + ObjectTypeDB::register_type(); OS::get_singleton()->yield(); //may take time to init diff --git a/scene/resources/curve.cpp b/scene/resources/curve.cpp index f39bbf4c4eb..ae2c07ff560 100644 --- a/scene/resources/curve.cpp +++ b/scene/resources/curve.cpp @@ -44,6 +44,7 @@ static _FORCE_INLINE_ T _bezier_interp(real_t t, T start, T control_1, T control + end * t3; } +#if 0 int Curve2D::get_point_count() const { @@ -379,6 +380,457 @@ Curve2D::Curve2D() { } +#endif + + + + +int Curve2D::get_point_count() const { + + return points.size(); +} +void Curve2D::add_point(const Vector2& p_pos, const Vector2& p_in, const Vector2& p_out,int p_atpos) { + + Point n; + n.pos=p_pos; + n.in=p_in; + n.out=p_out; + if (p_atpos>=0 && p_atposchanged); +} + +void Curve2D::set_point_pos(int p_index, const Vector2& p_pos) { + + ERR_FAIL_INDEX(p_index,points.size()); + + points[p_index].pos=p_pos; + baked_cache_dirty=true; + emit_signal(CoreStringNames::get_singleton()->changed); + +} +Vector2 Curve2D::get_point_pos(int p_index) const { + + ERR_FAIL_INDEX_V(p_index,points.size(),Vector2()); + return points[p_index].pos; + +} + + +void Curve2D::set_point_in(int p_index, const Vector2& p_in) { + + ERR_FAIL_INDEX(p_index,points.size()); + + points[p_index].in=p_in; + baked_cache_dirty=true; + emit_signal(CoreStringNames::get_singleton()->changed); + +} +Vector2 Curve2D::get_point_in(int p_index) const { + + ERR_FAIL_INDEX_V(p_index,points.size(),Vector2()); + return points[p_index].in; + +} + +void Curve2D::set_point_out(int p_index, const Vector2& p_out) { + + ERR_FAIL_INDEX(p_index,points.size()); + + points[p_index].out=p_out; + baked_cache_dirty=true; + emit_signal(CoreStringNames::get_singleton()->changed); + +} + +Vector2 Curve2D::get_point_out(int p_index) const { + + ERR_FAIL_INDEX_V(p_index,points.size(),Vector2()); + return points[p_index].out; + +} + + +void Curve2D::remove_point(int p_index) { + + ERR_FAIL_INDEX(p_index,points.size()); + points.remove(p_index); + baked_cache_dirty=true; + emit_signal(CoreStringNames::get_singleton()->changed); +} + +Vector2 Curve2D::interpolate(int p_index, float p_offset) const { + + int pc = points.size(); + ERR_FAIL_COND_V(pc==0,Vector2()); + + if (p_index >= pc-1) + return points[pc-1].pos; + else if (p_index<0) + return points[0].pos; + + Vector2 p0 = points[p_index].pos; + Vector2 p1 = p0+points[p_index].out; + Vector2 p3 = points[p_index+1].pos; + Vector2 p2 = p3+points[p_index+1].in; + + return _bezier_interp(p_offset,p0,p1,p2,p3); +} + +Vector2 Curve2D::interpolatef(real_t p_findex) const { + + + if (p_findex>0) + p_findex=0; + else if (p_findex>=points.size()) + p_findex=points.size(); + + return interpolate((int)p_findex,Math::fmod(p_findex,1.0)); + +} + + +void Curve2D::_bake_segment2d(Map& r_bake, float p_begin, float p_end,const Vector2& p_a,const Vector2& p_out,const Vector2& p_b, const Vector2& p_in,int p_depth,int p_max_depth,float p_tol) const { + + float mp = p_begin+(p_end-p_begin)*0.5; + Vector2 beg = _bezier_interp(p_begin,p_a,p_a+p_out,p_b+p_in,p_b); + Vector2 mid = _bezier_interp(mp,p_a,p_a+p_out,p_b+p_in,p_b); + Vector2 end = _bezier_interp(p_end,p_a,p_a+p_out,p_b+p_in,p_b); + + Vector2 na = (mid-beg).normalized(); + Vector2 nb = (end-mid).normalized(); + float dp = na.dot(nb); + + if (dp pointlist; + + + for(int i=0;i1) + divs=1; + + float step = divs*0.1; // 10 substeps ought to be enough? + float p = 0; + + while(p<1.0) { + + float np=p+step; + if (np>1.0) + np=1.0; + + + Vector2 npp = _bezier_interp(np, points[i].pos,points[i].pos+points[i].out,points[i+1].pos+points[i+1].in,points[i+1].pos); + float d = pos.distance_to(npp); + + if (d>bake_interval) { + // OK! between P and NP there _has_ to be Something, let's go searching! + + int iterations = 10; //lots of detail! + + float low = p; + float hi = np; + float mid = low+(hi-low)*0.5; + + for(int j=0;j::Element *E=pointlist.front();E;E=E->next()) { + + w[idx]=E->get(); + idx++; + } + +} + +float Curve2D::get_baked_length() const { + + if (baked_cache_dirty) + _bake(); + + return baked_max_ofs; +} +Vector2 Curve2D::interpolate_baked(float p_offset,bool p_cubic) const{ + + if (baked_cache_dirty) + _bake(); + + //validate// + int pc = baked_point_cache.size(); + if (pc==0) { + ERR_EXPLAIN("No points in Curve2D"); + ERR_FAIL_COND_V(pc==0,Vector2()); + } + + if (pc==1) + return baked_point_cache.get(0); + + int bpc=baked_point_cache.size(); + Vector2Array::Read r = baked_point_cache.read(); + + if (p_offset<0) + return r[0]; + if (p_offset>=baked_max_ofs) + return r[bpc-1]; + + int idx = Math::floor(p_offset/bake_interval); + float frac = Math::fmod(p_offset,bake_interval); + + if (idx>=bpc-1) { + return r[bpc-1]; + } else if (idx==bpc-2) { + frac/=Math::fmod(baked_max_ofs,bake_interval); + } else { + frac/=bake_interval; + } + + if (p_cubic) { + + Vector2 pre = idx>0? r[idx-1] : r[idx]; + Vector2 post = (idx<(bpc-2))? r[idx+2] : r[idx+1]; + return r[idx].cubic_interpolate(r[idx+1],pre,post,frac); + } else { + return r[idx].linear_interpolate(r[idx+1],frac); + } +} + + +Vector2Array Curve2D::get_baked_points() const { + + if (baked_cache_dirty) + _bake(); + + return baked_point_cache; +} + + +void Curve2D::set_bake_interval(float p_tolerance){ + + bake_interval=p_tolerance; + baked_cache_dirty=true; + emit_signal(CoreStringNames::get_singleton()->changed); + +} + +float Curve2D::get_bake_interval() const{ + + return bake_interval; +} + +Dictionary Curve2D::_get_data() const { + + Dictionary dc; + + Vector2Array d; + d.resize(points.size()*3); + Vector2Array::Write w = d.write(); + + + for(int i=0;i > midpoints; + + midpoints.resize(points.size()-1); + + int pc=1; + for(int i=0;i::Element *E=midpoints[i].front();E;E=E->next()) { + + pidx++; + bpw[pidx] = E->get(); + } + + pidx++; + bpw[pidx] = points[i+1].pos; + + } + + bpw=Vector2Array::Write (); + + return tess; + +} + +void Curve2D::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("get_point_count"),&Curve2D::get_point_count); + ObjectTypeDB::bind_method(_MD("add_point","pos","in","out","atpos"),&Curve2D::add_point,DEFVAL(Vector2()),DEFVAL(Vector2()),DEFVAL(-1)); + ObjectTypeDB::bind_method(_MD("set_point_pos","idx","pos"),&Curve2D::set_point_pos); + ObjectTypeDB::bind_method(_MD("get_point_pos","idx"),&Curve2D::get_point_pos); + ObjectTypeDB::bind_method(_MD("set_point_in","idx","pos"),&Curve2D::set_point_in); + ObjectTypeDB::bind_method(_MD("get_point_in","idx"),&Curve2D::get_point_in); + ObjectTypeDB::bind_method(_MD("set_point_out","idx","pos"),&Curve2D::set_point_out); + ObjectTypeDB::bind_method(_MD("get_point_out","idx"),&Curve2D::get_point_out); + ObjectTypeDB::bind_method(_MD("remove_point","idx"),&Curve2D::remove_point); + ObjectTypeDB::bind_method(_MD("interpolate","idx","t"),&Curve2D::interpolate); + ObjectTypeDB::bind_method(_MD("interpolatef","fofs"),&Curve2D::interpolatef); + //ObjectTypeDB::bind_method(_MD("bake","subdivs"),&Curve2D::bake,DEFVAL(10)); + ObjectTypeDB::bind_method(_MD("set_bake_interval","distance"),&Curve2D::set_bake_interval); + ObjectTypeDB::bind_method(_MD("get_bake_interval"),&Curve2D::get_bake_interval); + + ObjectTypeDB::bind_method(_MD("get_baked_length"),&Curve2D::get_baked_length); + ObjectTypeDB::bind_method(_MD("interpolate_baked","offset","cubic"),&Curve2D::interpolate_baked,DEFVAL(false)); + ObjectTypeDB::bind_method(_MD("get_baked_points"),&Curve2D::get_baked_points); + + ObjectTypeDB::bind_method(_MD("_get_data"),&Curve2D::_get_data); + ObjectTypeDB::bind_method(_MD("_set_data"),&Curve2D::_set_data); + + + ADD_PROPERTY( PropertyInfo( Variant::REAL, "bake_interval",PROPERTY_HINT_RANGE,"0.01,512,0.01"), _SCS("set_bake_interval"),_SCS("get_bake_interval")); + ADD_PROPERTY( PropertyInfo( Variant::INT, "_data",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), _SCS("_set_data"),_SCS("_get_data")); + /*ADD_PROPERTY( PropertyInfo( Variant::VECTOR3_ARRAY, "points_out"), _SCS("set_points_out"),_SCS("get_points_out")); + ADD_PROPERTY( PropertyInfo( Variant::VECTOR3_ARRAY, "points_pos"), _SCS("set_points_pos"),_SCS("get_points_pos")); +*/ +} + + + + +Curve2D::Curve2D() +{ + baked_cache_dirty=false; + baked_max_ofs=0; +/* add_point(Vector2(-1,0,0)); + add_point(Vector2(0,2,0)); + add_point(Vector2(0,3,5));*/ + bake_interval=5; + +} + @@ -513,217 +965,6 @@ Vector3 Curve3D::interpolatef(real_t p_findex) const { } -#if 0 -DVector Curve3D::bake(int p_subdivs) const { - - int pc = points.size(); - - DVector ret; - if (pc<3) - return ret; - - ret.resize((pc-1)*p_subdivs+1); - - DVector::Write w = ret.write(); - const Point *r = points.ptr(); - - for(int i=0;i::Write(); - - return ret; -} - - - -void Curve3D::advance(real_t p_distance,int &r_index, real_t &r_pos) const { - - int pc = points.size(); - ERR_FAIL_COND(pc<3); - if (r_index<0 || r_index>=(pc-1)) - return; - - Vector3 pos = interpolate(r_index,r_pos); - - float sign=p_distance<0 ? -1 : 1; - p_distance=Math::abs(p_distance); - - real_t base = r_index+r_pos; - real_t top = 0.1; //a tenth is in theory representative - int iterations=33; - - - - for(int i=0;i0 && o >=pc) { - top=pc-base; - break; - } else if (sign<0 && o <0) { - top=-base; - break; - } - - Vector3 new_d = interpolatef(o); - - if (new_d.distance_to(pos) > p_distance) - break; - top*=3.0; - } - - - real_t bottom = 0.0; - iterations=8; - real_t final_offset; - - - for(int i=0;i p_distance) { - bottom=middle; - } else { - top=middle; - } - final_offset=o; - } - - r_index=(int)final_offset; - r_pos=Math::fmod(final_offset,1.0); - -} - - -void Curve3D::get_approx_position_from_offset(real_t p_offset,int &r_index, real_t &r_pos,int p_subdivs) const { - - ERR_FAIL_COND(points.size()<3); - - real_t accum=0; - - - - for(int i=0;ip_offset) { - - - r_index=j-1; - if (d>0) { - real_t mf = (p_offset-(accum-d)) / d; - r_pos=frac-(1.0-mf); - } else { - r_pos=frac; - } - - return; - } - - prev_p=p; - } - } - - r_index=points.size()-1; - r_pos=1.0; - - -} - - -void Curve3D::set_points_in(const Vector3Array& p_points) { - - points.resize(p_points.size()); - for (int i=0; i& r_bake, float p_begin, float p_end,const Vector3& p_a,const Vector3& p_out,const Vector3& p_b, const Vector3& p_in,int p_depth,int p_max_depth,float p_tol) const { diff --git a/scene/resources/curve.h b/scene/resources/curve.h index 046359b9b6c..e361604ce31 100644 --- a/scene/resources/curve.h +++ b/scene/resources/curve.h @@ -30,7 +30,7 @@ #define CURVE_H #include "resource.h" - +#if 0 class Curve2D : public Resource { OBJ_TYPE(Curve2D,Resource); @@ -79,6 +79,79 @@ public: Curve2D(); }; +#endif + + +class Curve2D : public Resource { + + OBJ_TYPE(Curve2D,Resource); + + struct Point { + + Vector2 in; + Vector2 out; + Vector2 pos; + }; + + + Vector points; + + struct BakedPoint { + + float ofs; + Vector2 point; + }; + + mutable bool baked_cache_dirty; + mutable Vector2Array baked_point_cache; + mutable float baked_max_ofs; + + + void _bake() const; + + float bake_interval; + + void _bake_segment2d(Map& r_bake, float p_begin, float p_end,const Vector2& p_a,const Vector2& p_out,const Vector2& p_b, const Vector2& p_in,int p_depth,int p_max_depth,float p_tol) const; + Dictionary _get_data() const; + void _set_data(const Dictionary &p_data); + +protected: + + static void _bind_methods(); + + + +public: + + + int get_point_count() const; + void add_point(const Vector2& p_pos, const Vector2& p_in=Vector2(), const Vector2& p_out=Vector2(),int p_atpos=-1); + void set_point_pos(int p_index, const Vector2& p_pos); + Vector2 get_point_pos(int p_index) const; + void set_point_in(int p_index, const Vector2& p_in); + Vector2 get_point_in(int p_index) const; + void set_point_out(int p_index, const Vector2& p_out); + Vector2 get_point_out(int p_index) const; + void remove_point(int p_index); + + Vector2 interpolate(int p_index, float p_offset) const; + Vector2 interpolatef(real_t p_findex) const; + + + void set_bake_interval(float p_distance); + float get_bake_interval() const; + + + float get_baked_length() const; + Vector2 interpolate_baked(float p_offset,bool p_cubic=false) const; + Vector2Array get_baked_points() const; //useful for going thru + + Vector2Array tesselate(int p_max_stages=5,float p_tolerance=4) const; //useful for display + + + Curve2D(); +}; + class Curve3D : public Resource { diff --git a/scene/scene_string_names.cpp b/scene/scene_string_names.cpp index 40792f51391..0576d5a5b1f 100644 --- a/scene/scene_string_names.cpp +++ b/scene/scene_string_names.cpp @@ -112,6 +112,7 @@ SceneStringNames::SceneStringNames() { offset=StaticCString::create("offset"); unit_offset=StaticCString::create("unit_offset"); rotation_mode=StaticCString::create("rotation_mode"); + rotate=StaticCString::create("rotate"); h_offset=StaticCString::create("h_offset"); v_offset=StaticCString::create("v_offset"); diff --git a/scene/scene_string_names.h b/scene/scene_string_names.h index 866c0ea387c..d4de0555edb 100644 --- a/scene/scene_string_names.h +++ b/scene/scene_string_names.h @@ -128,6 +128,7 @@ public: StringName offset; StringName unit_offset; StringName rotation_mode; + StringName rotate; StringName v_offset; StringName h_offset; diff --git a/tools/editor/editor_settings.cpp b/tools/editor/editor_settings.cpp index 139d2379890..0b896a725ef 100644 --- a/tools/editor/editor_settings.cpp +++ b/tools/editor/editor_settings.cpp @@ -419,6 +419,12 @@ void EditorSettings::_load_defaults() { set("3d_editor/zoom_modifier",4); hints["3d_editor/zoom_modifier"]=PropertyInfo(Variant::INT,"3d_editor/zoom_modifier",PROPERTY_HINT_ENUM,"None,Shift,Alt,Meta,Ctrl"); + set("2d_editor/bone_width",5); + set("2d_editor/bone_color1",Color(1.0,1.0,1.0,0.9)); + set("2d_editor/bone_color2",Color(0.75,0.75,0.75,0.9)); + set("2d_editor/bone_selected_color",Color(0.9,0.45,0.45,0.9)); + set("2d_editor/bone_ik_color",Color(0.9,0.9,0.45,0.9)); + set("on_save/compress_binary_resources",true); set("on_save/save_modified_external_resources",true); set("on_save/save_paths_as_relative",false); diff --git a/tools/editor/icons/icon_bone.png b/tools/editor/icons/icon_bone.png new file mode 100644 index 0000000000000000000000000000000000000000..174b0bc167ae9a651c3d519ad0366642aa9ae120 GIT binary patch literal 2992 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 literal 0 HcmV?d00001 diff --git a/tools/editor/icons/icon_unbone.png b/tools/editor/icons/icon_unbone.png new file mode 100644 index 0000000000000000000000000000000000000000..819e8a8e5d3e24e0cfc3e3ba3b7d7639d8ca57cb GIT binary patch literal 3000 zcmV;p3rF;cP)Oz@Z0f2-7z;ux~O9+4z06=<WDR*FRcSTFz- zW=q650N5=6FiBTtNC2?60Km==3$g$R3;-}uh=nNt1bYBr$Ri_o0EC$U6h`t_Jn<{8 z5a%iY0C<_QJh>z}MS)ugEpZ1|S1ukX&Pf+56gFW3VVXcL!g-k)GJ!M?;PcD?0HBc- z5#WRK{dmp}uFlRjj{U%*%WZ25jX z{P*?XzTzZ-GF^d31o+^>%=Ap99M6&ogks$0k4OBs3;+Bb(;~!4V!2o<6ys46agIcq zjPo+3B8fthDa9qy|77CdEc*jK-!%ZRYCZvbku9iQV*~a}ClFY4z~c7+0P?$U!PF=S z1Au6Q;m>#f??3%Vpd|o+W=WE9003S@Bra6Svp>fO002awfhw>;8}z{#EWidF!3EsG z3;bXU&9EIRU@z1_9W=mEXoiz;4lcq~xDGvV5BgyU zp1~-*fe8db$Osc*A=-!mVv1NJjtCc-h4>-CNCXm#Bp}I%6j35eku^v$Qi@a{RY)E3 zJ#qp$hg?Rwkvqr$GJ^buyhkyVfwECO)C{#lxu`c9ghrwZ&}4KmnvWKso6vH!8a<3Q zq36)6Xb;+tK10Vaz~~qUGsJ8#F2=(`u{bOVlVi)VBCHIn#u~6ztOL7=^<&SmcLWlF zMZgI*1b0FpVIDz9SWH+>*hr`#93(Um+6gxa1B6k+CnA%mOSC4s5&6UzVlpv@SV$}* z))J2sFA#f(L&P^E5{W}HC%KRUNwK6<(h|}}(r!{C=`5+6G)NjFlgZj-YqAG9lq?`C z$c5yc>d>VnA`E_*3F2Qp##d8RZb=H01_mm@+|Cqnc9PsG(F5HIG_C zt)aG3uTh7n6Et<2In9F>NlT@zqLtGcXcuVrX|L#Xx)I%#9!{6gSJKPrN9dR61N3(c z4Tcqi$B1Vr8Jidf7-t!G7_XR2rWwr)$3XQ?}=hpK0&Z&W{| zep&sA23f;Q!%st`QJ}G3cbou<7-yIK2z4nfCCCtN2-XOGSWo##{8Q{ATurxr~;I`ytDs%xbip}RzP zziy}Qn4Z2~fSycmr`~zJ=lUFdFa1>gZThG6M+{g7vkW8#+YHVaJjFF}Z#*3@$J_By zLtVo_L#1JrVVB{Ak-5=4qt!-@Mh}c>#$4kh<88)m#-k<%CLtzEP3leVno>={htGUuD;o7bD)w_sX$S}eAxwzy?UvgBH(S?;#HZiQMoS*2K2 zT3xe7t(~nU*1N5{rxB;QPLocnp4Ml>u<^FZwyC!nu;thW+pe~4wtZn|Vi#w(#jeBd zlf9FDx_yoPJqHbk*$%56S{;6Kv~mM9!g3B(KJ}#RZ#@)!hR|78Dq|Iq-afF%KE1Brn_fm;Im z_u$xr8UFki1L{Ox>G0o)(&RAZ;=|I=wN2l97;cLaHH6leTB-XXa*h%dBOEvi`+x zi?=Txl?TadvyiL>SuF~-LZ;|cS}4~l2eM~nS7yJ>iOM;atDY;(?aZ^v+mJV$@1Ote z62cPUlD4IWOIIx&SmwQ~YB{nzae3Pc;}r!fhE@iwJh+OsDs9zItL;~pu715HdQEGA zUct(O!LkCy1<%NCg+}G`0PgpNm-?d@-hMgNe6^V+j6x$b<6@S<$+<4_1hi}Ti zncS4LsjI}fWY1>OX6feMEuLErma3QLmkw?X+1j)X-&VBk_4Y;EFPF_I+q;9dL%E~B zJh;4Nr^(LEJ3myURP{Rblsw%57T)g973R8o)DE9*xN#~;4_o$q%o z4K@u`jhx2fBXC4{U8Qn{*%*B$Ge=nny$HAYq{=vy|sI0 z_vss+H_qMky?OB#|JK!>IX&II^LlUh#rO5!7TtbwC;iULyV-Xq?ybB}ykGP{?LpZ? z-G|jbTmIbG@7#ZCz;~eY(cDM(28Dyq{*m>M4?_iynUBkc4TkHUI6gT!;y-fz>HMcd z&t%Ugo)`Y2{>!cx7B7DI)$7;J(U{Spm-3gBzioV_{p!H$8L!*M!p0uH$#^p{Ui4P` z?ZJ24cOCDe-w#jZd?0@)|7iKK^;6KN`;!@ylm7$*nDhK&GcDTy000JJOGiWi{{a60 z|De66lK=n!32;bRa{vGf6951U69E94oEQKA00(qQO+^RZ2Luf>F8Rl26aWAK>PbXF zR5;7k(mhH9K@bJtZ}taGOw1;bi6-*^rrtmn@dl<|!PrO?Oax(1VCq2(2ZMCrab$Whhhq#*H0kYznJv5=&g8sp|5r7=WMYuIf_T ubfaAuj4`!6$JdCEFVoZ>B;H@#Ewdkj$yIlP-=^9C0000 literal 0 HcmV?d00001 diff --git a/tools/editor/plugins/canvas_item_editor_plugin.cpp b/tools/editor/plugins/canvas_item_editor_plugin.cpp index 6540ae9288a..10028a2f211 100644 --- a/tools/editor/plugins/canvas_item_editor_plugin.cpp +++ b/tools/editor/plugins/canvas_item_editor_plugin.cpp @@ -35,7 +35,7 @@ #include "scene/2d/node_2d.h" #include "globals.h" #include "os/input.h" - +#include "tools/editor/editor_settings.h" void CanvasItemEditor::_unhandled_key_input(const InputEvent& p_ev) { if (!is_visible()) @@ -180,9 +180,9 @@ void CanvasItemEditor::_node_removed(Node *p_node) { void CanvasItemEditor::_keying_changed(bool p_changed) { if (p_changed) - animation_menu->show(); + animation_hb->show(); else - animation_menu->hide(); + animation_hb->hide(); } // slow but modern computers should have no problem @@ -639,30 +639,48 @@ void CanvasItemEditor::_viewport_input_event(const InputEvent& p_event) { if (b.button_index==BUTTON_RIGHT) { + + if (get_item_count() > 0 && drag!=DRAG_NONE) { //cancel drag + if (bone_ik_list.size()) { - List &selection = editor_selection->get_selected_node_list(); + for(List::Element *E=bone_ik_list.back();E;E=E->prev()) { - for(List::Element *E=selection.front();E;E=E->next()) { + E->get().node->edit_set_state(E->get().orig_state); + } - CanvasItem *canvas_item = E->get()->cast_to(); - if (!canvas_item) - continue; - if (!canvas_item->is_visible()) - continue; + bone_ik_list.clear(); - CanvasItemEditorSelectedItem *se=editor_selection->get_node_editor_data(canvas_item); - if (!se) - continue; + } else { - canvas_item->edit_set_state(se->undo_state); - if (canvas_item->cast_to()) - canvas_item->cast_to()->edit_set_pivot(se->undo_pivot); + List &selection = editor_selection->get_selected_node_list(); + + for(List::Element *E=selection.front();E;E=E->next()) { + + CanvasItem *canvas_item = E->get()->cast_to(); + if (!canvas_item) + continue; + if (!canvas_item->is_visible()) + continue; + + CanvasItemEditorSelectedItem *se=editor_selection->get_node_editor_data(canvas_item); + if (!se) + continue; + + canvas_item->edit_set_state(se->undo_state); + if (canvas_item->cast_to()) + canvas_item->cast_to()->edit_set_pivot(se->undo_pivot); + + } } + drag=DRAG_NONE; + viewport->update(); + can_move_pivot=false; + } else if (box_selecting) { box_selecting=false; viewport->update(); @@ -689,34 +707,55 @@ void CanvasItemEditor::_viewport_input_event(const InputEvent& p_event) { if (undo_redo) { - undo_redo->create_action("Edit CanvasItem"); + + if (bone_ik_list.size()) { - List &selection = editor_selection->get_selected_node_list(); + undo_redo->create_action("Edit IK Chain"); - for(List::Element *E=selection.front();E;E=E->next()) { + for(List::Element *E=bone_ik_list.back();E;E=E->prev()) { - CanvasItem *canvas_item = E->get()->cast_to(); - if (!canvas_item) - continue; - if (!canvas_item->is_visible()) - continue; - CanvasItemEditorSelectedItem *se=editor_selection->get_node_editor_data(canvas_item); - if (!se) - continue; + undo_redo->add_do_method(E->get().node,"edit_set_state",E->get().node->edit_get_state()); + undo_redo->add_undo_method(E->get().node,"edit_set_state",E->get().orig_state); + } - Variant state=canvas_item->edit_get_state(); - undo_redo->add_do_method(canvas_item,"edit_set_state",state); - undo_redo->add_undo_method(canvas_item,"edit_set_state",se->undo_state); - if (canvas_item->cast_to()) { - Node2D *pvt = canvas_item->cast_to(); - if (pvt->edit_has_pivot()) { - undo_redo->add_do_method(canvas_item,"edit_set_pivot",pvt->edit_get_pivot()); - undo_redo->add_undo_method(canvas_item,"edit_set_pivot",se->undo_pivot); + undo_redo->add_do_method(viewport,"update"); + undo_redo->add_undo_method(viewport,"update"); + + bone_ik_list.clear(); + + undo_redo->commit_action(); + } else { + + undo_redo->create_action("Edit CanvasItem"); + + + List &selection = editor_selection->get_selected_node_list(); + + for(List::Element *E=selection.front();E;E=E->next()) { + + CanvasItem *canvas_item = E->get()->cast_to(); + if (!canvas_item) + continue; + if (!canvas_item->is_visible()) + continue; + CanvasItemEditorSelectedItem *se=editor_selection->get_node_editor_data(canvas_item); + if (!se) + continue; + + Variant state=canvas_item->edit_get_state(); + undo_redo->add_do_method(canvas_item,"edit_set_state",state); + undo_redo->add_undo_method(canvas_item,"edit_set_state",se->undo_state); + if (canvas_item->cast_to()) { + Node2D *pvt = canvas_item->cast_to(); + if (pvt->edit_has_pivot()) { + undo_redo->add_do_method(canvas_item,"edit_set_pivot",pvt->edit_get_pivot()); + undo_redo->add_undo_method(canvas_item,"edit_set_pivot",se->undo_pivot); + } } } + undo_redo->commit_action(); } - undo_redo->commit_action(); } drag=DRAG_NONE; @@ -759,6 +798,86 @@ void CanvasItemEditor::_viewport_input_event(const InputEvent& p_event) { return; } + + List::Element *Cbone=NULL; //closest + + { + bone_ik_list.clear(); + float closest_dist=1e20; + int bone_width = EditorSettings::get_singleton()->get("2d_editor/bone_width"); + for(List::Element *E=bone_list.front();E;E=E->next()) { + + if (E->get().from == E->get().to) + continue; + Vector2 s[2]={ + E->get().from, + E->get().to + }; + + Vector2 p = Geometry::get_closest_point_to_segment_2d(Vector2(b.x,b.y),s); + float d = p.distance_to(Vector2(b.x,b.y)); + if (dget().bone); + if (obj) + b=obj->cast_to(); + + if (b) { + + + bool ik_found=false; + bool first=true; + + + + while(b) { + + CanvasItem *pi=b->get_parent_item(); + if (!pi) + break; + + float len=pi->get_global_transform().get_origin().distance_to(b->get_global_pos()); + b=pi->cast_to(); + if (!b) + break; + + if (first) { + + bone_orig_xform=b->get_global_transform(); + first=false; + } + + BoneIK bik; + bik.node=b; + bik.len=len; + bik.orig_state=b->edit_get_state(); + + bone_ik_list.push_back(bik); + + if (b->has_meta("_edit_ik_")) { + + ik_found=bone_ik_list.size()>1; + break; + } + + if (!pi->has_meta("_edit_bone_")) + break; + + } + + if (!ik_found) + bone_ik_list.clear(); + + } + } + } + CanvasItem *single_item = get_single_item(); if (single_item) { @@ -797,7 +916,7 @@ void CanvasItemEditor::_viewport_input_event(const InputEvent& p_event) { } } - if (drag!=DRAG_NONE) { + if (drag!=DRAG_NONE && (!Cbone || drag!=DRAG_ALL)) { drag_from=transform.affine_inverse().xform(click); se->undo_state=canvas_item->edit_get_state(); if (canvas_item->cast_to()) @@ -856,16 +975,30 @@ void CanvasItemEditor::_viewport_input_event(const InputEvent& p_event) { //no window.... ? click-=current_window->get_scroll(); }*/ - CanvasItem *c=_select_canvas_item_at_pos(click, scene,transform,Matrix32()); + CanvasItem *c=NULL; + + if (Cbone) { + + Object* obj=ObjectDB::get_instance(Cbone->get().bone); + if (obj) + c=obj->cast_to(); + if (c) + c=c->get_parent_item(); - CanvasItem* cn = c; + } + if (!c) { + c =_select_canvas_item_at_pos(click, scene,transform,Matrix32()); - while(cn) { - if (cn->has_meta("_edit_group_")) { - c=cn; + + CanvasItem* cn = c; + + while(cn) { + if (cn->has_meta("_edit_group_")) { + c=cn; + } + cn=cn->get_parent_item(); } - cn=cn->get_parent_item(); } Node* n = c; @@ -989,6 +1122,7 @@ void CanvasItemEditor::_viewport_input_event(const InputEvent& p_event) { List &selection = editor_selection->get_selected_node_list(); + for(List::Element *E=selection.front();E;E=E->next()) { CanvasItem *canvas_item = E->get()->cast_to(); @@ -1000,9 +1134,14 @@ void CanvasItemEditor::_viewport_input_event(const InputEvent& p_event) { if (!se) continue; - canvas_item->edit_set_state(se->undo_state); //reset state and reapply - if (canvas_item->cast_to()) - canvas_item->cast_to()->edit_set_pivot(se->undo_pivot); + bool dragging_bone = drag==DRAG_ALL && selection.size()==1 && bone_ik_list.size(); + + + if (!dragging_bone) { + canvas_item->edit_set_state(se->undo_state); //reset state and reapply + if (canvas_item->cast_to()) + canvas_item->cast_to()->edit_set_pivot(se->undo_pivot); + } Vector2 dfrom = drag_from; @@ -1153,10 +1292,145 @@ void CanvasItemEditor::_viewport_input_event(const InputEvent& p_event) { } - local_rect.pos=begin; - local_rect.size=end-begin; - canvas_item->edit_set_rect(local_rect); + + + if (!dragging_bone) { + + local_rect.pos=begin; + local_rect.size=end-begin; + canvas_item->edit_set_rect(local_rect); + + } else { + //ok, all that had to be done was done, now solve IK + + + + + Node2D *n2d = canvas_item->cast_to(); + Matrix32 final_xform = bone_orig_xform; + + + + if (n2d) { + + float total_len = 0; + for (List::Element *E=bone_ik_list.front();E;E=E->next()) { + if (E->prev()) + total_len+=E->get().len; + E->get().pos = E->get().node->get_global_transform().get_origin(); + } + + { + + final_xform.elements[2]+=dto-dfrom;//final_xform.affine_inverse().basis_xform_inv(drag_vector); + //n2d->set_global_transform(final_xform); + + } + + + CanvasItem *last = bone_ik_list.back()->get().node; + if (!last) + break; + + Vector2 root_pos = last->get_global_transform().get_origin(); + Vector2 leaf_pos = final_xform.get_origin(); + + if ((leaf_pos.distance_to(root_pos)) > total_len) { + //oops dude you went too far + //print_line("TOO FAR!"); + Vector2 rel = leaf_pos - root_pos; + rel = rel.normalized() * total_len; + leaf_pos=root_pos+rel; + + } + + bone_ik_list.front()->get().pos=leaf_pos; + + //print_line("BONE IK LIST "+itos(bone_ik_list.size())); + + + if (bone_ik_list.size()>2) { + int solver_iterations=64; + float solver_k=0.3; + + for(int i=0;i::Element *E=bone_ik_list.front();E;E=E->next()) { + + + + if (E==bone_ik_list.back()) { + + break; + } + + float len = E->next()->get().len; + + if (E->next()==bone_ik_list.back()) { + + //print_line("back"); + + Vector2 rel = E->get().pos - E->next()->get().pos; + //print_line("PREV "+E->get().pos); + Vector2 desired = E->next()->get().pos+rel.normalized()*len; + //print_line("DESIRED "+desired); + E->get().pos=E->get().pos.linear_interpolate(desired,solver_k); + //print_line("POST "+E->get().pos); + + + } else if (E==bone_ik_list.front()) { + //only adjust parent + //print_line("front"); + Vector2 rel = E->next()->get().pos - E->get().pos; + //print_line("PREV "+E->next()->get().pos); + Vector2 desired = E->get().pos+rel.normalized()*len; + //print_line("DESIRED "+desired); + E->next()->get().pos=E->next()->get().pos.linear_interpolate(desired,solver_k); + //print_line("POST "+E->next()->get().pos); + } else { + + Vector2 rel = E->next()->get().pos - E->get().pos; + Vector2 cen = (E->next()->get().pos + E->get().pos)*0.5; + rel=rel.linear_interpolate(rel.normalized()*len,solver_k); + rel*=0.5; + E->next()->get().pos=cen+rel; + E->get().pos=cen-rel; + //print_line("mid"); + + } + } + } + } + } + + for (List::Element *E=bone_ik_list.back();E;E=E->prev()) { + + Node2D *n = E->get().node; + + if (!E->prev()) { + //last goes to what it was + final_xform.set_origin(n->get_global_pos()); + n->set_global_transform(final_xform); + + } else { + Vector2 rel = (E->prev()->get().node->get_global_pos() - n->get_global_pos()).normalized(); + Vector2 rel2 = (E->prev()->get().pos - E->get().pos).normalized(); + float rot = rel.angle_to(rel2); + if (n->get_global_transform().basis_determinant()<0) { + //mirrored, rotate the other way + rot=-rot; + } + + n->rotate(rot); + } + + } + + + + break; + } } } @@ -1393,7 +1667,77 @@ void CanvasItemEditor::_viewport_draw() { } + int bone_width = EditorSettings::get_singleton()->get("2d_editor/bone_width"); + Color bone_color1 = EditorSettings::get_singleton()->get("2d_editor/bone_color1"); + Color bone_color2 = EditorSettings::get_singleton()->get("2d_editor/bone_color2"); + Color bone_ik_color = EditorSettings::get_singleton()->get("2d_editor/bone_ik_color"); + Color bone_selected_color = EditorSettings::get_singleton()->get("2d_editor/bone_selected_color"); + for(List::Element*E=bone_list.front();E;E=E->next()) { + + E->get().from=Vector2(); + E->get().to=Vector2(); + + Object *obj = ObjectDB::get_instance(E->get().bone); + if (!obj) + continue; + + Node2D* n2d = obj->cast_to(); + if (!n2d) + continue; + + if (!n2d->get_parent()) + continue; + + CanvasItem *pi = n2d->get_parent_item(); + + + Node2D* pn2d=n2d->get_parent()->cast_to(); + + if (!pn2d) + continue; + + Vector2 from = transform.xform(pn2d->get_global_pos()); + Vector2 to = transform.xform(n2d->get_global_pos()); + + E->get().from=from; + E->get().to=to; + + Vector2 rel = to-from; + Vector2 relt = rel.tangent().normalized()*bone_width; + + + + Vector bone_shape; + bone_shape.push_back(from); + bone_shape.push_back(from+rel*0.2+relt); + bone_shape.push_back(to); + bone_shape.push_back(from+rel*0.2-relt); + Vector colors; + if (pi->has_meta("_edit_ik_")) { + + colors.push_back(bone_ik_color); + colors.push_back(bone_ik_color); + colors.push_back(bone_ik_color); + colors.push_back(bone_ik_color); + } else { + colors.push_back(bone_color1); + colors.push_back(bone_color2); + colors.push_back(bone_color1); + colors.push_back(bone_color2); + } + + + VisualServer::get_singleton()->canvas_item_add_primitive(ci,bone_shape,colors,Vector(),RID()); + + if (editor_selection->is_selected(pi)) { + for(int i=0;icanvas_item_add_line(ci,bone_shape[i],bone_shape[(i+1)%bone_shape.size()],bone_selected_color,2); + } + } + + } } void CanvasItemEditor::_notification(int p_what) { @@ -1445,6 +1789,7 @@ void CanvasItemEditor::_notification(int p_what) { unlock_button->set_icon(get_icon("Unlock","EditorIcons")); group_button->set_icon(get_icon("Group","EditorIcons")); ungroup_button->set_icon(get_icon("Ungroup","EditorIcons")); + key_insert_button->set_icon(get_icon("Key","EditorIcons")); } @@ -1509,6 +1854,13 @@ void CanvasItemEditor::_find_canvas_items_span(Node *p_node, Rect2& r_rect, cons lock_list.push_back(lock); } + if (c->has_meta("_edit_bone_")) { + + BoneList bone; + bone.bone=c->get_instance_ID(); + bone_list.push_back(bone); + } + r_rect.expand_to( xform.xform(rect.pos) ); r_rect.expand_to( xform.xform(rect.pos+Point2(rect.size.x,0)) ); r_rect.expand_to( xform.xform(rect.pos+Point2(0,rect.size.y)) ); @@ -1541,6 +1893,7 @@ void CanvasItemEditor::_update_scrollbars() { Rect2 canvas_item_rect=Rect2(Point2(),screen_rect); lock_list.clear();; + bone_list.clear();; if (editor->get_edited_scene()) _find_canvas_items_span(editor->get_edited_scene(),canvas_item_rect,Matrix32()); @@ -1878,6 +2231,44 @@ void CanvasItemEditor::_popup_callback(int p_op) { editor->get_animation_editor()->insert_node_value_key(n2d,"transform/rot",Math::rad2deg(n2d->get_rot()),existing); if (key_scale) editor->get_animation_editor()->insert_node_value_key(n2d,"transform/scale",n2d->get_scale(),existing); + + + if (n2d->has_meta("_edit_bone_") && n2d->get_parent_item()) { + //look for an IK chain + List ik_chain; + + Node2D *n = n2d->get_parent_item()->cast_to(); + bool has_chain=false; + + while(n) { + + ik_chain.push_back(n); + if (n->has_meta("_edit_ik_")) { + has_chain=true; + break; + } + + if (!n->get_parent_item()) + break; + n=n->get_parent_item()->cast_to(); + } + + if (has_chain && ik_chain.size()) { + + for(List::Element *F=ik_chain.front();F;F=F->next()) { + + if (key_pos) + editor->get_animation_editor()->insert_node_value_key(F->get(),"transform/pos",F->get()->get_pos(),existing); + if (key_rot) + editor->get_animation_editor()->insert_node_value_key(F->get(),"transform/rot",Math::rad2deg(F->get()->get_rot()),existing); + if (key_scale) + editor->get_animation_editor()->insert_node_value_key(F->get(),"transform/scale",F->get()->get_scale(),existing); + + + } + } + } + } else if (canvas_item->cast_to()) { Control *ctrl = canvas_item->cast_to(); @@ -1891,10 +2282,20 @@ void CanvasItemEditor::_popup_callback(int p_op) { } } break; - case ANIM_INSERT_POS: - case ANIM_INSERT_ROT: - case ANIM_INSERT_SCALE: - case ANIM_INSERT_POS_ROT: + case ANIM_INSERT_POS: { + + key_pos = key_loc_button->is_pressed(); + } break; + case ANIM_INSERT_ROT: { + + key_pos = key_rot_button->is_pressed(); + } break; + case ANIM_INSERT_SCALE: { + + key_scale = key_scale_button->is_pressed(); + } break; + /* + case ANIM_INSERT_POS_ROT case ANIM_INSERT_POS_SCALE: case ANIM_INSERT_ROT_SCALE: case ANIM_INSERT_POS_ROT_SCALE: { @@ -1917,7 +2318,7 @@ void CanvasItemEditor::_popup_callback(int p_op) { animation_menu->get_popup()->set_item_checked(idx,i==p_op); } - } break; + } break;*/ case ANIM_COPY_POSE: { pose_clipboard.clear();; @@ -2061,6 +2462,85 @@ void CanvasItemEditor::_popup_callback(int p_op) { } } break; + case SKELETON_MAKE_BONES: { + + + + Map &selection = editor_selection->get_selection(); + + for(Map::Element *E=selection.front();E;E=E->next()) { + + Node2D *n2d = E->key()->cast_to(); + if (!n2d) + continue; + if (!n2d->is_visible()) + continue; + if (!n2d->get_parent_item()) + continue; + + n2d->set_meta("_edit_bone_",true); + + } + viewport->update(); + + } break; + case SKELETON_CLEAR_BONES: { + + Map &selection = editor_selection->get_selection(); + + for(Map::Element *E=selection.front();E;E=E->next()) { + + Node2D *n2d = E->key()->cast_to(); + if (!n2d) + continue; + if (!n2d->is_visible()) + continue; + + n2d->set_meta("_edit_bone_",Variant()); + + } + viewport->update(); + + } break; + case SKELETON_SET_IK_CHAIN: { + + List &selection = editor_selection->get_selected_node_list(); + + for(List::Element *E=selection.front();E;E=E->next()) { + + CanvasItem *canvas_item = E->get()->cast_to(); + if (!canvas_item) + continue; + if (!canvas_item->is_visible()) + continue; + + + canvas_item->set_meta("_edit_ik_",true); + + } + + viewport->update(); + + } break; + case SKELETON_CLEAR_IK_CHAIN: { + + Map &selection = editor_selection->get_selection(); + + for(Map::Element *E=selection.front();E;E=E->next()) { + + CanvasItem *n2d = E->key()->cast_to(); + if (!n2d) + continue; + if (!n2d->is_visible()) + continue; + + n2d->set_meta("_edit_ik_",Variant()); + + } + viewport->update(); + + } break; + } } #if 0 @@ -2296,6 +2776,18 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { p->add_check_item("Use Pixel Snap",SNAP_USE_PIXEL); p->add_separator(); p->add_item("Expand to Parent",EXPAND_TO_PARENT,KEY_MASK_CMD|KEY_P); + p->add_separator(); + p->add_submenu_item("Skeleton..","skeleton"); + PopupMenu *p2 = memnew(PopupMenu); + p->add_child(p2); + p2->set_name("skeleton"); + p2->add_item("Make Bones",SKELETON_MAKE_BONES,KEY_MASK_CMD|KEY_SHIFT|KEY_B); + p2->add_item("Clear Bones",SKELETON_CLEAR_BONES); + p2->add_separator(); + p2->add_item("Make IK Chain",SKELETON_SET_IK_CHAIN); + p2->add_item("Clear IK Chain",SKELETON_CLEAR_IK_CHAIN); + p2->connect("item_pressed", this,"_popup_callback"); + /* p->add_item("Align Horizontal",ALIGN_HORIZONTAL); @@ -2314,12 +2806,50 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { p->add_item("Zoom Out",ZOOM_OUT); p->add_item("Zoom Reset",ZOOM_RESET); p->add_item("Zoom Set..",ZOOM_SET); + p->add_separator(); p->add_item("Center Selection", VIEW_CENTER_TO_SELECTION, KEY_F); p->add_item("Frame Selection", VIEW_FRAME_TO_SELECTION, KEY_MASK_CMD|KEY_F); + + + animation_hb = memnew( HBoxContainer ); + hb->add_child(animation_hb); + animation_hb->add_child( memnew( VSeparator )); + animation_hb->hide(); + + key_loc_button = memnew( Button("loc")); + key_loc_button->set_toggle_mode(true); + key_loc_button->set_pressed(true); + key_loc_button->set_focus_mode(FOCUS_NONE); + key_loc_button->add_color_override("font_color",Color(1,0.6,0.6)); + key_loc_button->add_color_override("font_color_pressed",Color(0.6,1,0.6)); + key_loc_button->connect("pressed",this,"_popup_callback",varray(ANIM_INSERT_POS)); + animation_hb->add_child(key_loc_button); + key_rot_button = memnew( Button("rot")); + key_rot_button->set_toggle_mode(true); + key_rot_button->set_pressed(true); + key_rot_button->set_focus_mode(FOCUS_NONE); + key_rot_button->add_color_override("font_color",Color(1,0.6,0.6)); + key_rot_button->add_color_override("font_color_pressed",Color(0.6,1,0.6)); + key_rot_button->connect("pressed",this,"_popup_callback",varray(ANIM_INSERT_ROT)); + animation_hb->add_child(key_rot_button); + key_scale_button = memnew( Button("scl")); + key_scale_button->set_toggle_mode(true); + key_scale_button->set_focus_mode(FOCUS_NONE); + key_scale_button->add_color_override("font_color",Color(1,0.6,0.6)); + key_scale_button->add_color_override("font_color_pressed",Color(0.6,1,0.6)); + key_scale_button->connect("pressed",this,"_popup_callback",varray(ANIM_INSERT_SCALE)); + animation_hb->add_child(key_scale_button); + key_insert_button = memnew( Button ); + key_insert_button->set_focus_mode(FOCUS_NONE); + key_insert_button->connect("pressed",this,"_popup_callback",varray(ANIM_INSERT_KEY)); + key_insert_button->set_tooltip("Insert Keys (Insert)"); + + animation_hb->add_child(key_insert_button); + animation_menu = memnew( MenuButton ); animation_menu->set_text("Animation"); - hb->add_child(animation_menu); + animation_hb->add_child(animation_menu); animation_menu->get_popup()->connect("item_pressed", this,"_popup_callback"); p = animation_menu->get_popup(); @@ -2327,22 +2857,10 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) { p->add_item("Insert Key",ANIM_INSERT_KEY,KEY_INSERT); p->add_item("Insert Key (Existing Tracks)",ANIM_INSERT_KEY_EXISTING,KEY_MASK_CMD+KEY_INSERT); p->add_separator(); - p->add_check_item("Pos",ANIM_INSERT_POS); - p->add_check_item("Rot",ANIM_INSERT_ROT); - p->add_check_item("Scale",ANIM_INSERT_SCALE); - p->add_check_item("Pos+Rot",ANIM_INSERT_POS_ROT); - p->set_item_checked(p->get_item_index(ANIM_INSERT_POS_ROT),true); - p->add_check_item("Pos+Scale",ANIM_INSERT_POS_SCALE); - p->add_check_item("Rot+Scale",ANIM_INSERT_ROT_SCALE); - p->add_check_item("Loc+Rot+Scale",ANIM_INSERT_POS_ROT_SCALE); - p->add_separator(); p->add_item("Copy Pose",ANIM_COPY_POSE); p->add_item("Paste Pose",ANIM_PASTE_POSE); p->add_item("Clear Pose",ANIM_CLEAR_POSE,KEY_MASK_ALT|KEY_K); - animation_menu->hide(); - - value_dialog = memnew( AcceptDialog ); value_dialog->set_title("Set a Value"); value_dialog->get_ok()->set_text("Close"); diff --git a/tools/editor/plugins/canvas_item_editor_plugin.h b/tools/editor/plugins/canvas_item_editor_plugin.h index 3d9b50c01c7..425e691f078 100644 --- a/tools/editor/plugins/canvas_item_editor_plugin.h +++ b/tools/editor/plugins/canvas_item_editor_plugin.h @@ -95,15 +95,15 @@ class CanvasItemEditor : public VBoxContainer { ANIM_INSERT_POS, ANIM_INSERT_ROT, ANIM_INSERT_SCALE, - ANIM_INSERT_POS_ROT, - ANIM_INSERT_POS_SCALE, - ANIM_INSERT_ROT_SCALE, - ANIM_INSERT_POS_ROT_SCALE, ANIM_COPY_POSE, ANIM_PASTE_POSE, ANIM_CLEAR_POSE, VIEW_CENTER_TO_SELECTION, VIEW_FRAME_TO_SELECTION, + SKELETON_MAKE_BONES, + SKELETON_CLEAR_BONES, + SKELETON_SET_IK_CHAIN, + SKELETON_CLEAR_IK_CHAIN }; @@ -165,6 +165,26 @@ class CanvasItemEditor : public VBoxContainer { List lock_list; + struct BoneList { + + Vector2 from; + Vector2 to; + ObjectID bone; + }; + + List bone_list; + Matrix32 bone_orig_xform; + + struct BoneIK { + + Variant orig_state; + Vector2 pos; + float len; + Node2D *node; + }; + + List bone_ik_list; + struct PoseClipboard { Vector2 pos; @@ -189,7 +209,14 @@ class CanvasItemEditor : public VBoxContainer { MenuButton *edit_menu; MenuButton *view_menu; + HBoxContainer *animation_hb; MenuButton *animation_menu; + + Button *key_loc_button; + Button *key_rot_button; + Button *key_scale_button; + Button *key_insert_button; + //PopupMenu *popup; DragType drag; Point2 drag_from; diff --git a/tools/editor/plugins/path_2d_editor_plugin.cpp b/tools/editor/plugins/path_2d_editor_plugin.cpp index 5e4cd981271..33ea5f35881 100644 --- a/tools/editor/plugins/path_2d_editor_plugin.cpp +++ b/tools/editor/plugins/path_2d_editor_plugin.cpp @@ -31,7 +31,7 @@ #include "canvas_item_editor_plugin.h" #include "os/file_access.h" #include "tools/editor/editor_settings.h" - +#include "os/keyboard.h" void Path2DEditor::_notification(int p_what) { switch(p_what) { @@ -103,7 +103,7 @@ bool Path2DEditor::forward_input_event(const InputEvent& p_event) { // Test move point!! - if ( mb.pressed && mb.button_index==BUTTON_LEFT ) { + if ( mb.pressed && action==ACTION_NONE ) { Ref curve = node->get_curve(); @@ -115,21 +115,30 @@ bool Path2DEditor::forward_input_event(const InputEvent& p_event) { Point2 p = xform.xform( curve->get_point_pos(i) ); if (gpoint.distance_to(p) < grab_treshold ) { - if (!mb.mod.shift) { + if (mb.button_index==BUTTON_LEFT && !mb.mod.shift && mode==MODE_EDIT) { action=ACTION_MOVING_POINT; action_point=i; moving_from=curve->get_point_pos(i); moving_screen_from=gpoint; return true; + } else if ((mb.button_index==BUTTON_RIGHT && mode==MODE_EDIT) || (mb.button_index==BUTTON_LEFT && mode==MODE_DELETE)) { + + undo_redo->create_action("Remove Point from Curve"); + undo_redo->add_do_method(curve.ptr(),"remove_point",i); + undo_redo->add_undo_method(curve.ptr(),"add_point",curve->get_point_pos(i),curve->get_point_in(i),curve->get_point_out(i),i); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->commit_action(); + return true; } else pointunder=true; } } - if (i<(curve->get_point_count()-1)) { + if (mb.button_index==BUTTON_LEFT && i<(curve->get_point_count()-1)) { Point2 p = xform.xform( curve->get_point_pos(i)+curve->get_point_out(i) ); - if (gpoint.distance_to(p) < grab_treshold ) { + if (gpoint.distance_to(p) < grab_treshold && (mode == MODE_EDIT || mode==MODE_EDIT_CURVE) ) { action=ACTION_MOVING_OUT; action_point=i; @@ -139,9 +148,9 @@ bool Path2DEditor::forward_input_event(const InputEvent& p_event) { } } - if (i>0) { + if (mb.button_index==BUTTON_LEFT && i>0) { Point2 p = xform.xform( curve->get_point_pos(i)+curve->get_point_in(i) ); - if (gpoint.distance_to(p) < grab_treshold ) { + if (gpoint.distance_to(p) < grab_treshold && (mode == MODE_EDIT || mode==MODE_EDIT_CURVE)) { action=ACTION_MOVING_IN; action_point=i; @@ -160,15 +169,15 @@ bool Path2DEditor::forward_input_event(const InputEvent& p_event) { // Test add point in empty space! - if ( mb.pressed && mb.mod.control && mb.button_index==BUTTON_LEFT ) { + if ( mb.pressed && mb.button_index==BUTTON_LEFT && ((mb.mod.command && mode == MODE_EDIT) || mode == MODE_CREATE)) { Ref curve = node->get_curve(); undo_redo->create_action("Add Point to Curve"); undo_redo->add_do_method(curve.ptr(),"add_point",cpoint); undo_redo->add_undo_method(curve.ptr(),"remove_point",curve->get_point_count()); - undo_redo->add_do_method(canvas_item_editor,"update"); - undo_redo->add_undo_method(canvas_item_editor,"update"); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); action=ACTION_MOVING_POINT; @@ -195,8 +204,8 @@ bool Path2DEditor::forward_input_event(const InputEvent& p_event) { undo_redo->create_action("Move Point in Curve"); undo_redo->add_do_method(curve.ptr(),"set_point_pos",action_point,cpoint); undo_redo->add_undo_method(curve.ptr(),"set_point_pos",action_point,moving_from); - undo_redo->add_do_method(canvas_item_editor,"update"); - undo_redo->add_undo_method(canvas_item_editor,"update"); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); } break; @@ -205,8 +214,8 @@ bool Path2DEditor::forward_input_event(const InputEvent& p_event) { undo_redo->create_action("Move In-Control in Curve"); undo_redo->add_do_method(curve.ptr(),"set_point_in",action_point,new_pos); undo_redo->add_undo_method(curve.ptr(),"set_point_in",action_point,moving_from); - undo_redo->add_do_method(canvas_item_editor,"update"); - undo_redo->add_undo_method(canvas_item_editor,"update"); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); } break; @@ -215,8 +224,8 @@ bool Path2DEditor::forward_input_event(const InputEvent& p_event) { undo_redo->create_action("Move Out-Control in Curve"); undo_redo->add_do_method(curve.ptr(),"set_point_out",action_point,new_pos); undo_redo->add_undo_method(curve.ptr(),"set_point_out",action_point,moving_from); - undo_redo->add_do_method(canvas_item_editor,"update"); - undo_redo->add_undo_method(canvas_item_editor,"update"); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); } break; @@ -286,8 +295,8 @@ bool Path2DEditor::forward_input_event(const InputEvent& p_event) { undo_redo->add_undo_method(node,"set_polygon",poly); poly.push_back(cpoint); undo_redo->add_do_method(node,"set_polygon",poly); - undo_redo->add_do_method(canvas_item_editor,"update"); - undo_redo->add_undo_method(canvas_item_editor,"update"); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); return true; } @@ -365,8 +374,8 @@ bool Path2DEditor::forward_input_event(const InputEvent& p_event) { undo_redo->create_action("Edit Poly"); undo_redo->add_do_method(node,"set_polygon",poly); undo_redo->add_undo_method(node,"set_polygon",pre_move_edit); - undo_redo->add_do_method(canvas_item_editor,"update"); - undo_redo->add_undo_method(canvas_item_editor,"update"); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); edited_point=-1; @@ -400,8 +409,8 @@ bool Path2DEditor::forward_input_event(const InputEvent& p_event) { undo_redo->add_undo_method(node,"set_polygon",poly); poly.remove(closest_idx); undo_redo->add_do_method(node,"set_polygon",poly); - undo_redo->add_do_method(canvas_item_editor,"update"); - undo_redo->add_undo_method(canvas_item_editor,"update"); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); return true; } @@ -554,6 +563,60 @@ void Path2DEditor::_bind_methods() { //ObjectTypeDB::bind_method(_MD("_menu_option"),&Path2DEditor::_menu_option); ObjectTypeDB::bind_method(_MD("_canvas_draw"),&Path2DEditor::_canvas_draw); ObjectTypeDB::bind_method(_MD("_node_visibility_changed"),&Path2DEditor::_node_visibility_changed); + ObjectTypeDB::bind_method(_MD("_mode_selected"),&Path2DEditor::_mode_selected); +} + +void Path2DEditor::_mode_selected(int p_mode) { + + if (p_mode==MODE_CREATE) { + + curve_create->set_pressed(true); + curve_edit->set_pressed(false); + curve_edit_curve->set_pressed(false); + curve_del->set_pressed(false); + } else if (p_mode==MODE_EDIT) { + + curve_create->set_pressed(false); + curve_edit->set_pressed(true); + curve_edit_curve->set_pressed(false); + curve_del->set_pressed(false); + } else if (p_mode==MODE_EDIT_CURVE) { + + curve_create->set_pressed(false); + curve_edit->set_pressed(false); + curve_edit_curve->set_pressed(true); + curve_del->set_pressed(false); + } else if (p_mode==MODE_DELETE) { + + curve_create->set_pressed(false); + curve_edit->set_pressed(false); + curve_edit_curve->set_pressed(false); + curve_del->set_pressed(true); + } else if (p_mode==ACTION_CLOSE) { + + //? + + if (!node->get_curve().is_valid()) + return ; + if (node->get_curve()->get_point_count()<3) + return; + + Vector2 begin = node->get_curve()->get_point_pos(0); + Vector2 end = node->get_curve()->get_point_pos( node->get_curve()->get_point_count() -1 ); + if (begin.distance_to(end)create_action("Remove Point from Curve"); + undo_redo->add_do_method(node->get_curve().ptr(),"add_point",begin); + undo_redo->add_undo_method(node->get_curve().ptr(),"remove_point",node->get_curve()->get_point_count()); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->commit_action(); + return; + } + + mode=Mode(p_mode); + } Path2DEditor::Path2DEditor(EditorNode *p_editor) { @@ -573,6 +636,50 @@ Path2DEditor::Path2DEditor(EditorNode *p_editor) { options->get_popup()->connect("item_pressed", this,"_menu_option"); #endif + base_hb = memnew( HBoxContainer ); + CanvasItemEditor::get_singleton()->add_control_to_menu_panel(base_hb); + + sep = memnew( VSeparator); + base_hb->add_child(sep); + curve_edit = memnew( ToolButton ); + curve_edit->set_icon(CanvasItemEditor::get_singleton()->get_icon("CurveEdit","EditorIcons")); + curve_edit->set_toggle_mode(true); + curve_edit->set_focus_mode(Control::FOCUS_NONE); + curve_edit->set_tooltip("Select Points\nShift+Drag: Select Control Points\n"+keycode_get_string(KEY_MASK_CMD)+"Click: Add Point\nRight Click: Delete Point."); + curve_edit->connect("pressed",this,"_mode_selected",varray(MODE_EDIT)); + base_hb->add_child(curve_edit); + curve_edit_curve = memnew( ToolButton ); + curve_edit_curve->set_icon(CanvasItemEditor::get_singleton()->get_icon("CurveCurve","EditorIcons")); + curve_edit_curve->set_toggle_mode(true); + curve_edit_curve->set_focus_mode(Control::FOCUS_NONE); + curve_edit_curve->set_tooltip("Select Control Points (Shift+Drag)"); + curve_edit_curve->connect("pressed",this,"_mode_selected",varray(MODE_EDIT_CURVE)); + base_hb->add_child(curve_edit_curve); + curve_create = memnew( ToolButton ); + curve_create->set_icon(CanvasItemEditor::get_singleton()->get_icon("CurveCreate","EditorIcons")); + curve_create->set_toggle_mode(true); + curve_create->set_focus_mode(Control::FOCUS_NONE); + curve_create->set_tooltip("Add Point (in empty space)\nSplit Segment (in curve)."); + curve_create->connect("pressed",this,"_mode_selected",varray(MODE_CREATE)); + base_hb->add_child(curve_create); + curve_del = memnew( ToolButton ); + curve_del->set_icon(CanvasItemEditor::get_singleton()->get_icon("CurveDelete","EditorIcons")); + curve_del->set_toggle_mode(true); + curve_del->set_focus_mode(Control::FOCUS_NONE); + curve_del->set_tooltip("Delete Point."); + curve_del->connect("pressed",this,"_mode_selected",varray(MODE_DELETE)); + base_hb->add_child(curve_del); + curve_close = memnew( ToolButton ); + curve_close->set_icon(CanvasItemEditor::get_singleton()->get_icon("CurveClose","EditorIcons")); + curve_close->set_focus_mode(Control::FOCUS_NONE); + curve_close->set_tooltip("Close Curve"); + curve_close->connect("pressed",this,"_mode_selected",varray(ACTION_CLOSE)); + base_hb->add_child(curve_close); + base_hb->hide(); + + + + curve_edit->set_pressed(true); } @@ -592,9 +699,12 @@ void Path2DEditorPlugin::make_visible(bool p_visible) { if (p_visible) { path2d_editor->show(); + path2d_editor->base_hb->show(); + } else { path2d_editor->hide(); + path2d_editor->base_hb->hide(); path2d_editor->edit(NULL); } @@ -605,8 +715,9 @@ Path2DEditorPlugin::Path2DEditorPlugin(EditorNode *p_node) { editor=p_node; path2d_editor = memnew( Path2DEditor(p_node) ); CanvasItemEditor::get_singleton()->add_control_to_menu_panel(path2d_editor); - path2d_editor->hide(); + + } diff --git a/tools/editor/plugins/path_2d_editor_plugin.h b/tools/editor/plugins/path_2d_editor_plugin.h index 1ddda3f65fe..73de2cc838c 100644 --- a/tools/editor/plugins/path_2d_editor_plugin.h +++ b/tools/editor/plugins/path_2d_editor_plugin.h @@ -51,6 +51,24 @@ class Path2DEditor : public HBoxContainer { Panel *panel; Path2D *node; + HBoxContainer *base_hb; + Separator *sep; + + enum Mode { + MODE_CREATE, + MODE_EDIT, + MODE_EDIT_CURVE, + MODE_DELETE, + ACTION_CLOSE + }; + + Mode mode; + ToolButton *curve_create; + ToolButton *curve_edit; + ToolButton *curve_edit_curve; + ToolButton *curve_del; + ToolButton *curve_close; + enum Action { ACTION_NONE, @@ -65,10 +83,11 @@ class Path2DEditor : public HBoxContainer { Point2 moving_from; Point2 moving_screen_from; + void _mode_selected(int p_mode); void _canvas_draw(); void _node_visibility_changed(); - +friend class Path2DEditorPlugin; protected: void _notification(int p_what); void _node_removed(Node *p_node); diff --git a/tools/editor/plugins/path_editor_plugin.cpp b/tools/editor/plugins/path_editor_plugin.cpp index 61b1df9ca84..7b0ff971d21 100644 --- a/tools/editor/plugins/path_editor_plugin.cpp +++ b/tools/editor/plugins/path_editor_plugin.cpp @@ -564,12 +564,12 @@ PathEditorPlugin::PathEditorPlugin(EditorNode *p_node) { curve_del->set_focus_mode(Control::FOCUS_NONE); curve_del->set_tooltip("Delete Point."); SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_del); - curve_close = memnew( ToolButton ); + curve_close = memnew( ToolButton ); curve_close->set_icon(SpatialEditor::get_singleton()->get_icon("CurveClose","EditorIcons")); curve_close->hide(); - curve_close->set_focus_mode(Control::FOCUS_NONE); - curve_close->set_tooltip("Close Curve"); - SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_close); + curve_close->set_focus_mode(Control::FOCUS_NONE); + curve_close->set_tooltip("Close Curve"); + SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_close); diff --git a/tools/editor/plugins/path_editor_plugin.h b/tools/editor/plugins/path_editor_plugin.h index b938358d37d..d730d33551f 100644 --- a/tools/editor/plugins/path_editor_plugin.h +++ b/tools/editor/plugins/path_editor_plugin.h @@ -26,74 +26,74 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifndef PATH_EDITOR_PLUGIN_H -#define PATH_EDITOR_PLUGIN_H - - -#include "tools/editor/spatial_editor_gizmos.h" -#include "scene/3d/path.h" -class PathSpatialGizmo : public SpatialGizmoTool { - - OBJ_TYPE(PathSpatialGizmo,SpatialGizmoTool); - - Path* path; - mutable Vector3 original; - -public: - - virtual String get_handle_name(int p_idx) const; - virtual Variant get_handle_value(int p_idx) const; - virtual void set_handle(int p_idx,Camera *p_camera, const Point2& p_point); - virtual void commit_handle(int p_idx,const Variant& p_restore,bool p_cancel=false); - - void redraw(); - PathSpatialGizmo(Path* p_path=NULL); - -}; - -class PathEditorPlugin : public EditorPlugin { - - OBJ_TYPE( PathEditorPlugin, EditorPlugin ); - - - Separator *sep; - ToolButton *curve_create; - ToolButton *curve_edit; - ToolButton *curve_del; - ToolButton *curve_close; - - EditorNode *editor; - - - Path *path; - - void _mode_changed(int p_idx); - void _close_curve(); -protected: - void _notification(int p_what); - static void _bind_methods(); - -public: - - Path *get_edited_path() { return path; } - - static PathEditorPlugin* singleton; - Ref path_material; - Ref path_thin_material; - virtual bool forward_spatial_input_event(Camera* p_camera,const InputEvent& p_event); - -// virtual bool forward_input_event(const InputEvent& p_event) { return collision_polygon_editor->forward_input_event(p_event); } - virtual bool create_spatial_gizmo(Spatial* p_spatial); - virtual String get_name() const { return "Path"; } - bool has_main_screen() const { return false; } - virtual void edit(Object *p_node); - virtual bool handles(Object *p_node) const; - virtual void make_visible(bool p_visible); - - PathEditorPlugin(EditorNode *p_node); - ~PathEditorPlugin(); - -}; - - -#endif // PATH_EDITOR_PLUGIN_H +#ifndef PATH_EDITOR_PLUGIN_H +#define PATH_EDITOR_PLUGIN_H + + +#include "tools/editor/spatial_editor_gizmos.h" +#include "scene/3d/path.h" +class PathSpatialGizmo : public SpatialGizmoTool { + + OBJ_TYPE(PathSpatialGizmo,SpatialGizmoTool); + + Path* path; + mutable Vector3 original; + +public: + + virtual String get_handle_name(int p_idx) const; + virtual Variant get_handle_value(int p_idx) const; + virtual void set_handle(int p_idx,Camera *p_camera, const Point2& p_point); + virtual void commit_handle(int p_idx,const Variant& p_restore,bool p_cancel=false); + + void redraw(); + PathSpatialGizmo(Path* p_path=NULL); + +}; + +class PathEditorPlugin : public EditorPlugin { + + OBJ_TYPE( PathEditorPlugin, EditorPlugin ); + + + Separator *sep; + ToolButton *curve_create; + ToolButton *curve_edit; + ToolButton *curve_del; + ToolButton *curve_close; + + EditorNode *editor; + + + Path *path; + + void _mode_changed(int p_idx); + void _close_curve(); +protected: + void _notification(int p_what); + static void _bind_methods(); + +public: + + Path *get_edited_path() { return path; } + + static PathEditorPlugin* singleton; + Ref path_material; + Ref path_thin_material; + virtual bool forward_spatial_input_event(Camera* p_camera,const InputEvent& p_event); + +// virtual bool forward_input_event(const InputEvent& p_event) { return collision_polygon_editor->forward_input_event(p_event); } + virtual bool create_spatial_gizmo(Spatial* p_spatial); + virtual String get_name() const { return "Path"; } + bool has_main_screen() const { return false; } + virtual void edit(Object *p_node); + virtual bool handles(Object *p_node) const; + virtual void make_visible(bool p_visible); + + PathEditorPlugin(EditorNode *p_node); + ~PathEditorPlugin(); + +}; + + +#endif // PATH_EDITOR_PLUGIN_H