Blame view

sources/RoboforkApp/Controls/ZoomBox.cs 4.62 KB
729be9a6d   doan   New Project
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  using System;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Controls.Primitives;
  using System.Windows.Media;
  
  namespace RoboforkApp
  {
      public class ZoomBox : Control
      {
          private Thumb zoomThumb;
          private Canvas zoomCanvas;
          private Slider zoomSlider;
          private ScaleTransform scaleTransform;
          private DesignerCanvas designerCanvas;
  
          public ScrollViewer ScrollViewer
          {
              get { return (ScrollViewer)GetValue(ScrollViewerProperty); }
              set { SetValue(ScrollViewerProperty, value); }
          }
  
          public static readonly DependencyProperty ScrollViewerProperty =
              DependencyProperty.Register("ScrollViewer", typeof(ScrollViewer), typeof(ZoomBox));
  
          public override void OnApplyTemplate()
          {
              base.OnApplyTemplate();
  
              if (this.ScrollViewer == null)
                  return;
  
              this.designerCanvas = this.ScrollViewer.Content as DesignerCanvas;
              if (this.designerCanvas == null)
                  throw new Exception("DesignerCanvas must not be null!");
              
              this.zoomThumb = Template.FindName("PART_ZoomThumb", this) as Thumb;
              if (this.zoomThumb == null)
                  throw new Exception("PART_ZoomThumb template is missing!");
  
              this.zoomCanvas = Template.FindName("PART_ZoomCanvas", this) as Canvas;
              if (this.zoomCanvas == null)
                  throw new Exception("PART_ZoomCanvas template is missing!");
  
              this.zoomSlider = Template.FindName("PART_ZoomSlider", this) as Slider;
              if (this.zoomSlider == null)
                  throw new Exception("PART_ZoomSlider template is missing!");
  
              this.designerCanvas.LayoutUpdated += new EventHandler(this.DesignerCanvas_LayoutUpdated);
  
              this.zoomThumb.DragDelta += new DragDeltaEventHandler(this.Thumb_DragDelta);
  
              this.zoomSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(this.ZoomSlider_ValueChanged);
  
              this.scaleTransform = new ScaleTransform();
              this.designerCanvas.LayoutTransform = this.scaleTransform;
          }
  
          private void ZoomSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
          {
              double scale = e.NewValue / e.OldValue;
  
              double halfViewportHeight = this.ScrollViewer.ViewportHeight / 2;
              double newVerticalOffset = ((this.ScrollViewer.VerticalOffset + halfViewportHeight) * scale - halfViewportHeight);
  
              double halfViewportWidth = this.ScrollViewer.ViewportWidth / 2;
              double newHorizontalOffset = ((this.ScrollViewer.HorizontalOffset + halfViewportWidth) * scale - halfViewportWidth);
  
              this.scaleTransform.ScaleX *= scale;
              this.scaleTransform.ScaleY *= scale;
  
              this.ScrollViewer.ScrollToHorizontalOffset(newHorizontalOffset);
              this.ScrollViewer.ScrollToVerticalOffset(newVerticalOffset);
          }
  
          private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
          {
              double scale, xOffset, yOffset;
              this.InvalidateScale(out scale, out xOffset, out yOffset);
  
              this.ScrollViewer.ScrollToHorizontalOffset(this.ScrollViewer.HorizontalOffset + e.HorizontalChange / scale);
              this.ScrollViewer.ScrollToVerticalOffset(this.ScrollViewer.VerticalOffset + e.VerticalChange / scale);
          }
  
          private void DesignerCanvas_LayoutUpdated(object sender, EventArgs e)
          {
              double scale, xOffset, yOffset;
              this.InvalidateScale(out scale, out xOffset, out yOffset);
  
              this.zoomThumb.Width = this.ScrollViewer.ViewportWidth * scale;
              this.zoomThumb.Height = this.ScrollViewer.ViewportHeight * scale;
  
              Canvas.SetLeft(this.zoomThumb, xOffset + this.ScrollViewer.HorizontalOffset * scale);
              Canvas.SetTop(this.zoomThumb, yOffset + this.ScrollViewer.VerticalOffset * scale);
          }
  
          private void InvalidateScale(out double scale, out double xOffset, out double yOffset)
          {
              // designer canvas size
              double w = this.designerCanvas.ActualWidth * this.scaleTransform.ScaleX;
              double h = this.designerCanvas.ActualHeight * this.scaleTransform.ScaleY;
  
              // zoom canvas size
              double x = this.zoomCanvas.ActualWidth;
              double y = this.zoomCanvas.ActualHeight;
  
              double scaleX = x / w;
              double scaleY = y / h;
  
              scale = (scaleX < scaleY) ? scaleX : scaleY;
  
              xOffset = (x - scale * w) / 2;
              yOffset = (y - scale * h) / 2;
          }
      }
  }