72 lines
2.4 KiB
C#
72 lines
2.4 KiB
C#
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Media;
|
|
using System.Windows.Shapes;
|
|
|
|
namespace PamhagenSysCtrl.Helpers.Pipeline {
|
|
public class CenterValve : INode {
|
|
|
|
public const double DIAMETER = 4.8;
|
|
|
|
public double CenterX { get; set; }
|
|
public double CenterY { get; set; }
|
|
public string Label { get; set; }
|
|
public Brush? Highlight { get; set; }
|
|
|
|
public bool IsOpen => CallbackIsOpen();
|
|
public ISet<IEdge> Inputs { get; init; }
|
|
public ISet<IEdge> Outputs { get; init; }
|
|
|
|
protected Func<bool> CallbackIsOpen;
|
|
|
|
private Shape? _circle;
|
|
private TextBlock? _text;
|
|
|
|
public CenterValve(string label, double x, double y, Func<bool> cbOpen) {
|
|
CenterX = x;
|
|
CenterY = y;
|
|
Label = label;
|
|
CallbackIsOpen = cbOpen;
|
|
Inputs = new HashSet<IEdge>();
|
|
Outputs = new HashSet<IEdge>();
|
|
}
|
|
|
|
public void Draw(Canvas canvas) {
|
|
_circle = new Ellipse() {
|
|
Fill = Brushes.DarkGray,
|
|
Stroke = Brushes.Black,
|
|
};
|
|
_text = new() {
|
|
Text = Label,
|
|
TextAlignment = TextAlignment.Center,
|
|
VerticalAlignment = VerticalAlignment.Center,
|
|
SnapsToDevicePixels = true,
|
|
};
|
|
canvas.Children.Add(_circle);
|
|
canvas.Children.Add(_text);
|
|
}
|
|
|
|
public void Scale(double scale) {
|
|
var border = Graph.ToBorder(scale);
|
|
Graph.SetCenter(_circle, CenterX, CenterY, DIAMETER, DIAMETER, scale);
|
|
_circle?.StrokeThickness = border;
|
|
_text?.Width = DIAMETER * scale;
|
|
_text?.Height = DIAMETER * scale;
|
|
_text?.FontSize = 2 * scale;
|
|
_text?.SetValue(Canvas.LeftProperty, (CenterX - DIAMETER / 2 + 0.1) * scale);
|
|
_text?.SetValue(Canvas.TopProperty, (CenterY - DIAMETER / 2 + 1.1) * scale);
|
|
}
|
|
|
|
public bool IsInside(double x, double y) {
|
|
return Math.Sqrt(Math.Pow(CenterX - x, 2) + Math.Pow(CenterY - y, 2)) <= DIAMETER / 2;
|
|
}
|
|
|
|
public void Update(bool isHovering, bool isPressed) {
|
|
_circle?.Fill =
|
|
!IsOpen ? PamhagenBrushes.Red :
|
|
IsOpen ? PamhagenBrushes.Green :
|
|
Highlight ?? Brushes.DarkGray;
|
|
}
|
|
}
|
|
}
|