Files
pamhagen-sysctrl/PamhagenSysCtrl/Helpers/Pipeline/Tank.cs
T
lorenz.stechauner f251994c25
Test / Run tests (push) Successful in 13s
Add scaling to PlantSchemeWindow
2026-08-14 09:59:16 +02:00

81 lines
2.9 KiB
C#

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace PamhagenSysCtrl.Helpers.Pipeline {
public class Tank : INode, ISink, ISource {
public const double WIDTH = 12;
public const double HEIGHT = 20;
public double CenterX { get; set; }
public double CenterY { get; set; }
public double TopY => CenterY - HEIGHT / 2;
public double BottomY => CenterY + HEIGHT / 2;
public string Label { get; set; }
public FillState FillState => CallbackFillState();
public bool IsFull => FillState == FillState.Full;
public bool IsFilled => FillState != FillState.Empty;
public ISet<IEdge> Inputs { get; init; }
public ISet<IEdge> Outputs { get; init; }
public int? CapacityLiters { get; set; }
protected Func<FillState> CallbackFillState;
private Shape? _rect;
private TextBlock? _text;
public Tank(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) {
_rect = new Rectangle() {
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(_rect);
canvas.Children.Add(_text);
}
public void Scale(double scale) {
var border = Graph.ToBorder(scale);
_rect?.Height = Graph.ToPx(HEIGHT, scale);
_rect?.Width = Graph.ToPx(WIDTH, scale);
_rect?.StrokeThickness = border;
_rect?.SetValue(Canvas.LeftProperty, (CenterX - WIDTH / 2) * scale);
_rect?.SetValue(Canvas.TopProperty, (CenterY - HEIGHT / 2) * scale);
_text?.Width = Graph.ToPx(WIDTH, scale);
_text?.FontSize = 12;
_text?.SetValue(Canvas.LeftProperty, (CenterX - WIDTH / 2) * scale);
_text?.SetValue(Canvas.TopProperty, (CenterY * scale) - (CapacityLiters.HasValue ? 16 : 8));
}
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) {
_rect?.Fill = PamhagenBrushes.ForFillState(FillState,
isHovering && isPressed ? Brushes.DarkGray :
isHovering ? Brushes.LightGray :
Brushes.WhiteSmoke
);
}
}
}