Files
pamhagen-sysctrl/PamhagenSysCtrl/Helpers/Pipeline/PipeJoin.cs
T
2026-07-13 01:15:01 +02:00

79 lines
3.7 KiB
C#

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace PamhagenSysCtrl.Helpers.Pipeline {
public class PipeJoin : INode {
public const double SIZE = 18;
public const double POINT = 4;
public double CenterX { get; set; }
public double CenterY { get; set; }
public string Label { get; } = "";
public ISet<IEdge> Inputs { get; init; }
public ISet<IEdge> Outputs { get; init; }
private Polygon? _rect;
public PipeJoin(double x, double y) {
CenterX = x;
CenterY = y;
Inputs = new HashSet<IEdge>();
Outputs = new HashSet<IEdge>();
}
public void Draw(Canvas canvas) {
bool? top = Inputs.Any(e => e.Start.CenterY < CenterY && (!e.Orientation || e.Start.CenterX == CenterX)) ? false : Outputs.Any(e => e.End.CenterY < CenterY && (e.Orientation || e.Start.CenterX == CenterX)) ? true : null;
bool? right = Inputs.Any(e => e.Start.CenterX > CenterX && (e.Orientation || e.Start.CenterY == CenterY)) ? false : Outputs.Any(e => e.End.CenterX > CenterX && (!e.Orientation || e.Start.CenterY == CenterY)) ? true : null;
bool? bottom = Inputs.Any(e => e.Start.CenterY > CenterY && (!e.Orientation || e.Start.CenterX == CenterX)) ? false : Outputs.Any(e => e.End.CenterY > CenterY && (e.Orientation || e.Start.CenterX == CenterX)) ? true : null;
bool? left = Inputs.Any(e => e.Start.CenterX < CenterX && (e.Orientation || e.Start.CenterY == CenterY)) ? false : Outputs.Any(e => e.End.CenterX < CenterX && (!e.Orientation || e.Start.CenterY == CenterY)) ? true : null;
var points = new List<Point> {
new(0, 0)
};
if (top == true) {
points.AddRange([new(SIZE / 2 - POINT, 0), new(SIZE / 2, -POINT), new(SIZE / 2 + POINT, 0)]);
} else if (top == false) {
points.AddRange([new(SIZE / 2 - POINT, 0), new(SIZE / 2, POINT), new(SIZE / 2 + POINT, 0)]);
}
points.Add(new(SIZE, 0));
if (right == true) {
points.AddRange([new(SIZE, SIZE / 2 - POINT), new(SIZE + POINT, SIZE / 2), new(SIZE, SIZE / 2 + POINT)]);
} else if (right == false) {
points.AddRange([new(SIZE, SIZE / 2 - POINT), new(SIZE - POINT, SIZE / 2), new(SIZE, SIZE / 2 + POINT)]);
}
points.Add(new(SIZE, SIZE));
if (bottom == true) {
points.AddRange([new(SIZE / 2 + POINT, SIZE), new(SIZE / 2, SIZE + POINT), new(SIZE / 2 - POINT, SIZE)]);
} else if (bottom == false) {
points.AddRange([new(SIZE / 2 + POINT, SIZE), new(SIZE / 2, SIZE - POINT), new(SIZE / 2 - POINT, SIZE)]);
}
points.Add(new(0, SIZE));
if (left == true) {
points.AddRange([new(0, SIZE / 2 + POINT), new(-POINT, SIZE / 2), new(0, SIZE / 2 - POINT)]);
} else if (left == false) {
points.AddRange([new(0, SIZE / 2 + POINT), new(POINT, SIZE / 2), new(0, SIZE / 2 - POINT)]);
}
_rect = new Polygon() {
Points = [..points],
Fill = Brushes.Black,
Stroke = Brushes.Black,
StrokeThickness = 2,
};
_rect.SetValue(Canvas.LeftProperty, CenterX - SIZE / 2);
_rect.SetValue(Canvas.TopProperty, CenterY - SIZE / 2);
canvas.Children.Add(_rect);
}
public bool Update(double x, double y) {
var hover = Math.Abs(x - CenterX) <= SIZE / 2 && Math.Abs(y - CenterY) <= SIZE / 2;
_rect?.Stroke = hover ? Brushes.Black : Brushes.Black;
return hover;
}
}
}