CtrEditor/ObjetosSim/UserControls/ThreeLinesControl.xaml.cs

145 lines
5.0 KiB
C#
Raw Normal View History

2024-05-31 10:06:49 -03:00
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>
public partial class ThreeLinesControl : UserControl
{
2024-05-31 14:25:24 -03:00
private Rectangle _liz;
private Rectangle _lc;
private Rectangle _lde;
2024-05-31 10:06:49 -03:00
public ThreeLinesControl()
{
InitializeComponent();
this.Loaded += ThreeLinesControl_Loaded;
}
private void ThreeLinesControl_Loaded(object sender, RoutedEventArgs e)
{
2024-05-31 14:25:24 -03:00
CreateOrUpdateRectangles();
2024-05-31 10:06:49 -03:00
}
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;
2024-05-31 14:25:24 -03:00
control?.CreateOrUpdateRectangles();
2024-05-31 10:06:49 -03:00
}
2024-05-31 14:25:24 -03:00
private void CreateOrUpdateRectangles()
2024-05-31 10:06:49 -03:00
{
2024-05-31 14:25:24 -03:00
if (_liz == null || _lc == null || _lde == null)
2024-05-31 10:06:49 -03:00
{
2024-05-31 14:25:24 -03:00
// Crear Liz
_liz = new Rectangle
{
Width = AnchoRecto,
Height = AltoGuia,
Fill = Brushes.Blue
};
Canvas.SetLeft(_liz, 0);
Canvas.SetTop(_liz, -AltoGuia / 2);
Canvas.Children.Add(_liz);
// Crear Lc
_lc = new Rectangle
{
Width = CalculateLcWidth(),
Height = AltoGuia,
Fill = Brushes.Red,
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 = Brushes.Green
};
Canvas.SetLeft(_lde, AnchoRecto + AnchoCentro);
Canvas.SetTop(_lde, Altura - AltoGuia / 2);
Canvas.Children.Add(_lde);
}
else
2024-05-31 10:06:49 -03:00
{
2024-05-31 14:25:24 -03:00
// 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);
}
2024-05-31 10:06:49 -03:00
}
2024-05-31 14:25:24 -03:00
// Método auxiliar para calcular la hipotenusa
private double CalculateLcWidth()
{
return Math.Sqrt(Math.Pow(AnchoCentro, 2) + Math.Pow(Altura, 2));
}
2024-05-31 10:06:49 -03:00
private double GetRotationAngle(double anchoCentro, double altura)
{
return Math.Atan2(altura, anchoCentro) * 180 / Math.PI;
}
}
}