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.

100 lines
2.9 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;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Signal_Generator
{
public class DrawTask : Task, INotifyPropertyChanged
{
Canvas canvas;
MultiSignal multiSignal;
ProcedureModel procedureModel;
private State _state;
public State state
{
get { return _state; }
set
{
_state = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("state"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public enum State
{
Created, Running, Paused, Finished, Canceled
}
public DrawTask(Action a) : base(a) { }
public void buildTask(ProcedureModel pm, Canvas c)
{
canvas = c;
procedureModel = pm;
multiSignal = MultiSignal.getInstance();
state = State.Running;
drawSignalAtCanvas();
}
private void drawSignalAtCanvas()
{
double lastX = 0, lastY = 0;
double w = canvas.ActualWidth;
for (int i = 0; i != (int)w; i++)
{
procedureModel.CurrentAmplitude = multiSignal.currentAmplitude(i / 5.0).ToString();
procedureModel.CurrentTime = (i / 5.0).ToString();
double y = multiSignal.currentAmplitude(i / 5.0);
drawLine(lastX, lastY, i, y);
lastX = i;
lastY = y;
Thread.Sleep(10);
switch (state)
{
case State.Paused:
while (state == State.Paused)
Thread.Sleep(10);
break;
case State.Canceled: return;
}
}
state = State.Finished;
}
private void drawLine(double x1, double y1, double x2, double y2)
{
Action a = () => {
Line l = new Line();
l.StrokeThickness = 1;
l.Stroke = Brushes.Red;
l.X1 = x1;
l.Y1 = remap(y1);
l.X2 = x2;
l.Y2 = remap(y2);
canvas.Children.Add(l);
};
canvas.Dispatcher.Invoke(a);
}
private double remap(double x, double oldMin = -100.0, double oldMax = 100.0, double newMin = 100.0, double newMax = 0.0)
{
newMin = canvas.ActualHeight;
return newMin + (x - oldMin) * (newMax - newMin) / (oldMax - oldMin);
}
}
}