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 { /// /// UserControl para la bomba hidráulica de ejemplo /// public partial class ucHydPump : UserControl, IDataContainer { public osBase? Datos { get; set; } public int zIndex_fromFrames { get; set; } = 0; private Storyboard? _rotationAnimation; private bool _isHighlighted = false; public ucHydPump() { 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 osHydPump pump) { Datos = pump; pump.PropertyChanged += OnPumpPropertyChanged; UpdateVisualState(); } } private void OnPumpPropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == nameof(osHydPump.IsRunning) || e.PropertyName == nameof(osHydPump.SpeedRatio) || e.PropertyName == nameof(osHydPump.PumpDirection) || e.PropertyName == nameof(osHydPump.CurrentFlow) || e.PropertyName == nameof(osHydPump.CurrentPressure)) { UpdateVisualState(); } } /// /// Actualiza el estado visual de la bomba /// private void UpdateVisualState() { if (Datos is not osHydPump 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}"); } } /// /// Actualiza el LED de estado /// private void UpdateStatusLED(osHydPump pump) { if (pump.IsRunning && pump.SpeedRatio > 0) { StatusLED.Fill = new SolidColorBrush(Colors.LimeGreen); } else { StatusLED.Fill = new SolidColorBrush(Colors.Red); } } /// /// Actualiza la animación de rotación /// private void UpdateRotationAnimation(osHydPump 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; } } /// /// Actualiza la flecha de dirección /// private void UpdateDirectionArrow(osHydPump 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); } } /// /// Actualiza la información de estado /// private void UpdateStatusInfo(osHydPump 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 osHydPump 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; } ~ucHydPump() { _rotationAnimation?.Stop(); } } }