-
Notifications
You must be signed in to change notification settings - Fork 1
/
TracboxViewer.xaml.cs
133 lines (117 loc) · 4.47 KB
/
TracboxViewer.xaml.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using Tobii.Gaze.Core;
namespace GazeMonitor
{
public partial class TracboxViewer : Window
{
private Ellipse lastDrawedRight;
private Ellipse lastDrawedLeft;
private double rectangleLeft;
private double rectangleTop;
private double rectangleSize;
public TracboxViewer()
{
InitializeComponent();
}
private void DrawTrackbox()
{
rectangleSize = canvas.ActualHeight / 2;
Rectangle trackbox = new Rectangle() { Height = rectangleSize , Width = rectangleSize };
trackbox.Stroke = Brushes.White;
rectangleLeft = (canvas.ActualWidth - rectangleSize) / 2;
rectangleTop = (canvas.ActualHeight - rectangleSize) / 2;
Canvas.SetLeft(trackbox, rectangleLeft);
Canvas.SetTop(trackbox, rectangleTop);
canvas.Children.Add(trackbox);
DrawCircle(new Point(canvas.ActualWidth / 2, canvas.ActualHeight / 2), 25, false, Brushes.White);
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
this.Close();
}
}
internal void Start()
{
Show();
DrawTrackbox();
}
internal void EyeTrackerGazeData(object sender, GazeDataEventArgs e)
{
Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { DisplayGaze(e.GazeData); }));
}
internal void EyeTrackerError(object sender, EyeTrackerErrorEventArgs e)
{
Close();
}
private void DisplayGaze(GazeData data)
{
if (this.IsLoaded)
{
if (lastDrawedLeft != null)
canvas.Children.Remove(lastDrawedLeft);
if (lastDrawedRight != null)
canvas.Children.Remove(lastDrawedRight);
switch (data.TrackingStatus)
{
case TrackingStatus.BothEyesTracked:
DisplayEyeLeft(data.Left);
DisplayEyeRight(data.Right);
break;
case TrackingStatus.OnlyLeftEyeTracked:
case TrackingStatus.OneEyeTrackedProbablyLeft:
DisplayEyeLeft(data.Left);
break;
case TrackingStatus.OnlyRightEyeTracked:
case TrackingStatus.OneEyeTrackedProbablyRight:
DisplayEyeRight(data.Right);
break;
}
}
}
private void DisplayEyeRight(GazeDataEye gazeDataEye)
{
Point p = new Point(1 - gazeDataEye.EyePositionInTrackBoxNormalized.X, gazeDataEye.EyePositionInTrackBoxNormalized.Y);
lastDrawedRight = DrawCircle(Denormalize(p), (1 - gazeDataEye.EyePositionInTrackBoxNormalized.Z) * 50, true, Brushes.Red);
}
private void DisplayEyeLeft(GazeDataEye gazeDataEye)
{
Point p = new Point(1 - gazeDataEye.EyePositionInTrackBoxNormalized.X, gazeDataEye.EyePositionInTrackBoxNormalized.Y);
lastDrawedLeft = DrawCircle(Denormalize(p), (1 - gazeDataEye.EyePositionInTrackBoxNormalized.Z) * 50, true, Brushes.Green);
}
private Ellipse DrawCircle(Point center, double radius, bool filled, Brush brush)
{
if (radius > 0)
{
Ellipse circle = new Ellipse() { Height = radius * 2, Width = radius * 2 };
circle.Stroke = brush;
if (filled)
circle.Fill = brush;
Canvas.SetLeft(circle, center.X - radius);
Canvas.SetTop(circle, center.Y - radius);
canvas.Children.Add(circle);
return circle;
}
return null;
}
private Point Denormalize(Point p)
{
return new Point(p.X * rectangleSize + rectangleLeft, p.Y * rectangleSize + rectangleTop);
}
}
}