Skip to content

Commit 3527ad6

Browse files
committed
Implement basic straight wire routing capability
1 parent 7aeb66d commit 3527ad6

File tree

10 files changed

+153
-6
lines changed

10 files changed

+153
-6
lines changed

.vscode/extensions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"visualstudiotoolsforunity.vstuc"
4+
]
5+
}

.vscode/launch.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Attach to Unity",
6+
"type": "vstuc",
7+
"request": "attach"
8+
}
9+
]
10+
}

.vscode/settings.json

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"files.exclude": {
3+
"**/.DS_Store": true,
4+
"**/.git": true,
5+
"**/.vs": true,
6+
"**/.gitmodules": true,
7+
"**/.vsconfig": true,
8+
"**/*.booproj": true,
9+
"**/*.pidb": true,
10+
"**/*.suo": true,
11+
"**/*.user": true,
12+
"**/*.userprefs": true,
13+
"**/*.unityproj": true,
14+
"**/*.dll": true,
15+
"**/*.exe": true,
16+
"**/*.pdf": true,
17+
"**/*.mid": true,
18+
"**/*.midi": true,
19+
"**/*.wav": true,
20+
"**/*.gif": true,
21+
"**/*.ico": true,
22+
"**/*.jpg": true,
23+
"**/*.jpeg": true,
24+
"**/*.png": true,
25+
"**/*.psd": true,
26+
"**/*.tga": true,
27+
"**/*.tif": true,
28+
"**/*.tiff": true,
29+
"**/*.3ds": true,
30+
"**/*.3DS": true,
31+
"**/*.fbx": true,
32+
"**/*.FBX": true,
33+
"**/*.lxo": true,
34+
"**/*.LXO": true,
35+
"**/*.ma": true,
36+
"**/*.MA": true,
37+
"**/*.obj": true,
38+
"**/*.OBJ": true,
39+
"**/*.asset": true,
40+
"**/*.cubemap": true,
41+
"**/*.flare": true,
42+
"**/*.mat": true,
43+
"**/*.meta": true,
44+
"**/*.prefab": true,
45+
"**/*.unity": true,
46+
"build/": true,
47+
"Build/": true,
48+
"Library/": true,
49+
"library/": true,
50+
"obj/": true,
51+
"Obj/": true,
52+
"Logs/": true,
53+
"logs/": true,
54+
"ProjectSettings/": true,
55+
"UserSettings/": true,
56+
"temp/": true,
57+
"Temp/": true
58+
},
59+
"files.associations": {
60+
"*.asset": "yaml",
61+
"*.meta": "yaml",
62+
"*.prefab": "yaml",
63+
"*.unity": "yaml",
64+
},
65+
"explorer.fileNesting.enabled": true,
66+
"explorer.fileNesting.patterns": {
67+
"*.sln": "*.csproj",
68+
},
69+
"dotnet.defaultSolution": "Digital-Logic-Sim-Tests.sln"
70+
}

Assets/Scripts/Description/Types/ProjectDescription.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public struct ProjectDescription
2121
public bool Prefs_SimPaused;
2222
public int Prefs_SimTargetStepsPerSecond;
2323
public int Prefs_SimStepsPerClockTick;
24+
public int Prefs_WireRouting;
25+
26+
2427

2528
// List of all player-created chips (in order of creation -- oldest first)
2629
public string[] AllCustomChipNames;

Assets/Scripts/Game/Elements/WireInstance.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,26 @@ public void SetWirePoint(Vector2 p, int i)
207207
public void SetWirePointWithSnapping(Vector2 p, int i, Vector2 straightLineRefPoint)
208208
{
209209
if (Project.ActiveProject.ShouldSnapToGrid) p = GridHelper.SnapToGrid(p, true, true);
210-
if (Project.ActiveProject.ForceStraightWires) p = GridHelper.ForceStraightLine(straightLineRefPoint, p);
210+
if (Project.ActiveProject.ForceStraightWires && !Project.ActiveProject.ShouldRouteWires) p = GridHelper.ForceStraightLine(straightLineRefPoint, p);
211+
if (Project.ActiveProject.ForceStraightWires && Project.ActiveProject.ShouldRouteWires && WirePoints.Count > 3)
212+
{
213+
// If routing wires, we need to route the wire to the new point
214+
Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), p);
215+
p = points[1];
216+
SetWirePoint((points[0]), WirePoints.Count - 2);
217+
}
218+
else if (Project.ActiveProject.ShouldRouteWires && WirePoints.Count > 2)
219+
{
220+
Vector2[] points = GridHelper.RouteWire(GetWirePoint(WirePoints.Count - 3), p);
221+
p = points[1];
222+
SetWirePoint((points[0]), WirePoints.Count - 2);
223+
}
211224

212225
SetWirePoint(p, i);
213226
}
214227

228+
229+
215230
public void SetLastWirePoint(Vector2 p)
216231
{
217232
SetWirePointWithSnapping(p, WirePoints.Count - 1, GetWirePoint(WirePoints.Count - 2));

Assets/Scripts/Game/Helpers/GridHelper.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,33 @@ public static Vector2 ForceStraightLine(Vector2 prev, Vector2 curr)
5252

5353
return prev + offset;
5454
}
55+
56+
public static Vector2[] RouteWire(Vector2 prev, Vector2 curr)
57+
{
58+
Vector2[] points = new Vector2[2];
59+
Vector2 offset = curr - prev;
60+
61+
if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y))
62+
{
63+
// Horizontal mode: move horizontally, then 45-degree diagonal to curr
64+
float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y));
65+
float signX = Mathf.Sign(offset.x);
66+
float signY = Mathf.Sign(offset.y);
67+
Vector2 bend = new Vector2(curr.x - diagLen * signX, prev.y);
68+
points[0] = bend;
69+
points[1] = curr;
70+
}
71+
else
72+
{
73+
// Vertical mode: move vertically, then 45-degree diagonal to curr
74+
float diagLen = Mathf.Min(Mathf.Abs(offset.x), Mathf.Abs(offset.y));
75+
float signX = Mathf.Sign(offset.x);
76+
float signY = Mathf.Sign(offset.y);
77+
Vector2 bend = new Vector2(prev.x, curr.y - diagLen * signY);
78+
points[0] = bend;
79+
points[1] = curr;
80+
}
81+
return points;
82+
}
5583
}
5684
}

Assets/Scripts/Game/Interaction/ChipInteractionController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ void HandleLeftMouseDown()
465465
else if (CanAddWirePoint())
466466
{
467467
WireToPlace.AddWirePoint(InputHelper.MousePosWorld);
468+
if(Project.ActiveProject.ShouldRouteWires) WireToPlace.AddWirePoint(InputHelper.MousePosWorld);
468469
}
469470
}
470471
// Place subchip / devpin

Assets/Scripts/Game/Project/Project.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,9 @@ public void ToggleGridDisplay()
480480

481481
public bool ShouldSnapToGrid => KeyboardShortcuts.SnapModeHeld || (description.Prefs_Snapping == 1 && ShowGrid) || description.Prefs_Snapping == 2;
482482
public bool ForceStraightWires => KeyboardShortcuts.StraightLineModeHeld || (description.Prefs_StraightWires == 1 && ShowGrid) || description.Prefs_StraightWires == 2;
483+
public bool ShouldRouteWires => description.Prefs_WireRouting == 1 || description.Prefs_WireRouting == 2;
484+
485+
483486

484487
public void NotifyExit()
485488
{

Assets/Scripts/Graphics/UI/Menus/PreferencesMenu.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public static class PreferencesMenu
4545
"Always"
4646
};
4747

48+
static readonly string[] WireRoutingOptions =
49+
{
50+
"Off",
51+
"90°",
52+
"45°"
53+
};
54+
4855
static readonly string[] SimulationStatusOptions =
4956
{
5057
"Active",
@@ -60,6 +67,7 @@ public static class PreferencesMenu
6067
static readonly UIHandle ID_GridDisplay = new("PREFS_GridDisplay");
6168
static readonly UIHandle ID_Snapping = new("PREFS_Snapping");
6269
static readonly UIHandle ID_StraightWires = new("PREFS_StraightWires");
70+
static readonly UIHandle ID_WireRouting = new("PREFS_WireRouting");
6371
static readonly UIHandle ID_SimStatus = new("PREFS_SimStatus");
6472
static readonly UIHandle ID_SimFrequencyField = new("PREFS_SimTickTarget");
6573
static readonly UIHandle ID_ClockSpeedInput = new("PREFS_ClockSpeed");
@@ -102,6 +110,7 @@ public static void DrawMenu(Project project)
102110
DrawHeader("EDITING:");
103111
int snappingMode = DrawNextWheel("Snap to grid", SnappingOptions, ID_Snapping);
104112
int straightWireMode = DrawNextWheel("Straight wires", StraightWireOptions, ID_StraightWires);
113+
int wireRoutingMode = DrawNextWheel("Wire routing", WireRoutingOptions, ID_WireRouting);
105114

106115
DrawHeader("SIMULATION:");
107116
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)
136145
project.description.Prefs_ChipPinNamesDisplayMode = chipPinNamesMode;
137146
project.description.Prefs_GridDisplayMode = gridDisplayMode;
138147
project.description.Prefs_Snapping = snappingMode;
148+
project.description.Prefs_WireRouting = wireRoutingMode;
139149
project.description.Prefs_StraightWires = straightWireMode;
140150
project.description.Prefs_SimTargetStepsPerSecond = targetSimTicksPerSecond;
141151
project.description.Prefs_SimStepsPerClockTick = clockSpeed;
@@ -206,6 +216,7 @@ static void UpdateUIFromDescription()
206216
UI.GetWheelSelectorState(ID_GridDisplay).index = projDesc.Prefs_GridDisplayMode;
207217
UI.GetWheelSelectorState(ID_Snapping).index = projDesc.Prefs_Snapping;
208218
UI.GetWheelSelectorState(ID_StraightWires).index = projDesc.Prefs_StraightWires;
219+
UI.GetWheelSelectorState(ID_WireRouting).index = projDesc.Prefs_WireRouting;
209220
UI.GetWheelSelectorState(ID_SimStatus).index = projDesc.Prefs_SimPaused ? 1 : 0;
210221
// -- Input fields
211222
UI.GetInputFieldState(ID_SimFrequencyField).SetText(projDesc.Prefs_SimTargetStepsPerSecond + "", false);

TestData/Projects/MainTest/ProjectDescription.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
{
22
"ProjectName": "MainTest",
3-
"DLSVersion_LastSaved": "2.1.5",
3+
"DLSVersion_LastSaved": "2.1.6",
44
"DLSVersion_EarliestCompatible": "2.0.0",
5-
"CreationTime": "2025-03-14T18:23:30.404+01:00",
6-
"LastSaveTime": "2025-05-04T09:15:41.061+02:00",
5+
"CreationTime": "2025-03-14T19:23:30.404+02:00",
6+
"LastSaveTime": "2025-05-23T21:01:50.361+02:00",
77
"Prefs_MainPinNamesDisplayMode": 2,
88
"Prefs_ChipPinNamesDisplayMode": 1,
99
"Prefs_GridDisplayMode": 1,
10-
"Prefs_Snapping": 0,
11-
"Prefs_StraightWires": 0,
10+
"Prefs_Snapping": 2,
11+
"Prefs_StraightWires": 2,
1212
"Prefs_SimPaused": false,
1313
"Prefs_SimTargetStepsPerSecond": 150,
1414
"Prefs_SimStepsPerClockTick": 6,
15+
"Prefs_WireRouting": 1,
1516
"AllCustomChipNames":[
1617
"AND",
1718
"D-LATCH",

0 commit comments

Comments
 (0)