|
1 using System; |
|
2 using System.Collections.Generic; |
|
3 using System.Linq; |
|
4 using System.Diagnostics; |
|
5 using System.Text; |
|
6 using System.Windows; |
|
7 using System.Windows.Controls; |
|
8 using System.Windows.Data; |
|
9 using System.Windows.Documents; |
|
10 using System.Windows.Input; |
|
11 using System.Windows.Media; |
|
12 using System.Windows.Media.Imaging; |
|
13 using System.Windows.Media.Animation; |
|
14 using System.Windows.Navigation; |
|
15 using System.Windows.Shapes; |
|
16 using Microsoft.Surface; |
|
17 using Microsoft.Surface.Presentation; |
|
18 using Microsoft.Surface.Presentation.Controls; |
|
19 using System.Windows.Ink; |
|
20 |
|
21 namespace GestureControl |
|
22 { |
|
23 public class GestureControl : ContentControl |
|
24 { |
|
25 private double angle = 0; |
|
26 public static readonly DependencyProperty GestureProperty; |
|
27 public String Gesture |
|
28 { |
|
29 get { return (String)GetValue(GestureControl.GestureProperty); } |
|
30 set { SetValue(GestureControl.GestureProperty, value); } |
|
31 } |
|
32 |
|
33 #region GestureEvent |
|
34 public delegate void GestureRoutedEventHandler(object sender, GestureRoutedEventArgs e); |
|
35 public static readonly RoutedEvent gestureEvent = EventManager.RegisterRoutedEvent( |
|
36 "GestureEvent", RoutingStrategy.Bubble, typeof(GestureRoutedEventHandler), typeof(GestureControl)); |
|
37 |
|
38 public event GestureRoutedEventHandler GestureEvent |
|
39 { |
|
40 add { AddHandler(gestureEvent, value); } |
|
41 remove { RemoveHandler(gestureEvent, value); } |
|
42 } |
|
43 protected virtual void RaiseGestureEvent(String gesture) |
|
44 { |
|
45 try |
|
46 { |
|
47 GestureRoutedEventArgs newEventArgs = new GestureRoutedEventArgs(GestureControl.gestureEvent, gesture); |
|
48 RaiseEvent(newEventArgs); |
|
49 } |
|
50 catch (Exception e) { } |
|
51 } |
|
52 #endregion |
|
53 |
|
54 static GestureControl() |
|
55 { |
|
56 DefaultStyleKeyProperty.OverrideMetadata(typeof(GestureControl), new FrameworkPropertyMetadata(typeof(GestureControl))); |
|
57 |
|
58 GestureControl.GestureProperty = DependencyProperty.Register("Gesture", typeof(String), typeof(GestureControl), |
|
59 new FrameworkPropertyMetadata("None", new PropertyChangedCallback(OnGestureChanged))); |
|
60 } |
|
61 private static void OnGestureChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) |
|
62 { |
|
63 //Debug.WriteLine("changed", "Gesture"); |
|
64 } |
|
65 |
|
66 protected override void OnInitialized(EventArgs e) |
|
67 { |
|
68 base.OnInitialized(e); |
|
69 this.InitializeControl(); |
|
70 } |
|
71 protected void InitializeControl() |
|
72 { |
|
73 base.AddHandler(SurfaceControl.PreviewContactDownEvent, new RoutedEventHandler(AreaDown)); |
|
74 base.AddHandler(SurfaceInkCanvas.StrokeCollectedEvent, new RoutedEventHandler(AreaStrokeCollected)); |
|
75 } |
|
76 // get angle from the first contact of the stroke |
|
77 protected void AreaDown(object source, RoutedEventArgs e) |
|
78 { |
|
79 ContactEventArgs c = (ContactEventArgs)e; |
|
80 this.angle = c.Contact.GetOrientation(this) - 270; |
|
81 } |
|
82 |
|
83 protected void AreaStrokeCollected(object source, RoutedEventArgs e) |
|
84 { |
|
85 Debug.WriteLine("collected", "Stroke"); |
|
86 System.Windows.Controls.InkCanvasStrokeCollectedEventArgs tmp = e as System.Windows.Controls.InkCanvasStrokeCollectedEventArgs; |
|
87 // Apply a rotation with the angle of the first contact |
|
88 Matrix rot = Matrix.Identity; |
|
89 rot.Rotate(-this.angle); |
|
90 Stroke s = tmp.Stroke; |
|
91 s.Transform(rot, true); |
|
92 // Get a list of point from a Bezier curve |
|
93 StylusPointCollection tmp2 = s.GetBezierStylusPoints(); |
|
94 // Generate a list of SurfaceGesturePoint, just X and Y but can be improve |
|
95 List<SurfaceGesturePoint> pointList = new List<SurfaceGesturePoint>(); |
|
96 foreach (StylusPoint p in tmp2) |
|
97 pointList.Add(new SurfaceGesturePoint { X = p.X, Y = p.Y }); |
|
98 // create the gesture analyser and set the list |
|
99 SurfaceGesture gesture = new SurfaceGesture(pointList, 1); |
|
100 // try to get a pattern from the list, if one, raise a event |
|
101 this.Gesture = gesture.GetPattern(); |
|
102 if (this.Gesture != "None") |
|
103 { |
|
104 this.RaiseGestureEvent(this.Gesture); |
|
105 } |
|
106 // clear the stroke |
|
107 SurfaceInkCanvas ink = e.OriginalSource as SurfaceInkCanvas; |
|
108 ink.Strokes.Clear(); |
|
109 } |
|
110 } |
|
111 } |