Skip to content

Commit

Permalink
ramps: Move upper/lower height when z-moving start and end drag points.
Browse files Browse the repository at this point in the history
  • Loading branch information
freezy committed Apr 9, 2023
1 parent 3537772 commit a022f4d
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public class DragPointsHandler
private Vector3 _center = Vector3.zero;

private Vector3 _startPos;
private Dictionary<string, float> _startPosZ = new();
private readonly Dictionary<string, float> _startPosZ = new();
private float[] _startTopBottomZ;

/// <summary>
/// Every DragPointsInspector instantiates this to manage its curve handling.
Expand Down Expand Up @@ -314,6 +315,7 @@ public void OnSceneGUI(Event evt, DragPointPositionChange onChange = null)
// set start positions since clicked
if (evt.type == EventType.MouseDown) {
_startPos = _centerSelected;
_startTopBottomZ = DragPointInspector.TopBottomZ;
_startPosZ.Clear();
foreach (var cp in SelectedControlPoints) {
_startPosZ[cp.DragPointId] = cp.DragPoint.Center.Z;
Expand All @@ -329,11 +331,11 @@ public void OnSceneGUI(Event evt, DragPointPositionChange onChange = null)

Undo.RecordObject(MainComponent as MonoBehaviour, "move Drag Points");
foreach (var controlPoint in SelectedControlPoints) {
controlPoint.DragPoint.Center = new Vertex3D(
DragPointInspector.SetDragPointPosition(controlPoint.DragPoint, new Vertex3D(
controlPoint.DragPoint.Center.X + delta.x,
controlPoint.DragPoint.Center.Y + delta.y,
_startPosZ[controlPoint.DragPointId] + deltaZ
);
), SelectedControlPoints.Count, _startTopBottomZ);
}
onChange?.Invoke();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,16 @@ public interface IDragPointsInspector
/// The global z-offset of all drag points, to render properly in editor.
/// </summary>
float ZOffset { get; }

float[] TopBottomZ { get; }

/// <summary>
/// Sets the drag point position
/// </summary>
/// <param name="dragPoint">Drag point to which the new position is applied to</param>
/// <param name="value">The new value, where Z is the z-offset for ramps (and irrelevant for other items)</param>
/// <param name="numSelectedDragPoints">How many other drag points are selected</param>
/// <param name="topBottomZ">Only used for ramps, so top and bottom height can be adapted instead of the drag point's z.</param>
void SetDragPointPosition(DragPointData dragPoint, Vertex3D value, int numSelectedDragPoints = 1, float[] topBottomZ = null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ private void OnSceneGUI()
public ItemDataTransformType HandleType => ItemDataTransformType.TwoD;
public DragPointsInspectorHelper DragPointsHelper { get; private set; }
public float ZOffset => MeshComponent.PositionZ;
public float[] TopBottomZ => null;
public void SetDragPointPosition(DragPointData dragPoint, Vertex3D value, int numSelectedDragPoints,
float[] topBottomZ) => dragPoint.Center = value;

#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ private void OnSceneGUI()
public ItemDataTransformType HandleType => ItemDataTransformType.TwoD;
public DragPointsInspectorHelper DragPointsHelper { get; private set; }
public float ZOffset => 0f;

public float[] TopBottomZ => null;
public void SetDragPointPosition(DragPointData dragPoint, Vertex3D value, int numSelectedDragPoints,
float[] topBottomZ) => dragPoint.Center = value;

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ private void OnSceneGUI()
public ItemDataTransformType HandleType => ItemDataTransformType.ThreeD;
public DragPointsInspectorHelper DragPointsHelper { get; private set; }
public float ZOffset => 0f;
public float[] TopBottomZ => new[] { MainComponent._heightBottom, MainComponent._heightTop };

public void SetDragPointPosition(DragPointData dragPoint, Vertex3D value, int numSelectedDragPoints, float[] topBottomZ)
{
var isFirst = MainComponent.DragPoints[0].Id == dragPoint.Id;
var isLast = MainComponent.DragPoints[^1].Id == dragPoint.Id;
var zDiff = value.Z - dragPoint.Center.Z;

if (isFirst && numSelectedDragPoints == 1) {
MainComponent._heightBottom = topBottomZ[0] + zDiff;
dragPoint.Center.X = value.X;
dragPoint.Center.Y = value.Y;

} else if (isLast && numSelectedDragPoints == 1) {
MainComponent._heightTop = topBottomZ[1] + zDiff;
dragPoint.Center.X = value.X;
dragPoint.Center.Y = value.Y;

} else {
dragPoint.Center = value;
}
}

#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ private void OnSceneGUI()
public ItemDataTransformType HandleType => ItemDataTransformType.TwoD;
public DragPointsInspectorHelper DragPointsHelper { get; private set; }
public float ZOffset => MainComponent.Height;
public float[] TopBottomZ => null;
public void SetDragPointPosition(DragPointData dragPoint, Vertex3D value, int numSelectedDragPoints,
float[] topBottomZ) => dragPoint.Center = value;

#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ private void OnSceneGUI()
public ItemDataTransformType HandleType => ItemDataTransformType.TwoD;
public DragPointsInspectorHelper DragPointsHelper { get; private set; }
public float ZOffset => MainComponent.HeightTop + MainComponent.PlayfieldHeight;
public float[] TopBottomZ => null;
public void SetDragPointPosition(DragPointData dragPoint, Vertex3D value, int numSelectedDragPoints,
float[] topBottomZ) => dragPoint.Center = value;

#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public bool DragPointsActive {
public ItemDataTransformType HandleType => ItemDataTransformType.TwoD;
public DragPointsInspectorHelper DragPointsHelper { get; private set; }
public float ZOffset => MainComponent.PositionZ;
public float[] TopBottomZ => null;
public void SetDragPointPosition(DragPointData dragPoint, Vertex3D value, int numSelectedDragPoints,
float[] topBottomZ) => dragPoint.Center = value;

#endregion
}
Expand Down

0 comments on commit a022f4d

Please sign in to comment.