CtrEditor/ObjetosSim/UserControlFactory.cs

97 lines
3.3 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using CtrEditor.Simulacion;
namespace CtrEditor.ObjetosSim
{
public static class UserControlFactory
{
public static UserControl? GetControlForType(Type tipoObjeto)
{
if (tipoObjeto == typeof(osBotella))
return new ucBotella();
if (tipoObjeto == typeof(osTransporteTTop))
return new ucTransporteTTop();
if (tipoObjeto == typeof(osGuia))
return new ucGuia();
if (tipoObjeto == typeof(osTransporteGuias))
return new ucTransporteGuias();
//if (tipoObjeto == typeof(osTransporteCurva))
// return new ucTransporteCurva();
if (tipoObjeto == typeof(osVMmotorSim ))
return new ucVMmotorSim();
if (tipoObjeto == typeof(osBoton))
return new ucBoton();
if (tipoObjeto == typeof(osTanque))
return new ucTanque();
if (tipoObjeto == typeof(osSensTemperatura))
return new ucSensTemperatura();
if (tipoObjeto == typeof(osFiller))
return new ucFiller();
// Puedes añadir más condiciones para otros tipos
return null;
}
public static osBase? GetInstanceForType(Type tipoObjeto)
{
if (tipoObjeto == typeof(osBotella))
return new osBotella();
if (tipoObjeto == typeof(osTransporteTTop))
return new osTransporteTTop();
if (tipoObjeto == typeof(osGuia))
return new osGuia();
if (tipoObjeto == typeof(osTransporteGuias))
return new osTransporteGuias();
//if (tipoObjeto == typeof(osTransporteCurva))
// return new osTransporteCurva();
if (tipoObjeto == typeof(osVMmotorSim))
return new osVMmotorSim();
if (tipoObjeto == typeof(osBoton))
return new osBoton();
if (tipoObjeto == typeof(osTanque))
return new osTanque();
if (tipoObjeto == typeof(osSensTemperatura))
return new osSensTemperatura();
if (tipoObjeto == typeof(osFiller))
return new osFiller();
// Puedes añadir más condiciones para otros tipos
return null;
}
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;
}
public static void AssignDatos(UserControl userControl, osBase datos, SimulationManagerFP simulationManager)
{
if (userControl is IDataContainer dataContainer)
{
dataContainer.Datos = datos;
userControl.DataContext = datos;
datos.VisualRepresentation = userControl;
datos.simulationManager = simulationManager;
}
}
}
}