Files
pamhagen-sysctrl/PamhagenSysCtrl/Helpers/Pipeline/Trough.cs
T
lorenz.stechauner e626ae1910
Test / Run tests (push) Successful in 14s
Add stirrers to graph
2026-11-06 14:02:56 +01:00

92 lines
3.5 KiB
C#

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace PamhagenSysCtrl.Helpers.Pipeline {
public class Trough : INode, ISource {
public const double WIDTH = 20;
public const double HEIGHT = 12;
public double CenterX { get; set; }
public double CenterY { get; set; }
public double BottomY => CenterY + HEIGHT / 2;
public string Label { get; set; }
public FillState FillState => CallbackFillState();
public bool IsFilled => FillState != FillState.Empty;
public bool IsStirrerActive => CallbackStirrerActive();
public ISet<IEdge> Inputs { get; init; }
public ISet<IEdge> Outputs { get; init; }
protected Func<FillState> CallbackFillState;
protected Func<bool> CallbackStirrerActive;
private Shape? _inner;
private Shape? _outer;
private TextBlock? _text;
private Stirrer _stirrer;
private readonly bool _stirrerLeft;
public Trough(string label, double x, double y, bool stirrerLeft, Func<FillState> cbFillState, Func<bool> cbStirrerActive) {
CenterX = x;
CenterY = y;
Label = label;
CallbackFillState = cbFillState;
CallbackStirrerActive = cbStirrerActive;
Inputs = new HashSet<IEdge>();
Outputs = new HashSet<IEdge>();
_stirrer = new(x + (stirrerLeft ? -4 : 4), y, 4, stirrerLeft);
_stirrerLeft = stirrerLeft;
}
public void Draw(Canvas canvas) {
_outer = new Rectangle() {
Fill = Brushes.Black,
};
_inner = new Rectangle() {
Fill = Brushes.WhiteSmoke,
};
_text = new() {
Text = Label,
TextAlignment = TextAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
canvas.Children.Add(_outer);
canvas.Children.Add(_inner);
canvas.Children.Add(_text);
_stirrer.Draw(canvas);
}
public void Scale(double scale) {
var border = Graph.ToBorder(scale);
_outer?.Width = WIDTH * scale;
_outer?.Height = HEIGHT * scale;
_inner?.Width = WIDTH * scale - border * 2;
_inner?.Height = HEIGHT * scale - border;
_text?.Width = WIDTH * scale;
_text?.FontSize = 14;
_outer?.SetValue(Canvas.LeftProperty, (CenterX - WIDTH / 2) * scale);
_outer?.SetValue(Canvas.TopProperty, (CenterY - HEIGHT / 2) * scale);
_inner?.SetValue(Canvas.LeftProperty, (CenterX - WIDTH / 2) * scale + border);
_inner?.SetValue(Canvas.TopProperty, (CenterY - HEIGHT / 2) * scale);
_text?.SetValue(Canvas.LeftProperty, (CenterX - WIDTH / 2 + (_stirrerLeft ? 5 : -5)) * scale);
_text?.SetValue(Canvas.TopProperty, (CenterY * scale) - 10);
_stirrer.Scale(scale);
}
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) {
_inner?.Fill = PamhagenBrushes.ForFillState(FillState,
isHovering && isPressed ? Brushes.DarkGray :
isHovering ? Brushes.LightGray :
Brushes.WhiteSmoke
);
_stirrer.IsAnimationActive = IsStirrerActive;
}
}
}