226 lines
7.2 KiB
C#
226 lines
7.2 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Animation;
|
|
using CtrEditor.ObjetosSim;
|
|
using CtrEditor.FuncionesBase;
|
|
|
|
namespace CtrEditor.ObjetosSim.HydraulicComponents
|
|
{
|
|
/// <summary>
|
|
/// UserControl para la bomba hidráulica de ejemplo
|
|
/// </summary>
|
|
public partial class ucPumpExample : UserControl, IDataContainer
|
|
{
|
|
public osBase? Datos { get; set; }
|
|
public int zIndex_fromFrames { get; set; } = 0;
|
|
|
|
private Storyboard? _rotationAnimation;
|
|
private bool _isHighlighted = false;
|
|
|
|
public ucPumpExample()
|
|
{
|
|
InitializeComponent();
|
|
this.Loaded += OnLoaded;
|
|
this.Unloaded += OnUnloaded;
|
|
DataContextChanged += OnDataContextChanged;
|
|
}
|
|
|
|
private void OnLoaded(object sender, RoutedEventArgs e)
|
|
{
|
|
Datos?.ucLoaded();
|
|
UpdateVisualState();
|
|
}
|
|
|
|
private void OnUnloaded(object sender, RoutedEventArgs e)
|
|
{
|
|
Datos?.ucUnLoaded();
|
|
}
|
|
|
|
private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
if (DataContext is osPumpExample pump)
|
|
{
|
|
Datos = pump;
|
|
pump.PropertyChanged += OnPumpPropertyChanged;
|
|
UpdateVisualState();
|
|
}
|
|
}
|
|
|
|
private void OnPumpPropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
|
|
{
|
|
if (e.PropertyName == nameof(osPumpExample.IsRunning) ||
|
|
e.PropertyName == nameof(osPumpExample.SpeedRatio) ||
|
|
e.PropertyName == nameof(osPumpExample.PumpDirection) ||
|
|
e.PropertyName == nameof(osPumpExample.CurrentFlow) ||
|
|
e.PropertyName == nameof(osPumpExample.CurrentPressure))
|
|
{
|
|
UpdateVisualState();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Actualiza el estado visual de la bomba
|
|
/// </summary>
|
|
private void UpdateVisualState()
|
|
{
|
|
if (Datos is not osPumpExample pump) return;
|
|
|
|
try
|
|
{
|
|
Dispatcher.BeginInvoke(() =>
|
|
{
|
|
UpdateStatusLED(pump);
|
|
UpdateRotationAnimation(pump);
|
|
UpdateDirectionArrow(pump);
|
|
UpdateStatusInfo(pump);
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine($"Error updating pump visual state: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Actualiza el LED de estado
|
|
/// </summary>
|
|
private void UpdateStatusLED(osPumpExample pump)
|
|
{
|
|
if (pump.IsRunning && pump.SpeedRatio > 0)
|
|
{
|
|
StatusLED.Fill = new SolidColorBrush(Colors.LimeGreen);
|
|
}
|
|
else
|
|
{
|
|
StatusLED.Fill = new SolidColorBrush(Colors.Red);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Actualiza la animación de rotación
|
|
/// </summary>
|
|
private void UpdateRotationAnimation(osPumpExample pump)
|
|
{
|
|
_rotationAnimation?.Stop();
|
|
|
|
if (pump.IsRunning && pump.SpeedRatio > 0)
|
|
{
|
|
// Crear animación de rotación
|
|
var rotateTransform = new RotateTransform();
|
|
PumpBackground.RenderTransform = rotateTransform;
|
|
PumpBackground.RenderTransformOrigin = new Point(0.5, 0.5);
|
|
|
|
var animation = new DoubleAnimation
|
|
{
|
|
From = 0,
|
|
To = 360,
|
|
Duration = TimeSpan.FromSeconds(2.0 / pump.SpeedRatio), // Más rápido con mayor velocidad
|
|
RepeatBehavior = RepeatBehavior.Forever
|
|
};
|
|
|
|
_rotationAnimation = new Storyboard();
|
|
Storyboard.SetTarget(animation, rotateTransform);
|
|
Storyboard.SetTargetProperty(animation, new PropertyPath(RotateTransform.AngleProperty));
|
|
_rotationAnimation.Children.Add(animation);
|
|
_rotationAnimation.Begin();
|
|
}
|
|
else
|
|
{
|
|
PumpBackground.RenderTransform = null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Actualiza la flecha de dirección
|
|
/// </summary>
|
|
private void UpdateDirectionArrow(osPumpExample pump)
|
|
{
|
|
if (pump.PumpDirection == -1)
|
|
{
|
|
ArrowRotation.Angle = 180; // Invertir flecha
|
|
DirectionArrow.Fill = new SolidColorBrush(Colors.Orange);
|
|
}
|
|
else
|
|
{
|
|
ArrowRotation.Angle = 0;
|
|
DirectionArrow.Fill = new SolidColorBrush(Colors.DarkBlue);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Actualiza la información de estado
|
|
/// </summary>
|
|
private void UpdateStatusInfo(osPumpExample pump)
|
|
{
|
|
FlowText.Text = $"{pump.CurrentFlowLMin:F1} L/min";
|
|
PressureText.Text = $"{pump.CurrentPressureBar:F2} bar";
|
|
|
|
// Mostrar información si hay datos relevantes
|
|
if (Math.Abs(pump.CurrentFlow) > 0.001 || Math.Abs(pump.CurrentPressure) > 1000)
|
|
{
|
|
StatusInfo.Visibility = Visibility.Visible;
|
|
}
|
|
else
|
|
{
|
|
StatusInfo.Visibility = Visibility.Collapsed;
|
|
}
|
|
}
|
|
|
|
#region IDataContainer Implementation
|
|
|
|
public void Highlight(bool state)
|
|
{
|
|
_isHighlighted = state;
|
|
|
|
if (state)
|
|
{
|
|
PumpBackground.Stroke = new SolidColorBrush(Colors.Yellow);
|
|
PumpBackground.StrokeThickness = 3;
|
|
}
|
|
else
|
|
{
|
|
PumpBackground.Stroke = new SolidColorBrush(Colors.DarkSlateGray);
|
|
PumpBackground.StrokeThickness = 2;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e)
|
|
{
|
|
base.OnMouseEnter(e);
|
|
if (!_isHighlighted)
|
|
{
|
|
// Mostrar información al pasar el mouse
|
|
StatusInfo.Visibility = Visibility.Visible;
|
|
}
|
|
}
|
|
|
|
protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
|
|
{
|
|
base.OnMouseLeave(e);
|
|
if (!_isHighlighted && Datos is osPumpExample pump)
|
|
{
|
|
// Ocultar información si no hay datos relevantes
|
|
if (Math.Abs(pump.CurrentFlow) < 0.001 && Math.Abs(pump.CurrentPressure) < 1000)
|
|
{
|
|
StatusInfo.Visibility = Visibility.Collapsed;
|
|
}
|
|
}
|
|
}
|
|
|
|
public ZIndexEnum ZIndex_Base()
|
|
{
|
|
return ZIndexEnum.Estaticos;
|
|
}
|
|
|
|
~ucPumpExample()
|
|
{
|
|
_rotationAnimation?.Stop();
|
|
}
|
|
}
|
|
}
|