-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject2.pde
428 lines (360 loc) · 11.4 KB
/
project2.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
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
import java.awt.Graphics2D;
import java.awt.Shape;
import java.util.jar.*;
Graphics2D g2;
Shape[] clipStack;
int clipIdx;
DataClass data;
View rootView;
Object viewTarget; // null when overall, Season when season, Episode when episode
Animator viewTotalLines;
CharacterList characters;
Season[] seasons;
color shipMain = #73D689;
color shipDark = #3D7C52;
color shipLight = #A9F597;
color ship2Main = #25684D;
color ship2Dark = #153D39;
color ship2Light = #39A27F;
color shipRed = #9B342D;
color shipRedDark = #4C1819;
final int seasonEpsViewWidth = 600;
final int seasonEpsViewHeight = 100;
final int seasonEpsViewVGap = 8;
final int seasonEpsViewHeightNgram = 60;
final int ngramListH = 200;
final int overallButtonH = 20;
final int ngramViewH = 28 + ngramListH + 10 + overallButtonH;
SeasonEpsView seasonViews[];
Animator seasonY[];
Button overallButton;
PieChart pieChart;
NgramTable ngrams;
HashMap<Character,CharNgramTable> charNgrams;
boolean ngramMode = false;
Animator ngramModeAnimator;
Ngram activeNgram = null;
Character activeNgramChar = null;
NgramView ngramView;
Button ngramButton;
ListBox ngramList;
Button allActiveButton;
StatsView statsView;
Animator statsViewY;
void setupG2D()
{
g2 = ((PGraphicsJava2D)g).g2;
clipStack = new Shape[32];
clipIdx = 0;
}
void setup()
{
listDataSubdir("transcripts");
data=new DataClass("files");
loadCharacters();
loadSeasons();
loadNgrams();
viewTotalLines = new Animator();
ngramModeAnimator = new Animator(0);
size(1024, 768);
setupG2D();
smooth();
background(shipMain);
rootView = new View(0, 0, width, height);
overallButton = new Button(30, overallY(), 140, overallButtonH, "overall", 18, false, "Appearances",true);
overallButton.myFlag = true;
seasonViews = new SeasonEpsView[seasons.length];
seasonY = new Animator[seasons.length];
for (int i = 0; i < seasons.length; i++) {
float y = seasonEpsTop() + (seasonEpsViewHeight + seasonEpsViewVGap)*i;
seasonViews[i] = new SeasonEpsView(30, y, seasonEpsViewWidth, seasonEpsViewHeight, seasons[i]);
rootView.subviews.add(seasonViews[i]);
seasonY[i] = new Animator(y);
}
rootView.subviews.add(overallButton);
ngramView = new NgramView(30, ngramY(), seasonEpsViewWidth, ngramViewH);
rootView.subviews.add(ngramView);
ngramButton = new Button(0, ngramViewH-overallButtonH, 140, overallButtonH, "n-grams", 18, false, "n-grams", true);
ngramView.subviews.add(ngramButton);
ngramList = new ListBox(0, 28, 600, ngramListH, new MissingListDataSource("select a character"));
ngramView.subviews.add(ngramList);
PImage myImage;
Iterator i = characters.iterator();
for (int n=0; n < 8 && i.hasNext(); n++) {
Character character = (Character)i.next();
myImage = character.img;;
assert (character.img != null);
rootView.subviews.add(new CharacterButton(680+(n%4)*(80),90+(20+50)*(n/4),50,50,character));
}
rootView.subviews.add(new ListBox(680,260,300,200, characters));
allActiveButton = new Button(680 ,50, 160, 15, "View All Characters", 18, false, "View All Characters",true);
allActiveButton.myFlag = true;
rootView.subviews.add(allActiveButton);
characters.setAllActive(true);
pieChart=new PieChart(750,500,200,200);
rootView.subviews.add(pieChart);
//rootView.subviews.add(new InteractionChart(750,520,400,500,episodeCharacters,characters));
//uncomment the following two lines to add the interaction chart(basically a chart that has char coded color lines for each dialog he has in the episode)
// ArrayList episodeCharacters=data.getEpisodeCharactersList("S01E01");
// rootView.subviews.add(new InteractionChart(40,150,700,150,episodeCharacters));
statsView = new StatsView(30, height, 600, 400, null);
statsViewY = new Animator(height);
rootView.subviews.add(statsView);
setViewTarget(null);
}
void loadCharacters()
{
characters = new CharacterList("characters.txt");
}
String[] namesMatching(String[] names, String re)
{
int count = 0;
String[] matches = new String[names.length];
for (int i = 0; i < names.length; i++) {
if (match(names[i], re) != null) {
matches[count] = names[i];
count++;
}
}
return (String[]) subset(matches, 0, count);
}
void loadSeasons()
{
String[] names = namesMatching(listDataSubdir("transcripts"), "S(\\d+)");
Arrays.sort(names);
seasons = new Season[names.length];
for (int i = 0; i < names.length; i++) {
String[] groups = match(names[i], "S(\\d+)");
if (groups == null) continue;
seasons[i] = new Season(parseInt(groups[1]), "transcripts/"+names[i]);
}
}
void loadNgrams()
{
ngrams = new NgramTable("ngrams/sign-ngrams.txt");
charNgrams = new HashMap<Character,CharNgramTable>(characters.count());
Iterator it = characters.iterator();
while (it.hasNext()) {
Character c = (Character)it.next();
String path = "ngrams/characters/"+c.name+"-sign-ngrams.txt";
if (dataFileExists(path)) {
CharNgramTable cngt = new CharNgramTable(path);
charNgrams.put(c, cngt);
}
}
}
float seasonViewHeight()
{
return map(ngramModeAnimator.value, 0.0, 1.0, seasonEpsViewHeight, seasonEpsViewHeightNgram);
}
float ngramY()
{
return map(ngramModeAnimator.value, 0.0, 1.0, overallButtonH + 10 - ngramViewH - 1, 0);
// return map(ngramModeAnimator.value, 0.0, 1.0, height - 30, seasonEpsTop + (seasonViewHeight() + seasonEpsViewVGap)*seasons.length);
// return seasonEpsTop + (seasonViewHeight() + seasonEpsViewVGap)*seasons.length;
}
float overallY()
{
return ngramY() + ngramViewH + 10;
}
float seasonEpsTop()
{
if (ngramMode) {
return ngramViewH + 10 + overallButtonH + seasonEpsViewVGap;
} else {
return 30 + 10 + overallButtonH + seasonEpsViewVGap;
}
}
void draw()
{
background(shipMain); /* seems to be needed to actually clear the frame */
Animator.updateAll();
for (int i = 0; i < seasons.length; i++) {
seasonViews[i].y = seasonY[i].value;
seasonViews[i].setHeight(seasonViewHeight());
}
ngramView.y = ngramY();
overallButton.y = overallY();
statsView.y = statsViewY.value;
drawLabels();
//tint(255,255);
noStroke();
rootView.draw();
}
/* I can't believe this is not part of the Processing API! */
void clipRect(int x, int y, int w, int h)
{
g2.clipRect(x, y, w, h);
}
void clipRect(float x, float y, float w, float h)
{
g2.clipRect((int)x, (int)y, (int)w, (int)h);
}
void noClip()
{
g2.setClip(null);
}
void pushClip()
{
clipStack[clipIdx++] = g2.getClip();
}
void popClip()
{
g2.setClip(clipStack[--clipIdx]);
clipStack[clipIdx] = null;
}
void mousePressed()
{
rootView.mousePressed(mouseX, mouseY);
}
void mouseDragged()
{
rootView.mouseDragged(mouseX, mouseY);
}
void mouseClicked()
{
rootView.mouseClicked(mouseX, mouseY);
}
void setViewTarget(Object target)
{
viewTarget = target;
if (target == null) {
viewTotalLines.target(data.getWholeStatsTotal()); // for vivek: total lines over all series
pieChart.updateCharAnimators();
} else if (Season.class.isInstance(target)) {
Season season = (Season)target;
float total=data.getSeasonStatsTotal("S0"+season.number);
viewTotalLines.target(total); // for vivek: total lines over this season
pieChart.updateCharAnimators();
}
}
void retargetSeasonYs()
{
if (!ngramMode && Season.class.isInstance(viewTarget)) {
Season season = (Season)viewTarget;
int idx = season.number - 1;
for (int i = 0; i < idx; i++) {
seasonY[i].target((i-idx)*(seasonEpsViewHeight + seasonEpsViewVGap));
seasonViews[i].button.myFlag = false;
}
seasonY[idx].target(seasonEpsTop());
seasonViews[idx].button.myFlag = true;
statsViewY.target(seasonEpsTop() + seasonEpsViewHeight + seasonEpsViewVGap);
for (int i = idx+1; i < seasons.length; i++) {
seasonY[i].target(height + (i-idx-1)*(seasonEpsViewHeight + seasonEpsViewVGap));
seasonViews[i].button.myFlag = false;
}
} else {
for (int i = 0; i < seasons.length; i++) {
float y = seasonEpsTop() + ((ngramMode ? seasonEpsViewHeightNgram : seasonEpsViewHeight) + seasonEpsViewVGap)*i;
seasonY[i].target(y);
seasonViews[i].button.myFlag = false;
}
statsViewY.target(height);
}
}
void setNgramMode(boolean ngmode)
{
ngramMode = ngmode;
overallButton.myFlag = !ngmode;
ngramButton.myFlag = ngmode;
ngramModeAnimator.target(ngramMode ? 1.0 : 0.0);
retargetSeasonYs();
}
void buttonClicked(Object element)
{
PImage myImage;
if (Character.class.isInstance(element)) {
Character character = (Character)element;
character.setActive(!character.active);
characters.setAllActive(countActive == 0);
updateActiveTotals();
CharNgramTable cng = charNgrams.get(character);
activeNgramChar = character;
ngramList.data = cng != null ? cng : new MissingListDataSource("(no significant n-grams for this character)");
} else if (Season.class.isInstance(element)) {
Season season = (Season)element;
int idx = season.number - 1;
setViewTarget(season);
retargetSeasonYs();
statsView.mySeason = season.number;
} else if (String.class.isInstance(element)) {
if (element.equals("overall")) {
setViewTarget(null);
setNgramMode(false);
} else if (element.equals("n-grams")) {
setNgramMode(true);
} else if (element.equals("View All Characters")){
characters.setAllActive(true);
updateActiveTotals();
}
} else if (CharNgram.class.isInstance(element)) {
CharNgram charNgram = (CharNgram)element;
activeNgram = charNgram.ngram;
}
}
void updateActiveTotals()
{
for (int i = 0; i < seasons.length; i++) {
seasons[i].updateActiveTotals();
}
}
void drawLabels(){
text("Fry", 680+0*(80),142);
text("Bender", 680+1*(80),142);
text("Leela", 680+2*(80),142);
text("Farnsworth", 680+3*(80),142);
text("Zoidberg", 680+0*(80),215);
text("Amy", 680+1*(80),215);
text("Hermes", 680+2*(80),215);
text("Zapp", 680+3*(80),215);
}
String[] listDataSubdir(String subdir)
{
String[] items = null;
/* dataPath does not work for application, either. it's only useful in the IDE */
/* if (sketchPath != null) {
println(dataPath(subdir));
File dir = new File(dataPath(subdir));
items = dir.list();
println(items);
} else */
ClassLoader cl = getClass().getClassLoader();
URL url = cl.getResource("data/characters.txt"); /* just a random file that's known to exist */
if (url == null) { /* running in IDE */
File dir = new File(dataPath(subdir));
items = dir.list();
} else try { /* applet OR application */
JarURLConnection conn = (JarURLConnection)url.openConnection();
JarFile jar = conn.getJarFile();
Enumeration e = jar.entries();
String re = "data/" + subdir + "/(.*)";
/* note that jars don't have directory entries, or at least Processing's don't */
Set<String> itemSet = new LinkedHashSet<String>();
while (e.hasMoreElements()) {
JarEntry entry = (JarEntry)e.nextElement();
String[] groups = match(entry.getName(), re);
if (groups == null) continue;
String[] comps = split(groups[1], "/");
itemSet.add(comps[0]);
}
/*Iterator it = itemSet.iterator();
while (it.hasNext()) {
println(it.next());
}*/
items = (String[])itemSet.toArray(new String[0]);
} catch (IOException e) {
println(e);
}
return items;
}
boolean dataFileExists(String path)
{
InputStream is = createInput(path);
if (is == null) return false;
else {
try { is.close(); }
catch (IOException e) {}
return true;
}
}