Pipeline: Change Valve symbol and add abstract Valve
Test / Run tests (push) Successful in 15s

This commit is contained in:
2026-09-12 19:54:49 +02:00
parent f4ef8d2f16
commit 3d24c65758
6 changed files with 231 additions and 190 deletions
+88 -42
View File
@@ -4,88 +4,134 @@ using System.Windows.Media;
using System.Windows.Shapes;
namespace PamhagenSysCtrl.Helpers.Pipeline {
public class Valve : INode {
public abstract class Valve : INode {
public const double DIAMETER = 4.8;
public const double LENGTH = 4.8;
public const double WIDTH = 2.6;
public double CenterX { get; set; }
public double CenterY { get; set; }
public int Nr { get; set; }
public string Label => $"V{Nr}";
public abstract string Label { get; }
public Brush? Highlight { get; set; }
public readonly ISet<Path> LockedForPaths = new HashSet<Path>();
public int Orientation { get; set; }
public bool IsLocked => LockedForPaths.Any();
public bool IsOpen => CallbackIsOpen();
public bool WantOpen => CallbackWantOpen(this);
public ISet<IEdge> Inputs { get; init; }
public ISet<IEdge> Outputs { get; init; }
protected Func<bool> CallbackIsOpen;
protected Func<Valve, bool> CallbackWantOpen;
private Shape? _circle;
private TextBlock? _text;
protected Rectangle? _background;
protected Polygon? _triangles;
protected Polyline? _topLine;
protected Polyline? _bottomLine;
protected TextBlock? _text;
public Valve(int nr, double x, double y, Func<bool> cbOpen, Func<Valve, bool> cbWant) {
public Valve(double x, double y, int orientation, Func<bool> cbOpen) {
CenterX = x;
CenterY = y;
Nr = nr;
CallbackIsOpen = cbOpen;
CallbackWantOpen = cbWant;
Inputs = new HashSet<IEdge>();
Outputs = new HashSet<IEdge>();
Orientation = orientation;
}
public void Draw(Canvas canvas) {
_circle = new Ellipse() {
_background = new Rectangle() {
Fill = Brushes.White,
};
_triangles = new Polygon() {
Fill = Brushes.DarkGray,
Stroke = Brushes.Black,
};
_topLine = new Polyline() {
Stroke = Brushes.Black,
};
_bottomLine = new Polyline() {
Stroke = Brushes.Black,
};
_text = new() {
Text = Label,
TextAlignment = TextAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
SnapsToDevicePixels = true,
};
canvas.Children.Add(_circle);
canvas.Children.Add(_background);
canvas.Children.Add(_triangles);
canvas.Children.Add(_topLine);
canvas.Children.Add(_bottomLine);
canvas.Children.Add(_text);
}
public void Scale(double scale) {
var border = Graph.ToBorder(scale);
Graph.SetCenter(_circle, CenterX, CenterY, DIAMETER, DIAMETER, scale);
_circle?.StrokeThickness = border;
_text?.Width = DIAMETER * scale;
_text?.Height = DIAMETER * scale;
_text?.FontSize = 2 * scale;
_text?.SetValue(Canvas.LeftProperty, (CenterX - DIAMETER / 2 + 0.1) * scale);
_text?.SetValue(Canvas.TopProperty, (CenterY - DIAMETER / 2 + 1.1) * scale);
if (Orientation == 0 || Orientation == 2) {
Graph.SetCenter(_background, CenterX, CenterY, LENGTH / 2, LENGTH, scale);
_triangles?.Points = [
Graph.ToPx(CenterX - WIDTH / 2, CenterY - LENGTH / 2, scale),
Graph.ToPx(CenterX + WIDTH / 2, CenterY + LENGTH / 2, scale),
Graph.ToPx(CenterX - WIDTH / 2, CenterY + LENGTH / 2, scale),
Graph.ToPx(CenterX + WIDTH / 2, CenterY - LENGTH / 2, scale),
];
_topLine?.Points = [
Graph.ToPx(CenterX - WIDTH / 2, CenterY - LENGTH / 4, scale),
Graph.ToPx(CenterX - WIDTH / 4, CenterY, scale),
Graph.ToPx(CenterX - WIDTH / 2, CenterY + LENGTH / 4, scale),
];
_bottomLine?.Points = [
Graph.ToPx(CenterX + WIDTH / 2, CenterY - LENGTH / 4, scale),
Graph.ToPx(CenterX + WIDTH / 4, CenterY, scale),
Graph.ToPx(CenterX + WIDTH / 2, CenterY + LENGTH / 4, scale),
];
} else {
Graph.SetCenter(_background, CenterX, CenterY, LENGTH, LENGTH / 2, scale);
_triangles?.Points = [
Graph.ToPx(CenterX - LENGTH / 2, CenterY - WIDTH / 2, scale),
Graph.ToPx(CenterX + LENGTH / 2, CenterY + WIDTH / 2, scale),
Graph.ToPx(CenterX + LENGTH / 2, CenterY - WIDTH / 2, scale),
Graph.ToPx(CenterX - LENGTH / 2, CenterY + WIDTH / 2, scale),
];
_topLine?.Points = [
Graph.ToPx(CenterX - LENGTH / 4, CenterY - WIDTH / 2, scale),
Graph.ToPx(CenterX, CenterY - WIDTH / 4, scale),
Graph.ToPx(CenterX + LENGTH / 4, CenterY - WIDTH / 2, scale),
];
_bottomLine?.Points = [
Graph.ToPx(CenterX - LENGTH / 4, CenterY + WIDTH / 2, scale),
Graph.ToPx(CenterX, CenterY + WIDTH / 4, scale),
Graph.ToPx(CenterX + LENGTH / 4, CenterY + WIDTH / 2, scale),
];
}
_triangles?.StrokeThickness = border;
_topLine?.StrokeThickness = border;
_bottomLine?.StrokeThickness = border;
_text?.Width = 40;
_text?.Height = 16;
_text?.FontSize = 12;
_text?.TextAlignment = Orientation == 0 ? TextAlignment.Right : Orientation == 2 ? TextAlignment.Left : TextAlignment.Center;
if (Orientation == 0) {
_text?.SetValue(Canvas.LeftProperty, (CenterX - WIDTH / 1.5) * scale - 40);
_text?.SetValue(Canvas.TopProperty, CenterY * scale - 8);
} else if (Orientation == 2) {
_text?.SetValue(Canvas.LeftProperty, (CenterX + WIDTH / 1.5) * scale);
_text?.SetValue(Canvas.TopProperty, CenterY * scale - 8 );
} else if (Orientation == 1) {
_text?.SetValue(Canvas.LeftProperty, CenterX * scale - 20);
_text?.SetValue(Canvas.TopProperty, (CenterY - WIDTH / 2) * scale - 16);
} else {
_text?.SetValue(Canvas.LeftProperty, CenterX * scale - 20);
_text?.SetValue(Canvas.TopProperty, (CenterY + WIDTH / 2) * scale);
}
}
public bool IsInside(double x, double y) {
return Math.Sqrt(Math.Pow(CenterX - x, 2) + Math.Pow(CenterY - y, 2)) <= DIAMETER / 2;
}
public void Update(bool isHovering, bool isPressed) {
if (App.Plant?.ManualControlMode ?? false) {
_circle?.Fill =
!WantOpen && !IsOpen && !isHovering ? PamhagenBrushes.Red :
!WantOpen && !IsOpen ? PamhagenBrushes.Green :
!WantOpen && IsOpen ? PamhagenBrushes.DimRed :
WantOpen && !IsOpen ? PamhagenBrushes.DimGreen :
WantOpen && IsOpen && !isHovering ? PamhagenBrushes.Green :
WantOpen && IsOpen ? PamhagenBrushes.Red :
Highlight ?? (isHovering ? Brushes.LightGray : Brushes.DarkGray);
if (Orientation == 0 || Orientation == 2) {
return Math.Abs(x - CenterX) <= WIDTH / 2 && Math.Abs(y - CenterY) <= LENGTH / 2;
} else {
_circle?.Fill =
IsLocked && !WantOpen && !IsOpen ? PamhagenBrushes.DimOrange :
IsLocked && !WantOpen && IsOpen ? PamhagenBrushes.Red :
IsLocked && WantOpen && !IsOpen ? PamhagenBrushes.Yellow :
IsLocked && WantOpen && IsOpen ? PamhagenBrushes.Green :
Highlight ?? (isHovering ? Brushes.LightGray : Brushes.DarkGray);
return Math.Abs(x - CenterX) <= LENGTH / 2 && Math.Abs(y - CenterY) <= WIDTH / 2;
}
}
public abstract void Update(bool isHovering, bool isPressed);
}
}