-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkeletonController.cs
190 lines (160 loc) · 6.97 KB
/
SkeletonController.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Controls;
using Microsoft.Research.Kinect.Nui;
using Coding4Fun.Kinect.Wpf;
namespace SkeletalTracking
{
class SkeletonController
{
private MainWindow window;
public SkeletonController(MainWindow win)
{
window = win;
}
//This function will be implemented by you in the subclass files provided.
//A simple example of highlighting targets when hovered over has been provided below
//Note: targets is a dictionary that allows you to retrieve the corresponding target on screen
//and manipulate its state and position, as well as hide/show it (see class defn. below).
//It is indexed from 1, thus you can retrieve an individual target with the expression
//targets[3], which would retrieve the target labeled "3" on screen.
public virtual void processSkeletonFrame(SkeletonData skeleton, Dictionary<int, Target> targets)
{
/*Example implementation*/
foreach (var target in targets)
{
Target cur = target.Value;
int targetID = cur.id; //ID in range [1..5]
//Scale the joints to the size of the window
Joint leftHand = skeleton.Joints[JointID.HandLeft].ScaleTo(640, 480, window.k_xMaxJointScale, window.k_yMaxJointScale);
Joint rightHand = skeleton.Joints[JointID.HandRight].ScaleTo(640, 480, window.k_xMaxJointScale, window.k_yMaxJointScale);
//Calculate how far our left hand is from the target in both x and y directions
double deltaX_left = Math.Abs(leftHand.Position.X - cur.getXPosition());
double deltaY_left = Math.Abs(leftHand.Position.Y - cur.getYPosition());
//Calculate how far our right hand is from the target in both x and y directions
double deltaX_right = Math.Abs(rightHand.Position.X - cur.getXPosition());
double deltaY_right = Math.Abs(rightHand.Position.Y - cur.getYPosition());
//If we have a hit in a reasonable range, highlight the target
if (deltaX_left < 15 && deltaY_left < 15 || deltaX_right < 15 && deltaY_right < 15)
{
cur.setTargetSelected();
}
else
{
cur.setTargetUnselected();
}
}
}
//This is called when the controller becomes active. This allows you to place your targets and do any
//initialization that you don't want to repeat with each new skeleton frame. You may also
//directly move the targets in the MainWindow.xaml file to achieve the same initial repositioning.
public virtual void controllerActivated(Dictionary<int, Target> targets){
//targets[1].setTargetPosition(80, 200);
//targets[2].hideTarget();
//targets[2].showTarget();
//targets[5].isHidden();
//targets[3].setTargetHighlighted();
targets[6].hideTarget();
targets[7].hideTarget();
//reset 5 targets in arc
targets[1].setTargetUnselected();
targets[1].showTarget();
targets[1].setTargetPosition(23, 220);
targets[2].setTargetUnselected();
targets[2].showTarget();
targets[2].setTargetPosition(111, 96);
targets[3].setTargetUnselected();
targets[3].showTarget();
targets[3].setTargetPosition(262, 33);
targets[4].setTargetUnselected();
targets[4].showTarget();
targets[4].setTargetPosition(409, 96);
targets[5].setTargetUnselected();
targets[5].showTarget();
targets[5].setTargetPosition(505, 220);
}
//The default value that gets passed to MaxSkeletonX and MaxSkeletonY in the Coding4Fun Joint.ScaleTo function is 1.5f
//This function will change that so that your scaling in processSkeletonFrame aligns with the scaling done when we
//position the ellipses in the MainWindow.xaml.cs file.
public void adjustScale(float f)
{
window.k_xMaxJointScale = f;
window.k_yMaxJointScale = f;
}
}
public class Target
{
public int id;
private Brush _target_color;
private TextBlock _canvasEl;
private bool selected;
public Target(TextBlock target, int givenID)
{
_target_color = new SolidColorBrush(Colors.Red);
_canvasEl = target;
id = givenID;
showTarget();
selected = false;
}
public void setTargetPosition(double x, double y)
{
_canvasEl.SetValue(Canvas.LeftProperty, x);
_canvasEl.SetValue(Canvas.TopProperty, y);
}
public void setTargetHighlighted()
{
_target_color = new SolidColorBrush(Color.FromRgb(238, 221, 130));
_canvasEl.Background = new VisualBrush(generateEllipse((double)_canvasEl.GetValue(Canvas.WidthProperty) / 2, _target_color));
}
public void setTargetSelected()
{
_target_color = new SolidColorBrush(Color.FromRgb(34, 139, 34));
_canvasEl.Background = new VisualBrush(generateEllipse((double)_canvasEl.GetValue(Canvas.WidthProperty) / 2, _target_color));
selected = true;
}
public void setTargetUnselected()
{
_target_color = new SolidColorBrush(Colors.Red);
_canvasEl.Background = new VisualBrush(generateEllipse((double)_canvasEl.GetValue(Canvas.WidthProperty) / 2, _target_color));
selected = false;
}
public bool isSelected()
{
return selected;
}
public void hideTarget()
{
_canvasEl.Visibility = Visibility.Hidden;
}
public void showTarget()
{
_canvasEl.Visibility = Visibility.Visible;
}
public bool isHidden()
{
return _canvasEl.Visibility != Visibility.Visible;
}
public double getXPosition()
{
return (double)_canvasEl.GetValue(Canvas.LeftProperty) + ((double)_canvasEl.GetValue(Canvas.WidthProperty) / 2);
}
public double getYPosition()
{
return (double)_canvasEl.GetValue(Canvas.TopProperty) + ((double)_canvasEl.GetValue(Canvas.WidthProperty) / 2);
}
private Ellipse generateEllipse(double r, Brush color){
var circle = new Ellipse();
circle.Width = r * 2;
circle.Height = r * 2;
circle.Stroke = new SolidColorBrush(Colors.Black);
circle.StrokeThickness = 1;
circle.Fill = color;
return circle;
}
}
}