External/OxyPlot/OxyPlot/Manipulators/TrackerManipulator.cs
author Stephane Lenclud
Sat, 30 Jan 2016 23:01:51 +0100
branchMiniDisplay
changeset 454 f84878f52cd9
permissions -rw-r--r--
Disabling Nuvoton NCT6791D because of fan full speed bug on Asus Z97 WS.
     1 // --------------------------------------------------------------------------------------------------------------------
     2 // <copyright file="TrackerManipulator.cs" company="OxyPlot">
     3 //   The MIT License (MIT)
     4 //
     5 //   Copyright (c) 2012 Oystein Bjorke
     6 //
     7 //   Permission is hereby granted, free of charge, to any person obtaining a
     8 //   copy of this software and associated documentation files (the
     9 //   "Software"), to deal in the Software without restriction, including
    10 //   without limitation the rights to use, copy, modify, merge, publish,
    11 //   distribute, sublicense, and/or sell copies of the Software, and to
    12 //   permit persons to whom the Software is furnished to do so, subject to
    13 //   the following conditions:
    14 //
    15 //   The above copyright notice and this permission notice shall be included
    16 //   in all copies or substantial portions of the Software.
    17 //
    18 //   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    19 //   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    20 //   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    21 //   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    22 //   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    23 //   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    24 //   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    25 // </copyright>
    26 // <summary>
    27 //   The tracker manipulator.
    28 // </summary>
    29 // --------------------------------------------------------------------------------------------------------------------
    30 namespace OxyPlot
    31 {
    32     using OxyPlot.Series;
    33 
    34     /// <summary>
    35     /// Provides a plot control manipulator for tracker functionality.
    36     /// </summary>
    37     public class TrackerManipulator : ManipulatorBase
    38     {
    39         /// <summary>
    40         /// The current series.
    41         /// </summary>
    42         private ITrackableSeries currentSeries;
    43 
    44         /// <summary>
    45         /// Initializes a new instance of the <see cref="TrackerManipulator"/> class.
    46         /// </summary>
    47         /// <param name="plotControl">
    48         /// The plot control.
    49         /// </param>
    50         public TrackerManipulator(IPlotControl plotControl)
    51             : base(plotControl)
    52         {
    53             this.Snap = true;
    54             this.PointsOnly = false;
    55         }
    56 
    57         /// <summary>
    58         /// Gets or sets a value indicating whether to show tracker on points only (not interpolating).
    59         /// </summary>
    60         public bool PointsOnly { get; set; }
    61 
    62         /// <summary>
    63         /// Gets or sets a value indicating whether to snap to the nearest point.
    64         /// </summary>
    65         public bool Snap { get; set; }
    66 
    67         /// <summary>
    68         /// Occurs when a manipulation is complete.
    69         /// </summary>
    70         /// <param name="e">
    71         /// The <see cref="OxyPlot.ManipulationEventArgs"/> instance containing the event data.
    72         /// </param>
    73         public override void Completed(ManipulationEventArgs e)
    74         {
    75             base.Completed(e);
    76 
    77             if (this.currentSeries == null)
    78             {
    79                 return;
    80             }
    81 
    82             this.currentSeries = null;
    83             this.PlotControl.HideTracker();
    84         }
    85 
    86         /// <summary>
    87         /// Occurs when the input device changes position during a manipulation.
    88         /// </summary>
    89         /// <param name="e">
    90         /// The <see cref="OxyPlot.ManipulationEventArgs"/> instance containing the event data.
    91         /// </param>
    92         public override void Delta(ManipulationEventArgs e)
    93         {
    94             base.Delta(e);
    95             if (this.currentSeries == null)
    96             {
    97                 return;
    98             }
    99 
   100             if (!this.PlotControl.ActualModel.PlotArea.Contains(e.CurrentPosition.X, e.CurrentPosition.Y))
   101             {
   102                 return;
   103             }
   104 
   105             TrackerHitResult result = GetNearestHit(this.currentSeries, e.CurrentPosition, this.Snap, this.PointsOnly);
   106             if (result != null)
   107             {
   108                 result.PlotModel = this.PlotControl.ActualModel;
   109                 this.PlotControl.ShowTracker(result);
   110             }
   111         }
   112 
   113         /// <summary>
   114         /// Gets the cursor for the manipulation.
   115         /// </summary>
   116         /// <returns>
   117         /// The cursor.
   118         /// </returns>
   119         public override CursorType GetCursorType()
   120         {
   121             return CursorType.Default;
   122         }
   123 
   124         /// <summary>
   125         /// Occurs when an input device begins a manipulation on the plot.
   126         /// </summary>
   127         /// <param name="e">
   128         /// The <see cref="OxyPlot.ManipulationEventArgs"/> instance containing the event data.
   129         /// </param>
   130         public override void Started(ManipulationEventArgs e)
   131         {
   132             base.Started(e);
   133             this.currentSeries = this.PlotControl.GetSeriesFromPoint(e.CurrentPosition);
   134             this.Delta(e);
   135         }
   136 
   137         /// <summary>
   138         /// Gets the nearest tracker hit.
   139         /// </summary>
   140         /// <param name="s">
   141         /// The series.
   142         /// </param>
   143         /// <param name="point">
   144         /// The point.
   145         /// </param>
   146         /// <param name="snap">
   147         /// Snap to points.
   148         /// </param>
   149         /// <param name="pointsOnly">
   150         /// Check points only (no interpolation).
   151         /// </param>
   152         /// <returns>
   153         /// A tracker hit result.
   154         /// </returns>
   155         private static TrackerHitResult GetNearestHit(ITrackableSeries s, ScreenPoint point, bool snap, bool pointsOnly)
   156         {
   157             if (s == null)
   158             {
   159                 return null;
   160             }
   161 
   162             // Check data points only
   163             if (snap || pointsOnly)
   164             {
   165                 TrackerHitResult result = s.GetNearestPoint(point, false);
   166                 if (result != null)
   167                 {
   168                     if (result.Position.DistanceTo(point) < 20)
   169                     {
   170                         return result;
   171                     }
   172                 }
   173             }
   174 
   175             // Check between data points (if possible)
   176             if (!pointsOnly)
   177             {
   178                 TrackerHitResult result = s.GetNearestPoint(point, true);
   179                 return result;
   180             }
   181 
   182             return null;
   183         }
   184 
   185     }
   186 }