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