You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
5.2 KiB

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.Running:
case DrawTask.State.Paused:
return "Завершить";
default:
return "Запустить";
}
}
private Tuple<bool, string> updatePauseButtonContent()
{
if (currentTask == null) return new Tuple<bool, string>(false, "Пауза");
switch (currentTask.state)
{
case DrawTask.State.Running:
return new Tuple<bool, string>(true, "Пауза");
case DrawTask.State.Paused:
return new Tuple<bool, string>(true, "Возобновить");
default:
return new Tuple<bool, string>(false, "Пауза");
}
}
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 ()
{
if (currentTask != null)
switch (currentTask.state)
{
case DrawTask.State.Paused:
case DrawTask.State.Running:
currentTask.state = DrawTask.State.Canceled;
targetCanvas.Children.Clear();
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 = 1000;
public double Duration
{
get { return duration; }
set
{
duration = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("CurrentTime"));
}
}
}
}