Skip to content

Commit

Permalink
Fix broken roll data list
Browse files Browse the repository at this point in the history
  • Loading branch information
glabute committed May 16, 2024
1 parent 71d7e29 commit e568487
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 21 deletions.
50 changes: 40 additions & 10 deletions com.unity.cinemachine/Editor/Editors/CinemachineSplineRollEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,37 @@ public override VisualElement CreateInspectorGUI()
{ text = "Edit Data Points in Scene View" });
ux.AddSpace();

var prop = serializedObject.GetIterator();
if (prop.NextVisible(true))
InspectorUtility.AddRemainingProperties(ux, prop);

// Invalidate the mesh cache when the property changes (SetDirty() not always called!)
var splineData = target as CinemachineSplineRoll;
ux.TrackPropertyValue(serializedObject.FindProperty(() => splineData.Roll), (p) => SplineGizmoCache.Instance = null);
var rollProp = serializedObject.FindProperty(() => splineData.Roll);
ux.Add(new PropertyField(rollProp.FindPropertyRelative("m_IndexUnit"))
{ tooltip = "Defines how to interpret the Index field for each data point. "
+ "Knot is the recommended value because it remains robust if the spline points change." });

ux.AddHeader("Data Points");
var dataPointsProp = rollProp.FindPropertyRelative("m_DataPoints");
var list = ux.AddChild(new PropertyField(dataPointsProp));
list.OnInitialGeometry(() =>
{
var listView = list.Q<ListView>();
listView.reorderable = false;
listView.showFoldoutHeader = false;
listView.showBoundCollectionSize = false;
});

ux.TrackPropertyValue(dataPointsProp, (p) =>
{
// Invalidate the mesh cache when the property changes (SetDirty() not always called!)
SplineGizmoCache.Instance = null;
EditorApplication.delayCall += () => InspectorUtility.RepaintGameView();

if (p.arraySize > 1)
{
// Hack to set dirty to force a reorder
var item = splineData.Roll[0];
splineData.Roll[0] = item;
splineData.Roll.SortIfNecessary();
}
});

return ux;
}
Expand Down Expand Up @@ -158,12 +182,18 @@ public override VisualElement CreatePropertyGUI(SerializedProperty property)
const string indexTooltip = "The position on the Spline at which this data point will take effect. "
+ "The value is interpreted according to the Index Unit setting.";

var ux = new VisualElement { style = { flexDirection = FlexDirection.Row }};
ux.Add(new InspectorUtility.CompactPropertyField(property.FindPropertyRelative("m_Index")) { tooltip = indexTooltip });

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

var ux = new VisualElement { style = { flexDirection = FlexDirection.Row, flexGrow = 1, marginLeft = 3 }};

var label = ux.AddChild(new Label(indexProp.displayName) { tooltip = indexTooltip, style = { alignSelf = Align.Center }});
var indexField = ux.AddChild(new PropertyField(indexProp, "") { style = { flexGrow = 1, flexBasis = 20 } });
indexField.OnInitialGeometry(() => indexField.SafeSetIsDelayed());
InspectorUtility.AddDelayedFriendlyPropertyDragger(label, indexProp, indexField, false);

ux.Add(new InspectorUtility.CompactPropertyField(valueProp.FindPropertyRelative(() => def.Value)) { style = { marginLeft = 6 }});
return ux;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public override VisualElement CreatePropertyGUI(SerializedProperty property)
{ style = { flexGrow = 0, marginLeft = 8, alignSelf = Align.Center }, tooltip = childProperty.tooltip});
var childField = row.Contents.AddChild(new PropertyField(childProperty, "")
{ style = { flexGrow = 1, marginTop = -1, marginLeft = 5, marginBottom = -1 }});
childLabel.AddDelayedFriendlyPropertyDragger(childProperty, childField);
childLabel.AddDelayedFriendlyPropertyDragger(childProperty, childField, true);
childField.RemoveFromClassList(InspectorUtility.kAlignFieldClass);

row.TrackPropertyWithInitialCallback(enabledProp, (p) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override VisualElement CreatePropertyGUI(SerializedProperty property)
var valueLabel = new Label(" ") { style = { minWidth = InspectorUtility.SingleLineHeight * 2}};
var valueField = new InspectorUtility.CompactPropertyField(valueProp, "") { style = { flexGrow = 1}};
valueField.OnInitialGeometry(() => valueField.SafeSetIsDelayed());
valueLabel.AddDelayedFriendlyPropertyDragger(valueProp, valueField);
valueLabel.AddDelayedFriendlyPropertyDragger(valueProp, valueField, true);

var ux = new InspectorUtility.FoldoutWithOverlay(foldout, valueField, valueLabel);

Expand All @@ -38,7 +38,7 @@ public override VisualElement CreatePropertyGUI(SerializedProperty property)
valueFieldRow.Contents.SafeSetIsDelayed();
valueFieldRow.Contents.Q<FloatField>().style.marginLeft = 0;
});
valueFieldRow.Label.AddDelayedFriendlyPropertyDragger(valueProp, valueFieldRow.Contents);
valueFieldRow.Label.AddDelayedFriendlyPropertyDragger(valueProp, valueFieldRow.Contents, true);

var centerField = foldout.AddChild(new PropertyField(property.FindPropertyRelative(() => def.Center)));
var rangeContainer = foldout.AddChild(new VisualElement() { style = { flexDirection = FlexDirection.Row }});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override VisualElement CreatePropertyGUI(SerializedProperty property)
minField.Q<FloatField>().style.marginLeft = 2;
});

label.AddDelayedFriendlyPropertyDragger(xProp, minField);
label.AddDelayedFriendlyPropertyDragger(xProp, minField, true);
ux.Left.Add(label);
ux.Right.Add(minField);
ux.Right.Add(maxField);
Expand Down
24 changes: 17 additions & 7 deletions com.unity.cinemachine/Editor/Utility/InspectorUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,18 +530,28 @@ public static void AddSpace(this VisualElement ux)
/// <summary>
/// Add a property dragger to a float or int label, so that dragging it changes the property value.
/// </summary>
public static void AddDelayedFriendlyPropertyDragger(this Label label, SerializedProperty p, VisualElement field)
public static void AddDelayedFriendlyPropertyDragger(this Label label, SerializedProperty p, VisualElement field, bool noDelayOnDrag)
{
if (p.propertyType == SerializedPropertyType.Float
|| p.propertyType == SerializedPropertyType.Integer)
{
label.AddToClassList("unity-base-field__label--with-dragger");
label.OnInitialGeometry(() =>
{
if (p.propertyType == SerializedPropertyType.Float)
new DelayedFriendlyFieldDragger<float>(field.Q<FloatField>()).SetDragZone(label);
else if (p.propertyType == SerializedPropertyType.Integer)
new DelayedFriendlyFieldDragger<int>(field.Q<IntegerField>()).SetDragZone(label);
if (noDelayOnDrag)
{
if (p.propertyType == SerializedPropertyType.Float)
new DelayedFriendlyFieldDragger<float>(field.Q<FloatField>()).SetDragZone(label);
else if (p.propertyType == SerializedPropertyType.Integer)
new DelayedFriendlyFieldDragger<int>(field.Q<IntegerField>()).SetDragZone(label);
}
else
{
if (p.propertyType == SerializedPropertyType.Float)
new FieldMouseDragger<float>(field.Q<FloatField>()).SetDragZone(label);
else if (p.propertyType == SerializedPropertyType.Integer)
new FieldMouseDragger<int>(field.Q<IntegerField>()).SetDragZone(label);
}
});
}
}
Expand Down Expand Up @@ -776,7 +786,7 @@ public CompactPropertyField(SerializedProperty property, string label, float min
{ tooltip = property?.tooltip, style = { alignSelf = Align.Center, minWidth = minLabelWidth }});
Field = AddChild(this, new PropertyField(property, "") { style = { flexGrow = 1, flexBasis = 20 } });
if (Label != null)
AddDelayedFriendlyPropertyDragger(Label, property, Field);
AddDelayedFriendlyPropertyDragger(Label, property, Field, true);
}
}

Expand All @@ -789,7 +799,7 @@ public static LabeledRow PropertyRow(
var row = new LabeledRow(label ?? property.displayName, property.tooltip);
var field = propertyField = row.Contents.AddChild(new PropertyField(property, "")
{ style = { flexGrow = 1, flexBasis = SingleLineHeight * 5 }});
AddDelayedFriendlyPropertyDragger(row.Label, property, propertyField);
AddDelayedFriendlyPropertyDragger(row.Label, property, propertyField, true);

// Kill any left margin that gets inserted into the property field
field.OnInitialGeometry(() =>
Expand Down

0 comments on commit e568487

Please sign in to comment.