CtrEditor/MainViewModel.cs

143 lines
4.9 KiB
C#
Raw Normal View History

2024-05-03 05:13:25 -03:00
using CtrEditor;
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;
using System.Windows.Input;
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-02 11:06:45 -03:00
namespace CtrEditor
{
2024-05-03 05:13:25 -03:00
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
public class MainViewModel : INotifyPropertyChanged
{
public DatosDeTrabajo datosDeTrabajo { get; }
public ObservableCollection<string> listaImagenes { get; private set; } // Publicación de las claves del diccionario
2024-05-03 05:13:25 -03:00
private ObservableCollection<ICtrSimulado> _funciones;
private readonly DispatcherTimer _timerSimulacion;
private string _selectedImage;
2024-05-02 11:06:45 -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-02 11:06:45 -03:00
public MainViewModel()
{
OpenWorkDirectoryCommand = new RelayCommand(OpenWorkDirectory);
datosDeTrabajo = new DatosDeTrabajo();
listaImagenes = new ObservableCollection<string>(datosDeTrabajo.Imagenes.Keys);
2024-05-03 05:13:25 -03:00
_funciones = new ObservableCollection<ICtrSimulado>();
_funciones.Add(new OSBotella());
_funciones.Add(new OSPack());
directorioTrabajo = EstadoPersistente.Instance.directorio;
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);
StopSimulationCommand = new RelayCommand(StopSimulation);
}
public ICommand StartSimulationCommand { get; }
public ICommand StopSimulationCommand { get; }
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
public string directorioTrabajo
2024-05-02 11:06:45 -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)
{
EstadoPersistente.Instance.directorio = value; // Actualizar el estado persistente
2024-05-02 11:26:45 -03:00
EstadoPersistente.Instance.GuardarEstado(); // Guardar el estado actualizado
datosDeTrabajo.CargarImagenes();
listaImagenes = new ObservableCollection<string>(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
2024-05-02 11:26:45 -03:00
}
2024-05-02 11:06:45 -03:00
}
}
2024-05-03 05:13:25 -03:00
public ObservableCollection<ICtrSimulado> 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
}
}
}
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
{
directorioTrabajo = dialog.SelectedPath; // Actualizar la propiedad que también actualiza el estado persistente
2024-05-02 11:06:45 -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-02 11:06:45 -03:00
}
}