diff --git a/PamhagenSysCtrl/Helpers/PamhagenGraph.cs b/PamhagenSysCtrl/Helpers/PamhagenGraph.cs index 263b910..ffb39f1 100644 --- a/PamhagenSysCtrl/Helpers/PamhagenGraph.cs +++ b/PamhagenSysCtrl/Helpers/PamhagenGraph.cs @@ -4,6 +4,9 @@ using System.Windows.Media; namespace PamhagenSysCtrl.Helpers { public class PamhagenGraph : Graph { + private readonly List _selectedPaths = []; + public IEnumerable SelectedPaths => _selectedPaths; + public readonly Trough MW1; public readonly Trough MW2; @@ -347,7 +350,7 @@ namespace PamhagenSysCtrl.Helpers { } protected static Valve CreateValve(int n, double x, double y) { - return new($"V{n}", x, y, () => App.Plant?.IsValveOpen(4) ?? false, () => App.Plant?.WantValveOpen(4) ?? false); + return new(n, x, y, () => App.Plant?.IsValveOpen(4) ?? false, () => App.Plant?.WantValveOpen(4) ?? false); } protected static Tank CreateTank(int n, double x, double y, int capacity) { @@ -377,5 +380,49 @@ namespace PamhagenSysCtrl.Helpers { TraverseSubgraph(o, visited, edgeAction, valveAction); } } + + public void AddPath(Path path) { + _selectedPaths.Add(path); + UpdateValvesAdjacientToPath(path, true); + if (App.Plant is PamhagenPlant plant) { + foreach (var (edge, _) in path.Hops) { + if (edge is Valve v) { + plant.OpenV(v.Nr); + } + } + plant.CommitChanges(); + } + } + + public void RemovePath(Path path) { + _selectedPaths.Remove(path); + UpdateValvesAdjacientToPath(path, false); + foreach (var p in _selectedPaths) UpdateValvesAdjacientToPath(p, true); + if (App.Plant is PamhagenPlant plant) { + foreach (var (edge, _) in path.Hops) { + if (edge is Valve v) { + plant.CloseV(v.Nr); + } + } + plant.CommitChanges(); + } + } + + private void UpdateValvesAdjacientToPath(Path path, bool locked) { + foreach (var (edge, node) in path.Hops) { + TraverseSubgraph(node, valveAction: (v) => v.IsLocked = locked); + } + } + + public void ColorPipesAdjacientToPath(Path path, Brush? color1, Brush? color2) { + var color = color1; + HashSet visited = []; + foreach (var (edge, node) in path.Hops) { + edge.Highlight = color1; + visited.Add(edge); + if (node is Pump) color = color2; + TraverseSubgraph(node, visited, edgeAction: (e) => e.Highlight = color); + } + } } } diff --git a/PamhagenSysCtrl/Helpers/Pipeline/Valve.cs b/PamhagenSysCtrl/Helpers/Pipeline/Valve.cs index a0b91e9..f813b12 100644 --- a/PamhagenSysCtrl/Helpers/Pipeline/Valve.cs +++ b/PamhagenSysCtrl/Helpers/Pipeline/Valve.cs @@ -10,7 +10,8 @@ namespace PamhagenSysCtrl.Helpers.Pipeline { public double CenterX { get; set; } public double CenterY { get; set; } - public string Label { get; set; } + public int Nr { get; set; } + public string Label => $"V{Nr}"; public Brush? Highlight { get; set; } public bool IsLocked { get; set; } @@ -25,10 +26,10 @@ namespace PamhagenSysCtrl.Helpers.Pipeline { private Shape? _circle; private TextBlock? _text; - public Valve(string label, double x, double y, Func cbOpen, Func cbWant) { + public Valve(int nr, double x, double y, Func cbOpen, Func cbWant) { CenterX = x; CenterY = y; - Label = label; + Nr = nr; CallbackIsOpen = cbOpen; CallbackWantOpen = cbWant; Inputs = new HashSet(); @@ -61,7 +62,12 @@ namespace PamhagenSysCtrl.Helpers.Pipeline { public bool Update(double x, double y, bool down) { var hover = Math.Sqrt(Math.Pow(CenterX - x, 2) + Math.Pow(CenterY - y, 2)) <= DIAMETER / 2; - _circle?.Fill = IsLocked ? Brushes.Orange : Highlight ?? (hover ? Brushes.LightGray : Brushes.DarkGray); + _circle?.Fill = + IsLocked && !WantOpen && !IsOpen ? Brushes.Orange : + IsLocked && !WantOpen && IsOpen ? Brushes.Red : + IsLocked && WantOpen && !IsOpen ? Brushes.Yellow : + IsLocked && WantOpen && IsOpen ? Brushes.Green : + Highlight ?? (hover ? Brushes.LightGray : Brushes.DarkGray); return hover; } } diff --git a/PamhagenSysCtrl/Windows/PlantSchemeWindow.xaml.cs b/PamhagenSysCtrl/Windows/PlantSchemeWindow.xaml.cs index d598a29..56c3bbc 100644 --- a/PamhagenSysCtrl/Windows/PlantSchemeWindow.xaml.cs +++ b/PamhagenSysCtrl/Windows/PlantSchemeWindow.xaml.cs @@ -8,7 +8,6 @@ namespace PamhagenSysCtrl.Windows { public partial class PlantSchemeWindow : Window { private readonly PamhagenGraph Graph; - private readonly List Paths = []; private ISource? _lastSource; private List _path = []; @@ -39,16 +38,13 @@ namespace PamhagenSysCtrl.Windows { _lastSource = null; } foreach (var c in clicked) { - if (_lastSource == null && Paths.Any(p => p.Start == c)) { - var path = Paths.First(p => p.Start == c); - Paths.Remove(path); - UpdateValvesAdjacientToPath(path, false); - foreach (var p in Paths) UpdateValvesAdjacientToPath(p, true); + if (_lastSource == null && Graph.SelectedPaths.Any(p => p.Start == c)) { + var path = Graph.SelectedPaths.First(p => p.Start == c); + Graph.RemovePath(path); } else if (_lastSource != null && c is ISink sink) { var res = Graph.GetPath([_lastSource, .._path, sink], (n) => n is not ISink && (n is not Valve v || !v.IsLocked)); if (res is Path path) { - Paths.Add(path); - UpdateValvesAdjacientToPath(path, true); + Graph.AddPath(path); _lastSource = null; } } else if (c is ISource source) { @@ -70,8 +66,8 @@ namespace PamhagenSysCtrl.Windows { foreach (var e in Graph.Edges) { e.Highlight = null; } - foreach (var p in Paths) { - ColorPipesAdjacientToPath(p, Brushes.Green, Brushes.Orange); + foreach (var p in Graph.SelectedPaths) { + Graph.ColorPipesAdjacientToPath(p, Brushes.Green, Brushes.Orange); } var hover = Graph.Update(pos.X, pos.Y, down); if (_lastSource != null && (hover.Count > 0 || _path.Count > 0)) { @@ -82,12 +78,12 @@ namespace PamhagenSysCtrl.Windows { if (points.Count >= 2) { var res = Graph.GetPath(points, (n) => n is not ISink && (n is not Valve v || !v.IsLocked)); if (res is Path path) { - ColorPipesAdjacientToPath(path, Brushes.Green, Brushes.Orange); + Graph.ColorPipesAdjacientToPath(path, Brushes.Green, Brushes.Orange); } } - } else if (_lastSource == null && hover.Count > 0 && Paths.Any(p => p.Start == hover.First())) { - var path = Paths.First(p => p.Start == hover.First()); - ColorPipesAdjacientToPath(path, Brushes.Red, null); + } else if (_lastSource == null && hover.Count > 0 && Graph.SelectedPaths.Any(p => p.Start == hover.First())) { + var path = Graph.SelectedPaths.First(p => p.Start == hover.First()); + Graph.ColorPipesAdjacientToPath(path, Brushes.Red, null); } if (_lastSource != null) { foreach (var edge in _lastSource.Outputs) { @@ -100,25 +96,6 @@ namespace PamhagenSysCtrl.Windows { public void OnUpdate(object? sender, PlantEventArgs evt) { if (sender is not PamhagenPlant plant) return; - - - } - - private void UpdateValvesAdjacientToPath(Path path, bool locked) { - foreach (var (edge, node) in path.Hops) { - Graph.TraverseSubgraph(node, valveAction: (v) => v.IsLocked = locked); - } - } - - private void ColorPipesAdjacientToPath(Path path, Brush? color1, Brush? color2) { - var color = color1; - HashSet visited = []; - foreach (var (edge, node) in path.Hops) { - edge.Highlight = color1; - visited.Add(edge); - if (node is Pump) color = color2; - Graph.TraverseSubgraph(node, visited, edgeAction: (e) => e.Highlight = color); - } } } }