aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/RealTimeGraphX/GraphPainterBase.cs
blob: 12f6143fcac988329c87ab06e82bc6d20f34a02d (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using RealTimeGraphX.EventArguments;

namespace RealTimeGraphX
{
    /// <summary>
    /// Represents an <see cref="IGraphPainter"/> base class.
    /// </summary>
    /// <typeparam name="TDataSeries">The type of the data series.</typeparam>
    /// <typeparam name="TImage">The type of the image.</typeparam>
    /// <seealso cref="RealTimeGraphX.GraphInputOutputComponentBase{RealTimeGraphX.IGraphRenderer{TDataSeries}, RealTimeGraphX.IGraphSurface}" />
    /// <seealso cref="RealTimeGraphX.IGraphPainter{TDataSeries, TImage}" />
    public abstract class GraphPainterBase<TDataSeries, TImage> : GraphInputOutputComponentBase<IGraphRenderer<TDataSeries>, IGraphSurface>, IGraphPainter<TDataSeries, TImage> where TDataSeries : IGraphDataSeries
    {
        private bool _size_changed;

        #region Events

        /// <summary>
        /// Occurs when the painter has a new graph image available.
        /// </summary>
        public event EventHandler<PaintingCompletedEventArgs<TImage>> PaintingCompleted;

        /// <summary>
        /// Occurs when the current painter transformation (Scale/Translate) has changed.
        /// </summary>
        public event EventHandler<TransformChangedEventArgs> TransformChanged;

        /// <summary>
        /// Occurs when the current virtual (effective minimum/maximum after transformation) x-axis minimum/maximum has changed.
        /// </summary>
        public event EventHandler<RangeChangedEventArgs> VirtualRangeXChanged;

        /// <summary>
        /// Occurs when the current virtual (effective minimum/maximum after transformation) y-axis minimum/maximum has changed.
        /// </summary>
        public event EventHandler<RangeChangedEventArgs> VirtualRangeYChanged;

        #endregion

        #region Properties

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

        private GraphTransform _transform;
        /// <summary>
        /// Gets the current painter transform object.
        /// The transform object is constructed based on the current <see cref="ZoomRect" />.
        /// </summary>
        public GraphTransform Transform
        {
            get
            {
                return _transform;
            }
            private set
            {
                _transform = value;
                RaisePropertyChangedAuto();
            }
        }

        private GraphRect _zoomRect;
        /// <summary>
        /// Gets the last zooming rectangle that was passed on <see cref="SetZoomRect(GraphRect)" />.
        /// </summary>
        public GraphRect ZoomRect
        {
            get
            {
                return _zoomRect;
            }
            private set
            {
                _zoomRect = value;
                RaisePropertyChangedAuto();
            }
        }

        private GraphDataPointBase _virtualMinimumX;
        /// <summary>
        /// Gets the current virtual (effective minimum/maximum after transformation) x-axis minimum.
        /// </summary>
        public GraphDataPointBase VirtualMinimumX
        {
            get
            {
                return _virtualMinimumX;
            }
            private set
            {
                _virtualMinimumX = value;
                RaisePropertyChangedAuto();
            }
        }

        private GraphDataPointBase _virtualMaximumX;
        /// <summary>
        /// Gets the current virtual (effective minimum/maximum after transformation) x-axis maximum.
        /// </summary>
        public GraphDataPointBase VirtualMaximumX
        {
            get
            {
                return _virtualMaximumX;
            }
            private set
            {
                _virtualMaximumX = value;
                RaisePropertyChangedAuto();
            }
        }

        private GraphDataPointBase _virtualMinimumY;
        /// <summary>
        /// Gets the current virtual (effective minimum/maximum after transformation) y-axis minimum.
        /// </summary>
        public GraphDataPointBase VirtualMinimumY
        {
            get
            {
                return _virtualMinimumY;
            }
            private set
            {
                _virtualMinimumY = value;
                RaisePropertyChangedAuto();
            }
        }

        private GraphDataPointBase _virtualMaximumY;
        /// <summary>
        /// Gets the current virtual (effective minimum/maximum after transformation) y-axis maximum.
        /// </summary>
        public GraphDataPointBase VirtualMaximumY
        {
            get
            {
                return _virtualMaximumY;
            }
            private set
            {
                _virtualMaximumY = value;
                RaisePropertyChangedAuto();
            }
        }

        /// <summary>
        /// Gets the width of the connected output surface.
        /// </summary>
        public double SurfaceWidth { get; private set; }

        /// <summary>
        /// Gets the height of the connected output surface.
        /// </summary>
        public double SurfaceHeight { get; private set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="GraphPainterBase"/> class.
        /// </summary>
        public GraphPainterBase()
        {
            Transform = new GraphTransform();

            //VirtualMinimumX = GraphDataPointHelper.Init<XDataPoint>();
            //VirtualMinimumY = GraphDataPointHelper.Init<YDataPoint>();
            //VirtualMaximumX = GraphDataPointHelper.Init<XDataPoint>();
            //VirtualMaximumY = GraphDataPointHelper.Init<YDataPoint>();
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Sets the painter zooming rectangle.
        /// The zoom rectangle must be smaller than the size of the connected <see cref="IGraphSurface" /> size.
        /// </summary>
        /// <param name="rect">The rectangle.</param>
        public void SetZoomRect(GraphRect rect)
        {
            ZoomRect = rect;
            Input.Invalidate();
        }

        /// <summary>
        /// Resets the painter zooming rectangle.
        /// </summary>
        public void ResetZoomRect()
        {
            ZoomRect = new GraphRect();
            OnVirtualRangeXChanged();
            OnVirtualRangeYChanged();
            Input.Invalidate();
        }

        /// <param name="seriesCollection">The entire series collection.</param>
        /// <param name="series">The data series.</param>
        /// <param name="points">The points.</param>
        public void Draw(IEnumerable<TDataSeries> seriesCollection, TDataSeries series, IEnumerable<GraphPoint> points)
        {
            OnDraw(seriesCollection, series, points, _size_changed);
            _size_changed = false;
        }

        #endregion

        #region Protected Methods

        /// <summary>
        /// Called when the connected input <see cref="IGraphRenderer"/> invoked the <see cref="Draw(GraphDataSeries, IEnumerable{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 abstract void OnDraw(IEnumerable<TDataSeries> seriesCollection, TDataSeries series, IEnumerable<GraphPoint> points, bool sizeChanged);

        /// <summary>
        /// Called when the connected surface size had changed.
        /// </summary>
        /// <param name="e">The <see cref="SurfaceSizeChangedEventArgs"/> instance containing the event data.</param>
        protected virtual void OnSurfaceSizeChanged(SurfaceSizeChangedEventArgs e)
        {
            SurfaceWidth = e.Width;
            SurfaceHeight = e.Height;
        }

        /// <summary>
        /// Called when the virtual x-axis minimum/maximum range has changed.
        /// </summary>
        protected virtual void OnVirtualRangeXChanged()
        {
            if (ZoomRect.Width > 0 || ZoomRect.Height > 0)
            {
                var zoom_rect_left_percentage = ZoomRect.Left / Output.SurfaceWidth;
                var zoom_rect_right_percentage = ZoomRect.Right / Output.SurfaceWidth;

                VirtualMinimumX = GraphDataPointHelper.ComputeAbsolutePosition(Input.EffectiveMinimumX, Input.EffectiveMaximumX, zoom_rect_left_percentage) as GraphDataPointBase;
                VirtualMaximumX = GraphDataPointHelper.ComputeAbsolutePosition(Input.EffectiveMinimumX, Input.EffectiveMaximumX, zoom_rect_right_percentage) as GraphDataPointBase;
            }
            else
            {
                VirtualMinimumX = Input.EffectiveMinimumX;
                VirtualMaximumX = Input.EffectiveMaximumX;
            }

            VirtualRangeXChanged?.Invoke(this, new RangeChangedEventArgs(VirtualMinimumX, VirtualMaximumX));
        }

        /// <summary>
        /// Called when the virtual y-axis minimum/maximum range has changed.
        /// </summary>
        protected virtual void OnVirtualRangeYChanged()
        {
            if (ZoomRect.Width > 0 || ZoomRect.Height > 0)
            {
                var zoom_rect_top_percentage = ZoomRect.Top / Output.SurfaceHeight;
                var zoom_rect_bottom_percentage = ZoomRect.Bottom / Output.SurfaceHeight;

                VirtualMinimumY = Input.EffectiveMaximumY - GraphDataPointHelper.ComputeAbsolutePosition(Input.EffectiveMinimumY, Input.EffectiveMaximumY, zoom_rect_bottom_percentage) as GraphDataPointBase;
                VirtualMaximumY = Input.EffectiveMaximumY - GraphDataPointHelper.ComputeAbsolutePosition(Input.EffectiveMinimumY, Input.EffectiveMaximumY, zoom_rect_top_percentage) as GraphDataPointBase;
            }
            else
            {
                VirtualMinimumY = Input.EffectiveMinimumY;
                VirtualMaximumY = Input.EffectiveMaximumY;
            }

            VirtualRangeYChanged?.Invoke(this, new RangeChangedEventArgs(VirtualMinimumY, VirtualMaximumY));
        }

        /// <summary>
        /// Called when the painter transformation has changed.
        /// </summary>
        /// <param name="e">The <see cref="TransformChangedEventArgs"/> instance containing the event data.</param>
        protected virtual void OnTransformChanged(TransformChangedEventArgs e)
        {
            Transform = e.Transform;
            TransformChanged?.Invoke(this, e);
        }

        /// <summary>
        /// Returns a closed polygon points which can be used to fill a graph polygon/>.
        /// </summary>
        /// <param name="points">The points.</param>
        /// <returns></returns>
        protected virtual IEnumerable<GraphPoint> GetFillPoints(IEnumerable<GraphPoint> points)
        {
            List<GraphPoint> closed = points.ToList();
            closed.Add(new GraphPoint(points.Last().X, SurfaceWidth));
            closed.Add(new GraphPoint(0, SurfaceHeight));
            return closed;
        }

        /// <summary>
        /// Raises the <see cref="PaintingCompletedEventArgs{TImage}"/> event.
        /// </summary>
        /// <param name="image">The image.</param>
        protected virtual void OnPaintingCompleted(TImage image)
        {
            Output.InvokeOnSurface(() =>
            {
                Image = image;
                PaintingCompleted?.Invoke(this, new PaintingCompletedEventArgs<TImage>() { Image = image });
            });
        }

        #endregion

        #region Event Handlers

        private void Input_EffectiveRangeYChanged(object sender, RangeChangedEventArgs e)
        {
            OnVirtualRangeYChanged();
        }

        private void Input_EffectiveRangeXChanged(object sender, RangeChangedEventArgs e)
        {
            OnVirtualRangeXChanged();
        }

        private void _surface_SurfaceSizeChanged(object sender, SurfaceSizeChangedEventArgs e)
        {
            _size_changed = true;
            OnSurfaceSizeChanged(e);
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Connects this painter to the specified <see cref="IGraphRenderer"/>.
        /// </summary>
        /// <param name="renderer">The renderer.</param>
        /// <param name="fromInput">Specifies whether this call was made from the input <see cref="IGraphRenderer"/>.</param>
        public override void ConnectInput(IGraphRenderer<TDataSeries> renderer, bool fromInput = false)
        {
            renderer.ThrowIfNull("Cannot connect a null renderer.");

            DisconnectInput();

            Input = renderer;

            if (!fromInput)
            {
                Input.ConnectOutput(this, true);
            }

            Input.EffectiveRangeXChanged += Input_EffectiveRangeXChanged;
            Input.EffectiveRangeYChanged += Input_EffectiveRangeYChanged;
        }

        /// <summary>
        /// Disconnects the connected input <see cref="IGraphRenderer"/>.
        /// </summary>
        /// <param name="fromInput">Specifies whether this call was made from the connected input <see cref="IGraphRenderer"/>.</param>
        public override void DisconnectInput(bool fromInput = false)
        {
            if (Input != null)
            {
                if (!fromInput)
                {
                    Input.DisconnectOutput(true);
                }

                Input.EffectiveRangeXChanged -= Input_EffectiveRangeXChanged;
                Input.EffectiveRangeYChanged -= Input_EffectiveRangeYChanged;

                Input = null;
            }
        }

        /// <summary>
        /// Connects this painter to the specified <see cref="IGraphSurface"/>.
        /// </summary>
        /// <param name="surface">The graph surface.</param>
        /// <param name="fromOutput">Specifies whether this call was made from the connected output <see cref="IGraphSurface"/>.</param>
        public override void ConnectOutput(IGraphSurface surface, bool fromOutput = false)
        {
            surface.ThrowIfNull("Cannot connect a null surface.");

            DisconnectOutput();

            Output = surface;

            Output.SurfaceSizeChanged += _surface_SurfaceSizeChanged;

            if (!fromOutput)
            {
                Output.ConnectInput(this, true);
            }

            _size_changed = true;
        }

        /// <summary>
        /// Disconnects this painter from the current connected <see cref="IGraphSurface"/>.
        /// </summary>
        /// <param name="fromOutput">Specifies whether this call was made the output <see cref="IGraphSurface"/>.</param>
        public override void DisconnectOutput(bool fromOutput = false)
        {
            if (Output != null)
            {
                if (!fromOutput)
                {
                    Output.DisconnectInput(true);
                }

                Output.SurfaceSizeChanged -= _surface_SurfaceSizeChanged;

                Output = null;
            }
        }

        #endregion
    }
}