Exemplo n.º 1
0
        public void repaintPlotLines()
        {
            List <UIElement> deleted = new List <UIElement>();

            foreach (UIElement uie in canvas.Children)
            {
                if (uie is Line || uie is Ellipse)
                {
                    deleted.Add(uie);
                }
            }
            foreach (UIElement uie in deleted)
            {
                canvas.Children.Remove(uie);
            }

            double minVol       = minVoltage;
            double maxVol       = maxVoltage;
            double circleSize   = 4.0;
            int    sampleOffset = 0;

            for (int i = 0; i < nodes.Count; i++)
            {
                Line gridline;
                gridline = new Line()
                {
                    Stroke = Brushes.Black, StrokeThickness = 2
                };
                gridline.X1 = 0;
                gridline.X2 = Division.width * nodes.Count;
                gridline.Y1 = 0;
                gridline.Y2 = 0;
                canvas.Children.Add(gridline);
                gridline = new Line()
                {
                    Stroke = Brushes.Black, StrokeThickness = 2
                };
                gridline.X1 = 0;
                gridline.X2 = Division.width * nodes.Count;
                gridline.Y1 = height;
                gridline.Y2 = height;
                canvas.Children.Add(gridline);

                if (isOutput)
                {
                    Ellipse Node = new Ellipse()
                    {
                        Fill = Brushes.Black, StrokeThickness = 2, Width = 8, Height = 8
                    };
                    Canvas.SetLeft(Node, Division.width * i - circleSize);
                    if (isAnalog)
                    {
                        Canvas.SetTop(Node, plotHeightInCanvas(nodes[i].value, minVol, maxVol) - circleSize);
                    }
                    else
                    {
                        Canvas.SetTop(Node, height / 2 + (nodes[i].value == 0 ? height / 4 : -height / 4) - circleSize);
                    }
                    canvas.Children.Add(Node);
                }
                Line line;
                int  next = i + 1;
                if (next == nodes.Count)
                {
                    continue;
                }

                if (isOutput)
                {
                    // digital out
                    if (!isAnalog)
                    {
                        line = new Line()
                        {
                            Stroke = Brushes.LightGray, StrokeThickness = 2
                        };
                        line.X1 = Division.width * i;
                        line.X2 = Division.width * next;
                        line.Y1 = line.Y2 = nodes[i].value == 0 ? height / 2 + height / 4 : height / 2 - height / 4;
                        canvas.Children.Add(line);
                        if ((nodes[i].value == 0) != (nodes[next].value == 0))
                        {
                            line = new Line()
                            {
                                Stroke = Brushes.LightGray, StrokeThickness = 2
                            };
                            line.X1 = Division.width * next;
                            line.X2 = Division.width * next;
                            line.Y1 = nodes[i].value == 0 ? height / 2 + height / 4 : height / 2 - height / 4;
                            line.Y2 = nodes[next].value == 0 ? height / 2 + height / 4 : height / 2 - height / 4;
                            canvas.Children.Add(line);
                        }
                    }
                    // analog out
                    else
                    {
                        if (nodes[i].type == NodeType.Hold)
                        {
                            line = new Line()
                            {
                                Stroke = Brushes.LightGray, StrokeThickness = 2
                            };
                            line.X1 = Division.width * i;
                            line.X2 = Division.width * next;
                            line.Y1 = plotHeightInCanvas(nodes[i].value, minVol, maxVol);
                            line.Y2 = line.Y1;
                            canvas.Children.Add(line);
                            line = new Line()
                            {
                                Stroke = Brushes.LightGray, StrokeThickness = 2
                            };
                            line.X1 = Division.width * next;
                            line.X2 = Division.width * next;
                            line.Y1 = plotHeightInCanvas(nodes[i].value, minVol, maxVol);
                            line.Y2 = plotHeightInCanvas(nodes[next].value, minVol, maxVol);
                            canvas.Children.Add(line);
                        }
                        else if (nodes[i].type == NodeType.Linear)
                        {
                            line = new Line()
                            {
                                Stroke = Brushes.LightGray, StrokeThickness = 2
                            };
                            line.X1 = Division.width * i;
                            line.X2 = Division.width * next;
                            line.Y1 = plotHeightInCanvas(nodes[i].value, minVol, maxVol);
                            line.Y2 = plotHeightInCanvas(nodes[next].value, minVol, maxVol);
                            canvas.Children.Add(line);
                        }
                    }
                }
                else
                {
                    //analog in
                    if (isAnalog)
                    {
                        if (inputWaveArray != null)
                        {
                            int divisionSample = parent.getDivisionSampleCount(i);
                            int pixStep        = Division.width / 70;
                            int sampleStep     = (int)(1.0 * pixStep * divisionSample / Division.width);
                            for (int pix = 0; pix < Division.width; pix += pixStep)
                            {
                                int cur = (int)(1.0 * divisionSample * pix / Division.width);
                                if (sampleOffset + cur + sampleStep < inputWaveArray.Length)
                                {
                                    line = new Line()
                                    {
                                        Stroke = Brushes.Black, StrokeThickness = 1
                                    };
                                    line.X1 = Division.width * i + pix;
                                    line.X2 = Math.Min(Division.width * i + pix + pixStep, Division.width * next);
                                    line.Y1 = plotHeightInCanvas(inputWaveArray[sampleOffset + cur], minVol, maxVol);
                                    line.Y2 = plotHeightInCanvas(inputWaveArray[sampleOffset + Math.Min(cur + sampleStep, divisionSample - 1)], minVol, maxVol);
                                    canvas.Children.Add(line);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            sampleOffset += divisionSample;
                        }
                    }
                }
            }
        }