From 101a65ad2758df2e3021f153b9565d0162671f98 Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 3 May 2024 10:13:25 +0200 Subject: [PATCH] Terminado el timer --- CtrEditor.csproj | 2 +- ICtrSimulado.cs | 15 +++++ MainViewModel.cs | 55 ++++++++++++++++++- MainWindow.xaml | 30 ++++------ MainWindow.xaml.cs | 15 +++++ ObjetoSimuladoBotella.xaml.cs => OSBotella.cs | 9 ++- ObjetoSimuladoBotella.xaml => OSBotella.xaml | 2 +- ObjetoSimuladoPack.xaml => OSPack.xaml | 2 +- ObjetoSimuladoPack.xaml.cs => OSPack.xaml.cs | 9 ++- 9 files changed, 112 insertions(+), 27 deletions(-) create mode 100644 ICtrSimulado.cs rename ObjetoSimuladoBotella.xaml.cs => OSBotella.cs (71%) rename ObjetoSimuladoBotella.xaml => OSBotella.xaml (82%) rename ObjetoSimuladoPack.xaml => OSPack.xaml (83%) rename ObjetoSimuladoPack.xaml.cs => OSPack.xaml.cs (72%) diff --git a/CtrEditor.csproj b/CtrEditor.csproj index b51ef84..b4389f1 100644 --- a/CtrEditor.csproj +++ b/CtrEditor.csproj @@ -2,7 +2,7 @@ WinExe - net7.0-windows + net8.0-windows8.0 enable enable true diff --git a/ICtrSimulado.cs b/ICtrSimulado.cs new file mode 100644 index 0000000..54fbedf --- /dev/null +++ b/ICtrSimulado.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CtrEditor +{ + public interface ICtrSimulado + { + string Nombre { get; } + void Update(); + // other common methods and properties + } +} diff --git a/MainViewModel.cs b/MainViewModel.cs index 5a2f4f0..df7a70f 100644 --- a/MainViewModel.cs +++ b/MainViewModel.cs @@ -1,4 +1,5 @@ -using System; +using CtrEditor; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; @@ -12,26 +13,71 @@ 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 { @@ -50,6 +96,12 @@ namespace CtrEditor } } + public ObservableCollection CtrSimulado + { + get { return _funciones; } + set { _funciones = value; } + } + public string SelectedImage { get => _selectedImage; @@ -84,6 +136,7 @@ namespace CtrEditor { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } + } } diff --git a/MainWindow.xaml b/MainWindow.xaml index c2a763d..e1332c5 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -13,8 +13,8 @@ - - + + @@ -23,9 +23,9 @@ - - - + + + @@ -35,23 +35,13 @@ - + - + + + - - - - - - - - - - - - @@ -62,6 +52,8 @@ + + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index a52c644..2db11cd 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -35,6 +35,7 @@ namespace CtrEditor if (DataContext is MainViewModel viewModel) { viewModel.ImageSelected += ViewModel_ImageSelected; + viewModel.TickSimulacion += MainViewModel_TickSimulacion; } } private void ViewModel_ImageSelected(object sender, string imagePath) @@ -139,6 +140,20 @@ namespace CtrEditor e.Handled = true; // Evita el procesamiento adicional del evento } + private void MainViewModel_TickSimulacion(object sender, TickSimulacionEventArgs e) + { + // aquí puedes agregar la lógica para actualizar tus UserControl + // en el ImagenEnTrabajoCanvas + foreach (var child in ImagenEnTrabajoCanvas.Children) + { + if (child is ICtrSimulado uc) + { + // llama al método Update de cada UserControl + uc.Update(); + } + } + } + private void MainWindow_Closed(object sender, EventArgs e) { if (DataContext is MainViewModel viewModel) diff --git a/ObjetoSimuladoBotella.xaml.cs b/OSBotella.cs similarity index 71% rename from ObjetoSimuladoBotella.xaml.cs rename to OSBotella.cs index 98fba5d..3d21aeb 100644 --- a/ObjetoSimuladoBotella.xaml.cs +++ b/OSBotella.cs @@ -18,12 +18,17 @@ namespace CtrEditor /// /// Interaction logic for ObjetoSimuladoBotella.xaml /// - public partial class ObjetoSimuladoBotella : UserControl + public partial class OSBotella : UserControl, ICtrSimulado { - public ObjetoSimuladoBotella() + public OSBotella() { InitializeComponent(); } + public string Nombre => "Botellas"; + public void Update() + { + // implementation of Update method + } } } diff --git a/ObjetoSimuladoBotella.xaml b/OSBotella.xaml similarity index 82% rename from ObjetoSimuladoBotella.xaml rename to OSBotella.xaml index d3df96a..4f6e727 100644 --- a/ObjetoSimuladoBotella.xaml +++ b/OSBotella.xaml @@ -1,4 +1,4 @@ - diff --git a/ObjetoSimuladoPack.xaml b/OSPack.xaml similarity index 83% rename from ObjetoSimuladoPack.xaml rename to OSPack.xaml index f832e81..4521a6b 100644 --- a/ObjetoSimuladoPack.xaml +++ b/OSPack.xaml @@ -1,4 +1,4 @@ - diff --git a/ObjetoSimuladoPack.xaml.cs b/OSPack.xaml.cs similarity index 72% rename from ObjetoSimuladoPack.xaml.cs rename to OSPack.xaml.cs index 710e17d..37de810 100644 --- a/ObjetoSimuladoPack.xaml.cs +++ b/OSPack.xaml.cs @@ -18,12 +18,17 @@ namespace CtrEditor /// /// Interaction logic for ObjetoSimuladoPack.xaml /// - public partial class ObjetoSimuladoPack : UserControl + public partial class OSPack : UserControl, ICtrSimulado { - public ObjetoSimuladoPack() + public OSPack() { InitializeComponent(); } + public string Nombre => "Packs"; + public void Update() + { + // implementation of Update method + } } }