Skip to content

Commit

Permalink
Merge pull request #79 from ethanglide/main
Browse files Browse the repository at this point in the history
Fix rail undo/redo
  • Loading branch information
jupahe64 authored Nov 28, 2023
2 parents 2b4a522 + 01eb79f commit 5bfe25e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
52 changes: 43 additions & 9 deletions Fushigi/ui/SceneObjects/bgunit/BGUnitRailSceneObj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,11 @@ public void OnSelecting(CourseAreaEditContext ctx, LevelViewport viewport)

Vector3 posVec = viewport.ScreenToWorld(ImGui.GetMousePos());
Vector3 diff = posVec - mouseDownPos;

if (diff.X != 0 && diff.Y != 0 && !transformStart)
{
transformStart = true;
//Store each selected point for undoing
mTransformUndos.Clear();
foreach (var point in GetSelected(ctx))
{
if (!ChildPoints.TryGetValue(point, out RailPoint? childPoint))
continue;

mTransformUndos.Add(new TransformUndo(childPoint.Transform));
}
}

bool anyTransformed = false;
Expand All @@ -240,7 +233,19 @@ public void OnSelecting(CourseAreaEditContext ctx, LevelViewport viewport)
diff.X = MathF.Round(diff.X, MidpointRounding.AwayFromZero);
diff.Y = MathF.Round(diff.Y, MidpointRounding.AwayFromZero);
posVec.Z = rail.Points[i].Position.Z;
rail.Points[i].Position = childPoint.PreviousPosition + diff;

var newPos = childPoint.PreviousPosition + diff;

if (!ImGui.GetIO().KeyCtrl || IsValidPosition(newPos, i))
{
mTransformUndos.Add(new TransformUndo(
childPoint.Transform,
childPoint.PreviousPosition,
newPos));

rail.Points[i].Position = newPos;
}

anyTransformed = true;
}
}
Expand Down Expand Up @@ -349,6 +354,35 @@ private bool IsValidAngle(Vector2 point1, Vector2 point2)
return validAngles.Contains(MathF.Round(angle));
}

/// <summary>
/// Check if the proposed position is valid within the rail
/// </summary>
/// <param name="pos">new proposed position</param>
/// <param name="index">index of the point to set to the new position</param>
/// <returns>true if the point is valid there, false otherwise</returns>
private bool IsValidPosition(Vector3 pos, int index)
{
Vector2 newPos = new(pos.X, pos.Y);

// Check if the point has valid angles with the points coming before and after it
int[] offsets = [-1, 1];
foreach (var offset in offsets)
{
var neighborIndex = (index + offset) % rail.Points.Count;

if (neighborIndex < 0)
neighborIndex += rail.Points.Count;

Vector2 neighborPos = new(rail.Points[neighborIndex].Position.X,
rail.Points[neighborIndex].Position.Y);

if (!IsValidAngle(newPos, neighborPos))
return false;
}

return true;
}

void IViewportSelectable.OnSelect(CourseAreaEditContext editContext)
{
IViewportSelectable.DefaultSelect(editContext, rail);
Expand Down
24 changes: 9 additions & 15 deletions Fushigi/ui/undo/TransformUndo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Fushigi.util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Text;
Expand All @@ -13,32 +14,25 @@ public class TransformUndo : IRevertable
{
public string Name { get; set; }

public Transform Transform;
Transform Transform;
Vector3 OldPos;
Vector3 NewPos;

public Vector3 PreviousPosition;
public Vector3 PreviousRotation;
public Vector3 PreviousScale;

public TransformUndo(Transform transform, string name = $"{IconUtil.ICON_ARROWS_ALT} Transform")
public TransformUndo(Transform transform, Vector3 oldPos, Vector3 newPos, string name = $"{IconUtil.ICON_ARROWS_ALT} Transform")
{
//Undo display name
Name = name;
//The transform to update
Transform = transform;
//The state to revert to
PreviousPosition = transform.Position;
PreviousRotation = transform.RotationEuler;
PreviousScale = transform.Scale;
OldPos = oldPos;
NewPos = newPos;
}

public IRevertable Revert()
{
//Revert transform instance
var redo = new TransformUndo(Transform);
var redo = new TransformUndo(Transform, NewPos, OldPos);

Transform.Position = this.PreviousPosition;
Transform.RotationEuler = this.PreviousRotation;
Transform.Scale = this.PreviousScale;
Transform.Position = OldPos;
Transform.OnUpdate();

//Create revert stack
Expand Down

0 comments on commit 5bfe25e

Please sign in to comment.