-
Notifications
You must be signed in to change notification settings - Fork 7
/
NextWordsBuffer.pde
219 lines (189 loc) · 7.25 KB
/
NextWordsBuffer.pde
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
/*
* This file is part of StenoTutor.
*
* StenoTutor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* StenoTutor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2013 Emanuele Caruso. See LICENSE.txt for details.
*/
// Represents a multi-word buffer containing the next target line.
// It uses lot of fields from StenoTutor class
public class NextWordsBuffer {
// A list of integers containing all the words in the line
ArrayList<Integer> nextWords = new ArrayList<Integer>();
// A list of integers containing all the words in the next line
ArrayList<Integer> nextLineWords = new ArrayList<Integer>();
// Other state variables
int highlightedWordIndex;
int bufferSize;
// Default constructor
NextWordsBuffer(int bufferSize) {
this.bufferSize = bufferSize;
fillNewLine(1);
}
// Go to last item in the list
void goToListEnd() {
highlightedWordIndex = nextWords.size() - 1;
}
// Get current word dictionary index
int getCurrentWordIndex() {
return nextWords.get(highlightedWordIndex);
}
// Get next word dictionary index
int getNextWordIndex() {
highlightedWordIndex++;
if (highlightedWordIndex < nextWords.size()) {
addWordsToNextLine();
return nextWords.get(highlightedWordIndex);
} else {
fillNewLine(nextWords.get(highlightedWordIndex-1));
return getCurrentWordIndex();
}
}
// Tries to add a word to the next line
void addWordsToNextLine() {
if (isSingleWordBuffer) return;
int lastWordIndex;
if (nextLineWords.size() > 0) {
lastWordIndex = nextLineWords.get(nextLineWords.size() - 1);
} else {
lastWordIndex = nextWords.get(nextWords.size() - 1);
}
float usedBufferSize = getLineWidth(nextLineWords);
long[] penaltyLimits = calculatePenaltyLimits();
float partialLineWidth = getLineWidth(nextWords, max(highlightedWordIndex - 1, 0));
while (usedBufferSize < partialLineWidth) {
int nextWordIndex = getNextWordFromPool(lastWordIndex, penaltyLimits);
nextLineWords.add(nextWordIndex);
lastWordIndex = nextWordIndex;
textFont(font, mainTextFontSize);
usedBufferSize += textWidth(dictionary.get(nextWordIndex).word.trim() + " ");
}
// Remove this word because it finishes too far
if (nextLineWords.size() > 0) {
nextLineWords.remove(nextLineWords.size()-1);
}
}
// Get line width
float getLineWidth(ArrayList<Integer> words) {
float result = 0;
for (Integer wordIndex : words) {
result += textWidth(dictionary.get(wordIndex).word.trim() + " ");
}
return result;
}
// Get partial line width
float getLineWidth(ArrayList<Integer> words, int maxWordIndex) {
float result = 0;
for (int i = 0; i < maxWordIndex; i++) {
result += textWidth(dictionary.get(words.get(i)).word.trim() + " ");
}
return result;
}
// Fill a new line
void fillNewLine(int previousWordIndex) {
int lastWordIndex = previousWordIndex;
// Clear word list
nextWords.clear();
// Store the used space
float usedBufferSize = 0;
// Calculate current min and max penalty limits
long[] penaltyLimits = calculatePenaltyLimits();
// If there are words in the next line, first use them
for (Integer wordIndex : nextLineWords) {
nextWords.add(wordIndex);
textFont(font, mainTextFontSize);
usedBufferSize += textWidth(dictionary.get(wordIndex).word.trim() + " ");
lastWordIndex = wordIndex;
}
// Clear the next line, no longer needed
nextLineWords.clear();
// Fill the new line as long as there is space in the buffer
while (usedBufferSize < bufferSize) {
int nextWordIndex = getNextWordFromPool(lastWordIndex, penaltyLimits);
nextWords.add(nextWordIndex);
lastWordIndex = nextWordIndex;
textFont(font, mainTextFontSize);
usedBufferSize += textWidth(dictionary.get(nextWordIndex).word.trim() + " ");
// If only one word is required, break the loop
if(isSingleWordBuffer) break;
}
// Remove this word because it probably finishes off-screen,
// unless it's the only one
if(nextWords.size() > 1) nextWords.remove(nextWords.size()-1);
// Highlight first word
highlightedWordIndex = 0;
}
// Compute the next word. Slow-typed words have more possibilities
// to show up than fast-typed ones
int getNextWordFromPool(int previousWordIndex, long[] penaltyLimits) {
// Create word pool
ArrayList<Integer> wordPool = new ArrayList<Integer>();
// For each unlocked word, if it's not the current one and it
// isn't blacklisted, add it to the pool a number of times,
// based on word penalty.
for (int i = 0; i < startBaseWords + unlockedWords; i++) {
if (i == previousWordIndex || wordsBlacklist.contains(dictionary.get(i).word)) continue;
else {
int penalty = (int) map(wordStats.get(i).getWordPenalty(), penaltyLimits[0], penaltyLimits[1], 1, 100);
for (int j = 0; j < penalty; j++) wordPool.add(i);
}
}
// Fetch a random word from the word pool
return wordPool.get((int) random(0, wordPool.size()));
}
// Calculate current min and max penalty limits
long[] calculatePenaltyLimits() {
long currentMinPenalty = 1000000000;
long currentMaxPenalty = 0;
for (int i = 0; i < min(dictionary.size(), startBaseWords + unlockedWords); i++) {
if (i == currentWordIndex || wordsBlacklist.contains(dictionary.get(i).word)) continue;
long penalty = wordStats.get(i).getWordPenalty();
if (currentMinPenalty > penalty) currentMinPenalty = penalty;
if (currentMaxPenalty < penalty) currentMaxPenalty = penalty;
}
return new long[] {currentMinPenalty, currentMaxPenalty};
}
// Draw target line text
void showText(int x, int y) {
float currentX = x;
textFont(font, mainTextFontSize);
for (int i = 0; i < nextWords.size(); i++) {
int index = nextWords.get(i);
String word = dictionary.get(index).word;
if (i == highlightedWordIndex) {
noFill();
stroke(250, 200, 100);
line(currentX, y + mainTextFontSize / 5, currentX + textWidth(word), y + mainTextFontSize / 5);
fill(250, 200, 100);
}
text(word, currentX, y);
if (i == highlightedWordIndex) fill(isLessonPaused ? 200 : 250);
currentX += textWidth(word + " ");
}
// Draw next line
currentX = x;
for (int i = 0; i < nextLineWords.size(); i++) {
if (nextLineWords.size() < 3) {
fill(25);
} else {
fill(min(250, 25 * (nextLineWords.size() - i)));
}
int index = nextLineWords.get(i);
String word = dictionary.get(index).word;
text(word, currentX, y + mainTextFontSize);
fill(isLessonPaused ? 200 : 250);
currentX += textWidth(word + " ");
}
}
}