Skip to content

Commit

Permalink
Add rotation snapping to 15 degree increments #82
Browse files Browse the repository at this point in the history
Add #82
  • Loading branch information
ItsPepperpot committed Sep 29, 2024
1 parent 1c03ec0 commit 5ad1ccd
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions IndustrialPark/ArchiveEditor/ArchiveEditorFunctions_Gizmos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,10 @@ public static void ScreenUnclicked()
g.isSelected = false;
foreach (PositionLocalGizmo g in positionLocalGizmos)
g.isSelected = false;

// Reset the transformation values
totalDistanceMoved = 0;
totalDistanceRotated = 0;
}

private void RefreshAssetEditors()
Expand Down Expand Up @@ -703,6 +706,9 @@ public void MouseMoveForPosition(Matrix viewProjection, int distanceX, int dista
}
}

private static float totalDistanceRotated;
private static float originalRotation;

public void MouseMoveForRotation(Matrix viewProjection, int distanceX, bool grid)//, int distanceY)
{
bool assetModified = false;
Expand All @@ -729,10 +735,15 @@ public void MouseMoveForRotation(Matrix viewProjection, int distanceX, bool grid

//ra.Yaw -= (distanceX * direction.X - distanceY * direction.Y) / 10;

var prevYaw = ra.Yaw;
if (totalDistanceRotated == 0)
originalRotation = ra.Yaw;

totalDistanceRotated += distanceX;

var prevYaw = ra.Yaw;

if (grid)
ra.Yaw = SnapToGrid(ra.Yaw + distanceX, GizmoType.X);
ra.Yaw = SnapToIncrement(originalRotation + totalDistanceRotated);
else
ra.Yaw += distanceX;

Expand All @@ -751,10 +762,15 @@ public void MouseMoveForRotation(Matrix viewProjection, int distanceX, bool grid

//ra.Pitch -= (distanceX * direction.X - distanceY * direction.Y) / 10;

var prevPitch = ra.Pitch;
if (totalDistanceRotated == 0)
originalRotation = ra.Pitch;

totalDistanceRotated += distanceX;

var prevPitch = ra.Pitch;

if (grid)
ra.Pitch = SnapToGrid(ra.Pitch + distanceX, GizmoType.Y);
ra.Pitch = SnapToIncrement(originalRotation + totalDistanceRotated);
else
ra.Pitch += distanceX;

Expand All @@ -773,10 +789,15 @@ public void MouseMoveForRotation(Matrix viewProjection, int distanceX, bool grid

//ra.Roll -= (distanceX * direction.X - distanceY * direction.Y) / 10;

var prevRoll = ra.Roll;
if (totalDistanceRotated == 0)
originalRotation = ra.Roll;

totalDistanceRotated += distanceX;

var prevRoll = ra.Roll;

if (grid)
ra.Roll = SnapToGrid(ra.Roll + distanceX, GizmoType.Z);
ra.Roll = SnapToIncrement(originalRotation + totalDistanceRotated);
else
ra.Roll += distanceX;

Expand Down Expand Up @@ -970,6 +991,19 @@ public static GizmoMode ToggleGizmoType(GizmoMode mode = GizmoMode.Null)
return CurrentGizmoMode;
}

/// <summary>
/// Rounds a value to the nearest specified increment.
/// Useful for snapping an arbitrary rotation value
/// </summary>
/// <param name="value">The value to round</param>
/// <param name="increment">The increment</param>
/// <returns>The value rounded to the nearest increment</returns>
private float SnapToIncrement(float value, float increment = 15.0f)
{
// Round to the nearest increment
return (float)Math.Round(value / increment) * increment;
}

private float SnapToGrid(float value, GizmoType gizmo)
{
if (gizmo == GizmoType.X)
Expand Down

0 comments on commit 5ad1ccd

Please sign in to comment.