Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor additive rotation #39

Merged
merged 1 commit into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions SnapBuilder/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ internal class Config : ConfigFile
[JsonIgnore]
public Toggle ToggleRotation => toggleRotation ??= new Toggle(ToggleRotationKey, ToggleRotationMode, false);

[JsonIgnore]
public float RotationFactor => FineRotation.Enabled ? FineRotationRounding : RotationRounding;

[Toggle(LabelLanguageId = Lang.Option.DEFAULT_SNAPPING_ENABLED), OnChange(nameof(EnabledByDefaultChanged))]
public bool EnabledByDefault { get; set; } = true;
private void EnabledByDefaultChanged(ToggleChangedEventArgs e)
Expand Down
15 changes: 6 additions & 9 deletions SnapBuilder/SnapBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,8 @@ private static void ImproveHitNormal(ref RaycastHit hit)
}
}

private static void ApplyAdditiveRotation(ref float additiveRotation, out float rotationFactor)
private static void ApplyAdditiveRotation(ref float additiveRotation)
{
// Get the rotation factor from user options based on whether the fine snapping key is held or not
rotationFactor = Config.FineRotation.Enabled ? Config.FineRotationRounding : Config.RotationRounding;

// If the user is rotating, apply the additive rotation
if (GameInput.GetButtonHeld(Builder.buttonRotateCW)) // Clockwise
{
Expand All @@ -324,7 +321,7 @@ private static void ApplyAdditiveRotation(ref float additiveRotation, out float
if (buttonHeldTime > LastButtonHeldTime)
{ // Store rotation held time
LastButtonHeldTime = buttonHeldTime;
additiveRotation -= rotationFactor; // Decrement rotation
additiveRotation -= Config.RotationFactor; // Decrement rotation
}
}
else if (GameInput.GetButtonHeld(Builder.buttonRotateCCW)) // Counter-clockwise
Expand All @@ -339,7 +336,7 @@ private static void ApplyAdditiveRotation(ref float additiveRotation, out float
if (buttonHeldTime > LastButtonHeldTime)
{ // Store rotation held time
LastButtonHeldTime = buttonHeldTime;
additiveRotation += rotationFactor; // Increment rotation
additiveRotation += Config.RotationFactor; // Increment rotation
}
}
else if (GameInput.GetButtonUp(Builder.buttonRotateCW) || GameInput.GetButtonUp(Builder.buttonRotateCCW))
Expand All @@ -348,12 +345,12 @@ private static void ApplyAdditiveRotation(ref float additiveRotation, out float
}

// Round to the nearest rotation factor for rotation snapping
additiveRotation = RoundToNearest(additiveRotation, rotationFactor) % 360;
additiveRotation %= 360;
}

public static Quaternion CalculateRotation(ref float additiveRotation, RaycastHit hit, bool forceUpright)
{
ApplyAdditiveRotation(ref additiveRotation, out float rotationFactor);
ApplyAdditiveRotation(ref additiveRotation);
ImproveHitNormal(ref hit);

Transform hitTransform = GetAppropriateTransform(hit);
Expand Down Expand Up @@ -393,7 +390,7 @@ public static Quaternion CalculateRotation(ref float additiveRotation, RaycastHi

// Round/snap the Y axis of the child transform's local rotation based on the user's rotation factor, after adding the additiveRotation
child.transform.localEulerAngles
= new Vector3(0, RoundToNearest(child.transform.localEulerAngles.y + additiveRotation, rotationFactor) % 360, 0);
= new Vector3(0, RoundToNearest(child.transform.localEulerAngles.y + additiveRotation, Config.RotationFactor) % 360, 0);

Quaternion rotation = child.transform.rotation; // Our final rotation

Expand Down