1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Windows;
|
---|
6 | using System.Windows.Controls;
|
---|
7 | using System.Windows.Data;
|
---|
8 | using System.Windows.Documents;
|
---|
9 | using System.Windows.Input;
|
---|
10 | using System.Windows.Media;
|
---|
11 | using System.Windows.Media.Imaging;
|
---|
12 | using System.Windows.Navigation;
|
---|
13 | using System.Windows.Shapes;
|
---|
14 | using System.Windows.Controls.Primitives;
|
---|
15 | using Microsoft.Research.DynamicDataDisplay;
|
---|
16 |
|
---|
17 | namespace Microsoft.Research.DynamicDataDisplay.Charts.Shapes
|
---|
18 | {
|
---|
19 | /// <summary>
|
---|
20 | /// Represents a simple draggable point with position bound to point in viewport coordinates, which allows to drag iself by mouse.
|
---|
21 | /// </summary>
|
---|
22 | public partial class DraggablePoint : PositionalViewportUIContainer
|
---|
23 | {
|
---|
24 | /// <summary>
|
---|
25 | /// Initializes a new instance of the <see cref="DraggablePoint"/> class.
|
---|
26 | /// </summary>
|
---|
27 | public DraggablePoint()
|
---|
28 | {
|
---|
29 | InitializeComponent();
|
---|
30 |
|
---|
31 | ViewportPanel.SetX(this, Position.X);
|
---|
32 | ViewportPanel.SetY(this, Position.Y);
|
---|
33 | }
|
---|
34 |
|
---|
35 | /// <summary>
|
---|
36 | /// Initializes a new instance of the <see cref="DraggablePoint"/> class.
|
---|
37 | /// </summary>
|
---|
38 | /// <param name="position">The position of DraggablePoint.</param>
|
---|
39 | public DraggablePoint(Point position)
|
---|
40 | : this()
|
---|
41 | {
|
---|
42 | Position = position;
|
---|
43 | }
|
---|
44 |
|
---|
45 | bool dragging = false;
|
---|
46 | Point dragStart;
|
---|
47 | Vector shift;
|
---|
48 | protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
|
---|
49 | {
|
---|
50 | if (Plotter == null)
|
---|
51 | return;
|
---|
52 |
|
---|
53 | dragStart = e.GetPosition(Plotter.ViewportPanel).ScreenToData(Plotter.Viewport.Transform);
|
---|
54 | shift = Position - dragStart;
|
---|
55 | dragging = true;
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected override void OnMouseLeave(MouseEventArgs e)
|
---|
59 | {
|
---|
60 | ReleaseMouseCapture();
|
---|
61 | dragging = false;
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected override void OnMouseMove(MouseEventArgs e)
|
---|
65 | {
|
---|
66 | if (!dragging)
|
---|
67 | {
|
---|
68 | if (IsMouseCaptured)
|
---|
69 | ReleaseMouseCapture();
|
---|
70 |
|
---|
71 | return;
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (!IsMouseCaptured)
|
---|
75 | CaptureMouse();
|
---|
76 |
|
---|
77 | Point mouseInData = e.GetPosition(Plotter.ViewportPanel).ScreenToData(Plotter.Viewport.Transform);
|
---|
78 |
|
---|
79 | if (mouseInData != dragStart)
|
---|
80 | {
|
---|
81 | Position = mouseInData + shift;
|
---|
82 | e.Handled = true;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
|
---|
87 | {
|
---|
88 | if (dragging)
|
---|
89 | {
|
---|
90 | dragging = false;
|
---|
91 | if (IsMouseCaptured)
|
---|
92 | {
|
---|
93 | ReleaseMouseCapture();
|
---|
94 | e.Handled = true;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|