Skip to content

Commit

Permalink
remove bindItem from SplineRoll
Browse files Browse the repository at this point in the history
  • Loading branch information
glabute committed Jun 6, 2024
1 parent 4658905 commit fe16a6a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void SetOffset(Vector3 offset)
// local function
void OnIndexDraggerCreated(IDelayedFriendlyDragger dragger)
{
dragger.OnStartDrag = () => list.selectedIndex = index;
//dragger.OnStartDrag = () => list.selectedIndex = index;
dragger.OnDragValueChangedFloat = (v) => BringCameraToCustomSplinePoint(splineData, v);
}
};
Expand Down
56 changes: 37 additions & 19 deletions com.unity.cinemachine/Editor/Editors/CinemachineSplineRollEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,49 @@ public override VisualElement CreateInspectorGUI()

var arrayProp = rollProp.FindPropertyRelative("m_DataPoints");

list.makeItem = () => new BindableElement() { style = { flexDirection = FlexDirection.Row, marginRight = 4 }};
list.bindItem = (ux, index) =>
list.makeItem = () =>
{
// Remove children - items get recycled
for (int i = ux.childCount - 1; i >= 0; --i)
ux.RemoveAt(i);
var itemRootName = "ItemRoot";
var row = new BindableElement() { name = itemRootName, style = { flexDirection = FlexDirection.Row, marginRight = 4 }};

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 element = index < arrayProp.arraySize ? arrayProp.GetArrayElementAtIndex(index) : null;
var def = new CinemachineSplineRoll.RollData();
var indexProp = element.FindPropertyRelative("m_Index");
var valueProp = element.FindPropertyRelative("m_Value");

ux.Add(new VisualElement { pickingMode = PickingMode.Ignore, style = { width = 12 }}); // pass-through for selecting row in list
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());
label.AddDelayedFriendlyPropertyDragger(indexProp, indexField, (dragger) => dragger.OnStartDrag = () => list.selectedIndex = index);

ux.Add(new VisualElement { pickingMode = PickingMode.Ignore, style = { width = 12 }}); // pass-through for selecting row in list
ux.Add(new InspectorUtility.CompactPropertyField(valueProp.FindPropertyRelative(() => def.Value), "Roll") { style = { flexGrow = 1 }});
row.Add(new VisualElement { pickingMode = PickingMode.Ignore, style = { flexBasis = 12 }}); // pass-through for selecting row in list
var indexField = row.AddChild(InspectorUtility.CreateDraggableField(
typeof(float), "m_Index", indexTooltip, row.AddChild(new Label("Index")), out var dragger));
indexField.style.flexGrow = 1;
indexField.style.flexBasis = 50;
indexField.SafeSetIsDelayed();
if (dragger is IDelayedFriendlyDragger delayedDragger)
delayedDragger.OnStartDrag = (d) => SelectListElementContainingAnElement(list, d.DragElement, itemRootName);

((BindableElement)ux).BindProperty(element); // bind must be done at the end
var def = new CinemachineSplineRoll.RollData();
var rollTooltip = SerializedPropertyHelper.PropertyTooltip(() => def.Value);
row.Add(new VisualElement { pickingMode = PickingMode.Ignore, style = { flexBasis = 12 }}); // pass-through for selecting row in list
var rollField = row.AddChild(InspectorUtility.CreateDraggableField(
typeof(float), "m_Value.Value", rollTooltip, row.AddChild(new Label("Roll")), out dragger));
rollField.style.flexGrow = 1;
rollField.style.flexBasis = 50;
if (dragger is IDelayedFriendlyDragger delayedDragger2)
delayedDragger2.OnStartDrag = (d) => SelectListElementContainingAnElement(list, d.DragElement, itemRootName);

return row;

static void SelectListElementContainingAnElement(ListView list, VisualElement element, string itemRootName)
{
var container = list.Q("unity-content-container");
if (container == null)
return;
while (element != null && element.name != itemRootName)
element = element.parent;
if (element != null)
{
var index = container.IndexOf(element);
if (index >= 0)
list.selectedIndex = index;
}
}
};

SplineRollTool.s_OnDataLookAtDragged += OnToolDragged;
Expand Down
18 changes: 7 additions & 11 deletions com.unity.cinemachine/Editor/Utility/CmCameraInspectorUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -535,17 +535,13 @@ public static void AddChildCameras(
style = { flexBasis = 20, flexGrow = 1 }
}).SetEnabled(false);

var tooltip = "The child camera's Priority";
var dragger = row.AddChild(new Label(" ") { tooltip = tooltip });
dragger.AddToClassList("unity-base-field__label--with-dragger");
var priorityField = row.AddChild(new IntegerField
{
name = "priorityField",
tooltip = tooltip,
isDelayed = true,
style = { flexBasis = floatFieldWidth, flexGrow = 0, marginRight = 4 }
});
new DelayedFriendlyFieldDragger<int>(priorityField).SetDragZone(dragger);
var priorityField = row.AddChild(InspectorUtility.CreateDraggableField(
typeof(int), "", "The child camera's Priority", row.AddChild(new Label(" ")), out _));
priorityField.name = "priorityField";
priorityField.style.flexBasis = floatFieldWidth;
priorityField.style.flexGrow = 0;
priorityField.style.marginRight = 4;
priorityField.SafeSetIsDelayed();

return row;
};
Expand Down
30 changes: 18 additions & 12 deletions com.unity.cinemachine/Editor/Utility/DelayedFriendlyFieldDragger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ interface IDelayedFriendlyDragger
public bool CancelDelayedWhenDragging { get; set; }

/// <summary>Called when dragging starts.
public Action OnStartDrag { get; set; }
public Action<IDelayedFriendlyDragger> OnStartDrag { get; set; }

/// <summary>Called when dragging stops.
public Action OnStopDrag { get; set; }
public Action<IDelayedFriendlyDragger> OnStopDrag { get; set; }

/// <summary>Called when the value changes during dragging.</summary>
public Action<int> OnDragValueChangedInt { get; set; }

/// <summary>Called when the value changes during dragging.</summary>
public Action<float> OnDragValueChangedFloat { get; set; }

/// <summary>Get the VisualElement being dragged</summary>
public VisualElement DragElement { get; }
}

/// <summary>
Expand All @@ -48,24 +51,27 @@ public DelayedFriendlyFieldDragger(IValueField<T> drivenField)
/// <summary>Is dragging.</summary>
public bool dragging { get; set; }

/// <summary>Start value before drag.</summary>
/// <inheritdoc/>
public T startValue { get; set; }

/// <summary>If true, temporarily disable isDelayed when draggin</summary>
/// <inheritdoc/>
public bool CancelDelayedWhenDragging { get; set; }

/// <summary>Called when dragging starts.
public Action OnStartDrag { get; set; }
/// <inheritdoc/>
public Action<IDelayedFriendlyDragger> OnStartDrag { get; set; }

/// <summary>Called when dragging stops.
public Action OnStopDrag { get; set; }
/// <inheritdoc/>
public Action<IDelayedFriendlyDragger> OnStopDrag { get; set; }

/// <summary>Called when the value changes during dragging.</summary>
/// <inheritdoc/>
public Action<int> OnDragValueChangedInt { get; set; }

/// <summary>Called when the value changes during dragging.</summary>
/// <inheritdoc/>
public Action<float> OnDragValueChangedFloat { get; set; }

/// <inheritdoc/>
public VisualElement DragElement => m_DragElement;

/// <inheritdoc />
public sealed override void SetDragZone(VisualElement dragElement, Rect hotZone)
{
Expand Down Expand Up @@ -136,7 +142,7 @@ private void ProcessDownEvent(EventBase evt)
m_DrivenField.StartDragging();
EditorGUIUtility.SetWantsMouseJumping(1);

OnStartDrag?.Invoke();
OnStartDrag?.Invoke(this);
}

private void UpdateValueOnPointerMove(PointerMoveEvent evt)
Expand Down Expand Up @@ -175,7 +181,7 @@ private void ProcessUpEvent(EventBase evt, int pointerId)
{
if (dragging)
{
OnStopDrag?.Invoke();
OnStopDrag?.Invoke(this);
dragging = false;
m_DragElement.UnregisterCallback<PointerMoveEvent>(UpdateValueOnPointerMove);
m_DragElement.ReleasePointer(pointerId);
Expand Down
20 changes: 12 additions & 8 deletions com.unity.cinemachine/Editor/Utility/InspectorUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,32 +295,36 @@ public static void AddDelayedFriendlyPropertyDragger(

public static VisualElement CreateDraggableField(Expression<Func<object>> exp, Label label, out BaseFieldMouseDragger dragger)
{
VisualElement field;
var name = SerializedPropertyHelper.PropertyName(exp);
var bindingPath = SerializedPropertyHelper.PropertyName(exp);
var tooltip = SerializedPropertyHelper.PropertyTooltip(exp);
return CreateDraggableField(SerializedPropertyHelper.PropertyType(exp), bindingPath, tooltip, label, out dragger);
}

label.tooltip = tooltip;
public static VisualElement CreateDraggableField(Type type, string bindingPath, string tooltip, Label label, out BaseFieldMouseDragger dragger)
{
VisualElement field;
label.AddToClassList("unity-base-field__label--with-dragger");

var type = SerializedPropertyHelper.PropertyType(exp);
label.tooltip = tooltip;
label.style.alignSelf = Align.Center;
if (type == typeof(float))
{
field = new FloatField { bindingPath = name };
field = new FloatField { bindingPath = bindingPath, tooltip = tooltip };
dragger = new DelayedFriendlyFieldDragger<float>((FloatField)field);
}
else if (type == typeof(int))
{
field = new IntegerField { bindingPath = name };
field = new IntegerField { bindingPath = bindingPath, tooltip = tooltip };
dragger = new DelayedFriendlyFieldDragger<int>((IntegerField)field);
}
else
{
field = new PropertyField(null, "") { bindingPath = name };
field = new PropertyField(null, "") { bindingPath = bindingPath, tooltip = tooltip };
dragger = null;
}
dragger?.SetDragZone(label);
return field;
}


/// <summary>A small warning sybmol, suitable for embedding in an inspector row</summary>
/// <param name="tooltip">The tooltip text</param>
Expand Down

0 comments on commit fe16a6a

Please sign in to comment.