125 lines
5.2 KiB
C#
125 lines
5.2 KiB
C#
using PamhagenSysCtrl.Helpers;
|
|
using PamhagenSysCtrl.Helpers.Pipeline;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
|
|
namespace PamhagenSysCtrl.Windows {
|
|
public partial class PlantSchemeWindow : Window {
|
|
|
|
private readonly PamhagenGraph Graph;
|
|
private readonly List<Path> Paths = [];
|
|
|
|
private ISource? _lastSource;
|
|
private List<INode> _path = [];
|
|
private ISet<INode>? _lastSelected;
|
|
|
|
public PlantSchemeWindow() {
|
|
InitializeComponent();
|
|
App.Plant?.Update += OnUpdate;
|
|
Graph = new();
|
|
Graph.Draw(SchemeCanvas);
|
|
}
|
|
|
|
private void SchemeCanvas_MouseMove(object sender, MouseEventArgs evt) {
|
|
var p = evt.GetPosition(SchemeCanvas);
|
|
UpdateScheme(evt.GetPosition(SchemeCanvas), evt.LeftButton == MouseButtonState.Pressed);
|
|
}
|
|
|
|
private void SchemeCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs evt) {
|
|
var p = evt.GetPosition(SchemeCanvas);
|
|
_lastSelected = UpdateScheme(evt.GetPosition(SchemeCanvas), evt.LeftButton == MouseButtonState.Pressed);
|
|
}
|
|
|
|
private void SchemeCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs evt) {
|
|
var pos = evt.GetPosition(SchemeCanvas);
|
|
var curSelected = UpdateScheme(pos, evt.LeftButton == MouseButtonState.Pressed);
|
|
var clicked = curSelected.Intersect(_lastSelected ?? new HashSet<INode>()).ToList();
|
|
if (clicked.Count == 0) {
|
|
_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);
|
|
} 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);
|
|
_lastSource = null;
|
|
}
|
|
} else if (c is ISource source) {
|
|
_lastSource = source;
|
|
_path = [];
|
|
} else if (_lastSource != null) {
|
|
var path = Graph.GetPath([_lastSource, .. _path, c], (n) => n is not ISink && (n is not Valve v || !v.IsLocked));
|
|
if (path != null) {
|
|
_path.Add(c);
|
|
}
|
|
} else {
|
|
_lastSource = null;
|
|
}
|
|
}
|
|
UpdateScheme(pos, evt.LeftButton == MouseButtonState.Pressed);
|
|
}
|
|
|
|
private ISet<INode> UpdateScheme(Point pos, bool down) {
|
|
foreach (var e in Graph.Edges) {
|
|
e.Highlight = null;
|
|
}
|
|
foreach (var p in Paths) {
|
|
ColorPipesAdjacientToPath(p, Brushes.Green, Brushes.Orange);
|
|
}
|
|
var hover = Graph.Update(pos.X, pos.Y, down);
|
|
if (_lastSource != null && (hover.Count > 0 || _path.Count > 0)) {
|
|
List<INode> points = [_lastSource, .. _path];
|
|
if (hover.Count > 0 && !points.Contains(hover.First())) {
|
|
points.Add(hover.First());
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
} 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);
|
|
}
|
|
if (_lastSource != null) {
|
|
foreach (var edge in _lastSource.Outputs) {
|
|
edge.Highlight = Brushes.Green;
|
|
}
|
|
}
|
|
Graph.Update(pos.X, pos.Y, down);
|
|
return hover;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|