-
Notifications
You must be signed in to change notification settings - Fork 18
/
glyph-exercises.js
123 lines (109 loc) · 3.5 KB
/
glyph-exercises.js
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
/*
* Copyright (c) 2014 gm9
* This software is released under the MIT License.
* http://opensource.org/licenses/mit-license.php
*/
(function(){
var igt = gm9.IngressGlyphTools;
var Glyph = igt.Glyph;
function createExercisesElement()
{
var padSize = 300;
var fontSize = 40;
var exercisesElement = document.createElement("div");
exercisesElement.style.width = (padSize+2) + "px";
exercisesElement.style.marginLeft = "auto";
exercisesElement.style.marginRight = "auto";
exercisesElement.style.position = "relative";
function setBGColor(color)
{
pad.style.background = color;
}
var wordDiv = document.createElement("div");
exercisesElement.appendChild(wordDiv);
wordDiv.style.fontWeight = "bold";
wordDiv.style.fontSize = fontSize + "px";
wordDiv.style.textAlign = "center";
wordDiv.style.position = "absolute";
wordDiv.style.width = (padSize+2) + "px";
wordDiv.style.left = "0";
wordDiv.style.top = (padSize - fontSize)/2 + "px";
wordDiv.style.textShadow = "0 0 10px #000000";
function showWord(str)
{
hideWord();
wordDiv.appendChild(document.createTextNode(str));
}
function hideWord()
{
while(wordDiv.firstChild){wordDiv.removeChild(wordDiv.firstChild);}
}
var pad = igt.createInputPad({
size: padSize,
style: {
color: "white"
}});
exercisesElement.appendChild(pad);
var currentGlyph = null;
var currentWord = null;
function nextQuestion()
{
var wg = igt.glyphDic.getWordGlyphRandom();
currentGlyph = wg.glyph;
currentWord = wg.word;
showQuestion();
}
function showQuestion()
{
setBGColor("#303030");
showQuestionWord();
}
function showQuestionWord()
{
pad.clearGlyph();
pad.setLimitInputStroke(0);
showWord(currentWord);
setTimeout(showQuestionGlyph, 1000);
}
function showQuestionGlyph()
{
hideWord();
pad.setGlyph(currentGlyph);
setTimeout(inputAnswer, 1000);
}
function inputAnswer()
{
setBGColor("");
pad.clearGlyph();
pad.setLimitInputStroke(1);
pad.addEventListener("glyphstrokeend", onStrokeEnd, false);
}
function onStrokeEnd()
{
pad.removeEventListener("glyphstrokeend", onStrokeEnd, false);
showResult(Glyph.equals(pad.getGlyph(), currentGlyph));
}
function showResult(result){
if(result){
//correct
setBGColor("#8ef7d7");
showWord(currentWord);
setTimeout(nextQuestion, 1000);
}
else{
//incorrect
setBGColor("#ff5a5a");
setTimeout(showQuestion, 1000);
}
}
nextQuestion();
return exercisesElement;
}
function insertExercisesElementAfterLastScriptNode()
{
var exercisesElement = createExercisesElement();
igt.getLastScriptNode().parentNode.appendChild(exercisesElement);
igt.controlViewportMetaElement(exercisesElement);
}
igt.exercises = insertExercisesElementAfterLastScriptNode;
})();