Files
pamhagen-sysctrl/PamhagenSysCtrl/Helpers/Pipeline/Press.cs
T
2026-07-16 01:11:31 +02:00

90 lines
3.6 KiB
C#

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Shapes;
namespace PamhagenSysCtrl.Helpers.Pipeline {
public class Press : INode, ISink {
public const double DIAMETER = 120;
public double CenterX { get; set; }
public double CenterY { get; set; }
public string Label { get; set; }
public ISet<IEdge> Inputs { get; set; }
public ISet<IEdge> Outputs { get; set; }
public double TopY => CenterY - DIAMETER / 2;
public FillState FillState => CallbackFillState();
public bool IsFull => FillState == FillState.Full;
public int? CapacityLiters { get; set; }
protected Func<FillState> CallbackFillState;
private Shape? _frame;
private Shape? _circle;
private TextBlock? _text;
public Press(string label, double x, double y, int capacityLiters, Func<FillState> cbFillState) {
CenterX = x;
CenterY = y;
Label = label;
CapacityLiters = capacityLiters;
CallbackFillState = cbFillState;
Inputs = new HashSet<IEdge>();
Outputs = new HashSet<IEdge>();
}
public void Draw(Canvas canvas) {
_circle = new Ellipse() {
Width = DIAMETER,
Height = DIAMETER,
Stroke = Brushes.Black,
StrokeThickness = 2,
Fill = Brushes.WhiteSmoke,
};
_frame = new Polygon() {
Points = [
new(CenterX - DIAMETER / 2 - 10, CenterY), new(CenterX + DIAMETER / 2 + 10, CenterY),
new(CenterX + DIAMETER / 2 + 10, CenterY + DIAMETER / 2 + 30), new(CenterX + DIAMETER / 2 - 20, CenterY + DIAMETER / 2 + 30),
new(CenterX + DIAMETER / 2 - 20, CenterY + DIAMETER / 2 + 10), new(CenterX - DIAMETER / 2 + 20, CenterY + DIAMETER / 2 + 10),
new(CenterX - DIAMETER / 2 + 20, CenterY + DIAMETER / 2 + 30), new(CenterX - DIAMETER / 2 - 10, CenterY + DIAMETER / 2 + 30)
],
Stroke = Brushes.Black,
StrokeThickness = 2,
Fill = Brushes.WhiteSmoke,
};
_text = new() {
Text = CapacityLiters.HasValue ? $"{Label}\n{CapacityLiters:N0}" : Label,
FontSize = 14,
Width = DIAMETER,
TextAlignment = TextAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
_circle.SetValue(Canvas.LeftProperty, CenterX - DIAMETER / 2);
_circle.SetValue(Canvas.TopProperty, CenterY - DIAMETER / 2);
_text.SetValue(Canvas.LeftProperty, CenterX - DIAMETER / 2);
_text.SetValue(Canvas.TopProperty, CenterY - (CapacityLiters.HasValue ? 20 : 10));
canvas.Children.Add(_frame);
canvas.Children.Add(_circle);
canvas.Children.Add(_text);
}
public bool IsInside(double x, double y) {
return Math.Sqrt(Math.Pow(CenterX - x, 2) + Math.Pow(CenterY - y, 2)) <= DIAMETER / 2 || (Math.Abs(x - CenterX) <= DIAMETER / 2 + 10 && y >= CenterY && y <= CenterY + DIAMETER / 2 + 30);
}
public void Update(bool isHovering, bool isPressed) {
var color =
isHovering && isPressed ? Brushes.DarkGray :
isHovering ? Brushes.LightGray :
Brushes.WhiteSmoke;
_circle?.Fill = PamhagenBrushes.ForFillState(FillState, color);
_frame?.Fill = color;
}
}
}