Permalink
May 17, 2017
May 17, 2017
Newer
100644
134 lines (130 sloc)
4.37 KB
1
function() {
2
this.label = 'qw3';
3
this.onEnter = function(p) { window.isWritingToTextSketch = true; }
4
this.onExit = function(p) { window.isWritingToTextSketch = false; }
5
this.onDelete = function(p) { window.isWritingToTextSketch = false; }
6
this._isStartOfSentence = true;
7
this._counter = 0;
8
this._deleteStack = [];
9
this.setRecognizedStroke = function(stroke) {
10
if (stroke == null)
11
this._recognizedStroke = null;
12
else {
13
stroke = resampleCurve(stroke, 100);
14
let strokeLength = computeCurveLength(stroke);
15
for (let i = 0 ; i < stroke.length ; i++) {
16
stroke[i][0] += .1 * noise2(10, .02 * i * strokeLength);
17
stroke[i][1] += .1 * noise2(20, .02 * i * strokeLength);
18
}
19
this._recognizedStroke = stroke;
20
}
21
}
22
this.mouseDown = function(x, y, z) {
23
this.setRecognizedStroke(null);
24
if (! this.isChoosingWord)
25
this._stroke = [ this.inverseTransform([x, y, def(z)]) ];
26
}
27
this.mouseDrag = function(x, y, z) {
28
if (! this.isChoosingWord) {
29
this._stroke.push(this.inverseTransform([x, y, def(z)]));
30
if (this._counter++ % 10 == 0)
31
this._words = this.qw.lookupWord(this._stroke);
32
}
33
}
34
this.mouseUp = function(x, y, z) {
35
if (this.isChoosingWord) {
36
this.isChoosingWord = false;
37
var p = this.inverseTransform([x, y, def(z)]);
38
var choice = this._words[p[1] > 0 ? 0 : 1];
39
this.outputWord(choice.word);
40
this.setRecognizedStroke(choice.stroke);
41
this._words = null;
42
}
43
else if (this._stroke.length > 5) {
44
this._words = this.qw.lookupWord(this._stroke);
45
if (this._words.length == 1) {
46
this.outputWord(this._words[0].word);
47
this.setRecognizedStroke(this._words[0].stroke);
48
this._words = null;
49
}
50
else
51
this.isChoosingWord = true;
52
this._stroke = null;
53
}
54
}
55
this.outputWord = function(word) {
56
if (word.indexOf('DEL') == 0) {
57
if (this._deleteStack.length) {
58
let wordLength = this._deleteStack.pop();
59
for (let i = 0 ; i < wordLength ; i++)
60
sketchPage.handleTextSketchChar('del');
61
}
62
else
63
this._isStartOfSentence = true;
64
}
65
else {
66
for (let i = 0 ; i < word.length ; i++) {
67
let ch = word.substring(i, i+1);
68
if (ch.includes('.') || ch.includes('?') || ch.includes('!'))
69
this._isStartOfSentence = true;
70
else if (i == 1 && this._isStartOfSentence) {
71
ch = ch.toUpperCase();
72
this._isStartOfSentence = false;
73
}
74
if (word === ' i' && ch === 'i')
75
ch = 'I';
76
sketchPage.handleTextSketchChar(ch);
77
}
78
this._deleteStack.push(word.length);
79
}
80
}
81
this.qw = new QW3();
82
this.render = function() {
83
this.duringSketch(function() {
84
mCurve([[-.5,1],[.5,-1],[.5,1]]);
85
mCurve([[.5,1],[-.5,-1]]);
86
});
87
this.afterSketch(function() {
89
color(palette.color[this.colorId]);
90
lineWidth(this.mScale(.007));
91
for (var i = 0 ; i < this.qw.zones.length ; i++) {
92
var z = this.qw.zones[i];
93
mDrawDisk([z[0],z[1]], z[2]);
94
}
95
this.workingChar = '';
96
this.qw.sequenceToColAndRow();
97
textHeight(this.mScale(.22));
98
for (var col = 0 ; col < 9 ; col++)
99
for (var row = 0 ; row < 4 ; row++) {
100
var s = this.qw.rowAndColToChar(row, col);
101
if (s != ' ') {
102
var xy = this.qw.rowAndColToXY(row, col);
103
mText(s, xy, .5, .5);
104
if (col == this.qw.col && row == this.qw.row) {
105
mDrawDisk(xy, .1);
106
this.workingChar = s;
107
}
108
}
109
}
110
s = this.workingChar;
111
textHeight(this.mScale(s.length == 1 ? .3 : .17));
112
color(fadedColor(2 * faded, this.colorId));
113
mText(s.trim(), [0,0], .5,.5);
114
115
if (this._stroke) {
116
color('red');
117
lineWidth(4);
118
mCurve(this._stroke);
119
}
120
121
if (this._words) {
122
let n = this._words.length;
123
for (let i = 0 ; i < n ; i++)
124
mText(this._words[i].word, [0,0], .5, (.5 - i) * n);
125
}
126
127
if (this._recognizedStroke) {
128
color('cyan');
129
mCurve(this._recognizedStroke);
130
}
131
});
132
}
133
}
134