From 9ea9079ac6415da69f0eea787d70591dfd0830aa Mon Sep 17 00:00:00 2001 From: lemon Date: Wed, 24 Sep 2025 18:37:46 +0500 Subject: [PATCH] serialization: You can load .json files that don't have terminal positions in them now --- Models/Components/CustomComponent.cs | 2 +- Models/Components/DLatch.cs | 2 +- Models/Components/Decoder.cs | 2 +- Models/Components/Demultiplexer.cs | 2 +- Models/Components/Encoder.cs | 2 +- Models/Components/JKLatch.cs | 2 +- Models/Components/Multiplexer.cs | 2 +- Models/Components/SRLatch.cs | 2 +- Models/Components/TLatch.cs | 2 +- Models/Components/Wire.cs | 3 - Models/Core/Component.cs | 5 ++ Models/Core/Gate.cs | 2 +- Models/DTOs/CircuitDto.cs | 1 + Models/DTOs/ComponentDto.cs | 11 +++- Models/DTOs/TerminalDto.cs | 5 +- Services/XmlSerializationService.cs | 92 ---------------------------- 16 files changed, 27 insertions(+), 110 deletions(-) delete mode 100644 Services/XmlSerializationService.cs diff --git a/Models/Components/CustomComponent.cs b/Models/Components/CustomComponent.cs index 15067b8..c5cb21e 100644 --- a/Models/Components/CustomComponent.cs +++ b/Models/Components/CustomComponent.cs @@ -135,7 +135,7 @@ public override void DrawSelection(DrawingContext ctx) ); } - protected void AddTerminalPoints() + public override void AddTerminalPoints(bool notMode = false) { // Snap to multiples of Grid Spacing Point SnapToGrid(Point pt) diff --git a/Models/Components/DLatch.cs b/Models/Components/DLatch.cs index c6fc68d..9b3543e 100644 --- a/Models/Components/DLatch.cs +++ b/Models/Components/DLatch.cs @@ -72,7 +72,7 @@ public override void DrawSelection(DrawingContext ctx) ); } - private void AddTerminalPoints() + public override void AddTerminalPoints(bool notMode = false) { Point SnapToGrid(Point pt) { diff --git a/Models/Components/Decoder.cs b/Models/Components/Decoder.cs index 7ee35a4..7cce6a3 100644 --- a/Models/Components/Decoder.cs +++ b/Models/Components/Decoder.cs @@ -77,7 +77,7 @@ public override void DrawSelection(DrawingContext ctx) ); } - protected void AddTerminalPoints() + public override void AddTerminalPoints(bool notMode = false) { Point SnapToGrid(Point pt) { diff --git a/Models/Components/Demultiplexer.cs b/Models/Components/Demultiplexer.cs index 2d92c73..3233c34 100644 --- a/Models/Components/Demultiplexer.cs +++ b/Models/Components/Demultiplexer.cs @@ -82,7 +82,7 @@ public override void DrawSelection(DrawingContext ctx) } // Layout: one data input on the left (centered vertically), selection lines at bottom, many outputs on right - protected void AddTerminalPoints() + public override void AddTerminalPoints(bool notMode = false) { // Helper to snap to nearest grid Point SnapToGrid(Point pt) diff --git a/Models/Components/Encoder.cs b/Models/Components/Encoder.cs index 0eb5b92..80a630d 100644 --- a/Models/Components/Encoder.cs +++ b/Models/Components/Encoder.cs @@ -85,7 +85,7 @@ public override void DrawSelection(DrawingContext ctx) ); } - protected void AddTerminalPoints() + public override void AddTerminalPoints(bool notMode = false) { Point SnapToGrid(Point pt) { diff --git a/Models/Components/JKLatch.cs b/Models/Components/JKLatch.cs index 324bbfd..29b831c 100644 --- a/Models/Components/JKLatch.cs +++ b/Models/Components/JKLatch.cs @@ -83,7 +83,7 @@ public override void DrawSelection(DrawingContext ctx) ); } - private void AddTerminalPoints() + public override void AddTerminalPoints(bool notMode = false) { Point SnapToGrid(Point pt) { diff --git a/Models/Components/Multiplexer.cs b/Models/Components/Multiplexer.cs index 89c8958..e304631 100644 --- a/Models/Components/Multiplexer.cs +++ b/Models/Components/Multiplexer.cs @@ -75,7 +75,7 @@ public override void DrawSelection(DrawingContext ctx) } // Inputs are on the left, output on right, selection lines at bottom - protected void AddTerminalPoints() + public override void AddTerminalPoints(bool notMode = false) { // Helper to snap to nearest multiple of 10 Point SnapToGrid(Point pt) diff --git a/Models/Components/SRLatch.cs b/Models/Components/SRLatch.cs index c60b03b..885f518 100644 --- a/Models/Components/SRLatch.cs +++ b/Models/Components/SRLatch.cs @@ -83,7 +83,7 @@ public override void DrawSelection(DrawingContext ctx) ); } - private void AddTerminalPoints() + public override void AddTerminalPoints(bool notMode = false) { Point SnapToGrid(Point pt) { diff --git a/Models/Components/TLatch.cs b/Models/Components/TLatch.cs index 8ba1e9e..61bb9bd 100644 --- a/Models/Components/TLatch.cs +++ b/Models/Components/TLatch.cs @@ -76,7 +76,7 @@ public override void DrawSelection(DrawingContext ctx) ); } - private void AddTerminalPoints() + public override void AddTerminalPoints(bool notMode = false) { Point SnapToGrid(Point pt) { diff --git a/Models/Components/Wire.cs b/Models/Components/Wire.cs index 4b77b7f..4bb79b2 100644 --- a/Models/Components/Wire.cs +++ b/Models/Components/Wire.cs @@ -38,10 +38,7 @@ public Wire() : base(0, 0) IsCommitted = false; } - // DTO pattern for serializing - - public void AddPoint(Point point) { Points.Add(point); diff --git a/Models/Core/Component.cs b/Models/Core/Component.cs index 9e78660..d84774d 100644 --- a/Models/Core/Component.cs +++ b/Models/Core/Component.cs @@ -80,6 +80,11 @@ public virtual bool HitTest(Point point) return new Rect(0,0,Width,Height).Contains(point); } + public virtual void AddTerminalPoints(bool notMode = false) + { + + } + public override void Render(DrawingContext context) { // Applies rotation to the drawing diff --git a/Models/Core/Gate.cs b/Models/Core/Gate.cs index f929896..f1458e4 100644 --- a/Models/Core/Gate.cs +++ b/Models/Core/Gate.cs @@ -148,7 +148,7 @@ public override void DrawSelection(DrawingContext ctx) // The points are added differently for NOT-variants (NAND, NOT, NOR, XNOR) // notMode: Extra wire length to account for the bubble - protected void AddTerminalPoints(bool notMode = false) + public override void AddTerminalPoints(bool notMode = false) { double spacing = Height / (InputLineCount + 1); diff --git a/Models/DTOs/CircuitDto.cs b/Models/DTOs/CircuitDto.cs index cfb5bf9..46973ad 100644 --- a/Models/DTOs/CircuitDto.cs +++ b/Models/DTOs/CircuitDto.cs @@ -38,6 +38,7 @@ public static List ToCircuit(CircuitDto circuit) { terminal.Wires = terminal.Wires.Select(w => wireDict[w.Id]).ToList(); } + } diff --git a/Models/DTOs/ComponentDto.cs b/Models/DTOs/ComponentDto.cs index 57f48be..c5cb3ae 100644 --- a/Models/DTOs/ComponentDto.cs +++ b/Models/DTOs/ComponentDto.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using Avalonia; using Avalonia.Controls; using IRis.Models.Components; using IRis.Models.Core; @@ -78,8 +79,14 @@ public static ComponentDto ToDto(Component c) result.Rotation = dto.Rotation; result.IsSelected = dto.IsSelected; result.StoredStates = dto.StoredStates; - result.Terminals = dto.Terminals.Select(p => TerminalDto.ToTerminal(p)).ToArray(); - + + // Add terminal positions here by calling the function that generates them + result.AddTerminalPoints(); + for (int i = 0; i < dto.Terminals.Count; i++) + { + result.Terminals[i].Wires = TerminalDto.ToTerminal(dto.Terminals[i]).Wires; + } + Canvas.SetLeft(result, dto.X); Canvas.SetTop(result, dto.Y); diff --git a/Models/DTOs/TerminalDto.cs b/Models/DTOs/TerminalDto.cs index ac044b0..6a8a297 100644 --- a/Models/DTOs/TerminalDto.cs +++ b/Models/DTOs/TerminalDto.cs @@ -9,7 +9,6 @@ namespace IRis.Models.DTOs; public class TerminalDto { - public Point Position { get; set; } public List ConnectedWireIds { get; set; } @@ -17,7 +16,6 @@ public static TerminalDto ToDto(Terminal t) { return new TerminalDto() { - Position = t.Position, ConnectedWireIds = t.Wires.Select(w => w.Id).ToList(), }; } @@ -26,7 +24,8 @@ public static TerminalDto ToDto(Terminal t) // This is required for post processing public static Terminal ToTerminal(TerminalDto dto) { - return new Terminal(dto.Position) + // Placeholder position + return new Terminal(new Point(1,1)) { Wires = dto.ConnectedWireIds.Select(p => new Wire(){Id = p}).ToList() }; diff --git a/Services/XmlSerializationService.cs b/Services/XmlSerializationService.cs deleted file mode 100644 index f883da9..0000000 --- a/Services/XmlSerializationService.cs +++ /dev/null @@ -1,92 +0,0 @@ -// using System; -// using System.Collections.Generic; -// using System.IO; -// using System.Linq; -// using System.Threading.Tasks; -// using System.Xml; -// using System.Xml.Serialization; -// using Avalonia; -// using Avalonia.Controls; -// using IRis.Models; -// using IRis.Models.Components; -// using IRis.Models.Core; -// -// namespace IRis.Services; -// -// public class XmlSerializationService : ISerializationService -// { -// public void SerializeComponents(Simulation simulation, string? filePath) -// { -// if (filePath == null) -// { -// Console.WriteLine("No file selected!"); -// return; -// } -// -// XmlSerializer serializer = new XmlSerializer(typeof(CircuitDto)); -// List dtoList = simulation.Components.Select(p => p.ToDto()).ToList(); -// CircuitDto circuit = new CircuitDto() { Components = dtoList }; -// -// -// StreamWriter writer = new StreamWriter(filePath); -// serializer.Serialize(writer, circuit); -// -// writer.Close(); -// } -// -// -// public List DeserializeComponentsAsync(string xmlContent) -// { -// var serializer = new XmlSerializer(typeof(CircuitDto)); -// -// using (var reader = new StringReader(xmlContent)) -// { -// CircuitDto dto = (CircuitDto)serializer.Deserialize(reader)!; -// // Convert to components -// List components = dto.Components -// .Select(p => ISerializationService.ConvertDtoToComponent(p)) -// .ToList(); -// -// // Connect the wires to components -// foreach (Component thisComponent in components) -// { -// if (thisComponent is Wire || thisComponent.Terminals == null) continue; -// foreach (Terminal terminal in thisComponent.Terminals) -// { -// List connectedWires = GetConnectedWires(components, thisComponent, terminal); -// if (connectedWires == null) break; -// // Add connected wires to terminals -// foreach (Wire wire in connectedWires) -// { -// terminal.AddWire(wire); -// } -// } -// } -// return components; -// } -// } -// -// private static List GetConnectedWires(List components, Component thisComponent, Terminal terminal) -// { -// // Get absolute position of the terminal -// Point absolutePosition = new( -// terminal.Position.X + Canvas.GetLeft(thisComponent), -// terminal.Position.Y + Canvas.GetTop(thisComponent) -// ); -// List connectedWires = []; // Initialize an empty list -// -// foreach (Component component in components) -// { -// if (component is not Wire wire) continue; // We're looking for wires -// -// foreach (Point point in wire.Points) -// { -// if (point == absolutePosition) // If the wire point is exactly on the terminal -// { -// connectedWires.Add(wire); -// } -// } -// } -// return connectedWires; -// } -// } \ No newline at end of file