Files
pamhagen-sysctrl/PamhagenSysCtrl/Windows/PlantSchemeWindow.xaml.cs
T
2026-08-04 23:56:32 +02:00

207 lines
9.2 KiB
C#

using PamhagenSysCtrl.Helpers;
using PamhagenSysCtrl.Helpers.Pipeline;
using System.Windows;
using System.Windows.Input;
namespace PamhagenSysCtrl.Windows {
public partial class PlantSchemeWindow : Window {
private readonly PamhagenGraph Graph;
private ISource? _lastSource;
private List<INode> _path = [];
private ISet<INode>? _lastSelected;
private ISet<INode>? _hover;
private bool _mouseDown;
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);
if (!path.Hops.Any(h => (h.Node as Pump)?.IsActive ?? false)) {
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;
}
try {
if (c is EntryTrough trough) {
if (trough.IsActive) {
App.Plant?.StopTroughAuger();
} else {
App.Plant?.StartTroughAuger();
}
} else if (c is Rebler rebler) {
if (rebler.IsActive) {
App.Plant?.StopRebler();
} else {
App.Plant?.StartRebler();
}
} else if (c is Switcher switcher) {
if (App.Plant?.WantA1Selected ?? false) {
App.Plant?.SelectA2();
} else {
App.Plant?.SelectA1();
}
} else if (c is EncasedAuger auger) {
if (auger == Graph.Auger1) {
if (auger.IsActive) {
App.Plant?.StopAuger1();
} else {
App.Plant?.StartAuger1();
}
} else if (auger == Graph.Auger2) {
if (auger.IsActive) {
App.Plant?.StopAuger2();
} else {
App.Plant?.StartAuger2();
}
}
} else if (c is ConveyorBelt belt) {
if (belt.IsActive) {
App.Plant?.StopBelt();
} else {
App.Plant?.StartBelt();
}
} else if (c is Pump pump) {
if (pump == Graph.MP1) {
if (pump.IsActive) {
App.Plant?.StopMP1();
} else {
App.Plant?.StartMP1Forward();
}
} else if (pump == Graph.MP2) {
if (pump.IsActive) {
App.Plant?.StopMP2();
} else {
App.Plant?.StartMP2Forward();
}
} else if (pump == Graph.P12) {
if (pump.IsActive) {
App.Plant?.StopP12();
} else {
App.Plant?.StartP12();
}
} else if (pump == Graph.P3) {
if (pump.IsActive) {
App.Plant?.StopP3();
} else {
App.Plant?.StartP3();
}
} else if (pump == Graph.P4) {
if (pump.IsActive) {
App.Plant?.StopP4();
} else {
App.Plant?.StartP4();
}
} else if (pump == Graph.P5) {
if (pump.IsActive) {
App.Plant?.StopP5();
} else {
App.Plant?.StartP5();
}
} else if (pump == Graph.P6) {
if (pump.IsActive) {
App.Plant?.StopP6();
} else {
App.Plant?.StartP6();
}
}
}
} catch (NoClearanceException exc) {
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
UpdateScheme(pos, evt.LeftButton == MouseButtonState.Pressed);
}
private ISet<INode> UpdateScheme(Point pos, bool down) {
foreach (var e in Graph.Edges) {
e.Highlight = null;
e.Flow = null;
}
foreach (var n in Graph.Nodes) {
if (n is Pump p) {
p.Highlight = null;
}
}
foreach (var p in Graph.SelectedPaths) {
Graph.ColorPipesAdjacientToPath(p, PamhagenBrushes.Green, p.Hops.Any(h => (h.Node as Pump)?.IsActive ?? false), PamhagenBrushes.DimOrange);
}
_hover = Graph.GetHover(pos.X, pos.Y);
_mouseDown = 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) {
Graph.ColorPipesAdjacientToPath(path, PamhagenBrushes.Green, false, 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());
var pumpActive = path.Hops.Any(h => (h.Node as Pump)?.IsActive ?? false);
if (pumpActive) {
foreach (var pump in path.Hops.Select(h => h.Node as Pump).Where(p => p != null)) {
pump?.Highlight = PamhagenBrushes.Red;
}
} else {
Graph.ColorPipesAdjacientToPath(path, PamhagenBrushes.Red, false, 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;
UpdateScheme(Mouse.GetPosition(SchemeCanvas), Mouse.LeftButton == MouseButtonState.Pressed);
}
}
}