2024-05-04 06:00:52 -03:00
|
|
|
|
using System;
|
2024-05-02 11:06:45 -03:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using Ookii.Dialogs.Wpf;
|
2024-05-03 03:58:21 -03:00
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Media.Imaging;
|
2024-05-03 05:13:25 -03:00
|
|
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
|
using System.Windows.Threading;
|
2024-05-04 06:00:52 -03:00
|
|
|
|
using CtrEditor.ObjetosSim;
|
|
|
|
|
using System.IO;
|
2024-05-04 15:35:06 -03:00
|
|
|
|
// using System.Windows.Forms;
|
2024-05-04 06:00:52 -03:00
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using Newtonsoft.Json;
|
2024-05-04 15:35:06 -03:00
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
using System.Windows;
|
2024-05-02 11:06:45 -03:00
|
|
|
|
|
|
|
|
|
namespace CtrEditor
|
|
|
|
|
{
|
2024-05-03 05:13:25 -03:00
|
|
|
|
|
2024-05-02 11:06:45 -03:00
|
|
|
|
public class MainViewModel : INotifyPropertyChanged
|
|
|
|
|
{
|
2024-05-03 03:58:21 -03:00
|
|
|
|
public DatosDeTrabajo datosDeTrabajo { get; }
|
2024-05-04 06:00:52 -03:00
|
|
|
|
public ObservableCollection<string> listaImagenes { get; private set; } // Publicación de las claves del diccionario
|
|
|
|
|
public ObservableCollection<TipoSimulable> ListaOsBase { get; } = new ObservableCollection<TipoSimulable>();
|
2024-05-04 15:35:06 -03:00
|
|
|
|
private ObservableCollection<osBase> _objetosSimulables = new ObservableCollection<osBase>();
|
2024-05-04 06:00:52 -03:00
|
|
|
|
|
2024-05-03 05:13:25 -03:00
|
|
|
|
private readonly DispatcherTimer _timerSimulacion;
|
2024-05-04 06:00:52 -03:00
|
|
|
|
|
|
|
|
|
public ICommand StartSimulationCommand { get; }
|
|
|
|
|
public ICommand StopSimulationCommand { get; }
|
|
|
|
|
public ICommand ItemDoubleClickCommand { get; private set; }
|
|
|
|
|
|
2024-05-02 11:06:45 -03:00
|
|
|
|
|
2024-05-03 03:58:21 -03:00
|
|
|
|
// Evento que se dispara cuando se selecciona una nueva imagen
|
|
|
|
|
public event EventHandler<string> ImageSelected;
|
2024-05-03 05:13:25 -03:00
|
|
|
|
public event EventHandler<TickSimulacionEventArgs> TickSimulacion;
|
2024-05-04 15:35:06 -03:00
|
|
|
|
public event Action<UserControl> OnUserControlSelected;
|
2024-05-02 11:06:45 -03:00
|
|
|
|
|
|
|
|
|
public MainViewModel()
|
|
|
|
|
{
|
|
|
|
|
OpenWorkDirectoryCommand = new RelayCommand(OpenWorkDirectory);
|
2024-05-03 03:58:21 -03:00
|
|
|
|
datosDeTrabajo = new DatosDeTrabajo();
|
2024-05-04 06:00:52 -03:00
|
|
|
|
|
|
|
|
|
InitializeTipoSimulableList();
|
|
|
|
|
|
|
|
|
|
ItemDoubleClickCommand = new ParameterizedRelayCommand(ExecuteDoubleClick);
|
2024-05-03 05:13:25 -03:00
|
|
|
|
|
|
|
|
|
_timerSimulacion = new DispatcherTimer();
|
|
|
|
|
_timerSimulacion.Interval = TimeSpan.FromMilliseconds(100); // ajusta el intervalo según sea necesario
|
|
|
|
|
_timerSimulacion.Tick += OnTickSimulacion;
|
|
|
|
|
|
|
|
|
|
StartSimulationCommand = new RelayCommand(StartSimulation);
|
2024-05-04 06:00:52 -03:00
|
|
|
|
StopSimulationCommand = new RelayCommand(StopSimulation);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void LoadInitialData()
|
|
|
|
|
{
|
|
|
|
|
// Suponiendo que "SelectedImage" es una propiedad que al establecerse dispara "ImageSelected"
|
|
|
|
|
directorioTrabajo = EstadoPersistente.Instance.directorio;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private TipoSimulable _selectedItem = null;
|
|
|
|
|
public TipoSimulable SelectedItem
|
|
|
|
|
{
|
|
|
|
|
get => _selectedItem;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_selectedItem != value)
|
|
|
|
|
{
|
|
|
|
|
_selectedItem = value;
|
|
|
|
|
OnPropertyChanged(nameof(SelectedItem)); // Notificar que la propiedad ha cambiado
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ExecuteDoubleClick(object parameter)
|
|
|
|
|
{
|
|
|
|
|
if (parameter is TipoSimulable tipoSimulable)
|
|
|
|
|
{
|
2024-05-04 15:35:06 -03:00
|
|
|
|
// Crear una nueva instancia del osBase correspondiente
|
|
|
|
|
osBase? newosBase = UserControlFactory.GetInstanceForType(tipoSimulable.Tipo);
|
|
|
|
|
if (newosBase != null)
|
|
|
|
|
{
|
|
|
|
|
if (CrearUsercontrol(newosBase))
|
|
|
|
|
// Añadir el nuevo osBase a la colección de objetos simulables
|
|
|
|
|
ObjetosSimulables.Add(newosBase);
|
2024-05-04 06:00:52 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitializeTipoSimulableList()
|
|
|
|
|
{
|
|
|
|
|
var baseType = typeof(osBase);
|
|
|
|
|
var types = AppDomain.CurrentDomain.GetAssemblies()
|
|
|
|
|
.SelectMany(assembly => assembly.GetTypes())
|
|
|
|
|
.Where(type => type.IsSubclassOf(baseType) && !type.IsAbstract);
|
|
|
|
|
|
|
|
|
|
foreach (var type in types)
|
|
|
|
|
{
|
|
|
|
|
ListaOsBase.Add(new TipoSimulable { Nombre = type.Name, Tipo = type });
|
|
|
|
|
}
|
2024-05-03 05:13:25 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void StartSimulation()
|
|
|
|
|
{
|
|
|
|
|
_timerSimulacion.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StopSimulation()
|
|
|
|
|
{
|
|
|
|
|
_timerSimulacion.Stop();
|
2024-05-02 11:06:45 -03:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-03 05:13:25 -03:00
|
|
|
|
private void OnTickSimulacion(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var args = new TickSimulacionEventArgs();
|
|
|
|
|
OnTickSimulacion(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void OnTickSimulacion(TickSimulacionEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
TickSimulacion?.Invoke(this, e);
|
|
|
|
|
}
|
2024-05-02 11:06:45 -03:00
|
|
|
|
|
2024-05-03 03:58:21 -03:00
|
|
|
|
public string directorioTrabajo
|
2024-05-02 11:06:45 -03:00
|
|
|
|
{
|
2024-05-03 03:58:21 -03:00
|
|
|
|
get => EstadoPersistente.Instance.directorio;
|
2024-05-02 11:06:45 -03:00
|
|
|
|
set
|
|
|
|
|
{
|
2024-05-02 11:26:45 -03:00
|
|
|
|
if (value != null)
|
|
|
|
|
{
|
2024-05-03 03:58:21 -03:00
|
|
|
|
EstadoPersistente.Instance.directorio = value; // Actualizar el estado persistente
|
2024-05-02 11:26:45 -03:00
|
|
|
|
EstadoPersistente.Instance.GuardarEstado(); // Guardar el estado actualizado
|
2024-05-03 03:58:21 -03:00
|
|
|
|
datosDeTrabajo.CargarImagenes();
|
|
|
|
|
listaImagenes = new ObservableCollection<string>(datosDeTrabajo.Imagenes.Keys); // Actualizar claves
|
2024-05-04 06:00:52 -03:00
|
|
|
|
SelectedImage = null;
|
|
|
|
|
if (listaImagenes.FirstOrDefault() != null)
|
|
|
|
|
SelectedImage = listaImagenes.FirstOrDefault();
|
2024-05-03 03:58:21 -03:00
|
|
|
|
OnPropertyChanged(nameof(directorioTrabajo)); // Notificar el cambio de propiedad
|
|
|
|
|
OnPropertyChanged(nameof(listaImagenes)); // Notificar que la lista de imágenes ha cambiado
|
2024-05-02 11:26:45 -03:00
|
|
|
|
}
|
2024-05-02 11:06:45 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 06:00:52 -03:00
|
|
|
|
private string _selectedImage = null;
|
2024-05-03 03:58:21 -03:00
|
|
|
|
public string SelectedImage
|
|
|
|
|
{
|
|
|
|
|
get => _selectedImage;
|
|
|
|
|
set
|
|
|
|
|
{
|
2024-05-04 06:00:52 -03:00
|
|
|
|
if (_selectedImage != value && value != null)
|
2024-05-03 03:58:21 -03:00
|
|
|
|
{
|
2024-05-04 06:00:52 -03:00
|
|
|
|
SaveStateObjetosSimulables(); // Guarda el estado antes de cambiar la imagen
|
2024-05-03 03:58:21 -03:00
|
|
|
|
_selectedImage = value;
|
|
|
|
|
ImageSelected?.Invoke(this, datosDeTrabajo.Imagenes[value]); // Dispara el evento con la nueva ruta de imagen
|
2024-05-04 09:06:33 -03:00
|
|
|
|
LoadStateObjetosSimulables();
|
2024-05-03 03:58:21 -03:00
|
|
|
|
}
|
2024-05-04 06:00:52 -03:00
|
|
|
|
_selectedImage = value;
|
|
|
|
|
OnPropertyChanged(nameof(SelectedImage));
|
2024-05-03 03:58:21 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-04 06:00:52 -03:00
|
|
|
|
|
2024-05-02 11:06:45 -03:00
|
|
|
|
public ICommand OpenWorkDirectoryCommand { get; }
|
|
|
|
|
|
|
|
|
|
private void OpenWorkDirectory()
|
|
|
|
|
{
|
|
|
|
|
var dialog = new VistaFolderBrowserDialog();
|
|
|
|
|
if (dialog.ShowDialog() == true) // Mostrar el diálogo y comprobar si el resultado es positivo
|
|
|
|
|
{
|
2024-05-03 03:58:21 -03:00
|
|
|
|
directorioTrabajo = dialog.SelectedPath; // Actualizar la propiedad que también actualiza el estado persistente
|
2024-05-02 11:06:45 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 06:00:52 -03:00
|
|
|
|
//
|
|
|
|
|
// Lista de osBase
|
|
|
|
|
//
|
2024-05-04 15:35:06 -03:00
|
|
|
|
|
2024-05-04 06:00:52 -03:00
|
|
|
|
|
|
|
|
|
public ObservableCollection<osBase> ObjetosSimulables
|
|
|
|
|
{
|
2024-05-04 15:35:06 -03:00
|
|
|
|
get => _objetosSimulables;
|
2024-05-04 06:00:52 -03:00
|
|
|
|
set
|
|
|
|
|
{
|
2024-05-04 15:35:06 -03:00
|
|
|
|
if (_objetosSimulables != value)
|
2024-05-04 06:00:52 -03:00
|
|
|
|
{
|
2024-05-04 15:35:06 -03:00
|
|
|
|
_objetosSimulables = value;
|
2024-05-04 06:00:52 -03:00
|
|
|
|
OnPropertyChanged(nameof(ObjetosSimulables));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SaveStateObjetosSimulables()
|
|
|
|
|
{
|
|
|
|
|
if (_selectedImage != null)
|
|
|
|
|
{
|
|
|
|
|
var settings = new JsonSerializerSettings
|
|
|
|
|
{
|
|
|
|
|
Formatting = Formatting.Indented,
|
|
|
|
|
NullValueHandling = NullValueHandling.Ignore,
|
|
|
|
|
TypeNameHandling = TypeNameHandling.Auto
|
|
|
|
|
};
|
2024-05-04 15:35:06 -03:00
|
|
|
|
foreach (var obj in ObjetosSimulables)
|
2024-05-04 06:00:52 -03:00
|
|
|
|
{
|
|
|
|
|
obj.VisualRepresentation = null;
|
|
|
|
|
}
|
2024-05-04 15:35:06 -03:00
|
|
|
|
var serializedData = JsonConvert.SerializeObject(ObjetosSimulables, settings);
|
2024-05-04 06:00:52 -03:00
|
|
|
|
File.WriteAllText(datosDeTrabajo.ObtenerPathImagenConExtension(_selectedImage, ".json"), serializedData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void LoadStateObjetosSimulables()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ObjetosSimulables.Clear();
|
|
|
|
|
if (_selectedImage != null)
|
|
|
|
|
{
|
|
|
|
|
string jsonPath = datosDeTrabajo.ObtenerPathImagenConExtension(_selectedImage, ".json");
|
|
|
|
|
if (File.Exists(jsonPath))
|
|
|
|
|
{
|
|
|
|
|
string jsonString = File.ReadAllText(jsonPath);
|
|
|
|
|
var settings = new JsonSerializerSettings
|
|
|
|
|
{
|
|
|
|
|
TypeNameHandling = TypeNameHandling.Auto,
|
|
|
|
|
ObjectCreationHandling = ObjectCreationHandling.Replace,
|
|
|
|
|
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
|
|
|
|
|
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
|
|
|
|
|
};
|
2024-05-04 15:35:06 -03:00
|
|
|
|
if (jsonString != null)
|
2024-05-04 06:00:52 -03:00
|
|
|
|
{
|
2024-05-04 15:35:06 -03:00
|
|
|
|
ObjetosSimulables = JsonConvert.DeserializeObject<ObservableCollection<osBase>>(jsonString, settings);
|
|
|
|
|
|
|
|
|
|
// Ahora recorres la colección de objetos simulables
|
|
|
|
|
foreach (var objetoSimulable in ObjetosSimulables)
|
|
|
|
|
CrearUsercontrol(objetoSimulable);
|
2024-05-04 06:00:52 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch { /* Consider logging the error or handling it appropriately */ }
|
|
|
|
|
}
|
2024-05-02 11:06:45 -03:00
|
|
|
|
|
2024-05-04 15:35:06 -03:00
|
|
|
|
private bool CrearUsercontrol(osBase osObjeto)
|
|
|
|
|
{
|
|
|
|
|
Type tipoObjeto = osObjeto.GetType();
|
|
|
|
|
|
|
|
|
|
// Obtén el UserControl correspondiente para el tipo de objeto
|
|
|
|
|
UserControl? userControl = UserControlFactory.GetControlForType(tipoObjeto);
|
|
|
|
|
|
|
|
|
|
if (userControl != null)
|
|
|
|
|
{
|
|
|
|
|
// Asignar los datos al UserControl
|
|
|
|
|
UserControlFactory.AssignDatos(userControl, osObjeto);
|
|
|
|
|
|
|
|
|
|
OnUserControlSelected?.Invoke(userControl);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-03 03:58:21 -03:00
|
|
|
|
// Implementación de INotifyPropertyChanged...
|
2024-05-02 11:06:45 -03:00
|
|
|
|
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
|
|
|
|
{
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
2024-05-03 05:13:25 -03:00
|
|
|
|
|
2024-05-04 06:00:52 -03:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class TipoSimulable
|
|
|
|
|
{
|
|
|
|
|
public string Nombre { get; set; }
|
|
|
|
|
public Type Tipo { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class TickSimulacionEventArgs : EventArgs
|
|
|
|
|
{
|
|
|
|
|
// Aquí puedes agregar propiedades o campos para pasar información adicional
|
|
|
|
|
// en el evento TickSimulacion
|
2024-05-02 11:06:45 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|