From bf80827bfdad603fbee11db93910b00b7eb73c5a Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 1 Feb 2021 13:50:56 +0100 Subject: [PATCH] Add more Spline API for curve editing --- Source/Engine/Level/Actors/Spline.cpp | 24 ++++++++++++++++ Source/Engine/Level/Actors/Spline.h | 41 +++++++++++++++++++++------ Source/Engine/Level/Spline.cs | 22 ++++++++++++++ 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/Source/Engine/Level/Actors/Spline.cpp b/Source/Engine/Level/Actors/Spline.cpp index a3757253f..f522a8770 100644 --- a/Source/Engine/Level/Actors/Spline.cpp +++ b/Source/Engine/Level/Actors/Spline.cpp @@ -244,6 +244,14 @@ void Spline::ClearSpline() UpdateSpline(); } +void Spline::RemoveSplinePoint(int32 index, bool updateSpline) +{ + CHECK(index >= 0 && index < GetSplinePointsCount()); + Curve.GetKeyframes().RemoveAtKeepOrder(index); + if (updateSpline) + UpdateSpline(); +} + void Spline::SetSplinePoint(int32 index, const Vector3& point, bool updateSpline) { CHECK(index >= 0 && index < GetSplinePointsCount()); @@ -331,6 +339,22 @@ void Spline::AddSplineLocalPoint(const Transform& point, bool updateSpline) UpdateSpline(); } +void Spline::InsertSplinePoint(int32 index, float time, const Transform& point, bool updateSpline) +{ + const Keyframe k(time, _transform.WorldToLocal(point)); + Curve.GetKeyframes().Insert(index, k); + if (updateSpline) + UpdateSpline(); +} + +void Spline::InsertSplineLocalPoint(int32 index, float time, const Transform& point, bool updateSpline) +{ + const Keyframe k(time, point); + Curve.GetKeyframes().Insert(index, k); + if (updateSpline) + UpdateSpline(); +} + void Spline::SetTangentsLinear() { const int32 count = Curve.GetKeyframes().Count(); diff --git a/Source/Engine/Level/Actors/Spline.h b/Source/Engine/Level/Actors/Spline.h index 772aac821..7f8076eac 100644 --- a/Source/Engine/Level/Actors/Spline.h +++ b/Source/Engine/Level/Actors/Spline.h @@ -216,12 +216,19 @@ public: /// API_FUNCTION() void ClearSpline(); + /// + /// Removes the spline curve point at the given index. + /// + /// The curve keyframe index. Zero-based, smaller than GetSplinePointsCount(). + /// True if update spline after removing the point, otherwise false. + API_FUNCTION() void RemoveSplinePoint(int32 index, bool updateSpline = true); + /// /// Sets the spline curve at the given index (world-space). /// /// The curve keyframe index. Zero-based, smaller than GetSplinePointsCount(). /// The location of the point to set (world-space). - /// True if update spline after adding the point, otherwise false. + /// True if update spline after editing the point, otherwise false. API_FUNCTION() void SetSplinePoint(int32 index, const Vector3& point, bool updateSpline = true); /// @@ -229,7 +236,7 @@ public: /// /// The curve keyframe index. Zero-based, smaller than GetSplinePointsCount(). /// The location of the point to set (local-space). - /// True if update spline after adding the point, otherwise false. + /// True if update spline after editing the point, otherwise false. API_FUNCTION() void SetSplineLocalPoint(int32 index, const Vector3& point, bool updateSpline = true); /// @@ -237,7 +244,7 @@ public: /// /// The curve keyframe index. Zero-based, smaller than GetSplinePointsCount(). /// The transformation of the point to set (world-space). - /// True if update spline after adding the point, otherwise false. + /// True if update spline after editing the point, otherwise false. API_FUNCTION() void SetSplineTransform(int32 index, const Transform& point, bool updateSpline = true); /// @@ -245,7 +252,7 @@ public: /// /// The curve keyframe index. Zero-based, smaller than GetSplinePointsCount(). /// The transformation of the point to set (local-space). - /// True if update spline after adding the point, otherwise false. + /// True if update spline after editing the point, otherwise false. API_FUNCTION() void SetSplineLocalTransform(int32 index, const Transform& point, bool updateSpline = true); /// @@ -255,7 +262,7 @@ public: /// The curve keyframe index. Zero-based, smaller than GetSplinePointsCount(). /// The transformation of the tangent to set (world-space). /// True if set arrive tangent, otherwise sets leave tangent (in or out). - /// True if update spline after adding the point, otherwise false. + /// True if update spline after editing the point, otherwise false. API_FUNCTION() void SetSplineTangent(int32 index, const Transform& point, bool isIn, bool updateSpline = true); /// @@ -265,7 +272,7 @@ public: /// The curve keyframe index. Zero-based, smaller than GetSplinePointsCount(). /// The transformation of the tangent to set (local-space). /// True if set arrive tangent, otherwise sets leave tangent (in or out). - /// True if update spline after adding the point, otherwise false. + /// True if update spline after editing the point, otherwise false. API_FUNCTION() void SetSplineLocalTangent(int32 index, const Transform& point, bool isIn, bool updateSpline = true); /// @@ -273,14 +280,14 @@ public: /// /// The curve keyframe index. Zero-based, smaller than GetSplinePointsCount(). /// The time to set. - /// True if update spline after adding the point, otherwise false. + /// True if update spline after editing the point, otherwise false. API_FUNCTION() void SetSplinePointTime(int32 index, float time, bool updateSpline = true); /// /// Adds the point to the spline curve (at the end). /// /// The location of the point to add to the curve (world-space). - /// True if update spline after adding the point, otherwise false. + /// True if update spline after editing the point, otherwise false. API_FUNCTION() void AddSplinePoint(const Vector3& point, bool updateSpline = true); /// @@ -304,6 +311,24 @@ public: /// True if update spline after adding the point, otherwise false. API_FUNCTION() void AddSplineLocalPoint(const Transform& point, bool updateSpline = true); + /// + /// Inserts the spline curve point at the given index (world-space). + /// + /// The curve keyframe index. Zero-based, smaller than GetSplinePointsCount(). + /// The time value. + /// The location of the point to add to the curve (world-space). + /// True if update spline after removing the point, otherwise false. + API_FUNCTION() void InsertSplinePoint(int32 index, float time, const Transform& point, bool updateSpline = true); + + /// + /// Inserts the spline curve point at the given index (local-space). + /// + /// The curve keyframe index. Zero-based, smaller than GetSplinePointsCount(). + /// The time value. + /// The location of the point to add to the curve (local-space). + /// True if update spline after removing the point, otherwise false. + API_FUNCTION() void InsertSplineLocalPoint(int32 index, float time, const Transform& point, bool updateSpline = true); + /// /// Updates the curve tangent points to make curve linear. /// diff --git a/Source/Engine/Level/Spline.cs b/Source/Engine/Level/Spline.cs index 46a05cd5d..29c65763a 100644 --- a/Source/Engine/Level/Spline.cs +++ b/Source/Engine/Level/Spline.cs @@ -37,5 +37,27 @@ namespace FlaxEngine Internal_SetKeyframes(__unmanagedPtr, value); } } + + /// + /// Gets the spline keyframe. + /// + /// The spline point index. + /// The keyframe. + public BezierCurve.Keyframe GetSplineKeyframe(int index) + { + return SplineKeyframes[index]; + } + + /// + /// Sets the spline keyframe. + /// + /// The spline point index. + /// The keyframe. + public void SetSplineKeyframe(int index, BezierCurve.Keyframe keyframe) + { + SetSplineLocalTransform(index, keyframe.Value, false); + SetSplineLocalTangent(index, keyframe.Value + keyframe.TangentIn, true, false); + SetSplineLocalTangent(index, keyframe.Value + keyframe.TangentOut, false); + } } }