628 lines
30 KiB
C#
628 lines
30 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 const double _minMove = 50;
|
|
|
|
private readonly PamhagenGraph Graph;
|
|
|
|
private Point? _lastMousePos;
|
|
private bool _mouseDragging;
|
|
private ISource? _lastSource;
|
|
private List<INode> _path = [];
|
|
private ISet<INode>? _lastSelected;
|
|
private ISet<INode>? _hover;
|
|
|
|
public double Scale { get; set; } = 5;
|
|
public double RoundedScale => Math.Round(Scale * 2) / 2;
|
|
|
|
public Path? HoveringPath {
|
|
get {
|
|
foreach (var b in FastSelectPaths.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)> FastSelectPaths;
|
|
|
|
public PlantSchemeWindow() {
|
|
InitializeComponent();
|
|
Menu_Help_CheckForUpdates.IsEnabled = App.Config.UpdateUrl != null;
|
|
Graph = new();
|
|
Graph.Draw(SchemeCanvas, RoundedScale);
|
|
FastSelectPaths = 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;
|
|
InitializePaths();
|
|
}
|
|
|
|
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;
|
|
|
|
InitializePaths();
|
|
}
|
|
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 async void Menu_Help_CheckForUpdates_Click(object sender, RoutedEventArgs evt) {
|
|
await App.CheckForUpdates(true);
|
|
}
|
|
|
|
private void Menu_Help_About_Click(object sender, RoutedEventArgs evt) {
|
|
var window = new AboutWindow {
|
|
Owner = this,
|
|
};
|
|
window.Show();
|
|
}
|
|
|
|
private void FastSelectButton_EntryTroughStart_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.StartTroughAuger();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void FastSelectButton_EntryTroughStop_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.StopTroughAuger();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void FastSelectButton_MW1_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.SelectMW1();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void FastSelectButton_MW2_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.SelectMW2();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void FastSelectButton_MP1_Forward_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.StartMP1Forward();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void FastSelectButton_MP1_Stop_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.StopMP1();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void FastSelectButton_MP2_Forward_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.StartMP2Forward();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void FastSelectButton_MP2_Stop_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.StopMP2();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
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 || !FastSelectPaths.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 OnSizeChanged(object sender, SizeChangedEventArgs evt) {
|
|
var rx = evt.NewSize.Width / 1550.0;
|
|
var ry = evt.NewSize.Height / 900.0;
|
|
var r = Math.Min(rx, ry);
|
|
Scale = r * 5;
|
|
SchemeCanvas.Margin = new();
|
|
Graph.Scale(RoundedScale);
|
|
if (RoundedScale < 4) {
|
|
(FastSelectButtonsLeft.Parent as Border)?.Visibility = Visibility.Hidden;
|
|
(FastSelectButtonsRight.Parent as Border)?.Visibility = Visibility.Hidden;
|
|
} else if (RoundedScale <= 5) {
|
|
(FastSelectButtonsLeft.Parent as Border)?.Visibility = Visibility.Visible;
|
|
FastSelectButtonsLeft.ColumnDefinitions[0].Width = new(120);
|
|
FastSelectButtonsLeft.ColumnDefinitions[3].Width = new(120);
|
|
FastSelectButtonsLeft.RowDefinitions[0].Height = new(45);
|
|
FastSelectButtonsLeft.RowDefinitions[2].Height = new(40);
|
|
FastSelectButtonsLeft.RowDefinitions[4].Height = new(55);
|
|
FastSelectButtonsLeft.RowDefinitions[5].Height = new(55);
|
|
(FastSelectButtonsRight.Parent as Border)?.Visibility = Visibility.Visible;
|
|
foreach (var col in FastSelectButtonsRight.ColumnDefinitions) {
|
|
if (col.Width.Value <= 5) continue;
|
|
col.Width = new(120);
|
|
}
|
|
foreach (var row in FastSelectButtonsRight.RowDefinitions) {
|
|
if (row.Height.Value <= 5) continue;
|
|
row.Height = new(40);
|
|
}
|
|
foreach (var b in FastSelectPaths.Keys) {
|
|
b.FontSize = 14;
|
|
}
|
|
FastSelectButton_EntryTrough_Start.FontSize = 12;
|
|
FastSelectButton_EntryTrough_Start_Run.FontSize = 14;
|
|
FastSelectButton_EntryTrough_Stop.FontSize = 12;
|
|
FastSelectButton_EntryTrough_Stop_Run.FontSize = 14;
|
|
FastSelectButton_MW1.FontSize = 14;
|
|
FastSelectButton_MW2.FontSize = 14;
|
|
FastSelectButton_MP1_Stop.FontSize = 14;
|
|
FastSelectButton_MP1_Stop_Run.FontSize = 18;
|
|
FastSelectButton_MP1_Forward.FontSize = 14;
|
|
FastSelectButton_MP1_Forward_Run.FontSize = 18;
|
|
FastSelectButton_MP2_Stop.FontSize = 14;
|
|
FastSelectButton_MP2_Stop_Run.FontSize = 18;
|
|
FastSelectButton_MP2_Forward.FontSize = 14;
|
|
FastSelectButton_MP2_Forward_Run.FontSize = 18;
|
|
} else {
|
|
(FastSelectButtonsLeft.Parent as Border)?.Visibility = Visibility.Visible;
|
|
FastSelectButtonsLeft.ColumnDefinitions[0].Width = new(160);
|
|
FastSelectButtonsLeft.ColumnDefinitions[3].Width = new(160);
|
|
FastSelectButtonsLeft.RowDefinitions[0].Height = new(60);
|
|
FastSelectButtonsLeft.RowDefinitions[2].Height = new(50);
|
|
FastSelectButtonsLeft.RowDefinitions[4].Height = new(80);
|
|
FastSelectButtonsLeft.RowDefinitions[5].Height = new(80);
|
|
(FastSelectButtonsRight.Parent as Border)?.Visibility = Visibility.Visible;
|
|
foreach (var col in FastSelectButtonsRight.ColumnDefinitions) {
|
|
if (col.Width.Value <= 5) continue;
|
|
col.Width = new(160);
|
|
}
|
|
foreach (var row in FastSelectButtonsRight.RowDefinitions) {
|
|
if (row.Height.Value <= 5) continue;
|
|
row.Height = new(50);
|
|
}
|
|
foreach (var b in FastSelectPaths.Keys) {
|
|
b.FontSize = 16;
|
|
}
|
|
FastSelectButton_EntryTrough_Start.FontSize = 14;
|
|
FastSelectButton_EntryTrough_Start_Run.FontSize = 18;
|
|
FastSelectButton_EntryTrough_Stop.FontSize = 14;
|
|
FastSelectButton_EntryTrough_Stop_Run.FontSize = 18;
|
|
FastSelectButton_MW1.FontSize = 16;
|
|
FastSelectButton_MW2.FontSize = 16;
|
|
FastSelectButton_MP1_Stop.FontSize = 16;
|
|
FastSelectButton_MP1_Stop_Run.FontSize = 24;
|
|
FastSelectButton_MP1_Forward.FontSize = 16;
|
|
FastSelectButton_MP1_Forward_Run.FontSize = 24;
|
|
FastSelectButton_MP2_Stop.FontSize = 16;
|
|
FastSelectButton_MP2_Stop_Run.FontSize = 24;
|
|
FastSelectButton_MP2_Forward.FontSize = 16;
|
|
FastSelectButton_MP2_Forward_Run.FontSize = 24;
|
|
}
|
|
UpdateScheme(Mouse.GetPosition(SchemeCanvas), Mouse.LeftButton == MouseButtonState.Pressed);
|
|
}
|
|
|
|
private void SchemeCanvas_MouseWheel(object sender, MouseWheelEventArgs evt) {
|
|
Scale = Math.Max(1, Math.Min(20, Scale + evt.Delta / 500.0));
|
|
Graph.Scale(RoundedScale);
|
|
UpdateScheme(evt.GetPosition(SchemeCanvas), evt.LeftButton == MouseButtonState.Pressed);
|
|
}
|
|
|
|
private void SchemeCanvas_MouseMove(object sender, MouseEventArgs evt) {
|
|
var pos = evt.GetPosition(SchemeCanvas);
|
|
if (_lastMousePos is Point p) {
|
|
if (!_mouseDragging) {
|
|
var dx = p.X - pos.X;
|
|
var dy = p.Y - pos.Y;
|
|
_mouseDragging = Math.Sqrt(dx * dx + dy * dy) >= _minMove;
|
|
}
|
|
if (_mouseDragging) {
|
|
var dx = SchemeCanvas.Margin.Right - SchemeCanvas.Margin.Left + p.X - pos.X;
|
|
var dy = SchemeCanvas.Margin.Bottom - SchemeCanvas.Margin.Top + p.Y - pos.Y;
|
|
SchemeCanvas.Margin = new(dx > 0 ? 0 : -dx, dy > 0 ? 0 : -dy, dx < 0 ? 0 : dx, dy < 0 ? 0 : dy);
|
|
}
|
|
}
|
|
UpdateScheme(pos, evt.LeftButton == MouseButtonState.Pressed);
|
|
}
|
|
|
|
private void SchemeCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs evt) {
|
|
var pos = evt.GetPosition(SchemeCanvas);
|
|
_lastMousePos = pos;
|
|
_mouseDragging = false;
|
|
_lastSelected = UpdateScheme(pos, evt.LeftButton == MouseButtonState.Pressed);
|
|
}
|
|
|
|
private void SchemeCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs evt) {
|
|
var pos = evt.GetPosition(SchemeCanvas);
|
|
_lastMousePos = null;
|
|
_mouseDragging = false;
|
|
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 = false) {
|
|
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 UpdateFastSelectPathButton(IEnumerable<Path> paths, Path? hoverPathDeactivate, Button b) {
|
|
var e = FastSelectPaths[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;
|
|
}
|
|
}
|
|
|
|
SetFastSelectButton(FastSelectButton_EntryTrough_Start, App.Plant?.IsTroughAugerActive ?? false ? 1 : 0, !(App.Plant?.TroughAugerHasClearance ?? false));
|
|
SetFastSelectButton(FastSelectButton_EntryTrough_Stop, App.Plant?.IsTroughAugerActive ?? false ? 0 : 2);
|
|
SetFastSelectButton(FastSelectButton_MW1, App.Plant?.IsMW1Selected ?? false ? 1 : 0);
|
|
SetFastSelectButton(FastSelectButton_MW2, App.Plant?.IsMW2Selected ?? false ? 1 : 0);
|
|
SetFastSelectButton(FastSelectButton_MP1_Forward, App.Plant?.MP1 == MotorState.Forward ? 1 : 0, !(App.Plant?.MP1HasClearance ?? false));
|
|
SetFastSelectButton(FastSelectButton_MP1_Stop, App.Plant?.MP1 == MotorState.Halt ? 2 : 0);
|
|
SetFastSelectButton(FastSelectButton_MP2_Forward, App.Plant?.MP2 == MotorState.Forward ? 1 : 0, !(App.Plant?.MP2HasClearance ?? false));
|
|
SetFastSelectButton(FastSelectButton_MP2_Stop, App.Plant?.MP2 == MotorState.Halt ? 2 : 0);
|
|
foreach (var b in FastSelectPaths) {
|
|
SetFastSelectButton(b.Key, 0, false);
|
|
}
|
|
|
|
_hover = Graph.GetHover(pos.X, pos.Y, RoundedScale);
|
|
var hoverButtons = FastSelectPaths.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);
|
|
}
|
|
MP1_Target.Text = "Vor";
|
|
MP2_Target.Text = "Vor";
|
|
MP1_Target.FontSize = 16;
|
|
MP2_Target.FontSize = 16;
|
|
} 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);
|
|
}
|
|
|
|
if (Graph.SelectedPaths.Select(p => (Path?)p).FirstOrDefault(p => p?.Start == Graph.MW1) is Path pmw1) {
|
|
MP1_Target.Text = "\u2b9e " + string.Join(" \u2b9e ", pmw1.Hops
|
|
.Select(h => h.Node is Valve || h.Node is Pump || h.Node is CenterValve ? null : h.Node.Label)
|
|
.Where(l => !string.IsNullOrWhiteSpace(l)));
|
|
} else {
|
|
MP1_Target.Text = "Vor";
|
|
}
|
|
if (Graph.SelectedPaths.Select(p => (Path?)p).FirstOrDefault(p => p?.Start == Graph.MW2) is Path pmw2) {
|
|
MP2_Target.Text = "\u2b9e " + string.Join(" \u2b9e ", pmw2.Hops
|
|
.Select(h => h.Node is Valve || h.Node is Pump || h.Node is CenterValve ? null : h.Node.Label)
|
|
.Where(l => !string.IsNullOrWhiteSpace(l)));
|
|
} else {
|
|
MP2_Target.Text = "Vor";
|
|
}
|
|
var len1 = MP1_Target.Text.Length;
|
|
var len2 = MP2_Target.Text.Length;
|
|
MP1_Target.FontSize = len1 > 24 ? 8 : len1 > 20 ? 10 : len1 > 16 ? 12 : len1 > 12 ? 14 : 16;
|
|
MP2_Target.FontSize = len2 > 24 ? 8 : len2 > 20 ? 10 : len2 > 16 ? 12 : len2 > 12 ? 14 : 16;
|
|
}
|
|
|
|
var paths = Graph.SelectedPaths;
|
|
if (HoveringPath is Path hp) paths = paths.Append(hp);
|
|
foreach (var b in FastSelectPaths.Keys) {
|
|
UpdateFastSelectPathButton(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);
|
|
}
|
|
|
|
public void InitializePaths() {
|
|
var add = ReconstructSelectedPaths().ToList();
|
|
var remove = Graph.SelectedPaths.ToList();
|
|
foreach (var p in remove) Graph.RemovePath(p);
|
|
for (int i = 1; i <= 57; i++) {
|
|
App.Plant?.CloseV(i);
|
|
}
|
|
foreach (var p in add) {
|
|
if (p.Hops.Any(h => h.Node is Valve v && v.IsLocked)) continue;
|
|
Graph.AddPath(p);
|
|
}
|
|
}
|
|
|
|
public IEnumerable<Path> ReconstructSelectedPaths() {
|
|
if (App.Plant is not PamhagenPlant p) yield break;
|
|
var sources = Graph.Nodes.Where(n => n is ISource).ToList();
|
|
var sinks = Graph.Nodes.Where(n => n is ISink).ToList();
|
|
foreach (var src in sources) {
|
|
var paths = new List<Path>();
|
|
foreach (var sink in sinks) {
|
|
paths.AddRange(Graph.GetPaths(src, sink, null, (n) => n is not ISink && (n is not Valve v || p.WantValveOpen(v.Nr))));
|
|
}
|
|
if (paths.Count == 1)
|
|
yield return paths.First();
|
|
}
|
|
}
|
|
}
|
|
}
|