Skip to content

Commit

Permalink
Prettier SplineRoll inspector - requires minor API change
Browse files Browse the repository at this point in the history
  • Loading branch information
glabute committed May 15, 2024
1 parent 118b07f commit bce00bd
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static void DrawSplineGizmo(CinemachineSplineRoll splineRoll, Color pathColor, f
if (SplineGizmoCache.Instance == null
|| SplineGizmoCache.Instance.Mesh == null
|| SplineGizmoCache.Instance.Spline != spline.Spline
|| SplineGizmoCache.Instance.SplineData != splineRoll.Roll
|| SplineGizmoCache.Instance.RollData != splineRoll.Roll
|| SplineGizmoCache.Instance.Width != width
|| SplineGizmoCache.Instance.Resolution != resolution)
{
Expand Down Expand Up @@ -103,7 +103,7 @@ static void DrawSplineGizmo(CinemachineSplineRoll splineRoll, Color pathColor, f
{
Mesh = mesh,
Spline = spline.Spline,
SplineData = splineRoll.Roll,
RollData = splineRoll.Roll,
Width = width,
Resolution = resolution
};
Expand All @@ -122,7 +122,7 @@ static void DrawSplineGizmo(CinemachineSplineRoll splineRoll, Color pathColor, f
class SplineGizmoCache
{
public Mesh Mesh;
public SplineData<float> SplineData;
public SplineData<CinemachineSplineRoll.RollData> RollData;
public Spline Spline;
public float Width;
public int Resolution;
Expand All @@ -135,22 +135,36 @@ static SplineGizmoCache()
Instance = null;
EditorSplineUtility.AfterSplineWasModified -= OnSplineChanged;
EditorSplineUtility.AfterSplineWasModified += OnSplineChanged;
EditorSplineUtility.UnregisterSplineDataChanged<float>(OnSplineDataChanged);
EditorSplineUtility.RegisterSplineDataChanged<float>(OnSplineDataChanged);
EditorSplineUtility.UnregisterSplineDataChanged<CinemachineSplineRoll.RollData>(OnSplineDataChanged);
EditorSplineUtility.RegisterSplineDataChanged<CinemachineSplineRoll.RollData>(OnSplineDataChanged);
}
static void OnSplineChanged(Spline spline)
{
if (Instance != null && spline == Instance.Spline)
Instance = null;
}
static void OnSplineDataChanged(SplineData<float> data)
static void OnSplineDataChanged(SplineData<CinemachineSplineRoll.RollData> data)
{
if (Instance != null && data == Instance.SplineData)
if (Instance != null && data == Instance.RollData)
Instance = null;
}
}
}

[CustomPropertyDrawer(typeof(DataPoint<CinemachineSplineRoll.RollData>))]
class CinemachineLookAtDataOnSplineItemPropertyDrawer : PropertyDrawer
{
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
var ux = new VisualElement { style = { flexDirection = FlexDirection.Row }};
ux.Add(new InspectorUtility.CompactPropertyField(property.FindPropertyRelative("m_Index")));

var def = new CinemachineSplineRoll.RollData();
var valueProp = property.FindPropertyRelative("m_Value");
ux.Add(new InspectorUtility.CompactPropertyField(valueProp.FindPropertyRelative(() => def.Value)) { style = { marginLeft = 3 }});
return ux;
}
}

[EditorTool("Spline Roll Tool", typeof(CinemachineSplineRoll))]
sealed class SplineRollTool : EditorTool, IDrawSelectedHandles
Expand Down Expand Up @@ -190,7 +204,7 @@ public override void OnToolGUI(EditorWindow window)
if (!GetTargets(out var splineData, out var spline))
return;

Undo.RecordObject(splineData, "Modifying Roll SplineData");
Undo.RecordObject(splineData, "Modifying Roll RollData");
var color = Handles.selectedColor;
if (!Active)
color.a = k_UnselectedAlpha;
Expand Down Expand Up @@ -219,7 +233,7 @@ public void OnDrawHandles()
// inverse pre-calculation optimization
readonly Quaternion m_DefaultHandleOrientation = Quaternion.Euler(270, 0, 0);
readonly Quaternion m_DefaultHandleOrientationInverse = Quaternion.Euler(90, 0, 0);
bool DrawRollDataPoints(NativeSpline spline, SplineData<float> splineData, bool enabled)
bool DrawRollDataPoints(NativeSpline spline, SplineData<CinemachineSplineRoll.RollData> splineData, bool enabled)
{
var inUse = false;
for (var r = 0; r < splineData.Count; ++r)
Expand Down
6 changes: 3 additions & 3 deletions com.unity.cinemachine/Editor/Upgrader/UpgradeObjectToCm3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ static void UpgradePath(CinemachinePathBase pathBase)

spline = Undo.AddComponent<SplineContainer>(go);
var splineRoll = Undo.AddComponent<CinemachineSplineRoll>(go);
splineRoll.Roll = new SplineData<float>();
splineRoll.Roll = new SplineData<CinemachineSplineRoll.RollData>();

Undo.RecordObject(pathBase, "Upgrader: disable obsolete");
pathBase.enabled = false;
Expand All @@ -721,7 +721,7 @@ static void UpgradePath(CinemachinePathBase pathBase)
TangentIn = -waypoints[i].tangent,
TangentOut = waypoints[i].tangent,
});
splineRoll.Roll.Add(new DataPoint<float>(i, waypoints[i].roll));
splineRoll.Roll.Add(new DataPoint<CinemachineSplineRoll.RollData>(i, waypoints[i].roll));
}
break;
}
Expand All @@ -740,7 +740,7 @@ static void UpgradePath(CinemachinePathBase pathBase)
TangentIn = -tangent,
TangentOut = tangent,
});
splineRoll.Roll.Add(new DataPoint<float>(i, waypoints[i].roll));
splineRoll.Roll.Add(new DataPoint<CinemachineSplineRoll.RollData>(i, waypoints[i].roll));
}
break;
}
Expand Down
3 changes: 2 additions & 1 deletion com.unity.cinemachine/Editor/Utility/InspectorUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,11 @@ public CompactPropertyField(SerializedProperty property) : this(property, proper
public CompactPropertyField(SerializedProperty property, string label, float minLabelWidth = 0)
{
style.flexDirection = FlexDirection.Row;
style.flexGrow = 1;
if (!string.IsNullOrEmpty(label))
Label = AddChild(this, new Label(label)
{ tooltip = property?.tooltip, style = { alignSelf = Align.Center, minWidth = minLabelWidth }});
Field = AddChild(this, new PropertyField(property, "") { style = { flexGrow = 1, flexBasis = 10 } });
Field = AddChild(this, new PropertyField(property, "") { style = { flexGrow = 1, flexBasis = 20 } });
if (Label != null)
AddDelayedFriendlyPropertyDragger(Label, property, Field);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,35 @@ namespace Unity.Cinemachine
[HelpURL(Documentation.BaseURL + "manual/CinemachineSplineRoll.html")]
public class CinemachineSplineRoll : MonoBehaviour
{
/// <summary>Structure to hold roll value for a specific location on the track.</summary>
[Serializable]
public struct RollData
{
/// <summary>
/// Roll (in degrees) around the forward direction for specific location on the track.
/// When placed on a SplineContainer, this is going to be a global override that affects all vcams using the Spline.
/// When placed on a vcam, this is going to be a local override that only affects that vcam.
/// </summary>
public float Value;

/// <summary> Implicit conversion to float </summary>
/// <param name="roll"> The RollData setting to convert. </param>
/// <returns> The value of the RollData setting. </returns>
public static implicit operator float(RollData roll) => roll.Value;

/// <summary> Implicit conversion from float </summary>
/// <param name="roll"> The value with which to initialize the RollData setting. </param>
/// <returns>A new RollData setting with the given value. </returns>
public static implicit operator RollData(float roll) => new () { Value = roll };
}

/// <summary>Interpolator for the RollData</summary>
public struct LerpRollData : IInterpolator<RollData>
{
/// <inheritdoc/>
public RollData Interpolate(RollData a, RollData b, float t) => new() { Value = Mathf.Lerp(a.Value, b.Value, t) };
}

/// <summary>
/// Roll (in degrees) around the forward direction for specific location on the track.
/// When placed on a SplineContainer, this is going to be a global override that affects all vcams using the Spline.
Expand All @@ -26,7 +55,7 @@ public class CinemachineSplineRoll : MonoBehaviour
[Tooltip("Roll (in degrees) around the forward direction for specific location on the track.\n" +
"- When placed on a SplineContainer, this is going to be a global override that affects all vcams using the Spline.\n" +
"- When placed on a vcam, this is going to be a local override that only affects that vcam.")]
public SplineData<float> Roll;
public SplineData<RollData> Roll;

#if UNITY_EDITOR
// Only needed for drawing the gizmo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static bool LocalEvaluateSplineWithRoll(
if (roll != null && roll.enabled)
{
float rollValue = roll.Roll.Evaluate(spline.Spline, tNormalized,
PathIndexUnit.Normalized, new UnityEngine.Splines.Interpolators.LerpFloat());
PathIndexUnit.Normalized, new CinemachineSplineRoll.LerpRollData());
rotation = Quaternion.AngleAxis(-rollValue, fwd) * rotation;
}
return true;
Expand Down

0 comments on commit bce00bd

Please sign in to comment.