[12503] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using Microsoft.Research.DynamicDataDisplay.Navigation;
|
---|
| 6 | using System.Windows.Input;
|
---|
| 7 | using System.Diagnostics;
|
---|
| 8 | using System.Windows;
|
---|
| 9 | using System.Windows.Media.Animation;
|
---|
| 10 | using System.Windows.Threading;
|
---|
| 11 |
|
---|
| 12 | namespace Microsoft.Research.DynamicDataDisplay.Charts.Navigation
|
---|
| 13 | {
|
---|
| 14 | public class PhysicalNavigation : IPlotterElement
|
---|
| 15 | {
|
---|
| 16 | private readonly DispatcherTimer timer;
|
---|
| 17 |
|
---|
| 18 | public PhysicalNavigation()
|
---|
| 19 | {
|
---|
| 20 | timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(30) };
|
---|
| 21 | timer.Tick += timer_Tick;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | DateTime startTime;
|
---|
| 25 | private void timer_Tick(object sender, EventArgs e)
|
---|
| 26 | {
|
---|
| 27 | TimeSpan time = DateTime.Now - startTime;
|
---|
| 28 | animation.UseMouse = isMouseDown;
|
---|
| 29 | if (!isMouseDown)
|
---|
| 30 | {
|
---|
| 31 | animation.LiquidFrictionQuadraticCoeff = 1;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | plotter.Viewport.Visible = animation.GetValue(time);
|
---|
| 35 | if (animation.IsFinished && !isMouseDown)
|
---|
| 36 | {
|
---|
| 37 | timer.Stop();
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | private bool isMouseDown = false;
|
---|
| 42 | private PhysicalRectAnimation animation;
|
---|
| 43 | private Point clickPos;
|
---|
| 44 | private void OnMouseDown(object sender, MouseButtonEventArgs e)
|
---|
| 45 | {
|
---|
| 46 | if (e.ChangedButton == MouseButton.Left)
|
---|
| 47 | {
|
---|
| 48 | clickPos = e.GetPosition(plotter.CentralGrid);
|
---|
| 49 |
|
---|
| 50 | isMouseDown = true;
|
---|
| 51 | startTime = DateTime.Now;
|
---|
| 52 |
|
---|
| 53 | animation = new PhysicalRectAnimation(
|
---|
| 54 | plotter.Viewport,
|
---|
| 55 | e.GetPosition(plotter.ViewportPanel));
|
---|
| 56 |
|
---|
| 57 | timer.Start();
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | private void OnMouseUp(object sender, MouseButtonEventArgs e)
|
---|
| 62 | {
|
---|
| 63 | if (e.ChangedButton == MouseButton.Left)
|
---|
| 64 | {
|
---|
| 65 | isMouseDown = false;
|
---|
| 66 | if (clickPos == e.GetPosition(plotter.CentralGrid))
|
---|
| 67 | {
|
---|
| 68 | timer.Stop();
|
---|
| 69 | animation = null;
|
---|
| 70 | }
|
---|
| 71 | else
|
---|
| 72 | {
|
---|
| 73 | if (animation.IsFinished)
|
---|
| 74 | {
|
---|
| 75 | timer.Stop();
|
---|
| 76 | animation = null;
|
---|
| 77 | }
|
---|
| 78 | else
|
---|
| 79 | {
|
---|
| 80 | animation.UseMouse = false;
|
---|
| 81 | animation.LiquidFrictionQuadraticCoeff = 1;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | #region IPlotterElement Members
|
---|
| 88 |
|
---|
| 89 | private Plotter2D plotter;
|
---|
| 90 | void IPlotterElement.OnPlotterAttached(Plotter plotter)
|
---|
| 91 | {
|
---|
| 92 | this.plotter = (Plotter2D)plotter;
|
---|
| 93 |
|
---|
| 94 | Mouse.AddPreviewMouseDownHandler(plotter.CentralGrid, OnMouseDown);
|
---|
| 95 | Mouse.AddPreviewMouseUpHandler(plotter.CentralGrid, OnMouseUp);
|
---|
| 96 | plotter.CentralGrid.MouseLeave += CentralGrid_MouseLeave;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | private void CentralGrid_MouseLeave(object sender, MouseEventArgs e)
|
---|
| 100 | {
|
---|
| 101 | isMouseDown = false;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | void IPlotterElement.OnPlotterDetaching(Plotter plotter)
|
---|
| 105 | {
|
---|
| 106 | plotter.CentralGrid.MouseLeave -= CentralGrid_MouseLeave;
|
---|
| 107 | Mouse.RemovePreviewMouseDownHandler(plotter.CentralGrid, OnMouseDown);
|
---|
| 108 | Mouse.RemovePreviewMouseUpHandler(plotter.CentralGrid, OnMouseUp);
|
---|
| 109 |
|
---|
| 110 | this.plotter = null;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | Plotter IPlotterElement.Plotter
|
---|
| 114 | {
|
---|
| 115 | get { return plotter; }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | #endregion
|
---|
| 119 | }
|
---|
| 120 | }
|
---|