-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTest2.java
More file actions
1144 lines (1019 loc) · 46.7 KB
/
Test2.java
File metadata and controls
1144 lines (1019 loc) · 46.7 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.bc.memorytest;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.concurrent.TimeUnit;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.StringBuilder;
import com.badlogic.gdx.utils.TimeUtils;
import com.bc.memorytest.Globals.AppState;
/**
* Class to handle all Deary & Liewald Reaction Time Test logic.
*
* @author Bill Cassidy
*/
public class Test2 implements InputProcessor
{
private TestType testType;
private State state;
private final Globals globals;
private final Stage stage;
private final Vector3 touchPos;
private final Table tblMenu;
private final Label lblDLMenu;
private final TextButton btnRunSRTPractice;
private final TextButton btnRunSRTTest;
private final TextButton btnRunCRTPractice;
private final TextButton btnRunCRTTest;
private final TextButton btnMainMenu;
private final Sprite whiteSquareSprite;
private final Sprite whiteSquareXSprite;
private final StringBuilder sbTestIntro;
private final String strTapScreenToStart;
private final String strTestCompleted;
private final String strTapScreenToReturnToMenu;
private final String strSRTPracticeIntroA;
private final String strSRTPracticeIntroB;
private final String strSRTFullIntroA;
private final String strSRTFullIntroB;
private final String strCRTPracticeIntroA;
private final String strCRTPracticeIntroB;
private final String strCRTFullIntroA;
private final String strCRTFullIntroB;
private final String strSRTHeaderFileName;
private final String strSRTDetailFileName;
private final String strCRTHeaderFileName;
private final String strCRTDetailFileName;
private int noOfPracticeTrialsSRT;
private int noOfPracticeTrialsCRT;
private int noOfExperimentTrialsSRT;
private int noOfExperimentTrialsCRT;
private long responseRangeFromSRT;
private long responseRangeFromCRT;
private long responseRangeToSRT;
private long responseRangeToCRT;
private float interStimulusInvervalFromSRT;
private float interStimulusInvervalFromCRT;
private float interStimulusInvervalToSRT;
private float interStimulusInvervalToCRT;
private final Array<Sprite> whiteSquares;
private boolean showingX;
public boolean doGetTimeStart; // indicate to Main class if we should set responseTimeStart
public long responseTimeStart; // time when X shown
private long responseTimeEnd; // time when user touches screen after X is shown
private long currentResponse; // difference between response start & end times
private float interStimulusIntervalTimer;
private float currentInterStimulusInterval;
private int srtState;
private int crtState;
private int trialCounter;
private final float waitInterval;
private float waitElapsed;
private int currentNoOfTrials;
private float currentInterStimulusIntervalFrom;
private float currentInterStimulusIntervalTo;
private int xPosition;
private final int whiteSquaresCount;
private int touchedSquare;
// Header fields.
private int prematureResponses; // SRT & CRT
private int anticipatedResponses; // SRT & CRT
private int trialCount; // SRT (number of trials)
private int correctCount; // CRT
private int wrongCount; // CRT
private long startTime; // SRT & CRT
// Details records.
private final Array<SRTDetail> srtDetails;
private final Array<CRTDetail> crtDetails;
/** Enumerator used to indicate the test type. */
private enum TestType
{
SRT_PRACTICE,
SRT_FULL,
CRT_PRACTICE,
CRT_FULL
}
/** Enumerator used to indicate the current state of the Reaction Time Test component. */
private enum State
{
MENU,
SRT_PRACTICE,
SRT_FULL,
CRT_PRACTICE,
CRT_FULL
}
/**
* Test2 constructor. All class objects and variables are initialised here.
*
* @param globals
* An initialised Globals object.
*/
public Test2(final Globals globals)
{
this.globals = globals;
stage = new Stage();
touchPos = new Vector3(0, 0, 0);
waitInterval = 4;
whiteSquareSprite = new Sprite(globals.spritesAtlas.createSprite("WhiteSquare1"));
whiteSquareSprite.flip(false, true);
whiteSquareSprite.setPosition(globals.VIRTUAL_WIDTH / 2 - whiteSquareSprite.getWidth() / 2,
globals.VIRTUAL_HEIGHT / 2 - whiteSquareSprite.getHeight() / 2);
whiteSquareXSprite = new Sprite(globals.spritesAtlas.createSprite("WhiteSquareX"));
whiteSquareXSprite.flip(false, true);
whiteSquares = new Array<Sprite>();
srtDetails = new Array<SRTDetail>();
crtDetails = new Array<CRTDetail>();
// Create the 4 white squares used in the CRT test.
for (int i = 0; i < 4; i++)
{
whiteSquares.add(new Sprite(globals.spritesAtlas.createSprite("WhiteSquare1")));
whiteSquares.get(i).flip(false, true);
}
whiteSquaresCount = whiteSquares.size;
// Center the 4 CRT square positions.
int gap = 60;
// Calculate positions relative to center of screen.
// 2nd from left (- * - -)
Vector2 pos2 = new Vector2(globals.VIRTUAL_WIDTH / 2 - (whiteSquareSprite.getWidth() + gap),
globals.VIRTUAL_HEIGHT / 2 - whiteSquareSprite.getHeight() / 2);
// 1st from left (* - - -)
Vector2 pos1 = new Vector2(pos2.x - (whiteSquareSprite.getWidth() + gap),
globals.VIRTUAL_HEIGHT / 2 - whiteSquareSprite.getHeight() / 2);
// 3rd from left (- - * -)
Vector2 pos3 = new Vector2(pos2.x + gap + whiteSquareSprite.getWidth(),
globals.VIRTUAL_HEIGHT / 2 - whiteSquareSprite.getHeight() / 2);
// 4th from left (- - - *)
Vector2 pos4 = new Vector2(pos3.x + gap + whiteSquareSprite.getWidth(),
globals.VIRTUAL_HEIGHT / 2 - whiteSquareSprite.getHeight() / 2);
// Apply calculated positions to sprites.
whiteSquares.get(0).setPosition(pos1.x, pos1.y);
whiteSquares.get(1).setPosition(pos2.x, pos2.y);
whiteSquares.get(2).setPosition(pos3.x, pos3.y);
whiteSquares.get(3).setPosition(pos4.x, pos4.y);
// Set default values.
noOfPracticeTrialsSRT = 8;
noOfExperimentTrialsSRT = 20;
responseRangeFromSRT = 150; // milliseconds
responseRangeToSRT = 1500; // milliseconds
interStimulusInvervalFromSRT = 1.0f; // seconds and milliseconds
interStimulusInvervalToSRT = 3.0f; // seconds and milliseconds
noOfPracticeTrialsCRT = 8;
noOfExperimentTrialsCRT = 40;
responseRangeFromCRT = 200; // milliseconds
responseRangeToCRT = 1500; // milliseconds
interStimulusInvervalFromCRT = 1.0f; // seconds and milliseconds
interStimulusInvervalToCRT = 3.0f; // seconds and milliseconds
// Create strings.
strTapScreenToStart = "Tap Screen to Start";
strTestCompleted = "Test Completed - Thank You";
strTapScreenToReturnToMenu = "Tap Screen to Return to Menu";
strSRTPracticeIntroA =
"First of all you will have a practice session.\n" +
"A cross will appear in the box on the screen\n";
strSRTPracticeIntroB =
" times and each time it appears you should\n" +
"tap the screen as quickly as you can. Don't\n" +
"hold your pen/finger on the screen, just touch\n" +
"and release it when the cross appears. When\n" +
"you are ready, tap the start button to begin.";
strSRTFullIntroA =
"Now a cross will appear another ";
strSRTFullIntroB =
" times\n"+
"and you should tap the screen as quickly as\n" +
"you can, as in the practice. When you are ready,\n" +
"tap the start button to start.";
strCRTPracticeIntroA =
"In this test there will be four boxes on the screen.\n" +
"A cross will appear in one of them and you have to\n" +
"tap the box in which the cross appears as quickly\n" +
"as you can. As before you will have a practice of\n";
strCRTPracticeIntroB =
" crosses first. Remember, a cross can appear in\n" +
"any of the four boxes. When you are ready, tap\n" +
"the start button to start.";
strCRTFullIntroA =
"You will now see another ";
strCRTFullIntroB =
" crosses appear one after\n" +
"another and you should respond as quickly as you\n" +
"can by tapping the correct box, as in the practice.\n"+
"When you are ready, tap the start button to start.";
strSRTHeaderFileName = "srt_header.csv";
strSRTDetailFileName = "srt_detail.csv";
strCRTHeaderFileName = "crt_header.csv";
strCRTDetailFileName = "crt_detail.csv";
// Create StringBuilders.
sbTestIntro = new StringBuilder();
// Create widgets.
lblDLMenu = new Label("Reaction Time Menu", globals.skin);
btnRunSRTPractice = new TextButton("Run SRT Practice", globals.skin);
btnRunSRTTest = new TextButton("Run SRT Test", globals.skin);
btnRunCRTPractice = new TextButton("Run CRT Practice", globals.skin);
btnRunCRTTest = new TextButton("Run CRT Test", globals.skin);
btnMainMenu = new TextButton("Main Menu", globals.skin);
// Add listeners.
btnMainMenu.addListener(new ChangeListener()
{
public void changed(ChangeEvent event, Actor actor)
{
Gdx.input.setInputProcessor(null);
state = null;
globals.appState = AppState.MENU;
}
});
btnRunSRTPractice.addListener(new ChangeListener()
{
public void changed(ChangeEvent event, Actor actor)
{
sbTestIntro.setLength(0);
sbTestIntro.append(strSRTPracticeIntroA);
sbTestIntro.append(noOfPracticeTrialsSRT);
sbTestIntro.append(strSRTPracticeIntroB);
new Dialog("SRT Practice Instructions", globals.skin, "dialog")
{
protected void result (Object object)
{
//System.out.println("Chosen: " + object);
if (object.equals(true))
SetupTest(TestType.SRT_PRACTICE);
}
}.text(sbTestIntro.toString())
.button("Cancel", false)
.button("Start", true)
.show(stage);
}
});
btnRunSRTTest.addListener(new ChangeListener()
{
public void changed(ChangeEvent event, Actor actor)
{
sbTestIntro.setLength(0);
sbTestIntro.append(strSRTFullIntroA);
sbTestIntro.append(noOfExperimentTrialsSRT);
sbTestIntro.append(strSRTFullIntroB);
new Dialog("SRT Instructions", globals.skin, "dialog")
{
protected void result (Object object)
{
//System.out.println("Chosen: " + object);
if (object.equals(true))
SetupTest(TestType.SRT_FULL);
}
}.text(sbTestIntro.toString())
.button("Cancel", false)
.button("Start", true)
.show(stage);
}
});
btnRunCRTPractice.addListener(new ChangeListener()
{
public void changed(ChangeEvent event, Actor actor)
{
sbTestIntro.setLength(0);
sbTestIntro.append(strCRTPracticeIntroA);
sbTestIntro.append(noOfPracticeTrialsCRT);
sbTestIntro.append(strCRTPracticeIntroB);
new Dialog("CRT Practice Instructions", globals.skin, "dialog")
{
protected void result (Object object)
{
//System.out.println("Chosen: " + object);
if (object.equals(true))
SetupTest(TestType.CRT_PRACTICE);
}
}.text(sbTestIntro.toString())
.button("Cancel", false)
.button("Start", true)
.show(stage);
}
});
btnRunCRTTest.addListener(new ChangeListener()
{
public void changed(ChangeEvent event, Actor actor)
{
sbTestIntro.setLength(0);
sbTestIntro.append(strCRTFullIntroA);
sbTestIntro.append(noOfExperimentTrialsCRT);
sbTestIntro.append(strCRTFullIntroB);
new Dialog("CRT Instructions", globals.skin, "dialog")
{
protected void result (Object object)
{
//System.out.println("Chosen: " + object);
if (object.equals(true))
SetupTest(TestType.CRT_FULL);
}
}.text(sbTestIntro.toString())
.button("Cancel", false)
.button("Start", true)
.show(stage);
}
});
// Create tables for each menu.
tblMenu = new Table(globals.skin);
tblMenu.setFillParent(true);
}
/**
* Sets all values before each test type begins.
*
* @param type
* A valid TestType enum value indicating the test type to be run.
*/
private void SetupTest(TestType type)
{
doGetTimeStart = false;
srtState = 1;
crtState = 1;
globals.glClearColourR = 0;
globals.glClearColourG = 0;
globals.glClearColourB = 128.0f / 255f;
showingX = false;
trialCounter = 1;
globals.wascoSans30.setColor(Color.WHITE);
waitElapsed = 0;
prematureResponses = 0;
anticipatedResponses = 0;
trialCount = 0;
correctCount = 0;
wrongCount = 0;
touchedSquare = -1;
switch (type)
{
case SRT_PRACTICE:
currentNoOfTrials = noOfPracticeTrialsSRT;
currentInterStimulusIntervalFrom = interStimulusInvervalFromSRT;
currentInterStimulusIntervalTo = interStimulusInvervalToSRT;
whiteSquareXSprite.setPosition(whiteSquareSprite.getX(), whiteSquareSprite.getY());
state = State.SRT_PRACTICE;
break;
case SRT_FULL:
currentNoOfTrials = noOfExperimentTrialsSRT;
currentInterStimulusIntervalFrom = interStimulusInvervalFromSRT;
currentInterStimulusIntervalTo = interStimulusInvervalToSRT;
whiteSquareXSprite.setPosition(whiteSquareSprite.getX(), whiteSquareSprite.getY());
state = State.SRT_FULL;
trialCount = noOfExperimentTrialsSRT;
srtDetails.clear();
for (int i = 0; i < noOfExperimentTrialsSRT; i++)
{
srtDetails.add(new SRTDetail());
}
break;
case CRT_PRACTICE:
currentNoOfTrials = noOfPracticeTrialsCRT;
currentInterStimulusIntervalFrom = interStimulusInvervalFromCRT;
currentInterStimulusIntervalTo = interStimulusInvervalToCRT;
state = State.CRT_PRACTICE;
break;
case CRT_FULL:
currentNoOfTrials = noOfExperimentTrialsCRT;
currentInterStimulusIntervalFrom = interStimulusInvervalFromCRT;
currentInterStimulusIntervalTo = interStimulusInvervalToCRT;
state = State.CRT_FULL;
trialCount = noOfExperimentTrialsCRT;
crtDetails.clear();
for (int i = 0; i < noOfExperimentTrialsCRT; i++)
{
crtDetails.add(new CRTDetail());
}
break;
}
Gdx.input.setInputProcessor(this);
}
/**
* Initialises the table to be displayed.
*
* @param table
* An initialised Table object.
*/
private void SetTable(Table table)
{
tblMenu.clear();
globals.glClearColourR = 1;
globals.glClearColourG = 1;
globals.glClearColourB = 1;
switch (state)
{
case MENU: // Add widgets to tblMenu.
tblMenu.row().colspan(2).height(150);
tblMenu.add(lblDLMenu).align(Align.center);
tblMenu.row().colspan(2).height(150);
tblMenu.add(btnRunSRTPractice).colspan(1).width(500);
tblMenu.add(btnRunCRTPractice).colspan(1).width(500);
tblMenu.row().colspan(2).height(150);
tblMenu.add(btnRunSRTTest).colspan(1).width(500);
tblMenu.add(btnRunCRTTest).colspan(1).width(500);
tblMenu.row().colspan(2).height(150);
tblMenu.add(btnMainMenu).width(1000).colspan(2);
break;
}
// Setup stage.
stage.clear();
stage.addActor(table);
Gdx.input.setInputProcessor(stage);
//Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
/** Release all resources used by objects that are disposable. */
public void Dispose()
{
stage.dispose();
}
/** Update method called once per frame. Updates all logic before each draw call. */
public void Update()
{
if (state == null)
{
state = State.MENU;
SetTable(tblMenu);
}
switch (state)
{
case SRT_PRACTICE:
case SRT_FULL:
switch (srtState)
{
case 1: // Show intro message, await user input.
break;
case 2: // Setup current "X".
showingX = false;
// End test if user has completed all the trials.
if (trialCounter > currentNoOfTrials)
{
if (state == State.SRT_FULL)
Save();
srtState = 5;
return;
}
// Set a random Inter Stimulus Interval.
interStimulusIntervalTimer = 0;
currentInterStimulusInterval = MathUtils.random(currentInterStimulusIntervalFrom, currentInterStimulusIntervalTo);
srtState = 3;
break;
case 3: // Waiting for the currentInterStimulusInterval to elapse.
interStimulusIntervalTimer += Gdx.graphics.getDeltaTime();
if (interStimulusIntervalTimer >= currentInterStimulusInterval)
{
currentInterStimulusInterval = interStimulusIntervalTimer;
srtState = 4;
showingX = true;
//responseTimeStart = Calendar.getInstance().getTimeInMillis();
doGetTimeStart = true;
}
break;
case 4: // Show "X" and wait for user to touch screen.
break;
case 5: // Display test completed message for 5 seconds.
waitElapsed += Gdx.graphics.getDeltaTime();
if (waitElapsed >= waitInterval)
{
srtState = 6;
}
break;
case 6: // Display return to menu message.
break;
}
break;
case CRT_PRACTICE:
case CRT_FULL:
switch (crtState)
{
case 1: // Show intro message, await user input.
break;
case 2: // Setup current "X".
showingX = false;
// End test if user has completed all the trials.
if (trialCounter > currentNoOfTrials)
{
if (state == State.CRT_FULL)
Save();
crtState = 5;
return;
}
// Set a random Inter Stimulus Interval.
interStimulusIntervalTimer = 0;
currentInterStimulusInterval = MathUtils.random(currentInterStimulusIntervalFrom, currentInterStimulusIntervalTo);
// Randomly set the position of the "X".
xPosition = MathUtils.random(0, 3);
whiteSquareXSprite.setPosition(whiteSquares.get(xPosition).getX(),
whiteSquares.get(xPosition).getY());
crtState = 3;
break;
case 3: // Waiting for the currentInterStimulusInterval to elapse.
interStimulusIntervalTimer += Gdx.graphics.getDeltaTime();
if (interStimulusIntervalTimer >= currentInterStimulusInterval)
{
currentInterStimulusInterval = interStimulusIntervalTimer;
crtState = 4;
showingX = true;
//responseTimeStart = Calendar.getInstance().getTimeInMillis();
doGetTimeStart = true;
}
break;
case 4: // Show "X" and wait for user to touch a square.
break;
case 5: // Display test completed message for 5 seconds.
waitElapsed += Gdx.graphics.getDeltaTime();
if (waitElapsed >= waitInterval)
{
crtState = 6;
}
break;
case 6: // Display return to menu message.
break;
}
break;
default: // Menus
stage.act(Gdx.graphics.getDeltaTime());
break;
}
}
/**
* Draw method, called once per frame after logic updates.
*
* @param spriteBatch
* An initialised SpriteBatch object.
*/
public void Draw(SpriteBatch spriteBatch)
{
switch (state)
{
case MENU:
stage.draw();
break;
case SRT_PRACTICE:
case SRT_FULL:
// Debug info.
//globals.wascoSans30.draw(spriteBatch, "ISI: " + currentInterStimulusInterval, 5, 5);
//globals.wascoSans30.draw(spriteBatch, "Response: " + currentResponse, 400, 5);
//globals.wascoSans30.draw(spriteBatch, "trialCounter: " + trialCounter, 5, 5);
//globals.wascoSans30.draw(spriteBatch, "currentNoOfTrials: " + currentNoOfTrials, 400, 5);
if (srtState == 1)
{
globals.wascoSans30.drawWrapped(spriteBatch, strTapScreenToStart, 0,
100, globals.VIRTUAL_WIDTH, HAlignment.CENTER);
}
else if (srtState == 5 || srtState == 6)
{
globals.wascoSans30.drawWrapped(spriteBatch, strTestCompleted, 0,
100, globals.VIRTUAL_WIDTH, HAlignment.CENTER);
}
if (srtState == 6)
{
globals.wascoSans30.drawWrapped(spriteBatch, strTapScreenToReturnToMenu, 0,
150, globals.VIRTUAL_WIDTH, HAlignment.CENTER);
}
if (showingX)
whiteSquareXSprite.draw(spriteBatch);
else
whiteSquareSprite.draw(spriteBatch);
break;
case CRT_PRACTICE:
case CRT_FULL:
// Debug info.
//globals.wascoSans30.draw(spriteBatch, "ISI: " + currentInterStimulusInterval, 5, 5);
//globals.wascoSans30.draw(spriteBatch, "Response: " + currentResponse, 400, 5);
//globals.wascoSans30.draw(spriteBatch, "Touched: " + String.valueOf(touchedSquare + 1), 900, 5);
for (Sprite sprite : whiteSquares)
{
sprite.draw(spriteBatch);
}
// Debug info (square indexes).
// for (int i = 0; i < whiteSquares.size; i++)
// {
// globals.wascoSans30.setColor(Color.CYAN);
// globals.wascoSans30.draw(spriteBatch, String.valueOf(i), whiteSquares.get(i).getX(), whiteSquares.get(i).getY());
// }
if (showingX)
whiteSquareXSprite.draw(spriteBatch);
if (crtState == 1)
{
globals.wascoSans30.drawWrapped(spriteBatch, strTapScreenToStart, 0,
100, globals.VIRTUAL_WIDTH, HAlignment.CENTER);
}
else if (crtState == 5 || crtState == 6)
{
globals.wascoSans30.drawWrapped(spriteBatch, strTestCompleted, 0,
100, globals.VIRTUAL_WIDTH, HAlignment.CENTER);
}
if (crtState == 6)
{
globals.wascoSans30.drawWrapped(spriteBatch, strTapScreenToReturnToMenu, 0,
150, globals.VIRTUAL_WIDTH, HAlignment.CENTER);
}
break;
}
}
@Override
public boolean keyDown(int keycode)
{
return false;
}
@Override
public boolean keyUp(int keycode)
{
return false;
}
@Override
public boolean keyTyped(char character)
{
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)
{
//responseTimeEnd = System.currentTimeMillis(); // Calendar.getInstance().getTimeInMillis();
responseTimeEnd = TimeUtils.nanoTime();
// Disable multi-touch.
if (pointer > 0 || button > 0)
return false;
// Translate touch co-ords to virtual screen co-ords.
touchPos.x = screenX;
touchPos.y = screenY;
touchPos.z = 0;
// globals.camera.unproject(touchPos, globals.resizeViewport.x, globals.resizeViewport.y,
// globals.resizeViewport.width, globals.resizeViewport.height);
// Need this line only window size != VIRTUAL sizes in globals.
globals.camera.unproject(touchPos, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
switch (state)
{
case SRT_PRACTICE:
case SRT_FULL:
switch (srtState)
{
case 1: // User touched screen at start test prompt, so start the test.
srtState = 2;
startTime = responseTimeEnd;
return true;
case 3: // User touched screen before "X" was displayed - log as a premature response.
prematureResponses++;
break;
case 4: // User touched screen while "X" is displayed.
// Set current response time.
//responseTimeEnd = Calendar.getInstance().getTimeInMillis();
currentResponse = responseTimeEnd - responseTimeStart;
currentResponse = TimeUnit.MILLISECONDS.convert(currentResponse, TimeUnit.NANOSECONDS);
// Did the screen touch occur within the response range?
if (currentResponse >= responseRangeFromSRT && currentResponse <= responseRangeToSRT)
{
// Update details record if full test.
if (state == State.SRT_FULL)
{
srtDetails.get(trialCounter - 1).runNumber = trialCounter;
srtDetails.get(trialCounter - 1).response = currentResponse;
srtDetails.get(trialCounter - 1).interStimulusInterval = currentInterStimulusInterval;
}
trialCounter++;
//srtState = 2;
srtState = 7;
}
else if (currentResponse < responseRangeFromSRT)
{
// Input was before "response range from" so trial must be repeated.
anticipatedResponses++;
//srtState = 2;
srtState = 7;
}
else if (currentResponse > responseRangeToSRT)
{
// Input was after "response range to" so trial must be repeated.
//srtState = 2;
srtState = 7;
}
return true;
case 6: // Test completed - user touched screen to return to menu.
state = State.MENU;
SetTable(tblMenu);
break;
case 7: // Waiting for finger/pen to be lifted from screen.
break;
}
break;
case CRT_PRACTICE:
case CRT_FULL:
switch (crtState)
{
case 1: // User touched screen at start test prompt, so start the test.
crtState = 2;
startTime = responseTimeEnd;
return true;
case 3: // User touched screen before "X" was displayed - log as a premature response.
prematureResponses++;
break;
case 4: // User touched screen while "X" is displayed.
// Set current response time.
//responseTimeEnd = Calendar.getInstance().getTimeInMillis();
currentResponse = responseTimeEnd - responseTimeStart;
currentResponse = TimeUnit.MILLISECONDS.convert(currentResponse, TimeUnit.NANOSECONDS);
// Did the screen touch occur within the response range?
if (currentResponse >= responseRangeFromCRT && currentResponse <= responseRangeToCRT)
{
// Which square did the user touch?
touchedSquare = -1; // Minus one means screen was touched but no square was touched.
for (int i = 0; i < whiteSquaresCount; i++)
{
if (whiteSquares.get(i).getBoundingRectangle().contains(touchPos.x, touchPos.y))
{
touchedSquare = i;
break;
}
}
// Did user touch "X"?
if (touchedSquare != -1 && xPosition == touchedSquare)
{
correctCount++;
}
else
{
wrongCount++; // Any other box or any other part of the screen was touched.
}
// Update details record if full test.
if (state == State.CRT_FULL)
{
crtDetails.get(trialCounter - 1).runNumber = trialCounter;
crtDetails.get(trialCounter - 1).correctResponse = xPosition;
crtDetails.get(trialCounter - 1).subjectResponse = touchedSquare;
crtDetails.get(trialCounter - 1).responseTime = currentResponse;
crtDetails.get(trialCounter - 1).interStimulusInterval = currentInterStimulusInterval;
}
trialCounter++;
//crtState = 2;
crtState = 7;
}
else if (currentResponse < responseRangeFromCRT)
{
// Input was after ISI but before "response range from" so trial must be repeated.
anticipatedResponses++;
//crtState = 2;
crtState = 7;
}
else if (currentResponse > responseRangeToCRT)
{
// Input was after "response range to" so trial must be repeated.
//crtState = 2;
crtState = 7;
}
return true;
case 6: // Test completed - user touched screen to return to menu.
state = State.MENU;
SetTable(tblMenu);
break;
case 7: // Waiting for finger/pen to be lifted from screen.
break;
}
break;
}
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button)
{
// Disable multi-touch.
if (pointer > 0 || button > 0)
return false;
// Resume test if user was holding finger/pen down while X was displayed.
switch (state)
{
case SRT_PRACTICE:
case SRT_FULL:
if (srtState == 7)
srtState = 2;
break;
case CRT_PRACTICE:
case CRT_FULL:
if (crtState == 7)
crtState = 2;
break;
}
// // Quit test if two fingers touched the screen.
// if (state != State.MENU)
// {
// if (fingersOnScreen == 2)
// {
// fingersOnScreen = 0;
// state = State.MENU;
// SetTable(tblMenu);
// return false;
// }
// fingersOnScreen--;
// }
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer)
{
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY)
{
return false;
}
@Override
public boolean scrolled(int amount)
{
return false;
}
/** Append data for last test to csv files. */
private void Save()
{
if (Gdx.files.isLocalStorageAvailable())
{
BufferedWriter out = null;
boolean error = false;
switch (state)
{
case SRT_FULL:
// SRT Header
try
{
out = new BufferedWriter(new OutputStreamWriter(Gdx.files.local(strSRTHeaderFileName).write(true)));
out.write(globals.sbCurrentParticipantID.toString());
out.write(globals.strComma);
out.write(String.valueOf(prematureResponses));
out.write(globals.strComma);
out.write(String.valueOf(anticipatedResponses));
out.write(globals.strComma);
out.write(String.valueOf(trialCount));
out.write(globals.strComma);
out.write(Long.toString(startTime));
out.write(globals.strNewLine);
}
catch (GdxRuntimeException gre)
{
gre.printStackTrace();
//message += "Couldn't open localstorage/test.txt\n";
error = true;
}
catch (IOException ioE1)
{
ioE1.printStackTrace();
error = true;
}
finally
{
if (out != null)
{
try
{
out.close();
}
catch (IOException ioE2)
{
ioE2.printStackTrace();