using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Shapes; namespace Signal_Generator { public class ProcedureModel : ICommand, INotifyPropertyChanged { public DrawTask currentTask; Canvas targetCanvas; public ProcedureModel() { } public event EventHandler CanExecuteChanged; public void RaiseCanExecuteChanged() { CanExecuteChanged?.Invoke(this, new EventArgs()); } private void RaiseCanExecuteChanged(object o, PropertyChangedEventArgs e) { Action a = () => { RaiseCanExecuteChanged(); }; targetCanvas.Dispatcher.Invoke(a); } public bool CanExecute(object parameter) { if (parameter == null) return false; var parameters = parameter as object[]; var btn = parameters[0] as Button; var ms = MultiSignal.getInstance(); if ((ms == null) || (ms.Count() == 0)) return false; switch (btn.Name) { case "start_Button": btn.Content = updateStartButtonContent(); return true; default: btn.Content = updatePauseButtonContent().Item2; if (!updatePauseButtonContent().Item1) return false; return true; } } private string updateStartButtonContent() { if (currentTask == null) return "Запустить"; switch (currentTask.state) { case DrawTask.State.Created: return "Запустить"; case DrawTask.State.Running: return "Завершить"; case DrawTask.State.Paused: goto case DrawTask.State.Running; default: goto case DrawTask.State.Created; } } private Tuple updatePauseButtonContent() { if (currentTask == null) return new Tuple(false, "Пауза"); switch (currentTask.state) { case DrawTask.State.Created: return new Tuple(false, "Пауза"); case DrawTask.State.Running: return new Tuple(true, "Пауза"); case DrawTask.State.Paused: return new Tuple(true, "Возобновить"); default: goto case DrawTask.State.Created; } } public void Execute(object parameter) { var parameters = parameter as object[]; var btn = parameters[0] as Button; targetCanvas = parameters[1] as Canvas; switch (btn.Name) { case "start_Button": startOrCancelTask(); break; case "pause_Button": pauseOrResumeTask(); break; } } private void startOrCancelTask () { targetCanvas.Children.Clear(); if (currentTask != null) switch (currentTask.state) { case DrawTask.State.Paused: goto case DrawTask.State.Running; case DrawTask.State.Running: currentTask.state = DrawTask.State.Canceled; return; } Action newAction = () => { currentTask.PropertyChanged += RaiseCanExecuteChanged; currentTask.buildTask(this, targetCanvas); }; currentTask = new DrawTask(newAction); currentTask.Start(); } private void pauseOrResumeTask() { switch (currentTask.state) { case DrawTask.State.Paused: currentTask.state = DrawTask.State.Running; break; case DrawTask.State.Running: currentTask.state = DrawTask.State.Paused; break; } } public event PropertyChangedEventHandler PropertyChanged; private string currentAmplitude; public string CurrentAmplitude { get { return currentAmplitude; } set { currentAmplitude = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("CurrentAmplitude")); } } private string currentTime; public string CurrentTime { get { return currentTime; } set { currentTime = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("CurrentTime")); } } private double duration; public double Duration { get { return duration; } set { duration = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("CurrentTime")); } } } }