Implement path selection
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:PamhagenSysCtrl.Windows"
|
||||
Title="Schema - Anlagensteuerung Pamhagen" Height="800" Width="1600">
|
||||
<Grid MouseMove="SchemeCanvas_MouseMove">
|
||||
<Grid MouseMove="SchemeCanvas_MouseMove" MouseLeftButtonDown="SchemeCanvas_MouseLeftButtonDown" MouseLeftButtonUp="SchemeCanvas_MouseLeftButtonUp">
|
||||
<Canvas x:Name="SchemeCanvas">
|
||||
</Canvas>
|
||||
</Grid>
|
||||
|
||||
@@ -8,6 +8,10 @@ namespace PamhagenSysCtrl.Windows {
|
||||
public partial class PlantSchemeWindow : Window {
|
||||
|
||||
private readonly PamhagenGraph Graph;
|
||||
private readonly List<Path> Paths = [];
|
||||
|
||||
private ISource? _lastSource;
|
||||
private ISet<INode>? _lastSelected;
|
||||
|
||||
public PlantSchemeWindow() {
|
||||
InitializeComponent();
|
||||
@@ -18,18 +22,83 @@ namespace PamhagenSysCtrl.Windows {
|
||||
|
||||
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 p = evt.GetPosition(SchemeCanvas);
|
||||
var curSelected = UpdateScheme(evt.GetPosition(SchemeCanvas), evt.LeftButton == MouseButtonState.Pressed);
|
||||
var clicked = curSelected.Intersect(_lastSelected ?? new HashSet<INode>()).ToList();
|
||||
_lastSource?.IsSelected = false;
|
||||
if (clicked.Count == 0) {
|
||||
_lastSource = null;
|
||||
}
|
||||
foreach (var c in clicked) {
|
||||
if (_lastSource != null && c is ISink sink) {
|
||||
var path = Graph.GetPath(_lastSource, sink, (n) => n is not ISink && (n is not Valve v || !v.IsLocked));
|
||||
if (path != null) {
|
||||
Paths.Add(path.Value);
|
||||
_lastSource = null;
|
||||
}
|
||||
} else if (c is ISource source) {
|
||||
_lastSource = source;
|
||||
_lastSource.IsSelected = true;
|
||||
} else {
|
||||
_lastSource = null;
|
||||
}
|
||||
}
|
||||
UpdateScheme(evt.GetPosition(SchemeCanvas), evt.LeftButton == MouseButtonState.Pressed);
|
||||
}
|
||||
|
||||
private ISet<INode> UpdateScheme(Point pos, bool down) {
|
||||
foreach (var e in Graph.Edges) {
|
||||
e.Highlight = null;
|
||||
}
|
||||
var hover = Graph.Update(p.X, p.Y);
|
||||
if (hover.Count > 0) {
|
||||
var path = Graph.GetPath(Graph.MW1, hover.First(), (n) => n is not ISink);
|
||||
foreach (var p in Paths) {
|
||||
var c = Brushes.Green;
|
||||
foreach (var (edge, node) in p.Hops) {
|
||||
edge.Highlight = Brushes.Green;
|
||||
if (node is Pump) c = Brushes.Orange;
|
||||
ColorSubgraph(node, c, (v) => v.IsLocked = true);
|
||||
}
|
||||
}
|
||||
var hover = Graph.Update(pos.X, pos.Y, down);
|
||||
if (_lastSource != null && hover.Count > 0) {
|
||||
var path = Graph.GetPath(_lastSource, hover.First(), (n) => n is not ISink && (n is not Valve v || !v.IsLocked));
|
||||
if (path != null) {
|
||||
var c = Brushes.Green;
|
||||
foreach (var (edge, node) in path.Value.Hops) {
|
||||
edge.Highlight = Brushes.Green;
|
||||
if (node is Pump) c = Brushes.Orange;
|
||||
ColorSubgraph(node, c);
|
||||
}
|
||||
}
|
||||
Graph.Update(p.X, p.Y);
|
||||
Graph.Update(pos.X, pos.Y, down);
|
||||
}
|
||||
return hover;
|
||||
}
|
||||
|
||||
private void ColorSubgraph(INode start, Brush color, Action<Valve>? valveAction = null) {
|
||||
if (start is Valve v) {
|
||||
valveAction?.Invoke(v);
|
||||
return;
|
||||
} else if (start is ISink || start is Pump) {
|
||||
return;
|
||||
}
|
||||
foreach (var edge in start.Inputs) {
|
||||
if (edge.Highlight != null) continue;
|
||||
edge.Highlight = color;
|
||||
ColorSubgraph(edge.Start, color, valveAction);
|
||||
}
|
||||
foreach (var edge in start.Outputs) {
|
||||
if (edge.Highlight != null) continue;
|
||||
edge.Highlight = color;
|
||||
ColorSubgraph(edge.End, color, valveAction);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user