using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Shapes; namespace PamhagenSysCtrl.Helpers.Pipeline { public class Rebler : INode { public const double WIDTH = 24; public const double HEIGHT = 12; public double CenterX { get; set; } public double CenterY { get; set; } public string Label { get; set; } public ISet Inputs { get; init; } public ISet Outputs { get; init; } public bool IsActive => CallbackActive(); protected Func CallbackActive; private TextBlock? _text; private Rectangle? _rect; private Polygon? _hopperOuter; private Polygon? _hopperInner; private Polygon? _bottomInner; private Polygon? _bottomOuter; public Rebler(string label, double x, double y, Func cbActive) { CenterX = x; CenterY = y; Label = label; CallbackActive = cbActive; Inputs = new HashSet(); Outputs = new HashSet(); } public void Draw(Canvas canvas) { _rect = new Rectangle() { Fill = Brushes.WhiteSmoke, Stroke = Brushes.Black, StrokeThickness = 2, }; _hopperOuter = new Polygon() { Fill = Brushes.Black, }; _hopperInner = new Polygon() { Fill = Brushes.WhiteSmoke, }; _bottomOuter = new Polygon() { Fill = Brushes.Black, }; _bottomInner = new Polygon() { Fill = Brushes.WhiteSmoke, }; _text = new() { Text = Label, TextAlignment = TextAlignment.Center, VerticalAlignment = VerticalAlignment.Center, }; canvas.Children.Add(_hopperOuter); canvas.Children.Add(_bottomOuter); canvas.Children.Add(_bottomInner); canvas.Children.Add(_rect); canvas.Children.Add(_hopperInner); canvas.Children.Add(_text); } public void Scale(double scale) { var border = Graph.ToBorder(scale); Graph.SetCenter(_rect, CenterX, CenterY, WIDTH, HEIGHT, scale); _rect?.StrokeThickness = border; var left = CenterX - WIDTH / 2; var right = CenterX + WIDTH / 2; var top = CenterY - HEIGHT / 2; var bottom = CenterY + HEIGHT / 2; var hx = right - 4; var h = 3; var w = 5; _hopperOuter?.Points = [Graph.ToPx(hx - w, top - h, scale), Graph.ToPx(hx - w + 1, top, scale, dypx: border), Graph.ToPx(hx + w - 1, top, scale, dypx: border), Graph.ToPx(hx + w, top - h, scale)]; _hopperInner?.Points = [Graph.ToPx(hx - w, top - h, scale, dxpx: border), Graph.ToPx(hx - w + 1, top, scale, dxpx: border, dypx: border), Graph.ToPx(hx + w - 1, top, scale, dxpx: -border, dypx: border), Graph.ToPx(hx + w, top - h, scale, dxpx: -border)]; _bottomOuter?.Points = [Graph.ToPx(left + 5, bottom - 1, scale), Graph.ToPx(right - 5, bottom - 1, scale), Graph.ToPx(right - 3, bottom + 2, scale), Graph.ToPx(left + 3, bottom + 2, scale)]; _bottomInner?.Points = [Graph.ToPx(left + 5, bottom - 1, scale, dxpx: border), Graph.ToPx(right - 5, bottom - 1, scale, dxpx: -border), Graph.ToPx(right - 3, bottom + 2, scale, dxpx: -border), Graph.ToPx(left + 3, bottom + 2, scale, dxpx: border)]; _text?.FontSize = 12; _text?.Width = WIDTH * scale; _text?.SetValue(Canvas.LeftProperty, (CenterX - WIDTH / 2) * scale); _text?.SetValue(Canvas.TopProperty, (CenterY * scale) - 8); } public bool IsInside(double x, double y) { return Math.Abs(x - CenterX) <= WIDTH / 2 && Math.Abs(y - CenterY) <= HEIGHT / 2; } public void Update(bool isHovering, bool isPressed) { var color = IsActive && isHovering && isPressed ? PamhagenBrushes.Red : IsActive && isHovering ? PamhagenBrushes.Red : !IsActive && isHovering && isPressed ? PamhagenBrushes.Green : !IsActive && isHovering ? PamhagenBrushes.Green : IsActive ? PamhagenBrushes.Green : Brushes.WhiteSmoke; _rect?.Fill = color; _hopperInner?.Fill = color; _bottomInner?.Fill = color; } } }