2024-05-04 15:35:06 -03:00
|
|
|
|
using Newtonsoft.Json;
|
2024-05-04 06:00:52 -03:00
|
|
|
|
using System.Windows.Controls;
|
2024-05-14 03:15:54 -03:00
|
|
|
|
using CtrEditor.Simulacion;
|
2024-05-18 09:58:41 -03:00
|
|
|
|
using System.Reflection;
|
2024-05-31 10:06:49 -03:00
|
|
|
|
|
2024-05-23 14:56:14 -03:00
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using CtrEditor.ObjetosSim.UserControls;
|
|
|
|
|
using System.Collections;
|
2024-05-31 19:34:58 -03:00
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using System.Windows.Shapes;
|
2024-06-02 04:13:01 -03:00
|
|
|
|
using Xceed.Wpf.Toolkit.PropertyGrid;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using Xceed.Wpf.Toolkit;
|
|
|
|
|
using Xceed.Wpf.Toolkit.PropertyGrid.Editors;
|
2024-06-06 11:53:00 -03:00
|
|
|
|
using CtrEditor.ObjetosSim.Extraccion_Datos;
|
2024-05-04 06:00:52 -03:00
|
|
|
|
|
|
|
|
|
namespace CtrEditor.ObjetosSim
|
|
|
|
|
{
|
|
|
|
|
public static class UserControlFactory
|
|
|
|
|
{
|
2024-05-04 15:35:06 -03:00
|
|
|
|
public static UserControl? GetControlForType(Type tipoObjeto)
|
2024-05-04 06:00:52 -03:00
|
|
|
|
{
|
2024-05-18 09:58:41 -03:00
|
|
|
|
// Obtener el nombre del tipo de objeto
|
|
|
|
|
string typeName = tipoObjeto.Name;
|
2024-05-15 04:59:48 -03:00
|
|
|
|
|
2024-05-18 09:58:41 -03:00
|
|
|
|
// Cambiar las primeras dos letras de 'os' a 'uc'
|
|
|
|
|
if (typeName.StartsWith("os"))
|
|
|
|
|
{
|
|
|
|
|
string newTypeName = "uc" + typeName.Substring(2);
|
|
|
|
|
|
|
|
|
|
// Obtener el ensamblado donde se encuentra el tipo UserControl
|
|
|
|
|
Assembly assembly = Assembly.GetExecutingAssembly();
|
2024-05-04 06:00:52 -03:00
|
|
|
|
|
2024-05-18 09:58:41 -03:00
|
|
|
|
// Buscar el tipo en los ensamblados cargados
|
|
|
|
|
Type? controlType = AppDomain.CurrentDomain.GetAssemblies()
|
|
|
|
|
.SelectMany(a => a.GetTypes())
|
|
|
|
|
.FirstOrDefault(t => t.Name == newTypeName);
|
|
|
|
|
|
|
|
|
|
if (controlType != null && typeof(UserControl).IsAssignableFrom(controlType))
|
|
|
|
|
{
|
|
|
|
|
// Crear una instancia del tipo encontrado
|
|
|
|
|
return (UserControl?)Activator.CreateInstance(controlType);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-04 06:00:52 -03:00
|
|
|
|
|
2024-05-04 15:35:06 -03:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static osBase? GetInstanceForType(Type tipoObjeto)
|
|
|
|
|
{
|
2024-05-18 09:58:41 -03:00
|
|
|
|
// Verifica si el tipo pasado es un subtipo de osBase
|
|
|
|
|
if (!typeof(osBase).IsAssignableFrom(tipoObjeto))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("El tipo pasado no es un subtipo de osBase", nameof(tipoObjeto));
|
|
|
|
|
}
|
2024-05-04 06:00:52 -03:00
|
|
|
|
|
2024-05-18 09:58:41 -03:00
|
|
|
|
// Crear una instancia del tipo especificado
|
2024-06-09 05:39:31 -03:00
|
|
|
|
osBase obj = (osBase?)Activator.CreateInstance(tipoObjeto);
|
|
|
|
|
obj.InicializaNuevoObjeto();
|
|
|
|
|
return obj;
|
2024-05-04 06:00:52 -03:00
|
|
|
|
}
|
2024-05-04 15:35:06 -03:00
|
|
|
|
|
|
|
|
|
public static osBase? CreateInstanceAndPopulate(Type tipoObjeto, string jsonString)
|
|
|
|
|
{
|
|
|
|
|
osBase? instance = GetInstanceForType(tipoObjeto);
|
|
|
|
|
if (instance != null)
|
|
|
|
|
{
|
|
|
|
|
// Deserializa los datos desde jsonString y popula la instancia
|
|
|
|
|
JsonConvert.PopulateObject(jsonString, instance);
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-14 03:15:54 -03:00
|
|
|
|
public static void AssignDatos(UserControl userControl, osBase datos, SimulationManagerFP simulationManager)
|
2024-05-04 15:35:06 -03:00
|
|
|
|
{
|
|
|
|
|
if (userControl is IDataContainer dataContainer)
|
|
|
|
|
{
|
|
|
|
|
dataContainer.Datos = datos;
|
|
|
|
|
userControl.DataContext = datos;
|
|
|
|
|
datos.VisualRepresentation = userControl;
|
2024-05-14 03:15:54 -03:00
|
|
|
|
datos.simulationManager = simulationManager;
|
2024-05-04 15:35:06 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-04 06:00:52 -03:00
|
|
|
|
|
2024-06-09 05:39:31 -03:00
|
|
|
|
public static void LimpiarPropiedadesosDatos(PropertyGrid propertyGrid)
|
|
|
|
|
{
|
|
|
|
|
// Clear previous properties
|
|
|
|
|
propertyGrid.SelectedObject = null;
|
|
|
|
|
propertyGrid.PropertyDefinitions.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-06-02 04:13:01 -03:00
|
|
|
|
public static void CargarPropiedadesosDatos(object selectedObject, PropertyGrid propertyGrid)
|
|
|
|
|
{
|
2025-02-24 07:37:52 -03:00
|
|
|
|
// Limpia las propiedades previas
|
2024-06-02 04:13:01 -03:00
|
|
|
|
propertyGrid.SelectedObject = null;
|
|
|
|
|
propertyGrid.PropertyDefinitions.Clear();
|
|
|
|
|
|
2025-02-24 07:37:52 -03:00
|
|
|
|
// Obtén las propiedades del objeto (excluyendo las que tengan HiddenAttribute)
|
2024-06-02 04:13:01 -03:00
|
|
|
|
var properties = TypeDescriptor.GetProperties(selectedObject)
|
|
|
|
|
.Cast<PropertyDescriptor>()
|
2025-02-24 07:37:52 -03:00
|
|
|
|
.Where(prop => !prop.Attributes.OfType<HiddenAttribute>().Any())
|
|
|
|
|
.OrderBy(prop => prop.Name);
|
2024-06-06 11:53:00 -03:00
|
|
|
|
|
2024-06-02 04:13:01 -03:00
|
|
|
|
foreach (var property in properties)
|
|
|
|
|
{
|
2025-02-24 07:37:52 -03:00
|
|
|
|
if (property.Name.EndsWith("_oculto"))
|
|
|
|
|
continue;
|
|
|
|
|
|
2024-06-02 04:13:01 -03:00
|
|
|
|
var displayNameAttr = property.Attributes.OfType<DisplayNameAttribute>().FirstOrDefault();
|
|
|
|
|
var propertyDefinition = new PropertyDefinition
|
|
|
|
|
{
|
2025-02-24 07:37:52 -03:00
|
|
|
|
TargetProperties = new[] { property.Name },
|
|
|
|
|
DisplayName = displayNameAttr != null
|
|
|
|
|
? displayNameAttr.DisplayName
|
|
|
|
|
: property.Name.Replace("_", " ")
|
2024-06-02 04:13:01 -03:00
|
|
|
|
};
|
|
|
|
|
|
2025-02-24 07:37:52 -03:00
|
|
|
|
// Aquí se añaden todas las propiedades; para "ImagePath" no se requiere
|
|
|
|
|
// ninguna configuración adicional, pues el EditorTemplateDefinition en XAML se
|
|
|
|
|
// aplicará automáticamente.
|
|
|
|
|
if (property.PropertyType == typeof(double) ||
|
|
|
|
|
property.PropertyType == typeof(float) ||
|
|
|
|
|
property.PropertyType == typeof(string) ||
|
|
|
|
|
property.PropertyType == typeof(int) ||
|
|
|
|
|
property.PropertyType == typeof(bool) ||
|
|
|
|
|
property.PropertyType == typeof(osBase) ||
|
|
|
|
|
property.PropertyType == typeof(osVMmotorSim) ||
|
|
|
|
|
property.PropertyType == typeof(osBuscarCoincidencias) ||
|
|
|
|
|
property.PropertyType == typeof(Color))
|
2024-06-02 04:13:01 -03:00
|
|
|
|
{
|
2025-02-24 07:37:52 -03:00
|
|
|
|
propertyGrid.PropertyDefinitions.Add(propertyDefinition);
|
2024-06-02 04:13:01 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-04 15:35:06 -03:00
|
|
|
|
|
2024-06-02 04:13:01 -03:00
|
|
|
|
propertyGrid.SelectedObject = selectedObject;
|
|
|
|
|
}
|
2024-05-31 19:34:58 -03:00
|
|
|
|
|
2025-02-24 07:37:52 -03:00
|
|
|
|
|
|
|
|
|
|
2024-05-23 14:56:14 -03:00
|
|
|
|
}
|
2024-05-04 06:00:52 -03:00
|
|
|
|
}
|
2024-05-23 14:56:14 -03:00
|
|
|
|
|