CtrEditor/ObjetosSim/UserControls/ThreeLinesControl.xaml.cs

154 lines
5.1 KiB
C#

using CommunityToolkit.Mvvm.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace CtrEditor.ObjetosSim.UserControls
{
/// <summary>
/// Interaction logic for ThreeLinesControl.xaml
/// </summary>
///
[ObservableObject]
public partial class ThreeLinesControl : UserControl
{
private Rectangle _liz;
private Rectangle _lc;
private Rectangle _lde;
public ThreeLinesControl()
{
InitializeComponent();
this.Loaded += ThreeLinesControl_Loaded;
Color = Brushes.Gray;
}
private void ThreeLinesControl_Loaded(object sender, RoutedEventArgs e)
{
CreateOrUpdateRectangles();
}
[ObservableProperty]
private Brush color;
public double AnchoRecto
{
get { return (double)GetValue(AnchoRectoProperty); }
set { SetValue(AnchoRectoProperty, value); }
}
public static readonly DependencyProperty AnchoRectoProperty =
DependencyProperty.Register("AnchoRecto", typeof(double), typeof(ThreeLinesControl), new PropertyMetadata(100.0, OnDimensionsChanged));
public double AnchoCentro
{
get { return (double)GetValue(AnchoCentroProperty); }
set { SetValue(AnchoCentroProperty, value); }
}
public static readonly DependencyProperty AnchoCentroProperty =
DependencyProperty.Register("AnchoCentro", typeof(double), typeof(ThreeLinesControl), new PropertyMetadata(100.0, OnDimensionsChanged));
public double Altura
{
get { return (double)GetValue(AlturaProperty); }
set { SetValue(AlturaProperty, value); }
}
public static readonly DependencyProperty AlturaProperty =
DependencyProperty.Register("Altura", typeof(double), typeof(ThreeLinesControl), new PropertyMetadata(100.0, OnDimensionsChanged));
public double AltoGuia
{
get { return (double)GetValue(AltoGuiaProperty); }
set { SetValue(AltoGuiaProperty, value); }
}
public static readonly DependencyProperty AltoGuiaProperty =
DependencyProperty.Register("AltoGuia", typeof(double), typeof(ThreeLinesControl), new PropertyMetadata(10.0, OnDimensionsChanged));
private static void OnDimensionsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as ThreeLinesControl;
control?.CreateOrUpdateRectangles();
}
private void CreateOrUpdateRectangles()
{
if (_liz == null || _lc == null || _lde == null)
{
// Crear Liz
_liz = new Rectangle
{
Width = AnchoRecto,
Height = AltoGuia,
Fill = Color
};
Canvas.SetLeft(_liz, 0);
Canvas.SetTop(_liz, -AltoGuia / 2);
Canvas.Children.Add(_liz);
// Crear Lc
_lc = new Rectangle
{
Width = CalculateLcWidth(),
Height = AltoGuia,
Fill = Color,
RenderTransformOrigin = new Point(0, 0.5)
};
_lc.RenderTransform = new RotateTransform(GetRotationAngle(AnchoCentro, Altura));
Canvas.SetLeft(_lc, AnchoRecto);
Canvas.SetTop(_lc, -AltoGuia / 2);
Canvas.Children.Add(_lc);
// Crear Lde
_lde = new Rectangle
{
Width = AnchoRecto,
Height = AltoGuia,
Fill = Color
};
Canvas.SetLeft(_lde, AnchoRecto + AnchoCentro);
Canvas.SetTop(_lde, Altura - AltoGuia / 2);
Canvas.Children.Add(_lde);
}
else
{
// Actualizar Liz
_liz.Width = AnchoRecto;
_liz.Height = AltoGuia;
Canvas.SetLeft(_liz, 0);
Canvas.SetTop(_liz, -AltoGuia / 2);
// Actualizar Lc
_lc.Width = CalculateLcWidth();
_lc.Height = AltoGuia;
_lc.RenderTransform = new RotateTransform(GetRotationAngle(AnchoCentro, Altura));
Canvas.SetLeft(_lc, AnchoRecto);
Canvas.SetTop(_lc, -AltoGuia / 2);
// Actualizar Lde
_lde.Width = AnchoRecto;
_lde.Height = AltoGuia;
Canvas.SetLeft(_lde, AnchoRecto + AnchoCentro);
Canvas.SetTop(_lde, Altura - AltoGuia / 2);
}
}
// Método auxiliar para calcular la hipotenusa
private double CalculateLcWidth()
{
return Math.Sqrt(Math.Pow(AnchoCentro, 2) + Math.Pow(Altura, 2));
}
private double GetRotationAngle(double anchoCentro, double altura)
{
return Math.Atan2(altura, anchoCentro) * 180 / Math.PI;
}
}
}