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.

145 lines
4.5 KiB

using System;
using System.Collections.Generic;
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;
namespace Signal_Generator
{
public class ProcedureModel : ICommand
{
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<bool, string> updatePauseButtonContent()
{
if (currentTask == null) return new Tuple<bool, string>(false, "Пауза");
switch (currentTask.state)
{
case DrawTask.State.Created:
return new Tuple<bool, string>(false, "Пауза");
case DrawTask.State.Running:
return new Tuple<bool, string>(true, "Пауза");
case DrawTask.State.Paused:
return new Tuple<bool, string>(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(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;
}
}
}
}