From 3527ad6bf89a79867539cc396feb47d6a57efe84 Mon Sep 17 00:00:00 2001 From: jandrec Date: Fri, 23 May 2025 21:36:21 +0200 Subject: [PATCH 1/9] Implement basic straight wire routing capability --- .vscode/extensions.json | 5 ++ .vscode/launch.json | 10 +++ .vscode/settings.json | 70 +++++++++++++++++++ .../Description/Types/ProjectDescription.cs | 3 + Assets/Scripts/Game/Elements/WireInstance.cs | 17 ++++- Assets/Scripts/Game/Helpers/GridHelper.cs | 28 ++++++++ .../Interaction/ChipInteractionController.cs | 1 + Assets/Scripts/Game/Project/Project.cs | 3 + .../Graphics/UI/Menus/PreferencesMenu.cs | 11 +++ .../Projects/MainTest/ProjectDescription.json | 11 +-- 10 files changed, 153 insertions(+), 6 deletions(-) create mode 100644 .vscode/extensions.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 00000000..ddb6ff85 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "visualstudiotoolsforunity.vstuc" + ] +} diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..da60e25a --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,10 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Attach to Unity", + "type": "vstuc", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..0a8271c7 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,70 @@ +{ + "files.exclude": { + "**/.DS_Store": true, + "**/.git": true, + "**/.vs": true, + "**/.gitmodules": true, + "**/.vsconfig": true, + "**/*.booproj": true, + "**/*.pidb": true, + "**/*.suo": true, + "**/*.user": true, + "**/*.userprefs": true, + "**/*.unityproj": true, + "**/*.dll": true, + "**/*.exe": true, + "**/*.pdf": true, + "**/*.mid": true, + "**/*.midi": true, + "**/*.wav": true, + "**/*.gif": true, + "**/*.ico": true, + "**/*.jpg": true, + "**/*.jpeg": true, + "**/*.png": true, + "**/*.psd": true, + "**/*.tga": true, + "**/*.tif": true, + "**/*.tiff": true, + "**/*.3ds": true, + "**/*.3DS": true, + "**/*.fbx": true, + "**/*.FBX": true, + "**/*.lxo": true, + "**/*.LXO": true, + "**/*.ma": true, + "**/*.MA": true, + "**/*.obj": true, + "**/*.OBJ": true, + "**/*.asset": true, + "**/*.cubemap": true, + "**/*.flare": true, + "**/*.mat": true, + "**/*.meta": true, + "**/*.prefab": true, + "**/*.unity": true, + "build/": true, + "Build/": true, + "Library/": true, + "library/": true, + "obj/": true, + "Obj/": true, + "Logs/": true, + "logs/": true, + "ProjectSettings/": true, + "UserSettings/": true, + "temp/": true, + "Temp/": true + }, + "files.associations": { + "*.asset": "yaml", + "*.meta": "yaml", + "*.prefab": "yaml", + "*.unity": "yaml", + }, + "explorer.fileNesting.enabled": true, + "explorer.fileNesting.patterns": { + "*.sln": "*.csproj", + }, + "dotnet.defaultSolution": "Digital-Logic-Sim-Tests.sln" +} \ No newline at end of file diff --git a/Assets/Scripts/Description/Types/ProjectDescription.cs b/Assets/Scripts/Description/Types/ProjectDescription.cs index a8dc0f33..3ff1f1c8 100644 --- a/Assets/Scripts/Description/Types/ProjectDescription.cs +++ b/Assets/Scripts/Description/Types/ProjectDescription.cs @@ -21,6 +21,9 @@ public struct ProjectDescription public bool Prefs_SimPaused; public int Prefs_SimTargetStepsPerSecond; public int Prefs_SimStepsPerClockTick; + public int Prefs_WireRouting; + + // List of all player-created chips (in order of creation -- oldest first) public string[] AllCustomChipNames; diff --git a/Assets/Scripts/Game/Elements/WireInstance.cs b/Assets/Scripts/Game/Elements/WireInstance.cs index afe28ec4..35db063c 100644 --- a/Assets/Scripts/Game/Elements/WireInstance.cs +++ b/Assets/Scripts/Game/Elements/WireInstance.cs @@ -207,11 +207,26 @@ public void SetWirePoint(Vector2 p, int i) public void SetWirePointWithSnapping(Vector2 p, int i, Vector2 straightLineRefPoint) { if (Project.ActiveProject.ShouldSnapToGrid) p = GridHelper.SnapToGrid(p, true, true); - if (Project.ActiveProject.ForceStraightWires) p = GridHelper.ForceStraightLine(straightLineRefPoint, p); + if (Project.ActiveProject.ForceStraightWires && !Project.ActiveProject.ShouldRouteWires) p = GridHelper.ForceStraightLine(straightLineRefPoint, p); + if (Project.ActiveProject.ForceStraightWires && Project.ActiveProject.ShouldRouteWires && WirePoints.Count > 3) + { + // If routing wires, we need to route the wire to the new point + Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), p); + p = points[1]; + SetWirePoint((points[0]), WirePoints.Count - 2); + } + else if (Project.ActiveProject.ShouldRouteWires && WirePoints.Count > 2) + { + Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), p); + p = points[1]; + SetWirePoint((points[0]), WirePoints.Count - 2); + } SetWirePoint(p, i); } + + public void SetLastWirePoint(Vector2 p) { SetWirePointWithSnapping(p, WirePoints.Count - 1, GetWirePoint(WirePoints.Count - 2)); diff --git a/Assets/Scripts/Game/Helpers/GridHelper.cs b/Assets/Scripts/Game/Helpers/GridHelper.cs index 63df3ee2..a324a315 100644 --- a/Assets/Scripts/Game/Helpers/GridHelper.cs +++ b/Assets/Scripts/Game/Helpers/GridHelper.cs @@ -52,5 +52,33 @@ public static Vector2 ForceStraightLine(Vector2 prev, Vector2 curr) return prev + offset; } + + public static Vector2[] RouteWire(Vector2 prev, Vector2 curr) + { + Vector2[] points = new Vector2[2]; + Vector2 offset = curr - prev; + + if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)) + { + // Horizontal mode: move horizontally, then 45-degree diagonal to curr + float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y)); + float signX = Mathf.Sign(offset.x); + float signY = Mathf.Sign(offset.y); + Vector2 bend = new Vector2(curr.x - diagLen * signX, prev.y); + points[0] = bend; + points[1] = curr; + } + else + { + // Vertical mode: move vertically, then 45-degree diagonal to curr + float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y)); + float signX = Mathf.Sign(offset.x); + float signY = Mathf.Sign(offset.y); + Vector2 bend = new Vector2(prev.x, curr.y - diagLen * signY); + points[0] = bend; + points[1] = curr; + } + return points; + } } } \ No newline at end of file diff --git a/Assets/Scripts/Game/Interaction/ChipInteractionController.cs b/Assets/Scripts/Game/Interaction/ChipInteractionController.cs index da9491d3..193d09de 100644 --- a/Assets/Scripts/Game/Interaction/ChipInteractionController.cs +++ b/Assets/Scripts/Game/Interaction/ChipInteractionController.cs @@ -465,6 +465,7 @@ void HandleLeftMouseDown() else if (CanAddWirePoint()) { WireToPlace.AddWirePoint(InputHelper.MousePosWorld); + if(Project.ActiveProject.ShouldRouteWires) WireToPlace.AddWirePoint(InputHelper.MousePosWorld); } } // Place subchip / devpin diff --git a/Assets/Scripts/Game/Project/Project.cs b/Assets/Scripts/Game/Project/Project.cs index e2a5be6e..d6aca55a 100644 --- a/Assets/Scripts/Game/Project/Project.cs +++ b/Assets/Scripts/Game/Project/Project.cs @@ -480,6 +480,9 @@ public void ToggleGridDisplay() public bool ShouldSnapToGrid => KeyboardShortcuts.SnapModeHeld || (description.Prefs_Snapping == 1 && ShowGrid) || description.Prefs_Snapping == 2; public bool ForceStraightWires => KeyboardShortcuts.StraightLineModeHeld || (description.Prefs_StraightWires == 1 && ShowGrid) || description.Prefs_StraightWires == 2; + public bool ShouldRouteWires => description.Prefs_WireRouting == 1 || description.Prefs_WireRouting == 2; + + public void NotifyExit() { diff --git a/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs b/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs index 13909b2b..24e9fdb2 100644 --- a/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs +++ b/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs @@ -45,6 +45,13 @@ public static class PreferencesMenu "Always" }; + static readonly string[] WireRoutingOptions = + { + "Off", + "90°", + "45°" + }; + static readonly string[] SimulationStatusOptions = { "Active", @@ -60,6 +67,7 @@ public static class PreferencesMenu static readonly UIHandle ID_GridDisplay = new("PREFS_GridDisplay"); static readonly UIHandle ID_Snapping = new("PREFS_Snapping"); static readonly UIHandle ID_StraightWires = new("PREFS_StraightWires"); + static readonly UIHandle ID_WireRouting = new("PREFS_WireRouting"); static readonly UIHandle ID_SimStatus = new("PREFS_SimStatus"); static readonly UIHandle ID_SimFrequencyField = new("PREFS_SimTickTarget"); static readonly UIHandle ID_ClockSpeedInput = new("PREFS_ClockSpeed"); @@ -102,6 +110,7 @@ public static void DrawMenu(Project project) DrawHeader("EDITING:"); int snappingMode = DrawNextWheel("Snap to grid", SnappingOptions, ID_Snapping); int straightWireMode = DrawNextWheel("Straight wires", StraightWireOptions, ID_StraightWires); + int wireRoutingMode = DrawNextWheel("Wire routing", WireRoutingOptions, ID_WireRouting); DrawHeader("SIMULATION:"); bool pauseSim = MenuHelper.LabeledOptionsWheel(simStatusLabel, labelCol, labelPosCurr, entrySize, ID_SimStatus, SimulationStatusOptions, settingFieldSize.x, true) == 1; @@ -136,6 +145,7 @@ public static void DrawMenu(Project project) project.description.Prefs_ChipPinNamesDisplayMode = chipPinNamesMode; project.description.Prefs_GridDisplayMode = gridDisplayMode; project.description.Prefs_Snapping = snappingMode; + project.description.Prefs_WireRouting = wireRoutingMode; project.description.Prefs_StraightWires = straightWireMode; project.description.Prefs_SimTargetStepsPerSecond = targetSimTicksPerSecond; project.description.Prefs_SimStepsPerClockTick = clockSpeed; @@ -206,6 +216,7 @@ static void UpdateUIFromDescription() UI.GetWheelSelectorState(ID_GridDisplay).index = projDesc.Prefs_GridDisplayMode; UI.GetWheelSelectorState(ID_Snapping).index = projDesc.Prefs_Snapping; UI.GetWheelSelectorState(ID_StraightWires).index = projDesc.Prefs_StraightWires; + UI.GetWheelSelectorState(ID_WireRouting).index = projDesc.Prefs_WireRouting; UI.GetWheelSelectorState(ID_SimStatus).index = projDesc.Prefs_SimPaused ? 1 : 0; // -- Input fields UI.GetInputFieldState(ID_SimFrequencyField).SetText(projDesc.Prefs_SimTargetStepsPerSecond + "", false); diff --git a/TestData/Projects/MainTest/ProjectDescription.json b/TestData/Projects/MainTest/ProjectDescription.json index 6d1823ff..4fdc01a5 100644 --- a/TestData/Projects/MainTest/ProjectDescription.json +++ b/TestData/Projects/MainTest/ProjectDescription.json @@ -1,17 +1,18 @@ { "ProjectName": "MainTest", - "DLSVersion_LastSaved": "2.1.5", + "DLSVersion_LastSaved": "2.1.6", "DLSVersion_EarliestCompatible": "2.0.0", - "CreationTime": "2025-03-14T18:23:30.404+01:00", - "LastSaveTime": "2025-05-04T09:15:41.061+02:00", + "CreationTime": "2025-03-14T19:23:30.404+02:00", + "LastSaveTime": "2025-05-23T21:01:50.361+02:00", "Prefs_MainPinNamesDisplayMode": 2, "Prefs_ChipPinNamesDisplayMode": 1, "Prefs_GridDisplayMode": 1, - "Prefs_Snapping": 0, - "Prefs_StraightWires": 0, + "Prefs_Snapping": 2, + "Prefs_StraightWires": 2, "Prefs_SimPaused": false, "Prefs_SimTargetStepsPerSecond": 150, "Prefs_SimStepsPerClockTick": 6, + "Prefs_WireRouting": 1, "AllCustomChipNames":[ "AND", "D-LATCH", From 91ce41f81fed2ec0e2826e7899d5f4ca97b3038c Mon Sep 17 00:00:00 2001 From: jandrec Date: Fri, 23 May 2025 21:57:34 +0200 Subject: [PATCH 2/9] Revert "Implement basic straight wire routing capability" This reverts commit 3527ad6bf89a79867539cc396feb47d6a57efe84. --- .../Description/Types/ProjectDescription.cs | 3 -- Assets/Scripts/Game/Elements/WireInstance.cs | 17 +---------- Assets/Scripts/Game/Helpers/GridHelper.cs | 28 ------------------- .../Interaction/ChipInteractionController.cs | 1 - Assets/Scripts/Game/Project/Project.cs | 3 -- .../Graphics/UI/Menus/PreferencesMenu.cs | 11 -------- .../Projects/MainTest/ProjectDescription.json | 11 ++++---- 7 files changed, 6 insertions(+), 68 deletions(-) diff --git a/Assets/Scripts/Description/Types/ProjectDescription.cs b/Assets/Scripts/Description/Types/ProjectDescription.cs index 3ff1f1c8..a8dc0f33 100644 --- a/Assets/Scripts/Description/Types/ProjectDescription.cs +++ b/Assets/Scripts/Description/Types/ProjectDescription.cs @@ -21,9 +21,6 @@ public struct ProjectDescription public bool Prefs_SimPaused; public int Prefs_SimTargetStepsPerSecond; public int Prefs_SimStepsPerClockTick; - public int Prefs_WireRouting; - - // List of all player-created chips (in order of creation -- oldest first) public string[] AllCustomChipNames; diff --git a/Assets/Scripts/Game/Elements/WireInstance.cs b/Assets/Scripts/Game/Elements/WireInstance.cs index 35db063c..afe28ec4 100644 --- a/Assets/Scripts/Game/Elements/WireInstance.cs +++ b/Assets/Scripts/Game/Elements/WireInstance.cs @@ -207,26 +207,11 @@ public void SetWirePoint(Vector2 p, int i) public void SetWirePointWithSnapping(Vector2 p, int i, Vector2 straightLineRefPoint) { if (Project.ActiveProject.ShouldSnapToGrid) p = GridHelper.SnapToGrid(p, true, true); - if (Project.ActiveProject.ForceStraightWires && !Project.ActiveProject.ShouldRouteWires) p = GridHelper.ForceStraightLine(straightLineRefPoint, p); - if (Project.ActiveProject.ForceStraightWires && Project.ActiveProject.ShouldRouteWires && WirePoints.Count > 3) - { - // If routing wires, we need to route the wire to the new point - Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), p); - p = points[1]; - SetWirePoint((points[0]), WirePoints.Count - 2); - } - else if (Project.ActiveProject.ShouldRouteWires && WirePoints.Count > 2) - { - Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), p); - p = points[1]; - SetWirePoint((points[0]), WirePoints.Count - 2); - } + if (Project.ActiveProject.ForceStraightWires) p = GridHelper.ForceStraightLine(straightLineRefPoint, p); SetWirePoint(p, i); } - - public void SetLastWirePoint(Vector2 p) { SetWirePointWithSnapping(p, WirePoints.Count - 1, GetWirePoint(WirePoints.Count - 2)); diff --git a/Assets/Scripts/Game/Helpers/GridHelper.cs b/Assets/Scripts/Game/Helpers/GridHelper.cs index a324a315..63df3ee2 100644 --- a/Assets/Scripts/Game/Helpers/GridHelper.cs +++ b/Assets/Scripts/Game/Helpers/GridHelper.cs @@ -52,33 +52,5 @@ public static Vector2 ForceStraightLine(Vector2 prev, Vector2 curr) return prev + offset; } - - public static Vector2[] RouteWire(Vector2 prev, Vector2 curr) - { - Vector2[] points = new Vector2[2]; - Vector2 offset = curr - prev; - - if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)) - { - // Horizontal mode: move horizontally, then 45-degree diagonal to curr - float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y)); - float signX = Mathf.Sign(offset.x); - float signY = Mathf.Sign(offset.y); - Vector2 bend = new Vector2(curr.x - diagLen * signX, prev.y); - points[0] = bend; - points[1] = curr; - } - else - { - // Vertical mode: move vertically, then 45-degree diagonal to curr - float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y)); - float signX = Mathf.Sign(offset.x); - float signY = Mathf.Sign(offset.y); - Vector2 bend = new Vector2(prev.x, curr.y - diagLen * signY); - points[0] = bend; - points[1] = curr; - } - return points; - } } } \ No newline at end of file diff --git a/Assets/Scripts/Game/Interaction/ChipInteractionController.cs b/Assets/Scripts/Game/Interaction/ChipInteractionController.cs index 193d09de..da9491d3 100644 --- a/Assets/Scripts/Game/Interaction/ChipInteractionController.cs +++ b/Assets/Scripts/Game/Interaction/ChipInteractionController.cs @@ -465,7 +465,6 @@ void HandleLeftMouseDown() else if (CanAddWirePoint()) { WireToPlace.AddWirePoint(InputHelper.MousePosWorld); - if(Project.ActiveProject.ShouldRouteWires) WireToPlace.AddWirePoint(InputHelper.MousePosWorld); } } // Place subchip / devpin diff --git a/Assets/Scripts/Game/Project/Project.cs b/Assets/Scripts/Game/Project/Project.cs index d6aca55a..e2a5be6e 100644 --- a/Assets/Scripts/Game/Project/Project.cs +++ b/Assets/Scripts/Game/Project/Project.cs @@ -480,9 +480,6 @@ public void ToggleGridDisplay() public bool ShouldSnapToGrid => KeyboardShortcuts.SnapModeHeld || (description.Prefs_Snapping == 1 && ShowGrid) || description.Prefs_Snapping == 2; public bool ForceStraightWires => KeyboardShortcuts.StraightLineModeHeld || (description.Prefs_StraightWires == 1 && ShowGrid) || description.Prefs_StraightWires == 2; - public bool ShouldRouteWires => description.Prefs_WireRouting == 1 || description.Prefs_WireRouting == 2; - - public void NotifyExit() { diff --git a/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs b/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs index 24e9fdb2..13909b2b 100644 --- a/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs +++ b/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs @@ -45,13 +45,6 @@ public static class PreferencesMenu "Always" }; - static readonly string[] WireRoutingOptions = - { - "Off", - "90°", - "45°" - }; - static readonly string[] SimulationStatusOptions = { "Active", @@ -67,7 +60,6 @@ public static class PreferencesMenu static readonly UIHandle ID_GridDisplay = new("PREFS_GridDisplay"); static readonly UIHandle ID_Snapping = new("PREFS_Snapping"); static readonly UIHandle ID_StraightWires = new("PREFS_StraightWires"); - static readonly UIHandle ID_WireRouting = new("PREFS_WireRouting"); static readonly UIHandle ID_SimStatus = new("PREFS_SimStatus"); static readonly UIHandle ID_SimFrequencyField = new("PREFS_SimTickTarget"); static readonly UIHandle ID_ClockSpeedInput = new("PREFS_ClockSpeed"); @@ -110,7 +102,6 @@ public static void DrawMenu(Project project) DrawHeader("EDITING:"); int snappingMode = DrawNextWheel("Snap to grid", SnappingOptions, ID_Snapping); int straightWireMode = DrawNextWheel("Straight wires", StraightWireOptions, ID_StraightWires); - int wireRoutingMode = DrawNextWheel("Wire routing", WireRoutingOptions, ID_WireRouting); DrawHeader("SIMULATION:"); bool pauseSim = MenuHelper.LabeledOptionsWheel(simStatusLabel, labelCol, labelPosCurr, entrySize, ID_SimStatus, SimulationStatusOptions, settingFieldSize.x, true) == 1; @@ -145,7 +136,6 @@ public static void DrawMenu(Project project) project.description.Prefs_ChipPinNamesDisplayMode = chipPinNamesMode; project.description.Prefs_GridDisplayMode = gridDisplayMode; project.description.Prefs_Snapping = snappingMode; - project.description.Prefs_WireRouting = wireRoutingMode; project.description.Prefs_StraightWires = straightWireMode; project.description.Prefs_SimTargetStepsPerSecond = targetSimTicksPerSecond; project.description.Prefs_SimStepsPerClockTick = clockSpeed; @@ -216,7 +206,6 @@ static void UpdateUIFromDescription() UI.GetWheelSelectorState(ID_GridDisplay).index = projDesc.Prefs_GridDisplayMode; UI.GetWheelSelectorState(ID_Snapping).index = projDesc.Prefs_Snapping; UI.GetWheelSelectorState(ID_StraightWires).index = projDesc.Prefs_StraightWires; - UI.GetWheelSelectorState(ID_WireRouting).index = projDesc.Prefs_WireRouting; UI.GetWheelSelectorState(ID_SimStatus).index = projDesc.Prefs_SimPaused ? 1 : 0; // -- Input fields UI.GetInputFieldState(ID_SimFrequencyField).SetText(projDesc.Prefs_SimTargetStepsPerSecond + "", false); diff --git a/TestData/Projects/MainTest/ProjectDescription.json b/TestData/Projects/MainTest/ProjectDescription.json index 4fdc01a5..6d1823ff 100644 --- a/TestData/Projects/MainTest/ProjectDescription.json +++ b/TestData/Projects/MainTest/ProjectDescription.json @@ -1,18 +1,17 @@ { "ProjectName": "MainTest", - "DLSVersion_LastSaved": "2.1.6", + "DLSVersion_LastSaved": "2.1.5", "DLSVersion_EarliestCompatible": "2.0.0", - "CreationTime": "2025-03-14T19:23:30.404+02:00", - "LastSaveTime": "2025-05-23T21:01:50.361+02:00", + "CreationTime": "2025-03-14T18:23:30.404+01:00", + "LastSaveTime": "2025-05-04T09:15:41.061+02:00", "Prefs_MainPinNamesDisplayMode": 2, "Prefs_ChipPinNamesDisplayMode": 1, "Prefs_GridDisplayMode": 1, - "Prefs_Snapping": 2, - "Prefs_StraightWires": 2, + "Prefs_Snapping": 0, + "Prefs_StraightWires": 0, "Prefs_SimPaused": false, "Prefs_SimTargetStepsPerSecond": 150, "Prefs_SimStepsPerClockTick": 6, - "Prefs_WireRouting": 1, "AllCustomChipNames":[ "AND", "D-LATCH", From 2707edce1764577e410268a36da9385eec84af3d Mon Sep 17 00:00:00 2001 From: jandrec Date: Sat, 24 May 2025 10:34:07 +0200 Subject: [PATCH 3/9] Reapply "Implement basic straight wire routing capability" This reverts commit 91ce41f81fed2ec0e2826e7899d5f4ca97b3038c. --- .../Description/Types/ProjectDescription.cs | 3 ++ Assets/Scripts/Game/Elements/WireInstance.cs | 17 ++++++++++- Assets/Scripts/Game/Helpers/GridHelper.cs | 28 +++++++++++++++++++ .../Interaction/ChipInteractionController.cs | 1 + Assets/Scripts/Game/Project/Project.cs | 3 ++ .../Graphics/UI/Menus/PreferencesMenu.cs | 11 ++++++++ .../Projects/MainTest/ProjectDescription.json | 11 ++++---- 7 files changed, 68 insertions(+), 6 deletions(-) diff --git a/Assets/Scripts/Description/Types/ProjectDescription.cs b/Assets/Scripts/Description/Types/ProjectDescription.cs index a8dc0f33..3ff1f1c8 100644 --- a/Assets/Scripts/Description/Types/ProjectDescription.cs +++ b/Assets/Scripts/Description/Types/ProjectDescription.cs @@ -21,6 +21,9 @@ public struct ProjectDescription public bool Prefs_SimPaused; public int Prefs_SimTargetStepsPerSecond; public int Prefs_SimStepsPerClockTick; + public int Prefs_WireRouting; + + // List of all player-created chips (in order of creation -- oldest first) public string[] AllCustomChipNames; diff --git a/Assets/Scripts/Game/Elements/WireInstance.cs b/Assets/Scripts/Game/Elements/WireInstance.cs index afe28ec4..35db063c 100644 --- a/Assets/Scripts/Game/Elements/WireInstance.cs +++ b/Assets/Scripts/Game/Elements/WireInstance.cs @@ -207,11 +207,26 @@ public void SetWirePoint(Vector2 p, int i) public void SetWirePointWithSnapping(Vector2 p, int i, Vector2 straightLineRefPoint) { if (Project.ActiveProject.ShouldSnapToGrid) p = GridHelper.SnapToGrid(p, true, true); - if (Project.ActiveProject.ForceStraightWires) p = GridHelper.ForceStraightLine(straightLineRefPoint, p); + if (Project.ActiveProject.ForceStraightWires && !Project.ActiveProject.ShouldRouteWires) p = GridHelper.ForceStraightLine(straightLineRefPoint, p); + if (Project.ActiveProject.ForceStraightWires && Project.ActiveProject.ShouldRouteWires && WirePoints.Count > 3) + { + // If routing wires, we need to route the wire to the new point + Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), p); + p = points[1]; + SetWirePoint((points[0]), WirePoints.Count - 2); + } + else if (Project.ActiveProject.ShouldRouteWires && WirePoints.Count > 2) + { + Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), p); + p = points[1]; + SetWirePoint((points[0]), WirePoints.Count - 2); + } SetWirePoint(p, i); } + + public void SetLastWirePoint(Vector2 p) { SetWirePointWithSnapping(p, WirePoints.Count - 1, GetWirePoint(WirePoints.Count - 2)); diff --git a/Assets/Scripts/Game/Helpers/GridHelper.cs b/Assets/Scripts/Game/Helpers/GridHelper.cs index 63df3ee2..a324a315 100644 --- a/Assets/Scripts/Game/Helpers/GridHelper.cs +++ b/Assets/Scripts/Game/Helpers/GridHelper.cs @@ -52,5 +52,33 @@ public static Vector2 ForceStraightLine(Vector2 prev, Vector2 curr) return prev + offset; } + + public static Vector2[] RouteWire(Vector2 prev, Vector2 curr) + { + Vector2[] points = new Vector2[2]; + Vector2 offset = curr - prev; + + if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)) + { + // Horizontal mode: move horizontally, then 45-degree diagonal to curr + float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y)); + float signX = Mathf.Sign(offset.x); + float signY = Mathf.Sign(offset.y); + Vector2 bend = new Vector2(curr.x - diagLen * signX, prev.y); + points[0] = bend; + points[1] = curr; + } + else + { + // Vertical mode: move vertically, then 45-degree diagonal to curr + float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y)); + float signX = Mathf.Sign(offset.x); + float signY = Mathf.Sign(offset.y); + Vector2 bend = new Vector2(prev.x, curr.y - diagLen * signY); + points[0] = bend; + points[1] = curr; + } + return points; + } } } \ No newline at end of file diff --git a/Assets/Scripts/Game/Interaction/ChipInteractionController.cs b/Assets/Scripts/Game/Interaction/ChipInteractionController.cs index da9491d3..193d09de 100644 --- a/Assets/Scripts/Game/Interaction/ChipInteractionController.cs +++ b/Assets/Scripts/Game/Interaction/ChipInteractionController.cs @@ -465,6 +465,7 @@ void HandleLeftMouseDown() else if (CanAddWirePoint()) { WireToPlace.AddWirePoint(InputHelper.MousePosWorld); + if(Project.ActiveProject.ShouldRouteWires) WireToPlace.AddWirePoint(InputHelper.MousePosWorld); } } // Place subchip / devpin diff --git a/Assets/Scripts/Game/Project/Project.cs b/Assets/Scripts/Game/Project/Project.cs index e2a5be6e..d6aca55a 100644 --- a/Assets/Scripts/Game/Project/Project.cs +++ b/Assets/Scripts/Game/Project/Project.cs @@ -480,6 +480,9 @@ public void ToggleGridDisplay() public bool ShouldSnapToGrid => KeyboardShortcuts.SnapModeHeld || (description.Prefs_Snapping == 1 && ShowGrid) || description.Prefs_Snapping == 2; public bool ForceStraightWires => KeyboardShortcuts.StraightLineModeHeld || (description.Prefs_StraightWires == 1 && ShowGrid) || description.Prefs_StraightWires == 2; + public bool ShouldRouteWires => description.Prefs_WireRouting == 1 || description.Prefs_WireRouting == 2; + + public void NotifyExit() { diff --git a/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs b/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs index 13909b2b..24e9fdb2 100644 --- a/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs +++ b/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs @@ -45,6 +45,13 @@ public static class PreferencesMenu "Always" }; + static readonly string[] WireRoutingOptions = + { + "Off", + "90°", + "45°" + }; + static readonly string[] SimulationStatusOptions = { "Active", @@ -60,6 +67,7 @@ public static class PreferencesMenu static readonly UIHandle ID_GridDisplay = new("PREFS_GridDisplay"); static readonly UIHandle ID_Snapping = new("PREFS_Snapping"); static readonly UIHandle ID_StraightWires = new("PREFS_StraightWires"); + static readonly UIHandle ID_WireRouting = new("PREFS_WireRouting"); static readonly UIHandle ID_SimStatus = new("PREFS_SimStatus"); static readonly UIHandle ID_SimFrequencyField = new("PREFS_SimTickTarget"); static readonly UIHandle ID_ClockSpeedInput = new("PREFS_ClockSpeed"); @@ -102,6 +110,7 @@ public static void DrawMenu(Project project) DrawHeader("EDITING:"); int snappingMode = DrawNextWheel("Snap to grid", SnappingOptions, ID_Snapping); int straightWireMode = DrawNextWheel("Straight wires", StraightWireOptions, ID_StraightWires); + int wireRoutingMode = DrawNextWheel("Wire routing", WireRoutingOptions, ID_WireRouting); DrawHeader("SIMULATION:"); bool pauseSim = MenuHelper.LabeledOptionsWheel(simStatusLabel, labelCol, labelPosCurr, entrySize, ID_SimStatus, SimulationStatusOptions, settingFieldSize.x, true) == 1; @@ -136,6 +145,7 @@ public static void DrawMenu(Project project) project.description.Prefs_ChipPinNamesDisplayMode = chipPinNamesMode; project.description.Prefs_GridDisplayMode = gridDisplayMode; project.description.Prefs_Snapping = snappingMode; + project.description.Prefs_WireRouting = wireRoutingMode; project.description.Prefs_StraightWires = straightWireMode; project.description.Prefs_SimTargetStepsPerSecond = targetSimTicksPerSecond; project.description.Prefs_SimStepsPerClockTick = clockSpeed; @@ -206,6 +216,7 @@ static void UpdateUIFromDescription() UI.GetWheelSelectorState(ID_GridDisplay).index = projDesc.Prefs_GridDisplayMode; UI.GetWheelSelectorState(ID_Snapping).index = projDesc.Prefs_Snapping; UI.GetWheelSelectorState(ID_StraightWires).index = projDesc.Prefs_StraightWires; + UI.GetWheelSelectorState(ID_WireRouting).index = projDesc.Prefs_WireRouting; UI.GetWheelSelectorState(ID_SimStatus).index = projDesc.Prefs_SimPaused ? 1 : 0; // -- Input fields UI.GetInputFieldState(ID_SimFrequencyField).SetText(projDesc.Prefs_SimTargetStepsPerSecond + "", false); diff --git a/TestData/Projects/MainTest/ProjectDescription.json b/TestData/Projects/MainTest/ProjectDescription.json index 6d1823ff..4fdc01a5 100644 --- a/TestData/Projects/MainTest/ProjectDescription.json +++ b/TestData/Projects/MainTest/ProjectDescription.json @@ -1,17 +1,18 @@ { "ProjectName": "MainTest", - "DLSVersion_LastSaved": "2.1.5", + "DLSVersion_LastSaved": "2.1.6", "DLSVersion_EarliestCompatible": "2.0.0", - "CreationTime": "2025-03-14T18:23:30.404+01:00", - "LastSaveTime": "2025-05-04T09:15:41.061+02:00", + "CreationTime": "2025-03-14T19:23:30.404+02:00", + "LastSaveTime": "2025-05-23T21:01:50.361+02:00", "Prefs_MainPinNamesDisplayMode": 2, "Prefs_ChipPinNamesDisplayMode": 1, "Prefs_GridDisplayMode": 1, - "Prefs_Snapping": 0, - "Prefs_StraightWires": 0, + "Prefs_Snapping": 2, + "Prefs_StraightWires": 2, "Prefs_SimPaused": false, "Prefs_SimTargetStepsPerSecond": 150, "Prefs_SimStepsPerClockTick": 6, + "Prefs_WireRouting": 1, "AllCustomChipNames":[ "AND", "D-LATCH", From 6c090ff7a704750c1c8d18c3d49454d137acb11d Mon Sep 17 00:00:00 2001 From: jandrec Date: Sat, 24 May 2025 11:28:02 +0200 Subject: [PATCH 4/9] Simplified some logic, fixed issues Simplified the implementation so that most of the logic is being done in WireInstance and GridHelper. Completely removed changes to ChipInteraction Controller Fixed some bugs with the initial implementation too The router now turns on and off with the forceStraightWires flag Also, the router is completely inactive when editing wires, which I believe is more practical Finally, the router works from the first pin it's placed, originally, it would only work once a point on the wire has been placed down --- Assets/Scripts/Game/Elements/WireInstance.cs | 25 +++++++++++-------- .../Interaction/ChipInteractionController.cs | 1 - 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Assets/Scripts/Game/Elements/WireInstance.cs b/Assets/Scripts/Game/Elements/WireInstance.cs index 35db063c..eb3c53ec 100644 --- a/Assets/Scripts/Game/Elements/WireInstance.cs +++ b/Assets/Scripts/Game/Elements/WireInstance.cs @@ -63,7 +63,10 @@ public WireInstance(ConnectionInfo firstConnectionInfo, int spawnOrder) bitCount = firstPin.bitCount; originalWireConnectionPoint = firstConnectionInfo.connectionPoint; WirePoints.Add(GetAttachmentPoint(firstConnectionInfo)); - WirePoints.Add(WirePoints[0]); // end point to be controlled by mouse during placement mode + + if (Project.ActiveProject.ShouldRouteWires) WirePoints.Add(WirePoints[0]); // add second point to allow routing wires + + WirePoints.Add(WirePoints[WirePoints.Count - 1]); // end point to be controlled by mouse during placement mode BitWires = new BitWire[(int)bitCount]; this.spawnOrder = spawnOrder; @@ -207,20 +210,15 @@ public void SetWirePoint(Vector2 p, int i) public void SetWirePointWithSnapping(Vector2 p, int i, Vector2 straightLineRefPoint) { if (Project.ActiveProject.ShouldSnapToGrid) p = GridHelper.SnapToGrid(p, true, true); - if (Project.ActiveProject.ForceStraightWires && !Project.ActiveProject.ShouldRouteWires) p = GridHelper.ForceStraightLine(straightLineRefPoint, p); - if (Project.ActiveProject.ForceStraightWires && Project.ActiveProject.ShouldRouteWires && WirePoints.Count > 3) + if (Project.ActiveProject.ForceStraightWires && (!Project.ActiveProject.ShouldRouteWires || IsFullyConnected) ) p = GridHelper.ForceStraightLine(straightLineRefPoint, p); + if (Project.ActiveProject.ForceStraightWires && Project.ActiveProject.ShouldRouteWires && WirePoints.Count > 2 && !IsFullyConnected) { // If routing wires, we need to route the wire to the new point Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), p); p = points[1]; - SetWirePoint((points[0]), WirePoints.Count - 2); - } - else if (Project.ActiveProject.ShouldRouteWires && WirePoints.Count > 2) - { - Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), p); - p = points[1]; - SetWirePoint((points[0]), WirePoints.Count - 2); + SetWirePoint(points[0], WirePoints.Count - 2); } + SetWirePoint(p, i); } @@ -232,7 +230,12 @@ public void SetLastWirePoint(Vector2 p) SetWirePointWithSnapping(p, WirePoints.Count - 1, GetWirePoint(WirePoints.Count - 2)); } - public void AddWirePoint(Vector2 p) => WirePoints.Add(p); + public void AddWirePoint(Vector2 p) + { + WirePoints.Add(p); + if (!Project.ActiveProject.ShouldRouteWires) return; + WirePoints.Add(p); + } public void DeleteWirePoint(int i) { diff --git a/Assets/Scripts/Game/Interaction/ChipInteractionController.cs b/Assets/Scripts/Game/Interaction/ChipInteractionController.cs index 193d09de..da9491d3 100644 --- a/Assets/Scripts/Game/Interaction/ChipInteractionController.cs +++ b/Assets/Scripts/Game/Interaction/ChipInteractionController.cs @@ -465,7 +465,6 @@ void HandleLeftMouseDown() else if (CanAddWirePoint()) { WireToPlace.AddWirePoint(InputHelper.MousePosWorld); - if(Project.ActiveProject.ShouldRouteWires) WireToPlace.AddWirePoint(InputHelper.MousePosWorld); } } // Place subchip / devpin From 6a06d83ea2ad57451d198a82fe328a4d6872bb08 Mon Sep 17 00:00:00 2001 From: jandrec Date: Sat, 24 May 2025 11:58:32 +0200 Subject: [PATCH 5/9] Fixes Fixed an alignment issue where, when a wire is finished placing, it get's snapped to the target pin, this would result in a slight misalignment of the second to last point on the wire. The fix now reroutes that point when finishing placement of a wire. Removed 90 degree routing mode for now, until I am able to implement it later. Want to look at curved wires first. --- Assets/Scripts/Game/Elements/WireInstance.cs | 14 +++++++++++--- Assets/Scripts/Game/Project/Project.cs | 2 +- .../Scripts/Graphics/UI/Menus/PreferencesMenu.cs | 1 - 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/Game/Elements/WireInstance.cs b/Assets/Scripts/Game/Elements/WireInstance.cs index eb3c53ec..2ef8ad8e 100644 --- a/Assets/Scripts/Game/Elements/WireInstance.cs +++ b/Assets/Scripts/Game/Elements/WireInstance.cs @@ -66,7 +66,7 @@ public WireInstance(ConnectionInfo firstConnectionInfo, int spawnOrder) if (Project.ActiveProject.ShouldRouteWires) WirePoints.Add(WirePoints[0]); // add second point to allow routing wires - WirePoints.Add(WirePoints[WirePoints.Count - 1]); // end point to be controlled by mouse during placement mode + WirePoints.Add(WirePoints[^1]); // end point to be controlled by mouse during placement mode BitWires = new BitWire[(int)bitCount]; this.spawnOrder = spawnOrder; @@ -120,9 +120,17 @@ public void FinishPlacingWire(ConnectionInfo connection) PinInstance endPin = connection.pin; WirePoints[^1] = endPin.GetWorldPos(); + if (Project.ActiveProject.ShouldRouteWires) + { + // If routing wires, we need to router the second last point to the end pin + Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 2), endPin.GetWorldPos()); + SetWirePoint(points[0], WirePoints.Count - 2); + + } + // If wire connection started out at another wire, it is not known (until now when we have the end pin) whether that - // initial connection should be to the source or target pin of that wire (so correct if needed) - ConnectionInfo correctedFirstConnection = FirstConnectionInfo; + // initial connection should be to the source or target pin of that wire (so correct if needed) + ConnectionInfo correctedFirstConnection = FirstConnectionInfo; if (FirstConnectionInfo.pin.IsSourcePin == endPin.IsSourcePin) // same type, must fix { Debug.Assert(FirstConnectionInfo.IsConnectedAtWire, "Connection is source->source or target->target, but connection didn't start from wire?!"); diff --git a/Assets/Scripts/Game/Project/Project.cs b/Assets/Scripts/Game/Project/Project.cs index d6aca55a..5188b234 100644 --- a/Assets/Scripts/Game/Project/Project.cs +++ b/Assets/Scripts/Game/Project/Project.cs @@ -480,7 +480,7 @@ public void ToggleGridDisplay() public bool ShouldSnapToGrid => KeyboardShortcuts.SnapModeHeld || (description.Prefs_Snapping == 1 && ShowGrid) || description.Prefs_Snapping == 2; public bool ForceStraightWires => KeyboardShortcuts.StraightLineModeHeld || (description.Prefs_StraightWires == 1 && ShowGrid) || description.Prefs_StraightWires == 2; - public bool ShouldRouteWires => description.Prefs_WireRouting == 1 || description.Prefs_WireRouting == 2; + public bool ShouldRouteWires => description.Prefs_WireRouting == 1; diff --git a/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs b/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs index 24e9fdb2..012a4921 100644 --- a/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs +++ b/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs @@ -48,7 +48,6 @@ public static class PreferencesMenu static readonly string[] WireRoutingOptions = { "Off", - "90°", "45°" }; From cc490b39b1b6a426504e9229160e22e970ea3f6f Mon Sep 17 00:00:00 2001 From: jandrec Date: Tue, 27 May 2025 16:00:43 +0200 Subject: [PATCH 6/9] More Fixes Fixed a bug with routing when last point was connected to a pin Releasing shift key returns wire to straight line mode --- Assets/Scripts/Game/Elements/WireInstance.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/Game/Elements/WireInstance.cs b/Assets/Scripts/Game/Elements/WireInstance.cs index 2ef8ad8e..e7d55c9c 100644 --- a/Assets/Scripts/Game/Elements/WireInstance.cs +++ b/Assets/Scripts/Game/Elements/WireInstance.cs @@ -122,8 +122,8 @@ public void FinishPlacingWire(ConnectionInfo connection) if (Project.ActiveProject.ShouldRouteWires) { - // If routing wires, we need to router the second last point to the end pin - Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 2), endPin.GetWorldPos()); + // If routing wires, we need to reroute the second last point to account for the end pin snapping to the grid or chip pin + Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), endPin.GetWorldPos()); SetWirePoint(points[0], WirePoints.Count - 2); } @@ -225,7 +225,14 @@ public void SetWirePointWithSnapping(Vector2 p, int i, Vector2 straightLineRefPo Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), p); p = points[1]; SetWirePoint(points[0], WirePoints.Count - 2); + } + else + { + // If button to route wires is not pressed (or released), set the last two points to be the same (overlapping points can be removed later) + if (i == WirePoints.Count - 1 && WirePoints.Count > 2) WirePoints[WirePoints.Count - 2] = p; + } + SetWirePoint(p, i); From 46db21cc11823417c32bc05637825d9e39c01a59 Mon Sep 17 00:00:00 2001 From: jandrec Date: Tue, 27 May 2025 16:39:37 +0200 Subject: [PATCH 7/9] Added 90-degree routing mode --- Assets/Scripts/Game/Elements/WireInstance.cs | 4 +- Assets/Scripts/Game/Helpers/GridHelper.cs | 51 ++++++++++++++++++- Assets/Scripts/Game/Project/Project.cs | 10 +++- .../Graphics/UI/Menus/PreferencesMenu.cs | 3 +- 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/Assets/Scripts/Game/Elements/WireInstance.cs b/Assets/Scripts/Game/Elements/WireInstance.cs index e7d55c9c..acf0bc4c 100644 --- a/Assets/Scripts/Game/Elements/WireInstance.cs +++ b/Assets/Scripts/Game/Elements/WireInstance.cs @@ -123,7 +123,7 @@ public void FinishPlacingWire(ConnectionInfo connection) if (Project.ActiveProject.ShouldRouteWires) { // If routing wires, we need to reroute the second last point to account for the end pin snapping to the grid or chip pin - Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), endPin.GetWorldPos()); + Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), endPin.GetWorldPos(), Project.ActiveProject.WireRoutingMode); SetWirePoint(points[0], WirePoints.Count - 2); } @@ -222,7 +222,7 @@ public void SetWirePointWithSnapping(Vector2 p, int i, Vector2 straightLineRefPo if (Project.ActiveProject.ForceStraightWires && Project.ActiveProject.ShouldRouteWires && WirePoints.Count > 2 && !IsFullyConnected) { // If routing wires, we need to route the wire to the new point - Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), p); + Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), p, Project.ActiveProject.WireRoutingMode); p = points[1]; SetWirePoint(points[0], WirePoints.Count - 2); diff --git a/Assets/Scripts/Game/Helpers/GridHelper.cs b/Assets/Scripts/Game/Helpers/GridHelper.cs index a324a315..a7cdbbfd 100644 --- a/Assets/Scripts/Game/Helpers/GridHelper.cs +++ b/Assets/Scripts/Game/Helpers/GridHelper.cs @@ -58,12 +58,13 @@ public static Vector2[] RouteWire(Vector2 prev, Vector2 curr) Vector2[] points = new Vector2[2]; Vector2 offset = curr - prev; + if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)) { + // Horizontal mode: move horizontally, then 45-degree diagonal to curr float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y)); float signX = Mathf.Sign(offset.x); - float signY = Mathf.Sign(offset.y); Vector2 bend = new Vector2(curr.x - diagLen * signX, prev.y); points[0] = bend; points[1] = curr; @@ -72,7 +73,6 @@ public static Vector2[] RouteWire(Vector2 prev, Vector2 curr) { // Vertical mode: move vertically, then 45-degree diagonal to curr float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y)); - float signX = Mathf.Sign(offset.x); float signY = Mathf.Sign(offset.y); Vector2 bend = new Vector2(prev.x, curr.y - diagLen * signY); points[0] = bend; @@ -80,5 +80,52 @@ public static Vector2[] RouteWire(Vector2 prev, Vector2 curr) } return points; } + + public static Vector2[] RouteWire(Vector2 prev, Vector2 curr, int mode) + { + Vector2[] points = new Vector2[2]; + Vector2 offset = curr - prev; + + if (mode == 1) + { + if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)) + { + + // Horizontal mode: move horizontally, then 45-degree diagonal to curr + float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y)); + float signX = Mathf.Sign(offset.x); + Vector2 bend = new Vector2(curr.x - diagLen * signX, prev.y); + points[0] = bend; + points[1] = curr; + } + else + { + // Vertical mode: move vertically, then 45-degree diagonal to curr + float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y)); + float signY = Mathf.Sign(offset.y); + Vector2 bend = new Vector2(prev.x, curr.y - diagLen * signY); + points[0] = bend; + points[1] = curr; + } + } else if (mode == 2) + { + if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)) + { + // Horizontal mode: move horizontally, then vertically to curr + Vector2 bend = new Vector2(curr.x, prev.y); + points[0] = bend; + points[1] = curr; + } + else + { + // Vertical mode: move vertically, then horizontally to curr + Vector2 bend = new Vector2(prev.x, curr.y); + points[0] = bend; + points[1] = curr; + } + } + + return points; + } } } \ No newline at end of file diff --git a/Assets/Scripts/Game/Project/Project.cs b/Assets/Scripts/Game/Project/Project.cs index 5188b234..9b3c9cfc 100644 --- a/Assets/Scripts/Game/Project/Project.cs +++ b/Assets/Scripts/Game/Project/Project.cs @@ -480,7 +480,15 @@ public void ToggleGridDisplay() public bool ShouldSnapToGrid => KeyboardShortcuts.SnapModeHeld || (description.Prefs_Snapping == 1 && ShowGrid) || description.Prefs_Snapping == 2; public bool ForceStraightWires => KeyboardShortcuts.StraightLineModeHeld || (description.Prefs_StraightWires == 1 && ShowGrid) || description.Prefs_StraightWires == 2; - public bool ShouldRouteWires => description.Prefs_WireRouting == 1; + public bool ShouldRouteWires => description.Prefs_WireRouting == 1 || description.Prefs_WireRouting == 2; + + public int WireRoutingMode => description.Prefs_WireRouting switch + { + 0 => 0, // No routing + 1 => 1, // 45-degree routing + 2 => 2, // 90-degree routing + _ => 0 // Default to no routing + }; diff --git a/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs b/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs index 012a4921..bed8a72a 100644 --- a/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs +++ b/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs @@ -48,7 +48,8 @@ public static class PreferencesMenu static readonly string[] WireRoutingOptions = { "Off", - "45°" + "45°", + "90°", }; static readonly string[] SimulationStatusOptions = From 70499535ed68ee3363e5d05f794bd1440f55f218 Mon Sep 17 00:00:00 2001 From: jandrec Date: Tue, 27 May 2025 17:05:34 +0200 Subject: [PATCH 8/9] Re-Implemented Curved Wire Bends Re-implemented the curved wire bends from earlier versions Just used Sebastian's existing function to do so. Straight or Curved wires can be selected in the preferences menu --- .../Description/Types/ProjectDescription.cs | 1 + Assets/Scripts/Game/Project/Project.cs | 7 ++++++- .../Scripts/Graphics/UI/Menus/PreferencesMenu.cs | 10 ++++++++++ Assets/Scripts/Graphics/World/DevSceneDrawer.cs | 4 ++-- Assets/Scripts/Graphics/World/WireDrawer.cs | 16 +++++++++++++++- 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Assets/Scripts/Description/Types/ProjectDescription.cs b/Assets/Scripts/Description/Types/ProjectDescription.cs index 3ff1f1c8..bc4b2cfb 100644 --- a/Assets/Scripts/Description/Types/ProjectDescription.cs +++ b/Assets/Scripts/Description/Types/ProjectDescription.cs @@ -22,6 +22,7 @@ public struct ProjectDescription public int Prefs_SimTargetStepsPerSecond; public int Prefs_SimStepsPerClockTick; public int Prefs_WireRouting; + public int Prefs_WireStyle; diff --git a/Assets/Scripts/Game/Project/Project.cs b/Assets/Scripts/Game/Project/Project.cs index 9b3c9cfc..d1a059a3 100644 --- a/Assets/Scripts/Game/Project/Project.cs +++ b/Assets/Scripts/Game/Project/Project.cs @@ -490,7 +490,12 @@ public void ToggleGridDisplay() _ => 0 // Default to no routing }; - + public int WireStyle => description.Prefs_WireStyle switch + { + 0 => 0, // Stright wires + 1 => 1, // Curved wires + _ => 0 // Default to normal wires + }; public void NotifyExit() { diff --git a/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs b/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs index bed8a72a..97395c7e 100644 --- a/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs +++ b/Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs @@ -52,6 +52,12 @@ public static class PreferencesMenu "90°", }; + static readonly string[] WireStyleOptions = + { + "Straight", + "Curved" + }; + static readonly string[] SimulationStatusOptions = { "Active", @@ -68,6 +74,7 @@ public static class PreferencesMenu static readonly UIHandle ID_Snapping = new("PREFS_Snapping"); static readonly UIHandle ID_StraightWires = new("PREFS_StraightWires"); static readonly UIHandle ID_WireRouting = new("PREFS_WireRouting"); + static readonly UIHandle ID_WireStyle = new("PREFS_WireStyle"); static readonly UIHandle ID_SimStatus = new("PREFS_SimStatus"); static readonly UIHandle ID_SimFrequencyField = new("PREFS_SimTickTarget"); static readonly UIHandle ID_ClockSpeedInput = new("PREFS_ClockSpeed"); @@ -107,6 +114,7 @@ public static void DrawMenu(Project project) int mainPinNamesMode = DrawNextWheel("Show I/O pin names", PinDisplayOptions, ID_MainPinNames); int chipPinNamesMode = DrawNextWheel("Show chip pin names", PinDisplayOptions, ID_ChipPinNames); int gridDisplayMode = DrawNextWheel(showGridLabel, GridDisplayOptions, ID_GridDisplay); + int wireStyleMode = DrawNextWheel("Wire style", WireStyleOptions, ID_WireStyle); DrawHeader("EDITING:"); int snappingMode = DrawNextWheel("Snap to grid", SnappingOptions, ID_Snapping); int straightWireMode = DrawNextWheel("Straight wires", StraightWireOptions, ID_StraightWires); @@ -147,6 +155,7 @@ public static void DrawMenu(Project project) project.description.Prefs_Snapping = snappingMode; project.description.Prefs_WireRouting = wireRoutingMode; project.description.Prefs_StraightWires = straightWireMode; + project.description.Prefs_WireStyle = wireStyleMode; project.description.Prefs_SimTargetStepsPerSecond = targetSimTicksPerSecond; project.description.Prefs_SimStepsPerClockTick = clockSpeed; project.description.Prefs_SimPaused = pauseSim; @@ -217,6 +226,7 @@ static void UpdateUIFromDescription() UI.GetWheelSelectorState(ID_Snapping).index = projDesc.Prefs_Snapping; UI.GetWheelSelectorState(ID_StraightWires).index = projDesc.Prefs_StraightWires; UI.GetWheelSelectorState(ID_WireRouting).index = projDesc.Prefs_WireRouting; + UI.GetWheelSelectorState(ID_WireStyle).index = projDesc.Prefs_WireStyle; UI.GetWheelSelectorState(ID_SimStatus).index = projDesc.Prefs_SimPaused ? 1 : 0; // -- Input fields UI.GetInputFieldState(ID_SimFrequencyField).SetText(projDesc.Prefs_SimTargetStepsPerSecond + "", false); diff --git a/Assets/Scripts/Graphics/World/DevSceneDrawer.cs b/Assets/Scripts/Graphics/World/DevSceneDrawer.cs index eefb8307..66f601d0 100644 --- a/Assets/Scripts/Graphics/World/DevSceneDrawer.cs +++ b/Assets/Scripts/Graphics/World/DevSceneDrawer.cs @@ -749,7 +749,7 @@ static void DrawSingleBitWire(WireInstance wire) // Draw Color col = wire.GetColour(0); - float interactSqrDst = WireDrawer.DrawWireStraight(wire.BitWires[0].Points, thickness, col, mousePos); + float interactSqrDst = WireDrawer.DrawWire(wire.BitWires[0].Points, thickness, col, mousePos); // Draw connection point (if connects to wire) if (wire.ConnectedWire != null) @@ -782,7 +782,7 @@ static void DrawMultiBitWire(WireInstance wire) { WireInstance.BitWire bitWire = wire.BitWires[bitIndex]; Color col = wire.GetColour(bitIndex); - float sqrInteractDst = WireDrawer.DrawWireStraight(bitWire.Points, thickness, col, mousePos); + float sqrInteractDst = WireDrawer.DrawWire(bitWire.Points, thickness, col, mousePos); if (canInteract && sqrInteractDst < sqrDstThreshold) InteractionState.NotifyElementUnderMouse(wire); } } diff --git a/Assets/Scripts/Graphics/World/WireDrawer.cs b/Assets/Scripts/Graphics/World/WireDrawer.cs index a136ea4e..830c355a 100644 --- a/Assets/Scripts/Graphics/World/WireDrawer.cs +++ b/Assets/Scripts/Graphics/World/WireDrawer.cs @@ -1,3 +1,4 @@ +using DLS.Game; using Seb.Helpers; using Seb.Vis; using UnityEngine; @@ -5,7 +6,20 @@ namespace DLS.Graphics { public static class WireDrawer - { + { + + public static float DrawWire(Vector2[] points, float thickness, Color col, Vector2 interactPos) + { + if (Project.ActiveProject.WireStyle == 0) // 0 = straight, 1 = curved + { + return DrawWireStraight(points, thickness, col, interactPos); + } + else + { + return DrawWireCurved(points, thickness, col, interactPos); + } + } + public static float DrawWireStraight(Vector2[] points, float thickness, Color col, Vector2 interactPos) { float interactSqrDst = float.MaxValue; From a79b3545de191c227d5220abd9978ff20593e0bb Mon Sep 17 00:00:00 2001 From: jandrec Date: Wed, 28 May 2025 14:28:36 +0200 Subject: [PATCH 9/9] More tweaks to routing Most of this commit revolves around setting up some things to change the direction in which wires attempt to terminate The idea is to make it possible to change between wires ending on a vertical or horizontal line when being routed --- Assets/Scripts/Game/Elements/WireInstance.cs | 78 +++++++-- Assets/Scripts/Game/Helpers/GridHelper.cs | 159 ++++++++++++++---- .../Interaction/ChipInteractionController.cs | 35 +++- .../Game/Interaction/KeyboardShortcuts.cs | 1 + Assets/Scripts/Game/Project/Project.cs | 2 + 5 files changed, 222 insertions(+), 53 deletions(-) diff --git a/Assets/Scripts/Game/Elements/WireInstance.cs b/Assets/Scripts/Game/Elements/WireInstance.cs index acf0bc4c..ea79104e 100644 --- a/Assets/Scripts/Game/Elements/WireInstance.cs +++ b/Assets/Scripts/Game/Elements/WireInstance.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using DLS.Description; using DLS.Graphics; using UnityEngine; @@ -64,7 +65,12 @@ public WireInstance(ConnectionInfo firstConnectionInfo, int spawnOrder) originalWireConnectionPoint = firstConnectionInfo.connectionPoint; WirePoints.Add(GetAttachmentPoint(firstConnectionInfo)); - if (Project.ActiveProject.ShouldRouteWires) WirePoints.Add(WirePoints[0]); // add second point to allow routing wires + if (Project.ActiveProject.ShouldRouteWires) + { + WirePoints.Add(WirePoints[0]); // add second point to allow routing wires + WirePoints.Add(WirePoints[0]); // add third point to allow routing wires + //WirePoints.Add(WirePoints[0]); // add fourth point to allow routing wires + } WirePoints.Add(WirePoints[^1]); // end point to be controlled by mouse during placement mode @@ -123,8 +129,9 @@ public void FinishPlacingWire(ConnectionInfo connection) if (Project.ActiveProject.ShouldRouteWires) { // If routing wires, we need to reroute the second last point to account for the end pin snapping to the grid or chip pin - Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), endPin.GetWorldPos(), Project.ActiveProject.WireRoutingMode); - SetWirePoint(points[0], WirePoints.Count - 2); + Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), endPin.GetWorldPos(), Project.ActiveProject.WireRoutingMode, GetWireLength()); + SetWirePoint(points[0], WirePoints.Count - 3); + SetWirePoint(points[1], WirePoints.Count - 2); } @@ -133,7 +140,7 @@ public void FinishPlacingWire(ConnectionInfo connection) ConnectionInfo correctedFirstConnection = FirstConnectionInfo; if (FirstConnectionInfo.pin.IsSourcePin == endPin.IsSourcePin) // same type, must fix { - Debug.Assert(FirstConnectionInfo.IsConnectedAtWire, "Connection is source->source or target->target, but connection didn't start from wire?!"); + System.Diagnostics.Debug.Assert(FirstConnectionInfo.IsConnectedAtWire, "Connection is source->source or target->target, but connection didn't start from wire?!"); if (endPin.IsSourcePin) correctedFirstConnection.pin = GetBusCorrectedTargetPin(FirstConnectionInfo.connectedWire); else correctedFirstConnection.pin = FirstConnectionInfo.connectedWire.SourcePin; } @@ -218,27 +225,55 @@ public void SetWirePoint(Vector2 p, int i) public void SetWirePointWithSnapping(Vector2 p, int i, Vector2 straightLineRefPoint) { if (Project.ActiveProject.ShouldSnapToGrid) p = GridHelper.SnapToGrid(p, true, true); - if (Project.ActiveProject.ForceStraightWires && (!Project.ActiveProject.ShouldRouteWires || IsFullyConnected) ) p = GridHelper.ForceStraightLine(straightLineRefPoint, p); - if (Project.ActiveProject.ForceStraightWires && Project.ActiveProject.ShouldRouteWires && WirePoints.Count > 2 && !IsFullyConnected) + if (Project.ActiveProject.ForceStraightWires && (!Project.ActiveProject.ShouldRouteWires || IsFullyConnected)) p = GridHelper.ForceStraightLine(straightLineRefPoint, p); + if (Project.ActiveProject.ForceStraightWires && Project.ActiveProject.ShouldRouteWires /*&& WirePoints.Count > 3*/ && !IsFullyConnected) { // If routing wires, we need to route the wire to the new point - Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), p, Project.ActiveProject.WireRoutingMode); - p = points[1]; - SetWirePoint(points[0], WirePoints.Count - 2); + int refPointIndex = 0; + if (WirePoints.Count > 4) + { + refPointIndex = WirePoints.Count - 4; // use the point before the last two points + } + + Vector2[] points = GridHelper.RouteWire(GetWirePoint(refPointIndex), p, Project.ActiveProject.WireRoutingMode, GetWireLength()); + p = points[2]; + SetWirePoint(points[0], WirePoints.Count - 3); + SetWirePoint(points[1], WirePoints.Count - 2); + //SetWirePoint(points[2], WirePoints.Count - 2); } - else + else if (Project.ActiveProject.ShouldRouteWires) { // If button to route wires is not pressed (or released), set the last two points to be the same (overlapping points can be removed later) - if (i == WirePoints.Count - 1 && WirePoints.Count > 2) WirePoints[WirePoints.Count - 2] = p; + if (WirePoints.Count > 2) WirePoints[^2] = p; + if (WirePoints.Count > 3) WirePoints[^3] = p; + if (WirePoints.Count > 4) WirePoints[^4] = p; + } - - SetWirePoint(p, i); } + /* + public void SetWirePointWithSnapping(Vector2 p, int i, Vector2 straightLineRefPoint, ChipInteractionController.RotationTarget r) + { + if (Project.ActiveProject.ShouldSnapToGrid) p = GridHelper.SnapToGrid(p, true, true); + if (Project.ActiveProject.ForceStraightWires && (!Project.ActiveProject.ShouldRouteWires || IsFullyConnected)) p = GridHelper.ForceStraightLine(straightLineRefPoint, p); + if (Project.ActiveProject.ForceStraightWires && Project.ActiveProject.ShouldRouteWires && WirePoints.Count > 3 && !IsFullyConnected) + { + // If routing wires, we need to route the wire to the new point + Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 4), p, Project.ActiveProject.WireRoutingMode, GetWireLength()); + p = points[2]; + SetWirePoint(points[0], WirePoints.Count - 3); + SetWirePoint(points[1], WirePoints.Count - 2); + } + else + { + // If button to route wires is not pressed (or released), set the last two points to be the same (overlapping points can be removed later) + if (WirePoints.Count > 2) WirePoints[^2] = p; + } - + SetWirePoint(p, i); + }*/ public void SetLastWirePoint(Vector2 p) { @@ -250,6 +285,8 @@ public void AddWirePoint(Vector2 p) WirePoints.Add(p); if (!Project.ActiveProject.ShouldRouteWires) return; WirePoints.Add(p); + WirePoints.Add(p); + //WirePoints.Add(p); // add 3 extra points to allow routing wires } public void DeleteWirePoint(int i) @@ -278,7 +315,7 @@ public bool RemoveLastPoint() // Connection info for the end of this wire that connects to another wire public ref ConnectionInfo GetWireConnectionInfo() { - Debug.Assert(ConnectedWire != null, "No connected wire?"); + System.Diagnostics.Debug.Assert(ConnectedWire != null, "No connected wire?"); return ref SourceConnectionInfo.IsConnectedAtWire ? ref SourceConnectionInfo : ref TargetConnectionInfo; } @@ -356,6 +393,17 @@ public Color GetColour(int bitIndex) return col; } + + public float GetWireLength() + { + float length = 0f; + for (int i = 0; i < WirePoints.Count - 1; i++) + { + length += Vector2.Distance(WirePoints[i], WirePoints[i + 1]); + } + + return length; + } public static Vector2 ClosestPointOnLineSegment(Vector2 p, Vector2 a1, Vector2 a2) { diff --git a/Assets/Scripts/Game/Helpers/GridHelper.cs b/Assets/Scripts/Game/Helpers/GridHelper.cs index a7cdbbfd..15be05cb 100644 --- a/Assets/Scripts/Game/Helpers/GridHelper.cs +++ b/Assets/Scripts/Game/Helpers/GridHelper.cs @@ -1,4 +1,5 @@ -using UnityEngine; +using System; +using UnityEngine; using static DLS.Graphics.DrawSettings; namespace DLS.Game @@ -53,41 +54,15 @@ public static Vector2 ForceStraightLine(Vector2 prev, Vector2 curr) return prev + offset; } - public static Vector2[] RouteWire(Vector2 prev, Vector2 curr) - { - Vector2[] points = new Vector2[2]; - Vector2 offset = curr - prev; - - - if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)) - { - - // Horizontal mode: move horizontally, then 45-degree diagonal to curr - float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y)); - float signX = Mathf.Sign(offset.x); - Vector2 bend = new Vector2(curr.x - diagLen * signX, prev.y); - points[0] = bend; - points[1] = curr; - } - else - { - // Vertical mode: move vertically, then 45-degree diagonal to curr - float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y)); - float signY = Mathf.Sign(offset.y); - Vector2 bend = new Vector2(prev.x, curr.y - diagLen * signY); - points[0] = bend; - points[1] = curr; - } - return points; - } public static Vector2[] RouteWire(Vector2 prev, Vector2 curr, int mode) { Vector2[] points = new Vector2[2]; Vector2 offset = curr - prev; + if (mode == 1) - { + { if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)) { @@ -99,15 +74,16 @@ public static Vector2[] RouteWire(Vector2 prev, Vector2 curr, int mode) points[1] = curr; } else - { - // Vertical mode: move vertically, then 45-degree diagonal to curr - float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y)); - float signY = Mathf.Sign(offset.y); - Vector2 bend = new Vector2(prev.x, curr.y - diagLen * signY); - points[0] = bend; - points[1] = curr; + { + // Vertical mode: move vertically, then 45-degree diagonal to curr + float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y)); + float signY = Mathf.Sign(offset.y); + Vector2 bend = new Vector2(prev.x, curr.y - diagLen * signY); + points[0] = bend; + points[1] = curr; + } } - } else if (mode == 2) + else if (mode == 2) { if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)) { @@ -127,5 +103,114 @@ public static Vector2[] RouteWire(Vector2 prev, Vector2 curr, int mode) return points; } + + public static Vector2[] RouteWire(Vector2 prev, Vector2 curr, int mode, float length) + { + Vector2[] points = new Vector2[4]; + Vector2 offset = curr - prev; + float endLength = Project.ActiveProject.WireTerminationLength; + + if (mode == 1) + { + if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)) + { + // Horizontal mode: move horizontally, then 45-degree diagonal to bend2 then horizontally to curr + float signX = MathF.Sign(offset.x); + Vector2 bend0 = new(prev.x + endLength * signX, prev.y); + Vector2 bend2 = new(curr.x - endLength * signX, curr.y); + + float diagLen = Mathf.Abs(offset.y); + + Vector2 bend1 = new(bend2.x - diagLen * signX, prev.y); + /*points[0] = bend0; + points[1] = bend1; + points[2] = bend2; + points[3] = curr;*/ + + points[0] = bend1; + points[1] = bend2; + points[2] = curr; + } + else + { + // Vertical mode: move vertically, then 45-degree diagonal to bend2 then vertically to curr + float signY = Mathf.Sign(offset.y); + Vector2 bend0 = new(prev.x, prev.y + endLength * signY); + Vector2 bend2 = new(curr.x, curr.y - endLength * signY); + + float diagLen = Mathf.Abs(offset.x); + + Vector2 bend1 = new(prev.x, bend2.y - diagLen * signY); + /*points[0] = bend0; + points[1] = bend1; + points[2] = bend2; + points[3] = curr;*/ + points[0] = bend1; + points[1] = bend2; + points[2] = curr; + } + } + else if (mode == 2) + { + if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)) + { + float signX = Mathf.Sign(offset.x); + Vector2 bend0 = new(prev.x + endLength * signX, prev.y); + Vector2 bend2 = new(curr.x - endLength * signX, curr.y); + // Horizontal mode: move horizontally, then vertically to curr + Vector2 bend1 = new(bend2.x, prev.y); + /*points[0] = bend0; + points[1] = bend1; + points[2] = bend2; + points[3] = curr;*/ + points[0] = bend1; + points[1] = bend2; + points[2] = curr; + } + else + { + float signY = Mathf.Sign(offset.y); + Vector2 bend0 = new(prev.x, prev.y + endLength * signY); + Vector2 bend2 = new(curr.x, curr.y - endLength * signY); + // Vertical mode: move vertically, then horizontally to curr + Vector2 bend1 = new(prev.x, bend2.y); + /*points[0] = bend0; + points[1] = bend1; + points[2] = bend2; + points[3] = curr;*/ + points[0] = bend1; + points[1] = bend2; + points[2] = curr; + } + } + + return points; + } + + public static Vector2[] RouteWire(Vector2 prev, Vector2 curr, int mode, ChipInteractionController.RotationTarget r) + { + return null; // Placeholder for future implementation + + if (mode == 1) + { + //Mode 1: 45-degree bend + if (r == ChipInteractionController.RotationTarget.Left || r == ChipInteractionController.RotationTarget.Right) + { + //Wire must end on a horizontal line + Vector2[] points = new Vector2[2]; + Vector2 offset = curr - prev; + + if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)) + { + + } + } + + } + else if (mode == 2) + { + + } + } } } \ No newline at end of file diff --git a/Assets/Scripts/Game/Interaction/ChipInteractionController.cs b/Assets/Scripts/Game/Interaction/ChipInteractionController.cs index da9491d3..1bba2755 100644 --- a/Assets/Scripts/Game/Interaction/ChipInteractionController.cs +++ b/Assets/Scripts/Game/Interaction/ChipInteractionController.cs @@ -23,6 +23,7 @@ public class ChipInteractionController IMoveable[] Obstacles; // Obstacles are the non-selected items when a group of elements is being moved public Vector2 SelectionBoxStartPos; StraightLineMoveState straightLineMoveState; + RotationTarget wireRotationTarget = RotationTarget.Right; // Determines how wires are routed by attempting to terminate the wire in the specified direction bool hasExittedMultiModeSincePlacementStart; // ---- Wire edit state ---- @@ -113,7 +114,7 @@ public bool CanCompleteWireConnection(PinInstance endPin, WireInstance wireToCon } public static bool IsSelected(IMoveable element) => element.IsSelected; - + public void Delete(IMoveable element) { DeleteElements(new List(new[] { element })); @@ -190,6 +191,17 @@ void HandleKeyboardInput() if (KeyboardShortcuts.UndoShortcutTriggered) ActiveDevChip.UndoController.TryUndo(); else if (KeyboardShortcuts.RedoShortcutTriggered) ActiveDevChip.UndoController.TryRedo(); + if (KeyboardShortcuts.RotateShortcutTriggered) + { + if (IsCreatingWire) + { + wireRotationTarget = NextRotationTarget(wireRotationTarget); + } + else + { + wireRotationTarget = RotationTarget.Right; // Reset rotation target to right when not placing wire + } + } if (!KeyboardShortcuts.StraightLineModeHeld) straightLineMoveState = StraightLineMoveState.None; @@ -1062,5 +1074,26 @@ enum StraightLineMoveState Horizontal, Vertical } + + + public enum RotationTarget + { + Right, + Down, + Left, + Up + } + + static RotationTarget NextRotationTarget(RotationTarget current) + { + return current switch + { + RotationTarget.Right => RotationTarget.Down, + RotationTarget.Down => RotationTarget.Left, + RotationTarget.Left => RotationTarget.Up, + RotationTarget.Up => RotationTarget.Right, + _ => RotationTarget.Right, + }; + } } } \ No newline at end of file diff --git a/Assets/Scripts/Game/Interaction/KeyboardShortcuts.cs b/Assets/Scripts/Game/Interaction/KeyboardShortcuts.cs index 21f9dbd0..516563f7 100644 --- a/Assets/Scripts/Game/Interaction/KeyboardShortcuts.cs +++ b/Assets/Scripts/Game/Interaction/KeyboardShortcuts.cs @@ -33,6 +33,7 @@ public static class KeyboardShortcuts public static bool DeleteShortcutTriggered => InputHelper.IsKeyDownThisFrame(KeyCode.Backspace) || InputHelper.IsKeyDownThisFrame(KeyCode.Delete); public static bool SimNextStepShortcutTriggered => InputHelper.IsKeyDownThisFrame(KeyCode.Space) && !InputHelper.CtrlIsHeld; public static bool SimPauseToggleShortcutTriggered => CtrlShortcutTriggered(KeyCode.Space); + public static bool RotateShortcutTriggered => InputHelper.IsKeyDownThisFrame(KeyCode.R); // ---- Dev shortcuts ---- public static bool OpenSaveDataFolderShortcutTriggered => InputHelper.IsKeyDownThisFrame(KeyCode.O) && InputHelper.CtrlIsHeld && InputHelper.ShiftIsHeld && InputHelper.AltIsHeld; diff --git a/Assets/Scripts/Game/Project/Project.cs b/Assets/Scripts/Game/Project/Project.cs index d1a059a3..0a3cff28 100644 --- a/Assets/Scripts/Game/Project/Project.cs +++ b/Assets/Scripts/Game/Project/Project.cs @@ -482,6 +482,8 @@ public void ToggleGridDisplay() public bool ForceStraightWires => KeyboardShortcuts.StraightLineModeHeld || (description.Prefs_StraightWires == 1 && ShowGrid) || description.Prefs_StraightWires == 2; public bool ShouldRouteWires => description.Prefs_WireRouting == 1 || description.Prefs_WireRouting == 2; + public float WireTerminationLength = 0.5f; //Terminate Wires at 20% of its length when routing + public int WireRoutingMode => description.Prefs_WireRouting switch { 0 => 0, // No routing