using CtrEditor; using System; 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; using System.Windows.Input; using System.Collections.ObjectModel; using System.Windows.Media; using System.Windows.Media.Imaging; using static System.Runtime.InteropServices.JavaScript.JSType; using System.Windows.Threading; namespace CtrEditor { public class TickSimulacionEventArgs : EventArgs { // Aquí puedes agregar propiedades o campos para pasar información adicional // en el evento TickSimulacion } public class MainViewModel : INotifyPropertyChanged { public DatosDeTrabajo datosDeTrabajo { get; } public ObservableCollection listaImagenes { get; private set; } // Publicación de las claves del diccionario private ObservableCollection _funciones; private readonly DispatcherTimer _timerSimulacion; private string _selectedImage; // Evento que se dispara cuando se selecciona una nueva imagen public event EventHandler ImageSelected; public event EventHandler TickSimulacion; public MainViewModel() { OpenWorkDirectoryCommand = new RelayCommand(OpenWorkDirectory); datosDeTrabajo = new DatosDeTrabajo(); listaImagenes = new ObservableCollection(datosDeTrabajo.Imagenes.Keys); _funciones = new ObservableCollection(); _funciones.Add(new OSBotella()); _funciones.Add(new OSPack()); directorioTrabajo = EstadoPersistente.Instance.directorio; _timerSimulacion = new DispatcherTimer(); _timerSimulacion.Interval = TimeSpan.FromMilliseconds(100); // ajusta el intervalo según sea necesario _timerSimulacion.Tick += OnTickSimulacion; StartSimulationCommand = new RelayCommand(StartSimulation); StopSimulationCommand = new RelayCommand(StopSimulation); } public ICommand StartSimulationCommand { get; } public ICommand StopSimulationCommand { get; } private void StartSimulation() { _timerSimulacion.Start(); } private void StopSimulation() { _timerSimulacion.Stop(); } private void OnTickSimulacion(object sender, EventArgs e) { var args = new TickSimulacionEventArgs(); OnTickSimulacion(args); } protected virtual void OnTickSimulacion(TickSimulacionEventArgs e) { TickSimulacion?.Invoke(this, e); } public string directorioTrabajo { get => EstadoPersistente.Instance.directorio; set { if (value != null) { EstadoPersistente.Instance.directorio = value; // Actualizar el estado persistente EstadoPersistente.Instance.GuardarEstado(); // Guardar el estado actualizado datosDeTrabajo.CargarImagenes(); listaImagenes = new ObservableCollection(datosDeTrabajo.Imagenes.Keys); // Actualizar claves OnPropertyChanged(nameof(directorioTrabajo)); // Notificar el cambio de propiedad OnPropertyChanged(nameof(listaImagenes)); // Notificar que la lista de imágenes ha cambiado } } } public ObservableCollection CtrSimulado { get { return _funciones; } set { _funciones = value; } } public string SelectedImage { get => _selectedImage; set { if (_selectedImage != value) { _selectedImage = value; OnPropertyChanged(nameof(SelectedImage)); ImageSelected?.Invoke(this, datosDeTrabajo.Imagenes[value]); // Dispara el evento con la nueva ruta de imagen } } } 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 { directorioTrabajo = dialog.SelectedPath; // Actualizar la propiedad que también actualiza el estado persistente } } // Implementación de INotifyPropertyChanged... public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } }