Files
pamhagen-sysctrl/PamhagenSysCtrl/Helpers/Pipeline/EntryTrough.cs
T
lorenz.stechauner 35a25320ae
Test / Run tests (push) Successful in 13s
Remove BOM
2026-08-05 16:37:18 +02:00

117 lines
4.6 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 {
class EntryTrough : INode {
public const double WIDTH = 150;
public const double HEIGHT = 60;
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;
_auger?.IsAnimationActive = value;
if (value) {
_doorTransform?.BeginAnimation(
RotateTransform.AngleProperty,
new DoubleAnimation {
From = _doorTransform.Angle,
To = 60,
Duration = TimeSpan.FromSeconds(Math.Abs(_doorTransform.Angle - 60) / 60.0)
});
} else {
_doorTransform?.BeginAnimation(
RotateTransform.AngleProperty,
new DoubleAnimation {
From = _doorTransform.Angle,
To = 0,
Duration = TimeSpan.FromSeconds(Math.Abs(_doorTransform.Angle - 0) / 60.0)
});
}
}
}
protected Func<bool> CallbackActive;
private bool _animationActive;
private Shape? _inner;
private Shape? _outer;
private TextBlock? _text;
private Auger? _auger;
private Shape? _door;
private RotateTransform? _doorTransform;
public EntryTrough(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) {
var top = CenterY - HEIGHT / 2;
var right = CenterX + WIDTH / 2;
var bottom = CenterY + HEIGHT / 2;
var left = CenterX - WIDTH / 2;
_outer = new Polygon() {
Points = [new(left, top), new(right, top), new(right - 20, bottom), new(left, bottom), new(left, bottom - 20), new(left + 12.5, bottom - 20)],
Fill = Brushes.Black,
};
_inner = new Polygon() {
Points = [new(left + 2, top), new(right - 2, top), new(right - 20 - 2, bottom - 2), new(left, bottom - 2), new(left, bottom - 18), new(left + 15.5, bottom - 18)],
Fill = Brushes.WhiteSmoke,
};
_doorTransform = new RotateTransform(0, left, bottom - 24);
_door = new Line() {
X1 = left, Y1 = bottom - 24, X2 = left, Y2 = bottom + 4,
Stroke = Brushes.Black,
StrokeThickness = 4,
RenderTransform = _doorTransform,
};
_text = new() {
Text = Label,
FontSize = 12,
Width = WIDTH,
TextAlignment = TextAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
_text.SetValue(Canvas.LeftProperty, CenterX - WIDTH / 2);
_text.SetValue(Canvas.TopProperty, CenterY - 7.5 - 8);
canvas.Children.Add(_outer);
canvas.Children.Add(_inner);
canvas.Children.Add(_text);
canvas.Children.Add(_door);
_auger = new(CenterX - 11, CenterY + HEIGHT / 2 - 9, WIDTH - 25, 12);
_auger.Draw(canvas);
}
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;
_inner?.Fill =
IsAnimationActive && isHovering && isPressed ? PamhagenBrushes.Red :
IsAnimationActive && isHovering ? PamhagenBrushes.Red :
!IsAnimationActive && isHovering && isPressed ? PamhagenBrushes.Green :
!IsAnimationActive && isHovering ? PamhagenBrushes.Green :
IsAnimationActive ? PamhagenBrushes.Green :
Brushes.WhiteSmoke;
}
}
}