Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Charts/Shapes/DraggablePoint.xaml.cs @ 12747

Last change on this file since 12747 was 12503, checked in by aballeit, 10 years ago

#2283 added GUI and charts; fixed MCTS

File size: 2.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows;
6using System.Windows.Controls;
7using System.Windows.Data;
8using System.Windows.Documents;
9using System.Windows.Input;
10using System.Windows.Media;
11using System.Windows.Media.Imaging;
12using System.Windows.Navigation;
13using System.Windows.Shapes;
14using System.Windows.Controls.Primitives;
15using Microsoft.Research.DynamicDataDisplay;
16
17namespace 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}
Note: See TracBrowser for help on using the repository browser.