-
Notifications
You must be signed in to change notification settings - Fork 29
/
SceneMainController.java
259 lines (209 loc) · 6.88 KB
/
SceneMainController.java
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
/*
* Copyright (c) 2013-present, The Eye Tribe.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the LICENSE file in the root directory of this source tree.
*
*/
package com.theeyetribe.javafx.scenes;
import com.theeyetribe.clientsdk.GazeManager;
import com.theeyetribe.clientsdk.data.CalibrationResult;
import com.theeyetribe.clientsdk.data.GazeData;
import com.theeyetribe.clientsdk.data.Point2D;
import com.theeyetribe.clientsdk.utils.GazeUtils;
import com.theeyetribe.javafx.utils.FrameRateGazeDataDeque;
import com.theeyetribe.javafx.utils.GazeFrameCache;
import com.theeyetribe.javafx.utils.JavaFxCalibUtils;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import org.controlsfx.control.Rating;
/**
* Scene controller associated to the main scene
*/
public class SceneMainController extends SceneController
{
private final static long FRAME_CACHE_SIZE = 5000; //5 sec
@FXML
private ImageView eyeLeft;
@FXML
private ImageView eyeRight;
@FXML
private Button calibrationBtn;
@FXML
public Button evaluateBtn;
@FXML
public Button connectBtn;
@FXML
private Label connect;
@FXML
private Label fpsLabel;
@FXML
private Rating rating;
private FrameRateGazeDataDeque mFpsCache;
private int fpsUpdateCycle = 10;
private int fpsUpdateCount = 0;
private boolean mIsRecievingFrames;
public SceneMainController()
{
super();
}
@FXML
public void initialize()
{
super.initialize();
mFpsCache = new FrameRateGazeDataDeque(FRAME_CACHE_SIZE);
}
@Override
public void onStart()
{
super.onStart();
mIsRecievingFrames = false;
}
@Override
public void onGazeUpdate(GazeData gazeData)
{
Platform.runLater(() -> {
if (!mIsRecievingFrames)
{
progress.setVisible(false);
updateState();
mIsRecievingFrames = true;
}
double angle = GazeFrameCache.getInstance().getLastEyesAngle();
double scale = 1 - GazeFrameCache.getInstance().getLastUserPosition().z;
if (null != GazeFrameCache.getInstance().getLastLeftEye())
{
updateEye(
root,
eyeLeft,
GazeFrameCache.getInstance().getLastLeftEye(),
angle,
scale
);
eyeLeft.setVisible(true);
} else
eyeLeft.setVisible(false);
if (null != GazeFrameCache.getInstance().getLastRightEye())
{
updateEye(
root,
eyeRight,
GazeFrameCache.getInstance().getLastRightEye(),
angle,
scale
);
eyeRight.setVisible(true);
} else
eyeRight.setVisible(false);
Point2D gaze = GazeFrameCache.getInstance().getLastSmoothedGazeCoordinates();
if (null != gaze) {
gazeIndicator.setVisible(true);
javafx.geometry.Point2D rootAnchor = getGazeIndicatorAnchor(gaze);
gazeIndicator.setX(rootAnchor.getX());
gazeIndicator.setY(rootAnchor.getY());
} else
gazeIndicator.setVisible(false);
//remove deque elements until condition fulfilled
while (!mFpsCache.offerFirst(gazeData))
mFpsCache.pollLast();
if ((++fpsUpdateCount % fpsUpdateCycle) == 0) {
fpsLabel.setText(bundle.getString("label.fps") + " " + String.format("%.2f", mFpsCache.getAvgFramesPerSecond()));
}
});
}
private void updateEye(Pane root, ImageView eyeImageView, GazeData.Eye eye, double angle, double scale)
{
Point2D p = GazeUtils.getRelativeToRect(
eye.pupilCenterCoordinates,
(int) Math.round(root.getScene().getWidth()),
(int) Math.round(root.getScene().getHeight())
);
p.x -= eyeImageView.getFitWidth() * .5d;
p.y -= eyeImageView.getFitHeight() * .5d;
eyeImageView.setX(p.x);
eyeImageView.setY(p.y);
eyeImageView.setRotate(angle);
eyeImageView.setScaleX(scale);
eyeImageView.setScaleY(scale);
}
@Override
public void onConnectionStateChanged(boolean isConnected)
{
Platform.runLater(() -> updateState());
}
@Override
public void onScreenStatesChanged(int i, int i1, int i2, float v, float v1) {}
@Override
public void onTrackerStateChanged(int i) {}
@FXML
private void handleCalibrationButtonAction(ActionEvent event)
{
try
{
main.loadCalibrationScene();
}
catch (Exception e)
{
System.out.println("Error loading calibration scene " + e.getLocalizedMessage());
e.printStackTrace();
}
}
@FXML
private void handleConnectButtonAction(ActionEvent event)
{
//TODO: Handle progress connection timeout
GazeManager.getInstance().activateAsync();
}
@FXML
private void handleEvaluationButtonAction(ActionEvent event)
{
try
{
main.loadEvaluationScene();
}
catch (Exception e)
{
System.out.println("Error loading calibration scene " + e.getLocalizedMessage());
e.printStackTrace();
}
}
@Override
public void onCalibrationChanged(boolean b, CalibrationResult calibrationResult)
{
Platform.runLater(() -> updateState());
}
private void updateState()
{
if(GazeManager.getInstance().isActivated())
{
connectBtn.setVisible(false);
connect.setVisible(false);
if(GazeManager.getInstance().isCalibrated())
{
calibrationBtn.setVisible(true);
calibrationBtn.setText(bundle.getString("btn.recalibrate"));
evaluateBtn.setVisible(true);
rating.setRating(JavaFxCalibUtils.getCalibRating(GazeManager.getInstance().getLastCalibrationResult()));
}
else
{
calibrationBtn.setVisible(true);
calibrationBtn.setText(bundle.getString("btn.calibrate"));
evaluateBtn.setVisible(false);
}
rating.setVisible(true);
}
else
{
connect.setVisible(true);
connectBtn.setVisible(true);
calibrationBtn.setVisible(false);
}
progress.setVisible(false);
}
}