Add valve open/close logic

This commit is contained in:
2026-07-14 16:55:03 +02:00
parent 44a605598e
commit 77bcfcc3b8
3 changed files with 68 additions and 38 deletions
@@ -8,7 +8,6 @@ namespace PamhagenSysCtrl.Windows {
public partial class PlantSchemeWindow : Window {
private readonly PamhagenGraph Graph;
private readonly List<Path> Paths = [];
private ISource? _lastSource;
private List<INode> _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<IEdge> 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);
}
}
}
}