forked from selzoc/Tar-Heel-Typer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
256 lines (223 loc) · 7.84 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Tar Heel Typer</title>
<style type="text/css">
@import "http://ajax.googleapis.com/ajax/libs/dojo/1.5.0/dojo/resources/dojo.css";
@import "http://ajax.googleapis.com/ajax/libs/dojo/1.5.0/dijit/themes/claro/claro.css";
@import "css/application.css";
</style>
<script type="text/javascript">
var djConfig = {
isDebug: false,
parseOnLoad: true,
baseUrl: './',
modulePaths: {'tht' : 'sounds'}
};
</script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/dojo/1.5.0/dojo/dojo.xd.js"></script>
<script type="text/javascript" src="/libs/uow/trace.js"></script>
<script type="text/javascript" src="/libs/uow.js"></script>
<script type="text/javascript" src="application.js"></script>
<script type="text/javascript" src="bindings.js"></script>
<script type="text/javascript" src="lessons.js"></script>
<script type="text/javascript" src="distance.js"></script>
<script type="text/javascript">
// Get the audio api controller
var audio = null;
var currentLesson="";
var playingSound=false;
var masterVolume=1.0;
var speechVolume=1.0;
var soundVolume=1.0;
// Listen for key events
uow.ui.connectKeys();
// Current lesson
var lesson;
var index;
var sequenceIndex;
//Changes audio preferences whenever game prefs change
function prefsCallback(prefs, which) {
masterVolume=prefs.volume;
speechVolume=prefs.speechVolume;
soundVolume=prefs.soundVolume;
audio.setProperty({name : 'rate', value : prefs.speechRate, immediate : true});
audio.setProperty({name : 'volume', value : prefs.volume*(playingSound ? prefs.soundVolume : prefs.speechVolume), immediate : true});
}
// Pass messages to this function to have them spoken and displayed in the footer
// optional stop parameter preempts the previous speech
function talk(message, stop) {
console.log(message + "\t" + stop);
if(stop) {
audio.stop();
}
//Set audio properties
audio.say({text : message});
footer = document.getElementById("footer");
footer.innerHTML = message;
}
function talkLetters(letters) {
for(var i=0; i < letters.length; i++) {
// Semicolon is special case
if(letters[i] == ';') {
talk("'semicolon'", false);
}
else {
talk("'" + letters[i] + "'", false);
}
}
}
// Pass urls to this function to have the sound played
function playSound(soundUrl) {
var def=audio.play({url : soundUrl});
def.callBefore(function(){
playingSound=true;
audio.setProperty({name : 'volume', value : masterVolume*soundVolume, immediate : true});
});
def.callAfter(function(){
playingSound=false;
audio.setProperty({name : 'volume', value : masterVolume*speechVolume, immediate : true});
});
}
// Display stuff big in the center of the screen
function bigDisplay(text) {
document.getElementById('letterDisplay').innerHTML = text;
}
// Free mode key callback
function freemodeCallback(e) {
var text = freeModeBindings[String.fromCharCode(e.keyCode)][0];
var sound = freeModeBindings[String.fromCharCode(e.keyCode)][1];
bigDisplay(String.fromCharCode(e.keyCode));
if(text != undefined) {
talk("'" + text + "'", true);
}
if(sound != undefined) {
playSound(sound);
}
}
// Lesson key callback
function lessonCallback(e) {
// Store the key pressed as well as the key expected to be hit next in the lesson
var keyPressed = String.fromCharCode(e.keyCode);
// Semicolon is special case
if(e.keyCode == 186)
keyPressed = ';';
var expectedKey = lesson.sequences[index][sequenceIndex];
bigDisplay(keyPressed);
// If they typed the next letter in the sequence
if((isLetter(e.keyCode) && keyPressed.toLowerCase() == expectedKey) || keyPressed == expectedKey) {
// Semicolon is special case
if(keyPressed == ';') {
console.log("semicolon!");
talk("'semicolon'", true);
}
else {
talk("'" + keyPressed + "'", true);
}
sequenceIndex++;
}
// If not, direct them where to type and remind then of what is left to type
else {
// Semicolon is special case
if(expectedKey == ';') {
talk("You typed '" + keyPressed + "'. " + errorFeedback(expectedKey, keyPressed) + " for 'semicolon'", true);
}
else {
talk("You typed '" + keyPressed + "'. " + errorFeedback(expectedKey, keyPressed) + " for '" + expectedKey + "'", true);
}
// Remind them of the next 5 letters
if(sequenceIndex != lesson.sequences[index].length - 1) {
talk("Then type ", false);
talkLetters(lesson.sequences[index].substring(sequenceIndex + 1, sequenceIndex+5));
}
}
// If the sequence is done, reset the variables and prompt for the next sequence
if(sequenceIndex == lesson.sequences[index].length) {
sequenceIndex = 0;
index++;
talk("'" + keyPressed + "'; " + lesson.prompts[index], true);
}
// If the lesson is done, talk the lesson end
if(index == lesson.sequences.length) {
// Semicolon is special case
if(keyPressed == ';') {
talk("'semicolon'; " + lesson.finish, true);
}
else {
talk("'" + keyPressed + "'; " + lesson.finish, true);
}
}
}
// Sets the current lesson
function loadLesson(lessonName) {
index = 0;
sequenceIndex = 0;
lesson = lessons[lessonName];
currentLesson.innerHTML = lesson.name;
console.log(lesson);
talk(lesson.prompts[index], true);
}
// Is this a letter?
function isLetter(keyCode) {
if((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122)) {
return true;
}
else {
return false;
}
}
// For non-numeric or alphabetic keys, translate them to be spoken
function translateKeys(keyCode) {
if((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122))
return String.fromCharCode(keyCode);
else {
return keyMappings[keyCode] || "Unknown key pressed: " + keyCode;
}
}
// Subscribe key events to the appropriate callback function based on the hash tag
function initialize(hashValue){
// See if this is a valid lesson
if(lessons[hashValue] != undefined) {
loadLesson(hashValue);
dojo.subscribe('/uow/key/up', lessonCallback);
}
// If not, fall back to free mode
else {
talk("Try typing a letter in free mode.", true);
dojo.subscribe('/uow/key/up', freemodeCallback);
currentLesson.innerHTML = "Free mode";
}
}
dojo.addOnLoad(
function() {
uow.getAudio().then(function(a){
audio=a;
currentLesson=document.getElementById('currentLesson');
dojo.subscribe('/org/hark/prefs/response', prefsCallback);
dojo.publish('/org/hark/prefs/request');
// Pick the lesson or free mode based on the hash part of the url
initialize(dojo.hash());
// Determine the best size for letter display on load
var letterHeight = document.getElementById('Main').clientHeight * (3/4);
var letterElement = document.getElementById('letterDisplay');
letterElement.setAttribute('style', 'font-size:' + letterHeight + 'px');
});
}
);
</script>
</head>
<body class="claro">
<div dojoType="dijit.layout.BorderContainer" liveSplitters="true">
<div dojoType="dijit.layout.ContentPane" region="top" splitter="false">
<h1>Tar Heel Typer</h1>
<h2>Current Lesson: <span id="currentLesson"></span></h2>
</div>
<div dojoType="dijit.layout.ContentPane" region="center" title="Welcome" id="Main" tabindex="1" selected="true">
<p id="letterDisplay"></p>
</div>
<div dojoType="dijit.layout.ContentPane" id="footer" region="bottom" splitter="false"></div>
</div>
</div>
</body>
</html>