Files
pamhagen-sysctrl/PamhagenSysCtrl/Windows/PlantSchemeWindow.xaml.cs
T
lorenz.stechauner f0accc8df8
Test / Run tests (push) Successful in 14s
Add fast select buttons
2026-08-11 00:26:27 +02:00

378 lines
17 KiB
C#

using PamhagenSysCtrl.Helpers;
using PamhagenSysCtrl.Helpers.Pipeline;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
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;
private ISet<INode>? _hover;
public Path? HoveringPath {
get {
foreach (var b in FastSelectButtons.Where(b => b.Key.IsMouseOver)) {
if (Graph.SelectedPaths.Any(p => p.Start == b.Value.Source && p.Hops.Last().Node == b.Value.Sink))
continue;
return Graph.GetFreePath([b.Value.Source, b.Value.Sink], true);
}
if (_lastSource == null || _hover == null || !(_hover.Count > 0 || _path.Count > 0))
return null;
List<INode> points = [_lastSource, .. _path];
if (_hover.Count > 0 && !points.Contains(_hover.First())) {
points.Add(_hover.First());
}
if (points.Count >= 2) {
return Graph.GetFreePath(points);
} else {
return null;
}
}
}
protected Dictionary<Button, (ISource Source, Pump Pump, ISink Sink)> FastSelectButtons;
public PlantSchemeWindow() {
InitializeComponent();
Graph = new();
Graph.Draw(SchemeCanvas);
FastSelectButtons = new Dictionary<Button, (ISource, Pump, ISink)> {
[FastSelectButton_MW1_Press1] = (Graph.MW1, Graph.MP1, Graph.Press1),
[FastSelectButton_MW2_Press1] = (Graph.MW2, Graph.MP2, Graph.Press1),
[FastSelectButton_MW1_Press2] = (Graph.MW1, Graph.MP1, Graph.Press2),
[FastSelectButton_MW2_Press2] = (Graph.MW2, Graph.MP2, Graph.Press2),
[FastSelectButton_MW1_Tank1] = (Graph.MW1, Graph.MP1, Graph.Tank1),
[FastSelectButton_MW2_Tank1] = (Graph.MW2, Graph.MP2, Graph.Tank1),
[FastSelectButton_MW1_Tank2] = (Graph.MW1, Graph.MP1, Graph.Tank2),
[FastSelectButton_MW2_Tank2] = (Graph.MW2, Graph.MP2, Graph.Tank2),
[FastSelectButton_MW1_Tank3] = (Graph.MW1, Graph.MP1, Graph.Tank3),
[FastSelectButton_MW2_Tank3] = (Graph.MW2, Graph.MP2, Graph.Tank3),
[FastSelectButton_MW1_Tank4] = (Graph.MW1, Graph.MP1, Graph.Tank4),
[FastSelectButton_MW2_Tank4] = (Graph.MW2, Graph.MP2, Graph.Tank4),
[FastSelectButton_MW1_Tank5] = (Graph.MW1, Graph.MP1, Graph.Tank5),
[FastSelectButton_MW2_Tank5] = (Graph.MW2, Graph.MP2, Graph.Tank5),
};
App.Plant?.Update += OnUpdate;
}
private void OnClosed(object sender, EventArgs evt) {
Application.Current.Shutdown();
}
private void Menu_Settings_ManualControl_Changed(object sender, RoutedEventArgs evt) {
App.Plant?.ManualControlMode = Menu_Settings_ManualControl.IsChecked;
if (Menu_Settings_ManualControl.IsChecked) {
foreach (var win in Application.Current.Windows) {
if (win is ManualControlWindow w) {
w.Focus();
return;
}
}
var ctrl = new ManualControlWindow();
ctrl.Show();
} else {
foreach (var win in Application.Current.Windows) {
if (win is ManualControlWindow w) {
w.Close();
}
}
Menu_Settings_OverridePathClearances.IsChecked = false;
Menu_Settings_OverrideDryRunClearances.IsChecked = false;
}
OnUpdate(null, null);
}
private void Menu_Settings_OverridePathClearances_Changed(object sender, RoutedEventArgs evt) {
App.Plant?.OverridePathClearances = Menu_Settings_OverridePathClearances.IsChecked;
OnUpdate(null, null);
}
private void Menu_Settings_OverrideDryRunClearances_Changed(object sender, RoutedEventArgs evt) {
App.Plant?.OverrideDryRunClearances = Menu_Settings_OverrideDryRunClearances.IsChecked;
OnUpdate(null, null);
}
private void Menu_Settings_Directory_Click(object sender, RoutedEventArgs evt) {
try {
Process.Start("explorer.exe", App.DataPath);
} catch { }
}
private void Menu_Settings_Config_Click(object sender, RoutedEventArgs evt) {
try {
Process.Start(new ProcessStartInfo {
FileName = "notepad.exe",
Arguments = App.ConfigPath,
Verb = "runas",
UseShellExecute = true,
});
} catch { }
}
private void FastSelectButton_Enter(object sender, MouseEventArgs evt) {
UpdateScheme(evt.GetPosition(SchemeCanvas), evt.LeftButton == MouseButtonState.Pressed);
}
private void FastSelectButton_Leave(object sender, MouseEventArgs evt) {
UpdateScheme(evt.GetPosition(SchemeCanvas), evt.LeftButton == MouseButtonState.Pressed);
}
private void FastSelectButton_Click(object sender, RoutedEventArgs evt) {
if (sender is not Button b || !FastSelectButtons.TryGetValue(b, out var val))
return;
if (Graph.SelectedPaths.Select(p => (Path?)p).FirstOrDefault(p => p?.Start == val.Source, null) is Path p1) {
Graph.RemovePath(p1);
if (p1.Hops.Last().Node == val.Sink)
return;
}
if (Graph.GetFreePath([val.Source, val.Sink]) is Path p2) {
Graph.AddPath(p2);
_lastSource = null;
}
}
private void SchemeCanvas_MouseMove(object sender, MouseEventArgs evt) {
UpdateScheme(evt.GetPosition(SchemeCanvas), evt.LeftButton == MouseButtonState.Pressed);
}
private void SchemeCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs evt) {
_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 (App.Plant?.ManualControlMode ?? false) {
if (c is Valve v) {
if (v.WantOpen) {
App.Plant.CloseV(v.Nr);
} else {
App.Plant.OpenV(v.Nr);
}
}
} else {
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) {
if (Graph.GetFreePath([_lastSource, .. _path, sink]) is Path path) {
Graph.AddPath(path);
_lastSource = null;
}
} else if (_lastSource != null) {
var path = Graph.GetFreePath([_lastSource, .. _path, c]);
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?.WantMW1Selected ?? false) {
App.Plant?.SelectMW2();
} else {
App.Plant?.SelectMW1();
}
} else if (c is EncasedAuger auger) {
if (auger == Graph.Auger1) {
if (auger.IsActive) {
App.Plant?.StopAuger1();
} else {
App.Plant?.StartAuger1();
}
} else if (auger == Graph.Auger3) {
if (auger.IsActive) {
App.Plant?.StopAuger3();
} else {
App.Plant?.StartAuger3();
}
}
} else if (c is ConveyorBelt belt) {
if (belt.IsActive) {
App.Plant?.StopAuger2();
} else {
App.Plant?.StartAuger2();
}
} 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 static void SetFastSelectButton(Button b, int m, bool disabled) {
b.IsEnabled = !disabled;
if (m == 1) {
b.Background = Brushes.MintCream;
b.BorderBrush = Brushes.DarkGreen;
b.Foreground = Brushes.DarkGreen;
} else if (m == 2) {
b.Background = Brushes.MistyRose;
b.BorderBrush = Brushes.DarkRed;
b.Foreground = Brushes.DarkRed;
} else {
b.Background = Brushes.LightGray;
b.BorderBrush = Brushes.DimGray;
b.Foreground = Brushes.Black;
}
}
private void UpdateFastSelectButton(IEnumerable<Path> paths, Path? hoverPathDeactivate, Button b) {
var e = FastSelectButtons[b];
SetFastSelectButton(b,
paths.Any(p => p.Start == e.Source && p.Hops.Last().Node == e.Sink) ? (hoverPathDeactivate?.Start == e.Source && hoverPathDeactivate?.Hops.Last().Node == e.Sink) ? 2 : 1 : 0,
e.Pump.IsActive || Graph.GetFreePath([e.Source, e.Sink], overrideStart: true) == null);
}
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 b in FastSelectButtons) {
SetFastSelectButton(b.Key, 0, false);
}
_hover = Graph.GetHover(pos.X, pos.Y);
var hoverButtons = FastSelectButtons.Where(b => b.Key.IsMouseOver).ToDictionary();
Path? hoverPathDeactivate = null;
if (App.Plant?.ManualControlMode ?? false) {
foreach (var src in Graph.Nodes.Where(n => n is Pump)) {
Graph.ColorPipesByValves(src, PamhagenBrushes.Green, (src as Pump)?.State ?? default, PamhagenBrushes.DimOrange);
}
} else {
foreach (var p in Graph.SelectedPaths) {
Graph.ColorPipesAdjacientToPath(p, PamhagenBrushes.Green, p.Hops.Select(h => (h.Node as Pump)?.State ?? default).FirstOrDefault(n => n != MotorState.Halt), PamhagenBrushes.DimOrange);
}
if (_lastSource == null && _hover.Count > 0 && Graph.SelectedPaths.Select(p => (Path?)p).FirstOrDefault(p => p?.Start == _hover.First()) is Path path1) {
var pumpActive = path1.Hops.Any(h => (h.Node as Pump)?.IsActive ?? false);
if (pumpActive) {
foreach (var pump in path1.Hops.Select(h => h.Node as Pump).Where(p => p != null)) {
pump?.Highlight = PamhagenBrushes.Red;
}
} else {
hoverPathDeactivate = path1;
Graph.ColorPipesAdjacientToPath(path1, PamhagenBrushes.Red, default, null);
}
} else if (hoverButtons.Count != 0 && Graph.SelectedPaths.Select(p => (Path?)p).FirstOrDefault(p => p?.Start == hoverButtons.First().Value.Source) is Path path2) {
var pumpActive = path2.Hops.Any(h => (h.Node as Pump)?.IsActive ?? false);
if (pumpActive) {
foreach (var pump in path2.Hops.Select(h => h.Node as Pump).Where(p => p != null)) {
pump?.Highlight = PamhagenBrushes.Red;
}
} else {
hoverPathDeactivate = path2;
Graph.ColorPipesAdjacientToPath(path2, PamhagenBrushes.Red, default, null);
}
}
if (_lastSource != null) {
foreach (var edge in _lastSource.Outputs) {
edge.Highlight = PamhagenBrushes.Green;
}
}
if (HoveringPath is Path hoveringPath) {
Graph.ColorPipesAdjacientToPath(hoveringPath, PamhagenBrushes.Green, default, PamhagenBrushes.DimOrange);
}
}
var paths = Graph.SelectedPaths;
if (HoveringPath is Path hp) paths = paths.Append(hp);
foreach (var b in FastSelectButtons.Keys) {
UpdateFastSelectButton(paths, hoverPathDeactivate, b);
}
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);
}
}
}