aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/RealTimeGraphX.WPF/Painters/WpfScrollingGraphPainter.cs
blob: 32869e369a77877ff2606a08875ac2668099dda7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using RealTimeGraphX.EventArguments;
using RealTimeGraphX.WPF.DataSeries;

namespace RealTimeGraphX.WPF.Painters
{
    /// <summary>
    /// Represents a WPF implementation for <see cref="IGraphPainter{TImage}"/>.
    /// This painter provides a graph image of type <see cref="BitmapSource"/>.
    /// </summary>
    /// <seealso cref="RealTimeGraphX.GraphPainterBase" />
    /// <seealso cref="RealTimeGraphX.IGraphPainter{System.Windows.Media.Imaging.BitmapSource}" />
    public class WpfScrollingGraphPainter : GraphPainterBase<WpfDataSeries, BitmapSource>
    {
        private WriteableBitmap _writeable_bitmap;
        private Bitmap _gdi_bitmap;
        private Graphics g;

        /// <summary>
        /// Gets the current graph image.
        /// </summary>
        public override BitmapSource Image { get; protected set; }

        /// <summary>
        /// Called when the connected input <see cref="T:RealTimeGraphX.IGraphRenderer" /> invoked the <see cref="M:RealTimeGraphX.GraphPainterBase.Draw(RealTimeGraphX.GraphDataSeries,System.Collections.Generic.IEnumerable{RealTimeGraphX.Drawing.GraphPoint})" /> method.
        /// </summary>
        /// <param name="series">The series to draw.</param>
        /// <param name="points">The points to draw.</param>
        /// <param name="sizeChanged">Specifies whether there is a need to reinitialize the image because surface size change.</param>
        protected override void OnDraw(IEnumerable<WpfDataSeries> seriesCollection, WpfDataSeries series, IEnumerable<GraphPoint> points, bool sizeChanged)
        {
            if (sizeChanged)
            {
                if (g != null)
                {
                    g.Dispose();
                }

                _writeable_bitmap = new WriteableBitmap((int)Math.Max(Output.SurfaceWidth, 1), (int)Math.Max(Output.SurfaceHeight, 1), 96.0, 96.0, PixelFormats.Pbgra32, null);

                if (_gdi_bitmap != null)
                {
                    _gdi_bitmap.Dispose();
                }

                _gdi_bitmap = new Bitmap(_writeable_bitmap.PixelWidth, _writeable_bitmap.PixelHeight,
                                             _writeable_bitmap.BackBufferStride,
                                             System.Drawing.Imaging.PixelFormat.Format32bppPArgb,
                                             _writeable_bitmap.BackBuffer);

                _writeable_bitmap.Lock();
            }

            bool isFirst = series == seriesCollection.First();
            bool isLast = series == seriesCollection.Last();

            if (isFirst)
            {
                if (!sizeChanged)
                {
                    _writeable_bitmap.Lock();
                }

                g = Graphics.FromImage(_gdi_bitmap);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.Clear(System.Drawing.Color.Transparent);


                //Apply zooming.
                if (ZoomRect.Width > 0 && ZoomRect.Height > 0)
                {
                    var scale_x = (float)(Output.SurfaceWidth / ZoomRect.Width);
                    var scale_y = (float)(Output.SurfaceHeight / ZoomRect.Height);
                    var translate_x = (float)-ZoomRect.Left * scale_x;
                    var translate_y = (float)-ZoomRect.Top * scale_y;

                    g.TranslateTransform(translate_x, translate_y);
                    g.ScaleTransform(scale_x, scale_y);


                    if (scale_x != Transform.ScaleX || scale_y != Transform.ScaleY || translate_x != Transform.TranslateX || translate_y != Transform.TranslateY)
                    {
                        OnTransformChanged(new TransformChangedEventArgs()
                        {
                            Transform = new GraphTransform()
                            {
                                ScaleX = scale_x,
                                ScaleY = scale_y,
                                TranslateX = translate_x,
                                TranslateY = translate_y,
                            }
                        });

                        OnVirtualRangeXChanged();
                        OnVirtualRangeYChanged();
                    }
                }
            }

            OnDraw(seriesCollection, series, points, g);

            if (isLast)
            {
                _writeable_bitmap.AddDirtyRect(new Int32Rect(0, 0, _writeable_bitmap.PixelWidth, _writeable_bitmap.PixelHeight));
                _writeable_bitmap.Unlock();

                var cloned = _writeable_bitmap.Clone();
                cloned.Freeze();
                OnPaintingCompleted(cloned);

                g.Dispose();
            }
        }

        /// <summary>
        /// Called when the connected input <see cref="T:RealTimeGraphX.IGraphRenderer" /> invoked the <see cref="M:RealTimeGraphX.GraphPainterBase.Draw(RealTimeGraphX.GraphDataSeries,System.Collections.Generic.IEnumerable{RealTimeGraphX.Drawing.GraphPoint})" /> method.
        /// </summary>
        /// <param name="seriesCollection">The series collection.</param>
        /// <param name="series">The series.</param>
        /// <param name="points">The points.</param>
        /// <param name="g">The graphics object.</param>
        protected virtual void OnDraw(IEnumerable<WpfDataSeries> seriesCollection, WpfDataSeries series, IEnumerable<GraphPoint> points, Graphics g)
        {
            if (SurfaceWidth > 10 && SurfaceHeight > 10)
            {
                System.Drawing.Pen pen = new System.Drawing.Pen(series.GdiStroke, (float)series.StrokeThickness);

                List<PointF> gdiPoints = points.Select(x => new PointF((float)x.X, (float)x.Y)).ToList();

                if (series.GdiFill != null)
                {
                    if (series.GdiFill is System.Drawing.Drawing2D.LinearGradientBrush)
                    {
                        var gradient = (series.GdiFill as System.Drawing.Drawing2D.LinearGradientBrush);

                        gradient.ResetTransform();
                        gradient.ScaleTransform((float)(Output.SurfaceWidth / gradient.Rectangle.Width), (float)(Output.SurfaceHeight / gradient.Rectangle.Height));
                    }

                    g.FillPolygon(series.GdiFill, GetFillPoints(gdiPoints));
                }

                try
                {
                    g.DrawCurve(pen, gdiPoints.ToArray());
                }
                catch { }

                pen.Dispose();
            }
        }

        /// <summary>
        /// Gets a closed polygon version of the specified points to use with FillPolygon().
        /// </summary>
        /// <param name="points">The points.</param>
        /// <returns></returns>
        protected virtual PointF[] GetFillPoints(List<PointF> points)
        {
            List<PointF> closed = points.ToList();
            closed.Add(new PointF(points.Last().X, (float)SurfaceWidth));
            closed.Add(new PointF(0, (float)SurfaceHeight));
            return closed.ToArray();
        }
    }
}