102 lines
4.4 KiB
C#
102 lines
4.4 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 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 && 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) {
|
|
Graph.AddPath(path);
|
|
_lastSource = null;
|
|
}
|
|
} 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 if (c is ISource source) {
|
|
_lastSource = source;
|
|
_path = [];
|
|
} 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 Graph.SelectedPaths) {
|
|
Graph.ColorPipesAdjacientToPath(p, PamhagenBrushes.Green, PamhagenBrushes.DimOrange);
|
|
}
|
|
var hover = Graph.GetHover(pos.X, pos.Y);
|
|
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) {
|
|
Graph.ColorPipesAdjacientToPath(path, PamhagenBrushes.Green, PamhagenBrushes.DimOrange);
|
|
}
|
|
}
|
|
} 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, PamhagenBrushes.Red, null);
|
|
}
|
|
if (_lastSource != null) {
|
|
foreach (var edge in _lastSource.Outputs) {
|
|
edge.Highlight = PamhagenBrushes.Green;
|
|
}
|
|
}
|
|
Graph.Update(hover, down);
|
|
return hover;
|
|
}
|
|
|
|
public void OnUpdate(object? sender, PlantEventArgs evt) {
|
|
if (sender is not PamhagenPlant plant) return;
|
|
}
|
|
}
|
|
}
|