From e6be35488422f4773910911cf81e216a2eebd764 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Wed, 4 Oct 2023 16:16:33 +0200 Subject: [PATCH] Make connection context menu more intuitive --- .../Simulation/LogicGateSimulator.cs | 4 +- .../LogicGates/Interfaces/LogicGate.cs | 43 +++++++++++-------- .../Utilities/LogicGatesManager.cs | 42 +++++++++--------- 3 files changed, 49 insertions(+), 40 deletions(-) diff --git a/StoneRed.LogicSimulator/Simulation/LogicGateSimulator.cs b/StoneRed.LogicSimulator/Simulation/LogicGateSimulator.cs index c7c8602..ea34e65 100644 --- a/StoneRed.LogicSimulator/Simulation/LogicGateSimulator.cs +++ b/StoneRed.LogicSimulator/Simulation/LogicGateSimulator.cs @@ -75,7 +75,7 @@ internal class LogicGateSimulator logicGatesUpdated = logicGates.TryRemove(logicGate.Id, out _); } - public void SimulationThread() + private void SimulationThread() { int tps = 0; float iterations = 10000; @@ -118,10 +118,12 @@ internal class LogicGateSimulator ClockCalibrating = percentage > 5; } } + for (int i = 0; i < logicGatesArray.Length; i++) { logicGatesArray[i].NextTick(); } + for (int i = 0; i < logicGatesArray.Length; i++) { logicGatesArray[i].Update(); diff --git a/StoneRed.LogicSimulator/Simulation/LogicGates/Interfaces/LogicGate.cs b/StoneRed.LogicSimulator/Simulation/LogicGates/Interfaces/LogicGate.cs index fe1edc9..1a63acc 100644 --- a/StoneRed.LogicSimulator/Simulation/LogicGates/Interfaces/LogicGate.cs +++ b/StoneRed.LogicSimulator/Simulation/LogicGates/Interfaces/LogicGate.cs @@ -4,7 +4,6 @@ using StoneRed.LogicSimulator.Utilities; using System; using System.Collections.Generic; -using System.Linq; namespace StoneRed.LogicSimulator.Simulation.LogicGates.Interfaces; #pragma warning disable S112 // General exceptions should never be thrown @@ -16,13 +15,23 @@ internal abstract class LogicGate private int newInput; private int output; private int cachedOutput; + public IReadOnlyList LogicGateConnections => logicGateConnections.AsReadOnly(); + public abstract int OutputCount { get; set; } + public abstract int InputCount { get; set; } internal GraphicsDevice? GraphicsDevice { get; set; } internal LogicGateWorldData WorldData { get; init; } = new LogicGateWorldData(); internal ulong Id { get; set; } - public IReadOnlyList LogicGateConnections => logicGateConnections.AsReadOnly(); - public abstract int OutputCount { get; set; } - public abstract int InputCount { get; set; } + public override bool Equals(object? obj) + { + return obj is LogicGate gate && + Id == gate.Id; + } + + public override int GetHashCode() + { + return Id.GetHashCode(); + } internal void NextTick() { @@ -64,19 +73,25 @@ internal abstract class LogicGate } } - internal bool IsConnectedTo(LogicGate logicGate) + internal bool IsConnectedTo(LogicGate logicGate, int? inputIndex = null, int? outputIndex = null) { lock (logicGateConnections) { - return logicGateConnections.Any(c => c.LogicGate.Id == logicGate.Id); + return logicGateConnections.Exists(c => + c.LogicGate.Id == logicGate.Id + && (!inputIndex.HasValue || c.InputIndex == inputIndex) + && (!outputIndex.HasValue || c.OutputIndex == outputIndex)); } } - internal void Disconnect(LogicGate logicGate) + internal void Disconnect(LogicGate logicGate, int? inputIndex = null, int? outputIndex = null) { lock (logicGateConnections) { - int index = logicGateConnections.FindIndex(c => c.LogicGate.Id == logicGate.Id); + int index = logicGateConnections.FindIndex(c => + c.LogicGate.Id == logicGate.Id && + (!inputIndex.HasValue || c.InputIndex == inputIndex) && + (!outputIndex.HasValue || c.OutputIndex == outputIndex)); logicGateConnections.RemoveAt(index); } } @@ -98,16 +113,8 @@ internal abstract class LogicGate return cachedOutput.GetBit(index); } - public override bool Equals(object? obj) - { - return obj is LogicGate gate && - Id == gate.Id; - } - - public override int GetHashCode() - { - return Id.GetHashCode(); - } + protected internal virtual void Initialize() + { } protected abstract void Execute(); diff --git a/StoneRed.LogicSimulator/Utilities/LogicGatesManager.cs b/StoneRed.LogicSimulator/Utilities/LogicGatesManager.cs index 7d42a97..f8fa2ba 100644 --- a/StoneRed.LogicSimulator/Utilities/LogicGatesManager.cs +++ b/StoneRed.LogicSimulator/Utilities/LogicGatesManager.cs @@ -14,12 +14,30 @@ internal class LogicGatesManager { private readonly GraphicsDevice graphicsDevice; + private readonly Dictionary logicGates = new Dictionary(); + public LogicGatesManager(GraphicsDevice graphicsDevice) { this.graphicsDevice = graphicsDevice; } - private readonly Dictionary logicGates = new Dictionary(); + public static string GetTypeLogicGateName(Type type) + { + LogicGateNameAttribute nameAttribute = (LogicGateNameAttribute)type.GetCustomAttributes(typeof(LogicGateNameAttribute), false)[0]; + return nameAttribute.Name; + } + + public static bool TryGetTypeLogicGateName(Type type, [NotNullWhen(true)] out string? typeName) + { + if (type.GetCustomAttributes(typeof(LogicGateNameAttribute), false).FirstOrDefault() is not LogicGateNameAttribute nameAttribute) + { + typeName = null; + return false; + } + + typeName = nameAttribute.Name; + return true; + } public void LoadLogicGates() { @@ -55,28 +73,10 @@ internal class LogicGatesManager LogicGate logicGate = (LogicGate)Activator.CreateInstance(type)!; logicGate.GraphicsDevice = graphicsDevice; - + logicGate.Initialize(); return logicGate; } - public static string GetTypeLogicGateName(Type type) - { - LogicGateNameAttribute nameAttribute = (LogicGateNameAttribute)type.GetCustomAttributes(typeof(LogicGateNameAttribute), false)[0]; - return nameAttribute.Name; - } - - public static bool TryGetTypeLogicGateName(Type type, [NotNullWhen(true)] out string? typeName) - { - if (type.GetCustomAttributes(typeof(LogicGateNameAttribute), false).FirstOrDefault() is not LogicGateNameAttribute nameAttribute) - { - typeName = null; - return false; - } - - typeName = nameAttribute.Name; - return true; - } - public bool TryCreateLogicGate(string typeName, [NotNullWhen(true)] out LogicGate? logicGate) { if (!logicGates.ContainsKey(new LogicGateInfo(typeName, null))) @@ -89,7 +89,7 @@ internal class LogicGatesManager logicGate = (LogicGate)Activator.CreateInstance(type)!; logicGate.GraphicsDevice = graphicsDevice; - + logicGate.Initialize(); return true; } }