From 667cd18f5d03dfbbd912968bdcfd8d40ce8bbc7f Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 1 Jun 2024 00:34:58 +0200 Subject: [PATCH] Mejorado de CargarPropiedadesosDatos --- App.xaml | 1 + .../Estaticos/ucTransporteGuiasUnion.xaml | 10 +- .../Estaticos/ucTransporteGuiasUnion.xaml.cs | 3 + ObjetosSim/SensoresComandos/ucBoton.xaml.cs | 7 + ObjetosSim/UserControlFactory.cs | 254 +++++++++++------- .../UserControls/ThreeLinesControl.xaml.cs | 17 +- Simulacion/Aether.cs | 2 +- XAMLhelpers.cs | 16 ++ 8 files changed, 203 insertions(+), 107 deletions(-) diff --git a/App.xaml b/App.xaml index bdf9010..8993d5f 100644 --- a/App.xaml +++ b/App.xaml @@ -15,6 +15,7 @@ + diff --git a/ObjetosSim/Estaticos/ucTransporteGuiasUnion.xaml b/ObjetosSim/Estaticos/ucTransporteGuiasUnion.xaml index 1a0cae0..6cdeae4 100644 --- a/ObjetosSim/Estaticos/ucTransporteGuiasUnion.xaml +++ b/ObjetosSim/Estaticos/ucTransporteGuiasUnion.xaml @@ -67,20 +67,22 @@ AnchoCentro="{Binding AnchoCentral, Converter={StaticResource MeterToPixelConverter},ConverterParameter=1}" Altura="{Binding Alto, Converter={StaticResource MeterToPixelConverter},ConverterParameter=1}" AltoGuia="{Binding AltoGuia, Converter={StaticResource MeterToPixelConverter},ConverterParameter=1}" - Canvas.Top="{Binding Distance, Converter={StaticResource MeterToPixelConverter},ConverterParameter=-1}" - /> + Canvas.Top="{Binding Distance, Converter={StaticResource MeterToPixelConverter},ConverterParameter=-1}" + Color="Blue"/> + AltoGuia="{Binding AltoGuia, Converter={StaticResource MeterToPixelConverter},ConverterParameter=1}" + Color="Blue"> + - + diff --git a/ObjetosSim/Estaticos/ucTransporteGuiasUnion.xaml.cs b/ObjetosSim/Estaticos/ucTransporteGuiasUnion.xaml.cs index 863b833..e7f2b19 100644 --- a/ObjetosSim/Estaticos/ucTransporteGuiasUnion.xaml.cs +++ b/ObjetosSim/Estaticos/ucTransporteGuiasUnion.xaml.cs @@ -35,6 +35,9 @@ namespace CtrEditor.ObjetosSim set => SetProperty(ref nombre, value); } + [ObservableProperty] + string color; + [ObservableProperty] public string motorA; diff --git a/ObjetosSim/SensoresComandos/ucBoton.xaml.cs b/ObjetosSim/SensoresComandos/ucBoton.xaml.cs index 466e05d..cfb923e 100644 --- a/ObjetosSim/SensoresComandos/ucBoton.xaml.cs +++ b/ObjetosSim/SensoresComandos/ucBoton.xaml.cs @@ -30,6 +30,13 @@ namespace CtrEditor.ObjetosSim public bool tipo_NC; [ObservableProperty] private Brush color; + + partial void OnColorChanged(Brush value) + { + OnEstadoChanged(Estado); + } + + [ObservableProperty] private Brush colorButton_oculto; [ObservableProperty] diff --git a/ObjetosSim/UserControlFactory.cs b/ObjetosSim/UserControlFactory.cs index b6296df..70167f0 100644 --- a/ObjetosSim/UserControlFactory.cs +++ b/ObjetosSim/UserControlFactory.cs @@ -8,6 +8,8 @@ using System.Windows; using System.Windows.Media; using CtrEditor.ObjetosSim.UserControls; using System.Collections; +using System.Windows.Input; +using System.Windows.Shapes; namespace CtrEditor.ObjetosSim { @@ -76,10 +78,11 @@ namespace CtrEditor.ObjetosSim } + public static void CargarPropiedadesosDatos(Object selectedObject, StackPanel PanelEdicion, ResourceDictionary Resources) { PanelEdicion.Children.Clear(); - var properties = selectedObject.GetType().GetProperties(); + var properties = selectedObject.GetType().GetProperties().OrderBy(p => p.Name); // Ordenar alfabéticamente foreach (var property in properties) { @@ -93,117 +96,172 @@ namespace CtrEditor.ObjetosSim grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); var label = new Label + { + Content = property.Name.Replace("_", " ") + ":", + Margin = new Thickness(0, 0, 5, 0), + VerticalAlignment = VerticalAlignment.Center + }; + + Grid.SetColumn(label, 0); + grid.Children.Add(label); + + if (property.PropertyType == typeof(double) || property.PropertyType == typeof(float) || property.PropertyType == typeof(string) || property.PropertyType == typeof(int)) + { + var textBox = new TextBox { - Content = property.Name.Replace("_", " ") + ":", - Margin = new Thickness(0, 0, 5, 0), + Margin = new Thickness(0), + MinWidth = 200, + VerticalContentAlignment = VerticalAlignment.Center + }; + + var binding = new Binding(property.Name) + { + Source = selectedObject, + Mode = BindingMode.TwoWay, + UpdateSourceTrigger = UpdateSourceTrigger.LostFocus + }; + + if (property.PropertyType == typeof(float)) + { + if (Application.Current.Resources.Contains("floatFormatter")) + { + binding.Converter = Application.Current.Resources["floatFormatter"] as IValueConverter; + } + } + if (property.PropertyType == typeof(double)) + { + if (Application.Current.Resources.Contains("doubleFormatter")) + { + binding.Converter = Application.Current.Resources["doubleFormatter"] as IValueConverter; + } + } + + textBox.SetBinding(TextBox.TextProperty, binding); + + textBox.KeyDown += (sender, e) => + { + if (e.Key == Key.Enter || e.Key == Key.Down) + { + var request = new TraversalRequest(FocusNavigationDirection.Next); + request.Wrapped = true; + textBox.MoveFocus(request); + } + else if (e.Key == Key.Up) + { + var request = new TraversalRequest(FocusNavigationDirection.Previous); + request.Wrapped = true; + textBox.MoveFocus(request); + } + }; + + Grid.SetColumn(textBox, 1); + grid.Children.Add(textBox); + } + else if (property.PropertyType == typeof(bool)) + { + var checkBox = new CheckBox + { + Margin = new Thickness(5, 0, 0, 0), VerticalAlignment = VerticalAlignment.Center }; - Grid.SetColumn(label, 0); - grid.Children.Add(label); - - if (property.PropertyType == typeof(double) || property.PropertyType == typeof(float) || property.PropertyType == typeof(string) || property.PropertyType == typeof(int)) + var binding = new Binding(property.Name) { - var textBox = new TextBox - { - Margin = new Thickness(0), - MinWidth = 200, - VerticalContentAlignment = VerticalAlignment.Center - }; + Source = selectedObject, + Mode = BindingMode.TwoWay + }; - var binding = new Binding(property.Name) - { - Source = selectedObject, - Mode = BindingMode.TwoWay, - UpdateSourceTrigger = UpdateSourceTrigger.LostFocus - }; + checkBox.SetBinding(CheckBox.IsCheckedProperty, binding); - if (property.PropertyType == typeof(float)) + checkBox.KeyDown += (sender, e) => + { + if (e.Key == Key.Enter || e.Key == Key.Down) { - if (Resources != null) - binding.Converter = (FloatToFormattedStringConverter)Resources["floatFormatter"]; + var request = new TraversalRequest(FocusNavigationDirection.Next); + request.Wrapped = true; + checkBox.MoveFocus(request); } - if (property.PropertyType == typeof(double)) + else if (e.Key == Key.Up) { - if (Resources != null) - binding.Converter = (DoubleToFormattedStringConverter)Resources["doubleFormatter"]; + var request = new TraversalRequest(FocusNavigationDirection.Previous); + request.Wrapped = true; + checkBox.MoveFocus(request); } + }; - textBox.SetBinding(TextBox.TextProperty, binding); - - Grid.SetColumn(textBox, 1); - grid.Children.Add(textBox); - } - else if (property.PropertyType == typeof(bool)) - { - var checkBox = new CheckBox - { - Margin = new Thickness(5, 0, 0, 0), - VerticalAlignment = VerticalAlignment.Center - }; - - var binding = new Binding(property.Name) - { - Source = selectedObject, - Mode = BindingMode.TwoWay - }; - - checkBox.SetBinding(CheckBox.IsCheckedProperty, binding); - - Grid.SetColumn(checkBox, 1); - grid.Children.Add(checkBox); - } - else if (property.PropertyType == typeof(Brush)) - { - var listBox = new ListBox - { - ItemsSource = new List { "Rojo", "Azul", "Negro", "Verde", "Gris" }, - Margin = new Thickness(0), - MinWidth = 200 - }; - - listBox.SelectionChanged += (sender, e) => - { - if (listBox.SelectedItem != null) - { - switch (listBox.SelectedItem.ToString()) - { - case "Rojo": - property.SetValue(selectedObject, Brushes.Red); - break; - case "Azul": - property.SetValue(selectedObject, Brushes.Blue); - break; - case "Negro": - property.SetValue(selectedObject, Brushes.Black); - break; - case "Verde": - property.SetValue(selectedObject, Brushes.Green); - break; - case "Gris": - property.SetValue(selectedObject, Brushes.Gray); - break; - } - } - }; - - var binding = new Binding(property.Name) - { - Source = selectedObject, - Mode = BindingMode.TwoWay, - Converter = new BrushToColorNameConverter() - }; - - listBox.SetBinding(ListBox.SelectedItemProperty, binding); - - Grid.SetColumn(listBox, 1); - grid.Children.Add(listBox); - } - PanelEdicion.Children.Add(grid); + Grid.SetColumn(checkBox, 1); + grid.Children.Add(checkBox); } - } + else if (property.PropertyType == typeof(Brush)) + { + var comboBox = new ComboBox + { + ItemsSource = new List> + { + Tuple.Create("Rojo", Brushes.Red), + Tuple.Create("Azul", Brushes.Blue), + Tuple.Create("Negro", Brushes.Black), + Tuple.Create("Verde", Brushes.Green), + Tuple.Create("Gris", Brushes.Gray) + }, + SelectedValuePath = "Item2", + Margin = new Thickness(0), + MinWidth = 200 + }; - public static List CargarPropiedadesosDatosTags(Object selectedObject, StackPanel PanelEdicion, ResourceDictionary Resources) + var binding = new Binding(property.Name) + { + Source = selectedObject, + Mode = BindingMode.TwoWay, + UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged + }; + + comboBox.SetBinding(ComboBox.SelectedValueProperty, binding); + + comboBox.KeyDown += (sender, e) => + { + if (e.Key == Key.Enter || e.Key == Key.Down) + { + var request = new TraversalRequest(FocusNavigationDirection.Next); + request.Wrapped = true; + comboBox.MoveFocus(request); + } + else if (e.Key == Key.Up) + { + var request = new TraversalRequest(FocusNavigationDirection.Previous); + request.Wrapped = true; + comboBox.MoveFocus(request); + } + }; + + var template = new DataTemplate(); + var stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel)); + stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); + + var rectangleFactory = new FrameworkElementFactory(typeof(Rectangle)); + rectangleFactory.SetValue(Rectangle.WidthProperty, 16.0); + rectangleFactory.SetValue(Rectangle.HeightProperty, 16.0); + rectangleFactory.SetBinding(Rectangle.FillProperty, new Binding("Item2")); + stackPanelFactory.AppendChild(rectangleFactory); + + var textBlockFactory = new FrameworkElementFactory(typeof(TextBlock)); + textBlockFactory.SetBinding(TextBlock.TextProperty, new Binding("Item1")); + stackPanelFactory.AppendChild(textBlockFactory); + + template.VisualTree = stackPanelFactory; + comboBox.ItemTemplate = template; + + Grid.SetColumn(comboBox, 1); + grid.Children.Add(comboBox); + } + PanelEdicion.Children.Add(grid); + } + } + + + + + public static List CargarPropiedadesosDatosTags(Object selectedObject, StackPanel PanelEdicion, ResourceDictionary Resources) { PanelEdicion.Children.Clear(); var properties = selectedObject.GetType().GetProperties(); diff --git a/ObjetosSim/UserControls/ThreeLinesControl.xaml.cs b/ObjetosSim/UserControls/ThreeLinesControl.xaml.cs index 9e9313b..21be1f1 100644 --- a/ObjetosSim/UserControls/ThreeLinesControl.xaml.cs +++ b/ObjetosSim/UserControls/ThreeLinesControl.xaml.cs @@ -1,4 +1,5 @@ -using System.Windows; +using CommunityToolkit.Mvvm.ComponentModel; +using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Shapes; @@ -8,6 +9,10 @@ namespace CtrEditor.ObjetosSim.UserControls /// /// Interaction logic for ThreeLinesControl.xaml /// + /// + + + [ObservableObject] public partial class ThreeLinesControl : UserControl { private Rectangle _liz; @@ -18,6 +23,7 @@ namespace CtrEditor.ObjetosSim.UserControls { InitializeComponent(); this.Loaded += ThreeLinesControl_Loaded; + Color = Brushes.Gray; } private void ThreeLinesControl_Loaded(object sender, RoutedEventArgs e) @@ -25,6 +31,9 @@ namespace CtrEditor.ObjetosSim.UserControls CreateOrUpdateRectangles(); } + [ObservableProperty] + private Brush color; + public double AnchoRecto { get { return (double)GetValue(AnchoRectoProperty); } @@ -76,7 +85,7 @@ namespace CtrEditor.ObjetosSim.UserControls { Width = AnchoRecto, Height = AltoGuia, - Fill = Brushes.Blue + Fill = Color }; Canvas.SetLeft(_liz, 0); Canvas.SetTop(_liz, -AltoGuia / 2); @@ -87,7 +96,7 @@ namespace CtrEditor.ObjetosSim.UserControls { Width = CalculateLcWidth(), Height = AltoGuia, - Fill = Brushes.Red, + Fill = Color, RenderTransformOrigin = new Point(0, 0.5) }; _lc.RenderTransform = new RotateTransform(GetRotationAngle(AnchoCentro, Altura)); @@ -100,7 +109,7 @@ namespace CtrEditor.ObjetosSim.UserControls { Width = AnchoRecto, Height = AltoGuia, - Fill = Brushes.Green + Fill = Color }; Canvas.SetLeft(_lde, AnchoRecto + AnchoCentro); Canvas.SetTop(_lde, Altura - AltoGuia / 2); diff --git a/Simulacion/Aether.cs b/Simulacion/Aether.cs index 6084a5d..c444c84 100644 --- a/Simulacion/Aether.cs +++ b/Simulacion/Aether.cs @@ -447,7 +447,7 @@ namespace CtrEditor.Simulacion Body.SetFriction(0.2f); // Configurar amortiguamiento - Body.LinearDamping = 1f; // Ajustar para controlar la reducción de la velocidad lineal + Body.LinearDamping = 3f; // Ajustar para controlar la reducción de la velocidad lineal Body.AngularDamping = 1f; // Ajustar para controlar la reducción de la velocidad angular Body.SetRestitution(0f); // Baja restitución para menos rebote diff --git a/XAMLhelpers.cs b/XAMLhelpers.cs index 9b103fa..d6adc13 100644 --- a/XAMLhelpers.cs +++ b/XAMLhelpers.cs @@ -5,6 +5,22 @@ using System.Windows; namespace CtrEditor { + public class StringToBrushConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is string colorString) + { + return (Brush)new BrushConverter().ConvertFromString(colorString); + } + return Brushes.Transparent; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } public class SumConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)