Cambiando a MVVM para los UserControl

This commit is contained in:
Miguel 2024-05-04 15:53:47 +02:00
parent 82b6f9cffc
commit 58b429c7bf
3 changed files with 32 additions and 23 deletions

View File

@ -223,36 +223,24 @@ namespace CtrEditor
private void RotateControl(UserControl control, Point currentPosition)
{
// Calcular el vector desde el centro de rotación hasta el punto de inicio
double deltaX = _startPointUserControl.X - (Canvas.GetLeft(control) + control.ActualWidth / 2);
double deltaY = _startPointUserControl.Y - (Canvas.GetTop(control) + control.ActualHeight);
// Calcular el vector desde el centro de rotación hasta la posición actual del ratón
double deltaXCurrent = currentPosition.X - (Canvas.GetLeft(control) + control.ActualWidth / 2);
double deltaYCurrent = currentPosition.Y - (Canvas.GetTop(control) + control.ActualHeight);
// Calcular los ángulos
double initialAngle = Math.Atan2(deltaY, deltaX) * (180 / Math.PI);
double currentAngle = Math.Atan2(deltaYCurrent, deltaXCurrent) * (180 / Math.PI);
// Calcular la diferencia de ángulo y ajustar
double angleDelta = currentAngle - initialAngle;
double deltaX = currentPosition.X - _startPointUserControl.X;
double deltaY = currentPosition.Y - _startPointUserControl.Y;
double angle = Math.Atan2(deltaY, deltaX) * (180 / Math.PI);
RotateTransform rotateTransform = control.RenderTransform as RotateTransform;
rotateTransform.Angle = angleDelta;
rotateTransform.CenterX = 0;
rotateTransform.CenterY = 0;
rotateTransform.Angle = angle; // - _initialAngleUserControl; // Asegúrate de ajustar esta parte según cómo calcules el ángulo inicial
// Actualizar el ángulo mostrado
_angleDisplayTextBlock.Text = $"Ángulo: {angleDelta:F2}°";
_angleDisplayTextBlock.Text = $"Ángulo: {angle:F2}°";
PositionAngleDisplay(control);
}
private void PositionAngleDisplay(UserControl control)
{
// Posicionar el TextBlock sobre el control
Canvas.SetLeft(_angleDisplayTextBlock, Canvas.GetLeft(control) + control.Width / 2 - _angleDisplayTextBlock.ActualWidth / 2);
Canvas.SetTop(_angleDisplayTextBlock, Canvas.GetTop(control) - _angleDisplayTextBlock.ActualHeight - 5);
Canvas.SetLeft(_angleDisplayTextBlock, Canvas.GetLeft(control));
Canvas.SetTop(_angleDisplayTextBlock, Canvas.GetTop(control));
}
private void ResizeControl(UserControl control, Point currentPosition)
@ -261,13 +249,27 @@ namespace CtrEditor
double widthChange = currentPosition.X - _startPointUserControl.X;
// Actualizar el ancho del control
control.Width = Math.Max(control.ActualWidth + widthChange, control.MinWidth); // Asegurar que no sea menor que el mínimo
double newWidth = Math.Max(control.ActualWidth + widthChange, control.MinWidth);
control.Width = newWidth; // Asegurar que no sea menor que el mínimo
// Actualizar el ancho de los elementos internos
if (control.Content is Panel panel)
{
foreach (var child in panel.Children)
{
if (child is Rectangle rect)
{
rect.Width = newWidth; // Establece el nuevo ancho
}
}
}
// Actualizar el punto de inicio para el próximo evento de movimiento
_startPointUserControl = currentPosition;
}
private void UserControl_MouseEnter(object sender, MouseEventArgs e)
{
// Lógica a ejecutar cuando el mouse entra en el UserControl

View File

@ -4,6 +4,10 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CtrEditor"
mc:Ignorable="d" >
<Rectangle HorizontalAlignment="Left" Height="10" Margin="0,0,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Canvas>
<Rectangle Width="{Binding Datos.Ancho}" Height="{Binding Datos.Alto}" Fill="Gray"/>
</Canvas>
</UserControl>

View File

@ -23,6 +23,8 @@ namespace CtrEditor.ObjetosSim
public class osTransporteTTop : osBase
{
public double diametro { get; set; }
public double Ancho { get; set; }
public double Alto { get; set; }
// Otros datos y métodos relevantes para la simulación
public new string Nombre => "Transporte TTOP";
@ -38,5 +40,6 @@ namespace CtrEditor.ObjetosSim
{
InitializeComponent();
}
public osTransporteTTop Datos { get; set; }
}
}