Files
pamhagen-sysctrl/PamhagenSysCtrl/Helpers/Pipeline/ConveyorBelt.cs
T
2026-08-04 23:56:32 +02:00

114 lines
4.0 KiB
C#

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace PamhagenSysCtrl.Helpers.Pipeline {
public class ConveyorBelt : INode {
public const double WIDTH = 30;
public const double HEIGHT = 120;
public double CenterX { get; set; }
public double CenterY { get; set; }
public string Label { get; set; }
public ISet<IEdge> Inputs { get; init; }
public ISet<IEdge> Outputs { get; init; }
public bool IsActive => CallbackActive();
public bool IsAnimationActive {
get => _animationActive;
set {
_animationActive = value;
if (value) {
_storyboard?.Resume();
} else {
_storyboard?.Pause();
}
}
}
protected Func<bool> CallbackActive;
private bool _animationActive;
private Shape? _rect;
private Polyline? _polyline;
private Storyboard? _storyboard;
public ConveyorBelt(string label, double x, double y, Func<bool> cbActive) {
CenterX = x;
CenterY = y;
Label = label;
CallbackActive = cbActive;
Inputs = new HashSet<IEdge>();
Outputs = new HashSet<IEdge>();
}
public void Draw(Canvas canvas) {
_rect = new Rectangle() {
Width = WIDTH,
Height = HEIGHT,
Stroke = Brushes.Black,
StrokeThickness = 2,
Fill = Brushes.WhiteSmoke,
StrokeLineJoin = PenLineJoin.Round,
};
_rect.SetValue(Canvas.LeftProperty, CenterX - WIDTH / 2);
_rect.SetValue(Canvas.TopProperty, CenterY - HEIGHT / 2);
var d = 15;
int n = (int)(HEIGHT / d) + 2;
_polyline = new Polyline() {
Points = [.. Enumerable.Range(0, n).SelectMany(i => new List<Point>() {
new(-WIDTH, i * d), new(WIDTH, i * d), new(-WIDTH, i * d)
})],
Fill = Brushes.Transparent,
Stroke = Brushes.Black,
StrokeThickness = 2,
};
_polyline.SetValue(Canvas.TopProperty, 0.0);
var slideUp = new DoubleAnimation {
From = 0,
To = -d,
Duration = new Duration(TimeSpan.FromSeconds(1)),
RepeatBehavior = RepeatBehavior.Forever,
};
_storyboard = new Storyboard();
_storyboard.Children.Add(slideUp);
Storyboard.SetTarget(slideUp, _polyline);
Storyboard.SetTargetProperty(slideUp, new PropertyPath(Canvas.TopProperty));
var clipCanvas = new Canvas() {
Width = WIDTH - 15,
Height = HEIGHT - 8,
ClipToBounds = true,
};
clipCanvas.SetValue(Canvas.TopProperty, CenterY - HEIGHT / 2 + 4);
clipCanvas.SetValue(Canvas.LeftProperty, CenterX - WIDTH / 2 + 7.5);
clipCanvas.Children.Add(_polyline);
canvas.Children.Add(_rect);
canvas.Children.Add(clipCanvas);
_storyboard.Begin();
_storyboard.Pause();
}
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) {
IsAnimationActive = IsActive;
_rect?.Fill =
IsAnimationActive && isHovering && isPressed ? PamhagenBrushes.Red :
IsAnimationActive && isHovering ? PamhagenBrushes.Red :
!IsAnimationActive && isHovering && isPressed ? PamhagenBrushes.Green :
!IsAnimationActive && isHovering ? PamhagenBrushes.Green :
IsAnimationActive ? PamhagenBrushes.Green :
Brushes.WhiteSmoke;
}
}
}