Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core;
using Avalonia.Data.Core.Plugins;
using System.Linq;
using Avalonia.Markup.Xaml;
using IRis.Models;
using IRis.ViewModels;
using IRis.Views;
using System.Linq;

namespace IRis;

Expand All @@ -22,13 +21,13 @@ public override void OnFrameworkInitializationCompleted()
// NOTE: IMPLEMENT A THEME SYSTEM
// Set the default theme as 'Dark'
// ThemeManager.ChangeTheme(Application.Current, "Dark");

// Create a CanvasService object and pass its reference
// to both MainWindow and MainWindowViewModel
// This is the main service object for drawings and simulation
Simulation simulation = new Simulation();


if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
// Avoid duplicate validations from both Avalonia and the CommunityToolkit.
Expand Down
22 changes: 11 additions & 11 deletions Models/Commands.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using IRis.Models.Core;
using IRis.Models.Components;
using IRis.Models.Core;
using System;
using System.Collections.Generic;
using System.Linq;

namespace IRis.Models.Commands
{
Expand Down Expand Up @@ -56,7 +56,7 @@ public DeleteComponentsCommand(Canvas canvas, List<Component> components, List<C
_canvas = canvas;
_components = components;
_deletedComponents = new List<Component>(selectedComponents);
_originalPositions = selectedComponents.Select(c =>
_originalPositions = selectedComponents.Select(c =>
new Point(Canvas.GetLeft(c), Canvas.GetTop(c))).ToList();
}

Expand All @@ -75,7 +75,7 @@ public void Undo()
{
var component = _deletedComponents[i];
var position = _originalPositions[i];

Canvas.SetLeft(component, position.X);
Canvas.SetTop(component, position.Y);
_canvas.Children.Add(component);
Expand Down Expand Up @@ -147,11 +147,11 @@ public MoveComponentsCommand(List<Component> components, Point offset)
{
_components = new List<Component>(components);
_canvas = null!;

// Calculate original and new positions based on offset
_originalPositions = _components.Select(c =>
_originalPositions = _components.Select(c =>
new Point(Canvas.GetLeft(c), Canvas.GetTop(c))).ToList();
_newPositions = _originalPositions.Select(pos =>
_newPositions = _originalPositions.Select(pos =>
new Point(pos.X + offset.X, pos.Y + offset.Y)).ToList();
}

Expand All @@ -161,9 +161,9 @@ public MoveComponentsCommand(Canvas canvas, List<Component> components, List<Poi
_canvas = canvas;
_components = new List<Component>(components);
_newPositions = new List<Point>(newPositions);

// Store original positions for undo
_originalPositions = _components.Select(c =>
_originalPositions = _components.Select(c =>
new Point(Canvas.GetLeft(c), Canvas.GetTop(c))).ToList();
}

Expand Down
6 changes: 3 additions & 3 deletions Models/ComponentManagers.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using IRis.Models.Components;
using IRis.Models.Core;
using System;
using System.Collections.Generic;
using System.Linq;

namespace IRis.Models;

Expand Down
9 changes: 4 additions & 5 deletions Models/Components/AndGate.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Avalonia;
using Avalonia.Media;
using IRis.Models.Core;
using System;
using System.Linq;


namespace IRis.Models.Components;
namespace IRis.Models.Components;


public class AndGate : Gate
Expand All @@ -20,7 +19,7 @@ public override void Draw(DrawingContext ctx)
// 3. Draw terminals (lines + circles)
DrawTerminals(ctx);

this.DrawAnd(ctx);
DrawAnd(ctx);

base.Draw(ctx);
}
Expand All @@ -36,10 +35,10 @@ public override void ComputeOutput()
if (!inputTerminals.All(t => t.Wires.Any()) || !outputTerminal.Wires.Any()) return;

// For each input terminal, OR together all connected wire values
var inputValues = inputTerminals.Select(terminal =>
var inputValues = inputTerminals.Select(terminal =>
terminal.Wires.Any(w => w.Value == LogicState.High)).ToList();

if (inputValues.All(value => value == true)) // All inputs must be high
if (inputValues.All(value => value)) // All inputs must be high
{
foreach (Wire wire in outputTerminal.Wires)
{
Expand Down
72 changes: 25 additions & 47 deletions Models/Components/CustomComponent.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Avalonia;
using Avalonia.Media;
using IRis.Models.Core;
using IRis.Models;
using IRis.Services;
using System;
using System.Collections.Generic;

namespace IRis.Models.Components;

public class CustomComponent : Component, IOutputProvider
public class CustomComponent : CircuitComponent, IOutputProvider
{
protected string ComponentName;
public int InputCount;
protected int OutputCount;
protected List<CircuitFormulaConversionService.CircuitFormula> OutputFormulas;
public CustomComponent(string name, int inputCount = 2, int outputCount = 1,

public CustomComponent(string name, int inputCount = 2, int outputCount = 1,
List<CircuitFormulaConversionService.CircuitFormula>? outputFormulas = null,
double width = ComponentDefaults.DefaultMuxWidth,
double height = ComponentDefaults.DefaultMuxHeight)
Expand All @@ -28,7 +25,7 @@ public CustomComponent(string name, int inputCount = 2, int outputCount = 1,
InputCount = inputCount;
OutputCount = outputCount;
OutputFormulas = outputFormulas ?? [];

// Calculate dimensions based on input/output count
int maxTerminals = Math.Max(InputCount, OutputCount);
// maxTerminals = (maxTerminals % 2 == 1) ? maxTerminals + 1 : maxTerminals;
Expand All @@ -55,13 +52,13 @@ public void ComputeOutput()
{
// Get the formula for this output
var formula = OutputFormulas[outputIndex];

// Build input dictionary with actual values from terminals
var inputs = GetInputValues();

// Evaluate the formula using the service method
bool result = CircuitFormulaConversionService.EvaluateFormula(formula.Formula, inputs);

// Set the output terminal value
int outputTerminalIndex = InputCount + outputIndex;
if (Terminals![outputTerminalIndex].Wire != null)
Expand All @@ -84,49 +81,49 @@ public void ComputeOutput()
private Dictionary<string, bool> GetInputValues()
{
var inputs = new Dictionary<string, bool>();

// Get values from input terminals and map them to formula variables
for (int i = 0; i < InputCount; i++)
{
bool inputValue = false;

// Check if terminal has a wire and get its value
if (i < Terminals!.Length && Terminals[i].Wire != null)
{
inputValue = Terminals[i].Wire!.Value == LogicState.High;
}

// Add both Input_X format (used by formula service) and A,B,C format
inputs[$"Input_{i + 1}"] = inputValue;

// Also support A, B, C... format for compatibility
char inputVar = (char)('A' + i);
inputs[inputVar.ToString()] = inputValue;
}

return inputs;
}

public override void Draw(DrawingContext ctx)
{
// Component Body
ctx.DrawRectangle(ComponentDefaults.GateFillBrush,
ComponentDefaults.GatePen,
ComponentDefaults.GatePen,
new Rect(0, 0, Width, Height));

DrawTerminalsAndLabels(ctx);

base.Draw(ctx);
}

public override void DrawSelection(DrawingContext ctx)
{
double expandX = ComponentDefaults.TerminalWireLength + ComponentDefaults.TerminalRadius;
double expandY = ComponentDefaults.TerminalRadius;

ctx.DrawRectangle(
ComponentDefaults.SelectionBrush,
ComponentDefaults.SelectionPen,
ComponentDefaults.SelectionBrush,
ComponentDefaults.SelectionPen,
new Rect(
-expandX,
-expandY,
Expand Down Expand Up @@ -189,14 +186,8 @@ protected void DrawTerminalsAndLabels(DrawingContext ctx)

// Draw input label
char inputLabel = (char)('A' + i);
var text = new FormattedText(
inputLabel.ToString(),
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
ComponentDefaults.LabelTypeface,
ComponentDefaults.LabelSize,
ComponentDefaults.LabelBrush
);
var text = inputLabel.ToString().CreateFormattedText();

ctx.DrawText(text, new Point(4.5, Terminals[i].Position.Y - 6));
}

Expand All @@ -216,14 +207,8 @@ protected void DrawTerminalsAndLabels(DrawingContext ctx)

// Draw output label (Y for single output, Y0, Y1, etc. for multiple)
string outputLabel = OutputCount == 1 ? "Y" : $"Y{i}";
var text = new FormattedText(
outputLabel,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
ComponentDefaults.LabelTypeface,
ComponentDefaults.LabelSize,
ComponentDefaults.LabelBrush
);
var text = outputLabel.CreateFormattedText();

ctx.DrawText(text, new Point(Width - 20, Terminals[terminalIndex].Position.Y - 6));
}

Expand All @@ -232,14 +217,7 @@ protected void DrawTerminalsAndLabels(DrawingContext ctx)
{
string displayText = ComponentName;

var formulaText = new FormattedText(
displayText,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
ComponentDefaults.LabelTypeface,
ComponentDefaults.LabelSize,
ComponentDefaults.LabelBrush
);
var formulaText = displayText.CreateFormattedText();

double textX = (Width - formulaText.Width) / 2;
double textY = (Height - formulaText.Height) / 2;
Expand Down
Loading
Loading