From 1f799b12c97100c1eee2c692c8fddc774c9dff68 Mon Sep 17 00:00:00 2001 From: ethanglide Date: Thu, 23 Nov 2023 21:31:43 -0500 Subject: [PATCH 1/4] Add rail valid point snapping If you hold down ctrl, it will only let you place the rail in a position that has valid angles with its neighbouring points --- .../SceneObjects/bgunit/BGUnitRailSceneObj.cs | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/Fushigi/ui/SceneObjects/bgunit/BGUnitRailSceneObj.cs b/Fushigi/ui/SceneObjects/bgunit/BGUnitRailSceneObj.cs index e63c8efc..a23f24c9 100644 --- a/Fushigi/ui/SceneObjects/bgunit/BGUnitRailSceneObj.cs +++ b/Fushigi/ui/SceneObjects/bgunit/BGUnitRailSceneObj.cs @@ -240,7 +240,14 @@ 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)) + { + rail.Points[i].Position = newPos; + } + anyTransformed = true; } } @@ -349,6 +356,35 @@ private bool IsValidAngle(Vector2 point1, Vector2 point2) return validAngles.Contains(MathF.Round(angle)); } + /// + /// Check if the proposed position is valid within the rail + /// + /// new proposed position + /// index of the point to set to the new position + /// true if the point is valid there, false otherwise + 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); From 8290a07c45f0a3d52effea82cc05bbad13fc4003 Mon Sep 17 00:00:00 2001 From: ethanglide Date: Mon, 27 Nov 2023 17:35:11 -0500 Subject: [PATCH 2/4] Fix rail move undo issue Still need to fix the new redo issue! --- Fushigi/ui/SceneObjects/bgunit/BGUnitRailSceneObj.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Fushigi/ui/SceneObjects/bgunit/BGUnitRailSceneObj.cs b/Fushigi/ui/SceneObjects/bgunit/BGUnitRailSceneObj.cs index a23f24c9..4b1a509e 100644 --- a/Fushigi/ui/SceneObjects/bgunit/BGUnitRailSceneObj.cs +++ b/Fushigi/ui/SceneObjects/bgunit/BGUnitRailSceneObj.cs @@ -224,6 +224,7 @@ public void OnSelecting(CourseAreaEditContext ctx, LevelViewport viewport) if (!ChildPoints.TryGetValue(point, out RailPoint? childPoint)) continue; + childPoint.Transform.Position = childPoint.PreviousPosition; mTransformUndos.Add(new TransformUndo(childPoint.Transform)); } } From c2a8ffc32ff1c3a7c75666db39171fc19889819a Mon Sep 17 00:00:00 2001 From: ethanglide Date: Mon, 27 Nov 2023 21:40:59 -0500 Subject: [PATCH 3/4] Clean up TransformUndo Believe it or not, all of this code that was deleted is not used. There is still an issue of not being able to redo. I think that this whole thing will likely need to be redone to be working completely. --- Fushigi/ui/undo/TransformUndo.cs | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/Fushigi/ui/undo/TransformUndo.cs b/Fushigi/ui/undo/TransformUndo.cs index aaf87313..c00f095e 100644 --- a/Fushigi/ui/undo/TransformUndo.cs +++ b/Fushigi/ui/undo/TransformUndo.cs @@ -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; @@ -13,22 +14,13 @@ public class TransformUndo : IRevertable { public string Name { get; set; } - public Transform Transform; - - public Vector3 PreviousPosition; - public Vector3 PreviousRotation; - public Vector3 PreviousScale; + Transform Transform; public TransformUndo(Transform transform, 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; } public IRevertable Revert() @@ -36,9 +28,6 @@ public IRevertable Revert() //Revert transform instance var redo = new TransformUndo(Transform); - Transform.Position = this.PreviousPosition; - Transform.RotationEuler = this.PreviousRotation; - Transform.Scale = this.PreviousScale; Transform.OnUpdate(); //Create revert stack From 01eb79f9b0f35167ac8ea0eefaf420f98b2de8ac Mon Sep 17 00:00:00 2001 From: ethanglide Date: Mon, 27 Nov 2023 22:09:27 -0500 Subject: [PATCH 4/4] Fix rail move redo issue --- .../ui/SceneObjects/bgunit/BGUnitRailSceneObj.cs | 15 ++++++--------- Fushigi/ui/undo/TransformUndo.cs | 9 +++++++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Fushigi/ui/SceneObjects/bgunit/BGUnitRailSceneObj.cs b/Fushigi/ui/SceneObjects/bgunit/BGUnitRailSceneObj.cs index 4b1a509e..18df4ecb 100644 --- a/Fushigi/ui/SceneObjects/bgunit/BGUnitRailSceneObj.cs +++ b/Fushigi/ui/SceneObjects/bgunit/BGUnitRailSceneObj.cs @@ -214,19 +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; - - childPoint.Transform.Position = childPoint.PreviousPosition; - mTransformUndos.Add(new TransformUndo(childPoint.Transform)); - } } bool anyTransformed = false; @@ -246,6 +238,11 @@ public void OnSelecting(CourseAreaEditContext ctx, LevelViewport viewport) if (!ImGui.GetIO().KeyCtrl || IsValidPosition(newPos, i)) { + mTransformUndos.Add(new TransformUndo( + childPoint.Transform, + childPoint.PreviousPosition, + newPos)); + rail.Points[i].Position = newPos; } diff --git a/Fushigi/ui/undo/TransformUndo.cs b/Fushigi/ui/undo/TransformUndo.cs index c00f095e..29b800f7 100644 --- a/Fushigi/ui/undo/TransformUndo.cs +++ b/Fushigi/ui/undo/TransformUndo.cs @@ -15,19 +15,24 @@ public class TransformUndo : IRevertable public string Name { get; set; } Transform Transform; + Vector3 OldPos; + Vector3 NewPos; - 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; Transform = transform; + OldPos = oldPos; + NewPos = newPos; } public IRevertable Revert() { //Revert transform instance - var redo = new TransformUndo(Transform); + var redo = new TransformUndo(Transform, NewPos, OldPos); + Transform.Position = OldPos; Transform.OnUpdate(); //Create revert stack