aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/RealTimeGraphX.WPF/Components/GraphSurfaceComponentBase.cs
blob: e0cf424aa3b6843ef408640e595000cdd272f2c7 (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
using RealTimeGraphX.EventArguments;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace RealTimeGraphX
{
    /// <summary>
    /// Represents an <see cref="IGraphSurfaceComponent"/> base class.
    /// </summary>
    /// <seealso cref="System.Windows.Controls.Control" />
    /// <seealso cref="RealTimeGraphX.IGraphSurfaceComponent" />
    public class GraphSurfaceComponentBase : Control, IGraphSurfaceComponent
    {
        /// <summary>
        /// Gets or sets the surface.
        /// </summary>
        public IGraphSurface Surface
        {
            get { return (IGraphSurface)GetValue(SurfaceProperty); }
            set { SetValue(SurfaceProperty, value); }
        }
        public static readonly DependencyProperty SurfaceProperty =
            DependencyProperty.Register("Surface", typeof(IGraphSurface), typeof(GraphSurfaceComponentBase), new PropertyMetadata(null, (d, e) => (d as GraphSurfaceComponentBase).OnSurfaceChanged()));

        /// <summary>
        /// Called when the <see cref="Surface"/> property has been changed.
        /// </summary>
        protected virtual void OnSurfaceChanged()
        {
            if (!this.IsInDesignMode() &&  Surface != null)
            {
                Surface.InputChanged -= Surface_InputChanged;
                Surface.InputChanged += Surface_InputChanged;

                if (Surface.Input != null)
                {
                    OnSurfacePainterChanged(Surface.Input);
                }
            }
        }

        /// <summary>
        /// Handles the InputChanged event of the Surface.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="InputChangedEventArgs{IGraphRenderer}"/> instance containing the event data.</param>
        private void Surface_InputChanged(object sender, InputChangedEventArgs<IGraphPainter> e)
        {
            OnSurfacePainterChanged(e.Input);
        }

        /// <summary>
        /// Called when surface painter has changed.
        /// </summary>
        /// <param name="painter">The painter.</param>
        protected virtual void OnSurfacePainterChanged(IGraphPainter painter)
        {
            //Do Nothing..
        }

        /// <summary>
        /// Invokes the specified method on the component dispatcher.
        /// </summary>
        /// <param name="action">The action.</param>
        protected void InvokeUI(Action action)
        {
            Dispatcher.BeginInvoke(action);
        }
    }
}