Files
pamhagen-sysctrl/PamhagenSysCtrl/Helpers/Pipeline/Pipe.cs
T
lorenz.stechauner 36c1ced254
Test / Run tests (push) Successful in 16s
[WIP] Scaling
2026-08-13 18:15:58 +02:00

86 lines
3.4 KiB
C#

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace PamhagenSysCtrl.Helpers.Pipeline {
public class Pipe : IEdge {
public INode Start { get; set; }
public INode End { get; set; }
public string? Label { get; set; }
public bool IsTwoWay { get; set; }
public bool Orientation { get; set; }
public Brush? Highlight { get; set; }
public (bool TowardEnd, double PathOffset)? Flow { get; set; }
public double FlowLength => _flowArrows?.Length ?? 0;
public double X1 => Start.CenterX;
public double Y1 => Start.CenterY;
public double X2 => Orientation ? Start.CenterX : End.CenterX;
public double Y2 => Orientation ? End.CenterY : Start.CenterY;
public double X3 => End.CenterX;
public double Y3 => End.CenterY;
private Polyline? _outer;
private Polyline? _inner;
private TextBlock? _text;
private FlowArrows? _flowArrows;
private double? _textOffsetX;
public Pipe(INode start, INode end, string? label = null, bool orientation = false, bool twoWay = false, double? textOffsetX = null) {
Start = start;
End = end;
Label = label;
_textOffsetX = textOffsetX;
Orientation = orientation;
IsTwoWay = twoWay;
}
public void Draw(Canvas canvas) {
_outer = new Polyline() {
Stroke = Brushes.Black,
StrokeStartLineCap = PenLineCap.Round,
StrokeEndLineCap = PenLineCap.Round,
StrokeLineJoin = PenLineJoin.Round,
};
_inner = new Polyline() {
Stroke = Brushes.DarkGray,
StrokeStartLineCap = PenLineCap.Round,
StrokeEndLineCap = PenLineCap.Round,
StrokeLineJoin = PenLineJoin.Round,
};
canvas.Children.Add(_outer);
canvas.Children.Add(_inner);
if (Label != null) {
_text = new() {
Text = Label,
TextAlignment = TextAlignment.Center,
VerticalAlignment = VerticalAlignment.Bottom,
HorizontalAlignment = HorizontalAlignment.Center,
SnapsToDevicePixels = true,
};
canvas.Children.Add(_text);
}
_flowArrows = FlowArrows.Create(canvas, _inner);
}
public void Scale(double scale) {
var border = Graph.ToBorder(scale);
_outer?.Points = [Graph.ToPx(X1, Y1, scale), Graph.ToPx(X2, Y2, scale), Graph.ToPx(X3, Y3, scale)];
_inner?.Points = [Graph.ToPx(X1, Y1, scale), Graph.ToPx(X2, Y2, scale), Graph.ToPx(X3, Y3, scale)];
_inner?.StrokeThickness = Math.Round(1.2 * scale / 2) * 2;
_outer?.StrokeThickness = Math.Round(1.2 * scale / 2) * 2 + border * 2;
_text?.FontSize = 12;
_text?.Width = 10 * scale;
_text?.SetValue(Canvas.LeftProperty, (_textOffsetX != null ? (Orientation ? X2 : X1) + _textOffsetX : (Orientation ? (X2 + X3) / 2 : (X1 + X2) / 2) - 5) * scale);
_text?.SetValue(Canvas.TopProperty, Y2 * scale - 22);
}
public void Update() {
_inner?.Stroke = Highlight ?? Brushes.DarkGray;
_flowArrows?.Update(Flow);
}
}
}