174
|
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; |
176
|
20 |
using System.Threading; |
|
21 |
using System.IO; |
|
22 |
using System.Xml; |
|
23 |
using System.Reflection; |
|
24 |
using System.Xml.Linq; |
174
|
25 |
|
|
26 |
namespace GestureControl |
|
27 |
{ |
|
28 |
public class GestureControl : ContentControl |
|
29 |
{ |
|
30 |
private double angle = 0; |
|
31 |
public static readonly DependencyProperty GestureProperty; |
176
|
32 |
Thread inProgess; |
|
33 |
List<SurfaceGesture> _Gestures = new List<SurfaceGesture>(); |
|
34 |
|
|
35 |
/// <summary> |
|
36 |
/// List of pattern readed in the patterns.xml |
|
37 |
/// </summary> |
|
38 |
public List<SurfaceGesturePattern> Pattern = new List<SurfaceGesturePattern>(); |
|
39 |
|
174
|
40 |
public String Gesture |
|
41 |
{ |
|
42 |
get { return (String)GetValue(GestureControl.GestureProperty); } |
|
43 |
set { SetValue(GestureControl.GestureProperty, value); } |
|
44 |
} |
|
45 |
|
|
46 |
#region GestureEvent |
176
|
47 |
|
174
|
48 |
public delegate void GestureRoutedEventHandler(object sender, GestureRoutedEventArgs e); |
176
|
49 |
|
|
50 |
public static readonly RoutedEvent gestureEvent = EventManager.RegisterRoutedEvent("GestureEvent", RoutingStrategy.Bubble, typeof(GestureRoutedEventHandler), typeof(GestureControl)); |
174
|
51 |
|
|
52 |
public event GestureRoutedEventHandler GestureEvent |
|
53 |
{ |
|
54 |
add { AddHandler(gestureEvent, value); } |
|
55 |
remove { RemoveHandler(gestureEvent, value); } |
|
56 |
} |
176
|
57 |
|
|
58 |
public delegate void RaiseGestureEventCallBack(string gesture); |
|
59 |
|
|
60 |
public void RaiseGestureEvent(string gesture) |
174
|
61 |
{ |
176
|
62 |
if (this.Dispatcher.CheckAccess()) |
174
|
63 |
{ |
176
|
64 |
try |
|
65 |
{ |
|
66 |
GestureRoutedEventArgs newEventArgs = new GestureRoutedEventArgs(GestureControl.gestureEvent, gesture); |
|
67 |
RaiseEvent(newEventArgs); |
|
68 |
} |
|
69 |
catch (Exception e) { } |
174
|
70 |
} |
176
|
71 |
else |
|
72 |
{ |
|
73 |
this.Dispatcher.BeginInvoke(new RaiseGestureEventCallBack(RaiseGestureEvent), gesture); |
|
74 |
} |
174
|
75 |
} |
176
|
76 |
|
174
|
77 |
#endregion |
|
78 |
|
|
79 |
static GestureControl() |
|
80 |
{ |
|
81 |
DefaultStyleKeyProperty.OverrideMetadata(typeof(GestureControl), new FrameworkPropertyMetadata(typeof(GestureControl))); |
|
82 |
|
|
83 |
GestureControl.GestureProperty = DependencyProperty.Register("Gesture", typeof(String), typeof(GestureControl), |
|
84 |
new FrameworkPropertyMetadata("None", new PropertyChangedCallback(OnGestureChanged))); |
|
85 |
} |
176
|
86 |
|
174
|
87 |
private static void OnGestureChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) |
|
88 |
{ |
|
89 |
//Debug.WriteLine("changed", "Gesture"); |
|
90 |
} |
|
91 |
|
|
92 |
protected override void OnInitialized(EventArgs e) |
|
93 |
{ |
|
94 |
base.OnInitialized(e); |
|
95 |
this.InitializeControl(); |
|
96 |
} |
176
|
97 |
|
174
|
98 |
protected void InitializeControl() |
|
99 |
{ |
176
|
100 |
base.AddHandler(SurfaceControl.PreviewContactDownEvent, new RoutedEventHandler(AreaDown)); |
|
101 |
base.AddHandler(SurfaceInkCanvas.StrokeCollectedEvent, new RoutedEventHandler(AreaStrokeCollected)); |
174
|
102 |
} |
176
|
103 |
|
174
|
104 |
// get angle from the first contact of the stroke |
|
105 |
protected void AreaDown(object source, RoutedEventArgs e) |
|
106 |
{ |
|
107 |
ContactEventArgs c = (ContactEventArgs)e; |
|
108 |
this.angle = c.Contact.GetOrientation(this) - 270; |
|
109 |
} |
|
110 |
|
|
111 |
protected void AreaStrokeCollected(object source, RoutedEventArgs e) |
|
112 |
{ |
176
|
113 |
if (inProgess == null) |
|
114 |
{ |
|
115 |
inProgess = new Thread(RunCapture); |
|
116 |
inProgess.Start(); |
|
117 |
} |
|
118 |
else |
|
119 |
{ |
|
120 |
inProgess.Abort(); |
|
121 |
inProgess = null; |
|
122 |
inProgess = new Thread(RunCapture); |
|
123 |
inProgess.Start(); |
|
124 |
} |
174
|
125 |
Debug.WriteLine("collected", "Stroke"); |
|
126 |
System.Windows.Controls.InkCanvasStrokeCollectedEventArgs tmp = e as System.Windows.Controls.InkCanvasStrokeCollectedEventArgs; |
|
127 |
// Apply a rotation with the angle of the first contact |
|
128 |
Matrix rot = Matrix.Identity; |
|
129 |
rot.Rotate(-this.angle); |
|
130 |
Stroke s = tmp.Stroke; |
|
131 |
s.Transform(rot, true); |
|
132 |
// Get a list of point from a Bezier curve |
|
133 |
StylusPointCollection tmp2 = s.GetBezierStylusPoints(); |
|
134 |
// Generate a list of SurfaceGesturePoint, just X and Y but can be improve |
|
135 |
List<SurfaceGesturePoint> pointList = new List<SurfaceGesturePoint>(); |
|
136 |
foreach (StylusPoint p in tmp2) |
|
137 |
pointList.Add(new SurfaceGesturePoint { X = p.X, Y = p.Y }); |
|
138 |
// create the gesture analyser and set the list |
|
139 |
SurfaceGesture gesture = new SurfaceGesture(pointList, 1); |
|
140 |
// try to get a pattern from the list, if one, raise a event |
176
|
141 |
_Gestures.Add(gesture); |
174
|
142 |
SurfaceInkCanvas ink = e.OriginalSource as SurfaceInkCanvas; |
|
143 |
ink.Strokes.Clear(); |
|
144 |
} |
176
|
145 |
|
|
146 |
void RunCapture() |
|
147 |
{ |
|
148 |
try |
|
149 |
{ |
|
150 |
Thread.Sleep(1000); |
|
151 |
CommonPattern(); |
|
152 |
string gesture = GetPattern(_Gestures); |
|
153 |
if (gesture != "None") |
|
154 |
{ |
|
155 |
this.RaiseGestureEvent(gesture); |
|
156 |
} |
|
157 |
_Gestures.Clear(); |
|
158 |
inProgess = null; |
|
159 |
} |
|
160 |
catch (Exception) |
|
161 |
{ |
|
162 |
inProgess = null; |
|
163 |
} |
|
164 |
} |
|
165 |
|
|
166 |
#region Pattern |
|
167 |
/// <ary> |
|
168 |
/// return a String with the recognized pattern, "None" if no pattern |
|
169 |
/// </summary> |
|
170 |
/// <returns></returns> |
|
171 |
public String GetPattern(List<SurfaceGesture> gestures) |
|
172 |
{ |
|
173 |
int found = 0; |
|
174 |
List<SurfaceGesture> tmp = new List<SurfaceGesture>(); |
|
175 |
foreach (SurfaceGesturePattern p in Pattern) |
|
176 |
{ |
|
177 |
if (p.Count == gestures.Count) |
|
178 |
{ |
|
179 |
int index = 0; |
|
180 |
for (index = 0; index < p.Count; index++) |
|
181 |
{ |
|
182 |
if (p[index].Count == gestures[index].Count) |
|
183 |
{ |
|
184 |
int i; |
|
185 |
for (i = 0; i < p[index].Count; i++) |
|
186 |
{ |
|
187 |
if (gestures[index][i].Direction != p[index][i].Direction) |
|
188 |
i = p[index].Count + 1; |
|
189 |
} |
|
190 |
if (i == p[index].Count) |
|
191 |
found++; |
|
192 |
|
|
193 |
} |
|
194 |
} |
|
195 |
if (found == p.Count) |
|
196 |
return p.Name; |
|
197 |
} |
|
198 |
} |
|
199 |
|
|
200 |
return "None"; |
|
201 |
} |
|
202 |
|
|
203 |
/// <summary> |
|
204 |
/// Load know patterns from the Resources/Patterns.xml file |
|
205 |
/// </summary> |
|
206 |
private void CommonPattern() |
|
207 |
{ |
|
208 |
try |
|
209 |
{ |
|
210 |
#region Load Patterns |
|
211 |
Pattern.Clear(); |
|
212 |
System.IO.Stream file = Assembly.GetExecutingAssembly().GetManifestResourceStream("GestureControl.Resources.Patterns.xml"); |
|
213 |
XmlDocument xml = new XmlDocument(); |
|
214 |
xml.Load(file); |
|
215 |
XmlElement root = xml.DocumentElement; |
|
216 |
XmlNodeList nodes = root.SelectNodes("//Pattern"); |
|
217 |
SurfaceGesturePattern item; |
|
218 |
foreach (XmlNode nod in nodes) |
|
219 |
{ |
|
220 |
string name = nod["Name"].InnerText; |
|
221 |
if (nod.ChildNodes.Count < 2) |
|
222 |
continue; |
|
223 |
item = new SurfaceGesturePattern() { Name = name }; |
|
224 |
List<XmlNode> childs = new List<XmlNode>(); |
|
225 |
foreach (XmlNode elt in nod.ChildNodes) |
|
226 |
if (elt.Name.Equals("Childs")) |
|
227 |
foreach (XmlNode c in elt.ChildNodes) |
|
228 |
childs.Add(c); |
|
229 |
|
|
230 |
SurfaceGesturePatternItem p; |
|
231 |
int j = 0; |
|
232 |
foreach (XmlNode node in childs) |
|
233 |
{ |
|
234 |
string childname = node["Name"].InnerText; |
|
235 |
List<XmlNode> subNodes = new List<XmlNode>(); |
|
236 |
foreach (XmlNode elt in node.ChildNodes) |
|
237 |
if (elt.Name.Equals("Directions")) |
|
238 |
foreach (XmlNode c in elt.ChildNodes) |
|
239 |
subNodes.Add(c); |
|
240 |
if (subNodes.Count == 0) |
|
241 |
continue; |
|
242 |
|
|
243 |
p = new SurfaceGesturePatternItem() { Name = childname }; |
|
244 |
|
|
245 |
foreach (XmlNode subNode in subNodes) |
|
246 |
{ |
|
247 |
switch (subNode.InnerText) |
|
248 |
{ |
|
249 |
case "Up": |
|
250 |
p.Add(new SurfaceGestureVector() { Direction = SurfaceGestureVectorDirection.UP }); |
|
251 |
break; |
|
252 |
case "Down": |
|
253 |
p.Add(new SurfaceGestureVector() { Direction = SurfaceGestureVectorDirection.DOWN }); |
|
254 |
break; |
|
255 |
case "Left": |
|
256 |
p.Add(new SurfaceGestureVector() { Direction = SurfaceGestureVectorDirection.LEFT }); |
|
257 |
break; |
|
258 |
case "Right": |
|
259 |
p.Add(new SurfaceGestureVector() { Direction = SurfaceGestureVectorDirection.RIGHT }); |
|
260 |
break; |
|
261 |
case "DownRight": |
|
262 |
p.Add(new SurfaceGestureVector() { Direction = SurfaceGestureVectorDirection.DOWNRIGHT }); |
|
263 |
break; |
|
264 |
case "DownLeft": |
|
265 |
p.Add(new SurfaceGestureVector() { Direction = SurfaceGestureVectorDirection.DOWNLEFT }); |
|
266 |
break; |
|
267 |
case "UpRight": |
|
268 |
p.Add(new SurfaceGestureVector() { Direction = SurfaceGestureVectorDirection.UPRIGHT }); |
|
269 |
break; |
|
270 |
case "UpLeft": |
|
271 |
p.Add(new SurfaceGestureVector() { Direction = SurfaceGestureVectorDirection.UPLEFT }); |
|
272 |
break; |
|
273 |
default: |
|
274 |
break; |
|
275 |
} |
|
276 |
} |
|
277 |
item.Add(p); |
|
278 |
} |
|
279 |
this.Pattern.Add(item); |
|
280 |
} |
|
281 |
#endregion |
|
282 |
} |
|
283 |
catch |
|
284 |
{ |
|
285 |
throw new Exception("Error loading Patterns.xml"); |
|
286 |
} |
|
287 |
} |
|
288 |
|
|
289 |
#endregion |
174
|
290 |
} |
|
291 |
} |