90 lines
3.7 KiB
C#
90 lines
3.7 KiB
C#
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Media;
|
|
using System.Windows.Shapes;
|
|
|
|
namespace PamhagenSysCtrl.Helpers.Pipeline {
|
|
public class Press : INode, ISink {
|
|
|
|
public const double DIAMETER = 24;
|
|
|
|
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 Polygon? _frame;
|
|
private Ellipse? _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() {
|
|
Stroke = Brushes.Black,
|
|
Fill = Brushes.WhiteSmoke,
|
|
};
|
|
_frame = new Polygon() {
|
|
Stroke = Brushes.Black,
|
|
Fill = Brushes.WhiteSmoke,
|
|
};
|
|
_text = new() {
|
|
Text = CapacityLiters.HasValue ? $"{Label}\n{CapacityLiters:N0}" : Label,
|
|
TextAlignment = TextAlignment.Center,
|
|
VerticalAlignment = VerticalAlignment.Center,
|
|
};
|
|
canvas.Children.Add(_frame);
|
|
canvas.Children.Add(_circle);
|
|
canvas.Children.Add(_text);
|
|
}
|
|
|
|
public void Scale(double scale) {
|
|
var border = Graph.ToBorder(scale);
|
|
_circle?.StrokeThickness = border;
|
|
Graph.SetCenter(_circle, CenterX, CenterY, DIAMETER, DIAMETER, scale);
|
|
_frame?.Points = [
|
|
Graph.ToPx(CenterX - DIAMETER / 2 - 2, CenterY, scale), Graph.ToPx(CenterX + DIAMETER / 2 + 2, CenterY, scale),
|
|
Graph.ToPx(CenterX + DIAMETER / 2 + 2, CenterY + DIAMETER / 2 + 6, scale), Graph.ToPx(CenterX + DIAMETER / 2 - 4, CenterY + DIAMETER / 2 + 6, scale),
|
|
Graph.ToPx(CenterX + DIAMETER / 2 - 4, CenterY + DIAMETER / 2 + 2, scale), Graph.ToPx(CenterX - DIAMETER / 2 + 4, CenterY + DIAMETER / 2 + 2, scale),
|
|
Graph.ToPx(CenterX - DIAMETER / 2 + 4, CenterY + DIAMETER / 2 + 6, scale), Graph.ToPx(CenterX - DIAMETER / 2 - 2, CenterY + DIAMETER / 2 + 6, scale),
|
|
];
|
|
_frame?.StrokeThickness = border;
|
|
_text?.FontSize = 14;
|
|
_text?.Width = DIAMETER * scale;
|
|
_text?.SetValue(Canvas.LeftProperty, (CenterX - DIAMETER / 2) * scale);
|
|
_text?.SetValue(Canvas.TopProperty, (CenterY * scale) - (CapacityLiters.HasValue ? 20 : 10));
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|