-
Notifications
You must be signed in to change notification settings - Fork 0
/
Environs.Cli.TouchDevice.cpp
343 lines (299 loc) · 10.3 KB
/
Environs.Cli.TouchDevice.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
* Environs CLI touchdevice extension
* ------------------------------------------------------------------
* Copyright (c) Chi-Tai Dang
*
* @author Chi-Tai Dang
* @version 1.0
* @remarks
*
* This file is part of the Environs framework developed at the
* Lab for Human Centered Multimedia of the University of Augsburg.
* http://hcm-lab.de/environs
*
* Environ is free software; you can redistribute it and/or modify
* it under the terms of the Eclipse Public License v1.0.
* A copy of the license may be obtained at:
* http://www.eclipse.org/org/documents/epl-v10.html
* --------------------------------------------------------------------
*/
#include "stdafx.h"
#if (defined(CLI_CPP) && (defined(CLI_STT) || defined(CLI_PS)))
#include "Environs.Cli.STT.h"
#include "Environs.Cli.Forwards.h"
#include "Environs.Cli.TouchDevice.h"
namespace environs
{
/// <summary>
/// Constructor for a new input pack.
/// </summary>
/// <param name="pack">The environs input pack.</param>
EnvironsTouchDevice::EnvironsTouchDevice ( int nativeID, InputPack ^ pack ) : TouchDevice ( pack->id )
{
this->nativeID = nativeID;
this->pack = pack;
}
void EnvironsTouchDevice::Init ( ENV_CLIENTWINDOW ^ clientWindow )
{
if ( clientWindow != nullptr )
{
touchSource = PresentationSource::FromVisual ( clientWindow );
if ( touchSource != nullptr )
return;
}
touchSource = Mouse::PrimaryDevice->ActiveSource;
}
/// <summary>
/// Call this method to signal a new touch down event.
/// </summary>
/// <param name="nativeID">The device id.</param>
/// <param name="pack">The input package.</param>
void EnvironsTouchDevice::ContactDown ( int nativeID, InputPack ^ pack )
{
try
{
if ( deviceDictionary->ContainsKey ( pack->id ) )
{
return;
}
EnvironsTouchDevice ^ device = gcnew EnvironsTouchDevice ( nativeID, pack );
deviceDictionary->Add ( pack->id, device );
//PresentationSource.CurrentSources.FromVisual(SurfaceEnvirons.instance.winForm.);
//device.SetActiveSource(System.Windows.Input.Stylus.CurrentStylusDevice.ActiveSource);
//device.SetActiveSource(PresentationSource.FromVisual(SurfaceEnvirons.instance.winForm.Handle));
//device.SetActiveSource(PresentationSource.FromVisual(SurfaceEnvirons.instance.surfaceWindow));
//device.SetActiveSource(Mouse.PrimaryDevice.ActiveSource);
device->SetActiveSource ( touchSource );
device->Activate ();
device->ReportDown ();
}
//Ignore InvalidOperationException due to race condition on Surface hardware
catch ( InvalidOperationException ^e )
{
Debug::WriteLine ( e->Message );
Debug::WriteLine ( e->StackTrace );
}
}
/// <summary>
/// Call this method to signal that a contact has moved to a new location.
/// </summary>
/// <param name="nativeID">The device id.</param>
/// <param name="pack">The input package.</param>
void EnvironsTouchDevice::ContactChanged ( int nativeID, InputPack ^ pack )
{
try
{
if ( !deviceDictionary->ContainsKey ( pack->id ) )
{
ContactDown ( nativeID, pack );
}
EnvironsTouchDevice ^ device = deviceDictionary [ pack->id ];
if ( device != nullptr &&
device->IsActive )
{
if ( device->pack->x != pack->x || device->pack->y != pack->y || device->pack->angle != pack->angle || device->pack->size != pack->size )
{
device->pack = pack;
device->ReportMove ();
}
}
}
//Ignore InvalidOperationException due to race condition on Surface hardware
catch ( InvalidOperationException ^)
{
}
}
/// <summary>
/// Call this method to signal that a contact has been left from the surface at the given position.
/// </summary>
/// <param name="nativeID">The device id.</param>
/// <param name="pack">The input package.</param>
void EnvironsTouchDevice::ContactUp ( int nativeID, InputPack ^ pack )
{
try
{
if ( !deviceDictionary->ContainsKey ( pack->id ) )
{
ContactDown ( nativeID, pack );
}
EnvironsTouchDevice ^ device = deviceDictionary [ pack->id ];
if ( device != nullptr &&
device->IsActive )
{
device->ReportUp ();
device->Deactivate ();
deviceDictionary->Remove ( pack->id );
}
}
//Ignore InvalidOperationException due to race condition on Surface hardware
catch ( InvalidOperationException ^ )
{
}
}
bool EnvironsTouchDevice::HasContactChanged ( int nativeID, InputPack ^ pack )
{
try
{
if ( deviceDictionary->ContainsKey ( pack->id ) )
{
EnvironsTouchDevice ^device = deviceDictionary [ pack->id ];
int a1 = ( int ) device->pack->angle;
int a2 = ( int ) pack->angle;
if ( pack->type == INPUT_TYPE_MARKER )
{
if ( Math::Abs ( device->pack->x - pack->x ) > 30 || Math::Abs ( device->pack->y - pack->y ) > 30 || a1 != a2 )
{
return true;
}
return false;
}
if ( device->pack->x != pack->x || device->pack->y != pack->y || device->pack->size != pack->size || a1 != a2 )
{
return true;
}
}
}
//Ignore InvalidOperationException due to race condition on Surface hardware
catch ( InvalidOperationException ^)
{
}
return false;
}
/// <summary>
/// RegisterEvents is not used yet.
/// </summary>
/// <param name="root"></param>
void EnvironsTouchDevice::RegisterEvents ( FrameworkElement ^ root )
{
}
int EnvironsTouchDevice::GetNativeID ( int touchID )
{
if ( deviceDictionary->ContainsKey ( touchID ) )
{
EnvironsTouchDevice ^ device = deviceDictionary [ touchID ];
if ( device != nullptr )
return device->nativeID;
}
return 0;
}
/// <summary>
/// GetIntermediateTouchPoints determines the current state of all active contact events.
/// </summary>
/// <param name="relativeTo">An InputElement to which the contacts' locations should be computed relative to. If set to <c>null</c>, then the application window is used as reference.</param>
/// <returns>A TouchPointCollection containing all the active contact events.</returns>
TouchPointCollection ^ EnvironsTouchDevice::GetIntermediateTouchPoints ( IInputElement ^ relativeTo )
{
TouchPointCollection ^ collection = gcnew TouchPointCollection ();
UIElement ^ element = ( UIElement^ ) relativeTo;
if ( element == nullptr )
return collection;
try
{
/*foreach (IntermediateContact c in Contact.GetIntermediateContacts())
{
Point point = c.GetPosition(null);
if (relativeTo != null)
{
point = this.ActiveSource.RootVisual.TransformToDescendant((Visual)relativeTo).Transform(point);
}
collection.Add(new TouchPoint(this, point, c.BoundingRect, TouchAction.Move));
}
*/
Point point ( pack->x, pack->y );
if ( relativeTo != nullptr )
{
point = ActiveSource->RootVisual->TransformToDescendant ( ( System::Windows::Media::Visual ^ ) relativeTo )->Transform ( point );
}
collection->Add ( gcnew TouchPoint ( this, point, Rect ( pack->x - 10, pack->y - 10, 20, 20 ), TouchAction::Move ) );
}
//Ignore InvalidOperationException due to race condition on Surface hardware
catch ( InvalidOperationException ^ )
{
}
return collection;
}
/// <summary>
/// Determines the position and boundings of this contact event instance.
/// </summary>
/// <param name="relativeTo">An InputElement to which the contacts' locations should be computed relative to. If set to <c>null</c>, then the application window is used as reference.</param>
/// <returns>A TouchPoint with position and boundings.</returns>
TouchPoint ^ EnvironsTouchDevice::GetTouchPoint ( IInputElement ^ relativeTo )
{
try
{
Point point ( pack->x, pack->y );
if ( relativeTo != nullptr )
{
point = ActiveSource->RootVisual->TransformToDescendant ( ( System::Windows::Media::Visual ^ ) relativeTo )->Transform ( point );
}
Rect rect ( pack->x - 10, pack->y - 10, 20, 20 ); // this.Contact.BoundingRect;
return gcnew TouchPoint ( this, point, rect, TouchAction::Move );
}
//Ignore InvalidOperationException due to race condition on Surface hardware
catch ( InvalidOperationException ^)
{
}
return nullptr;
}
/// <summary>
/// Determine whether this contact is a finger or not.
/// </summary>
/// <returns>Returns true for a finger contact and false otherwise.</returns>
bool EnvironsTouchDevice::GetIsFingerRecognized ()
{
return ( pack->type == INPUT_TYPE_FINGER );
}
/// <summary>
/// Determine whether this contact is a marker or not.
/// </summary>
/// <returns>Returns true for a marker contact and false otherwise.</returns>
bool EnvironsTouchDevice::GetIsTagRecognized ()
{
return ( pack->type == INPUT_TYPE_MARKER );
}
/// <summary>
/// Determine whether this contact is a marker or not.
/// </summary>
/// <returns>Returns true for a marker contact and false otherwise.</returns>
bool EnvironsTouchDevice::GetIsPenRecognized ()
{
return ( pack->type == INPUT_TYPE_PEN );
}
/// <summary>
/// Determine whether this contact is a marker or not.
/// </summary>
/// <returns>Returns true for a marker contact and false otherwise.</returns>
Microsoft::Surface::Presentation::Input::TagData ^ EnvironsTouchDevice::GetTagData ()
{
Microsoft::Surface::Presentation::Input::TagData ^ data = gcnew Microsoft::Surface::Presentation::Input::TagData ( 0, 0, ( long ) pack->value, ( long ) pack->value );
return data;
}
/// <summary>
/// Determine whether this contact is a marker or not.
/// </summary>
/// <returns>Returns true for a marker contact and false otherwise.</returns>
double EnvironsTouchDevice::GetOrientation ()
{
return ( double ) pack->angle;
}
/// <summary>
/// Determine whether this contact is a marker or not.
/// </summary>
/// <returns>Returns true for a marker contact and false otherwise.</returns>
System::Windows::Point ^ EnvironsTouchDevice::GetPosition ()
{
System::Windows::Point ^ p = gcnew System::Windows::Point ();
p->X = pack->x;
p->Y = pack->y;
return p;
}
/// <summary>
/// Determine whether this contact is a marker or not.
/// </summary>
/// <returns>Returns true for a marker contact and false otherwise.</returns>
double EnvironsTouchDevice::GetPhysicalArea ()
{
return pack->axisx * pack->axisy;
}
}
#endif