Repository navigation
Expand file tree
/
Copy pathcode_swarm.java
More file actions
1648 lines (1452 loc) · 45.8 KB
/
Copy pathcode_swarm.java
File metadata and controls
1648 lines (1452 loc) · 45.8 KB
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2008 Michael Ogawa
*
* This file is part of code_swarm.
*
* code_swarm 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.
*
* code_swarm 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 code_swarm. If not, see <http://www.gnu.org/licenses/>.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.awt.Color;
import javax.vecmath.Vector2f;
import org.codeswarm.dependencies.sun.tools.javac.util.Pair;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;
/**
*
*
*/
public class code_swarm extends PApplet {
/** @remark needed for any serializable class */
public static final long serialVersionUID = 0;
// User-defined variables
int FRAME_RATE = 24;
long UPDATE_DELTA = -1;
String SPRITE_FILE = "particle.png";
String SCREENSHOT_FILE;
int background;
// Data storage
BlockingQueue<FileEvent> eventsQueue;
boolean isInputSorted = false;
protected Map<String, FileNode> nodes;
protected Map<Pair<FileNode, PersonNode>, Edge> edges;
protected Map<String, PersonNode> people;
// Liveness cache
LinkedList<PersonNode> livingPeople = new LinkedList<PersonNode>();
LinkedList<Edge> livingEdges = new LinkedList<Edge>();
LinkedList<FileNode> livingNodes = new LinkedList<FileNode>();
LinkedList<ColorBins> history;
LinkedList<Integer> peopleHistory;
boolean finishedLoading = false;
// Temporary variables
FileEvent currentEvent;
Date nextDate;
Date prevDate;
FileNode prevNode;
int maxTouches;
// Graphics objects
PFont font;
PFont boldFont;
PFont infoFont;
PImage sprite;
// Graphics state variables
boolean looping = true;
boolean coolDown = false;
boolean showHistogram = true;
boolean showActivity = false;
boolean showDate = true;
boolean showLegend = false;
boolean showPopular = false;
boolean showEdges = false;
boolean showEngine = false;
boolean showHelp = false;
boolean takeSnapshots = false;
boolean showDebug = false;
boolean drawNamesSharp = false;
boolean drawNamesHalos = false;
boolean drawFilesSharp = false;
boolean drawFilesFuzzy = false;
boolean drawFilesJelly = false;
//used to ensure that input is sorted when we are told it is
long maximumDateSeenSoFar = 0;
// Color mapper
ColorAssigner colorAssigner;
int currentColor;
// Edge Length
protected static int EDGE_LEN = 25;
// Drawable object life decrement
private int EDGE_LIFE_INIT = 255;
private int FILE_LIFE_INIT = 255;
private int PERSON_LIFE_INIT = 255;
private int EDGE_LIFE_DECREMENT = -1;
private int FILE_LIFE_DECREMENT = -1;
private int PERSON_LIFE_DECREMENT = -1;
private float DEFAULT_NODE_SPEED = 7.0f;
private float DEFAULT_FILE_SPEED = 7.0f;
private float DEFAULT_PERSON_SPEED = 2.0f;
private float FILE_MASS = 1.0f;
private float PERSON_MASS = 10.0f;
private int HIGHLIGHT_PCT = 5;
// Physics engine configuration
String physicsEngineConfigDir;
String physicsEngineSelection;
LinkedList<peConfig> mPhysicsEngineChoices = new LinkedList<peConfig>();
PhysicsEngine mPhysicsEngine = null;
private boolean safeToToggle = false;
private boolean wantToToggle = false;
private boolean toggleDirection = false;
// Default Physics Engine (class) name
static final String PHYSICS_ENGINE_LEGACY = "PhysicsEngineLegacy";
// Formats the date string nicely
DateFormat formatter = DateFormat.getDateInstance();
protected static CodeSwarmConfig cfg;
private long lastDrawDuration = 0;
private String loadingMessage = "Reading input file";
protected static int width=0;
protected static int height=0;
private int maxFramesSaved;
protected int maxBackgroundThreads;
protected ExecutorService backgroundExecutor;
/**
* Used for utility functions
* current members:
* drawPoint: Pass coords and color
* drawLine: Pass coords and color
*/
public static Utils utils = null;
/**
* Initialization
*/
public void setup() {
utils = new Utils();
width=cfg.getIntProperty(CodeSwarmConfig.WIDTH_KEY,640);
if (width <= 0) {
width = 640;
}
height=cfg.getIntProperty(CodeSwarmConfig.HEIGHT_KEY,480);
if (height <= 0) {
height = 480;
}
maxBackgroundThreads=cfg.getIntProperty(CodeSwarmConfig.MAX_THREADS_KEY,4);
if (maxBackgroundThreads <= 0) {
maxBackgroundThreads = 4;
}
backgroundExecutor = new ThreadPoolExecutor(1, maxBackgroundThreads, Long.MAX_VALUE, TimeUnit.NANOSECONDS, new ArrayBlockingQueue<Runnable>(4 * maxBackgroundThreads), new ThreadPoolExecutor.CallerRunsPolicy());
if (cfg.getBooleanProperty(CodeSwarmConfig.USE_OPEN_GL, false)) {
size(width, height, OPENGL);
} else {
size(width, height);
}
if (cfg.getBooleanProperty(CodeSwarmConfig.SHOW_LEGEND, false)) {
showLegend = true;
} else {
showLegend = false;
}
if (cfg.getBooleanProperty(CodeSwarmConfig.SHOW_HISTORY, false)) {
showHistogram = true;
} else {
showHistogram = false;
}
if (cfg.getBooleanProperty(CodeSwarmConfig.SHOW_ACTIVITY, false)) {
showActivity = true;
} else {
showActivity = false;
}
if (cfg.getBooleanProperty(CodeSwarmConfig.SHOW_DATE, false)) {
showDate = true;
} else {
showDate = false;
}
if (cfg.getBooleanProperty(CodeSwarmConfig.SHOW_EDGES, false)) {
showEdges = true;
} else {
showEdges = false;
}
if (cfg.getBooleanProperty(CodeSwarmConfig.SHOW_DEBUG, false)) {
showDebug = true;
} else {
showDebug = false;
}
if (cfg.getBooleanProperty(CodeSwarmConfig.TAKE_SNAPSHOTS_KEY,false)) {
takeSnapshots = true;
} else {
takeSnapshots = false;
}
if (cfg.getBooleanProperty(CodeSwarmConfig.DRAW_NAMES_SHARP, true)) {
drawNamesSharp = true;
} else {
drawNamesSharp = false;
}
if (cfg.getBooleanProperty(CodeSwarmConfig.DRAW_NAMES_HALOS, false)) {
drawNamesHalos = true;
} else {
drawNamesHalos = false;
}
if (cfg.getBooleanProperty(CodeSwarmConfig.DRAW_FILES_SHARP, false)) {
drawFilesSharp = true;
} else {
drawFilesSharp = false;
}
if (cfg.getBooleanProperty(CodeSwarmConfig.DRAW_FILES_FUZZY, true)) {
drawFilesFuzzy = true;
} else {
drawFilesFuzzy = false;
}
if (cfg.getBooleanProperty(CodeSwarmConfig.DRAW_FILES_JELLY, false)) {
drawFilesJelly = true;
} else {
drawFilesJelly = false;
}
background = cfg.getBackground().getRGB();
// Ensure we have sane values.
EDGE_LIFE_INIT = cfg.getIntProperty(CodeSwarmConfig.EDGE_LIFE_KEY,255);
if (EDGE_LIFE_INIT <= 0) {
EDGE_LIFE_INIT = 255;
}
FILE_LIFE_INIT = cfg.getIntProperty(CodeSwarmConfig.FILE_LIFE_KEY,255);
if (FILE_LIFE_INIT <= 0) {
FILE_LIFE_INIT = 255;
}
PERSON_LIFE_INIT = cfg.getIntProperty(CodeSwarmConfig.PERSON_LIFE_KEY,255);
if (PERSON_LIFE_INIT <= 0) {
PERSON_LIFE_INIT = 255;
}
/* enforce decrements < 0 */
EDGE_LIFE_DECREMENT = cfg.getIntProperty(CodeSwarmConfig.EDGE_DECREMENT_KEY,-2);
if (EDGE_LIFE_DECREMENT >= 0) {
EDGE_LIFE_DECREMENT = -2;
}
FILE_LIFE_DECREMENT = cfg.getIntProperty(CodeSwarmConfig.FILE_DECREMENT_KEY,-2);
if (FILE_LIFE_DECREMENT >= 0) {
FILE_LIFE_DECREMENT = -2;
}
PERSON_LIFE_DECREMENT = cfg.getIntProperty(CodeSwarmConfig.PERSON_DECREMENT_KEY,-1);
if (PERSON_LIFE_DECREMENT >= 0) {
PERSON_LIFE_DECREMENT = -1;
}
DEFAULT_NODE_SPEED = cfg.getFloatProperty(CodeSwarmConfig.NODE_SPEED_KEY, 7.0f);
DEFAULT_FILE_SPEED = cfg.getFloatProperty(CodeSwarmConfig.FILE_SPEED_KEY, DEFAULT_NODE_SPEED);
DEFAULT_PERSON_SPEED = cfg.getFloatProperty(CodeSwarmConfig.PERSON_SPEED_KEY, DEFAULT_NODE_SPEED);
FILE_MASS = cfg.getFloatProperty(CodeSwarmConfig.FILE_MASS_KEY,1.0f);
PERSON_MASS = cfg.getFloatProperty(CodeSwarmConfig.PERSON_MASS_KEY,1.0f);
HIGHLIGHT_PCT = cfg.getIntProperty(CodeSwarmConfig.HIGHLIGHT_PCT_KEY,5);
if (HIGHLIGHT_PCT < 0 || HIGHLIGHT_PCT > 100) {
HIGHLIGHT_PCT = 5;
}
UPDATE_DELTA = cfg.getIntProperty(CodeSwarmConfig.MSEC_PER_FRAME_KEY, -1);
if (UPDATE_DELTA == -1) {
int framesperday = cfg.getIntProperty(CodeSwarmConfig.FRAMES_PER_DAY_KEY, 4);
if (framesperday > 0) {
UPDATE_DELTA = (86400000 / framesperday);
}
}
if (UPDATE_DELTA <= 0) {
// Default to 4 frames per day.
UPDATE_DELTA = 21600000;
}
isInputSorted = cfg.getBooleanProperty(CodeSwarmConfig.IS_INPUT_SORTED_KEY, false);
/**
* This section loads config files and calls the setup method for all physics engines.
*/
physicsEngineConfigDir = cfg.getStringProperty( CodeSwarmConfig.PHYSICS_ENGINE_CONF_DIR, "physics_engine");
File f = new File(physicsEngineConfigDir);
String[] configFiles = null;
if ( f.exists() && f.isDirectory() ) {
configFiles = f.list();
}
for (int i=0; configFiles != null && i<configFiles.length; i++) {
if (configFiles[i].endsWith(".config")) {
Properties p = new Properties();
String ConfigPath = physicsEngineConfigDir + System.getProperty("file.separator") + configFiles[i];
try {
p.load(new FileInputStream(ConfigPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
String ClassName = p.getProperty("name", "__DEFAULT__");
if ( ! ClassName.equals("__DEFAULT__")) {
PhysicsEngine pe = getPhysicsEngine(ClassName);
pe.setup(this, p);
peConfig pec = new peConfig(ClassName,pe);
mPhysicsEngineChoices.add(pec);
} else {
System.out.println("Skipping config file '" + ConfigPath + "'. Must specify class name via the 'name' parameter.");
System.exit(1);
}
}
}
if (mPhysicsEngineChoices.size() == 0) {
System.out.println("No physics engine config files found in '" + physicsEngineConfigDir + "'.");
System.exit(1);
}
// Physics engine configuration and instantiation
physicsEngineSelection = cfg.getStringProperty( CodeSwarmConfig.PHYSICS_ENGINE_SELECTION, PHYSICS_ENGINE_LEGACY );
for (peConfig p : mPhysicsEngineChoices) {
if (physicsEngineSelection.equals(p.name)) {
mPhysicsEngine = p.pe;
}
}
if (mPhysicsEngine == null) {
System.out.println("No physics engine matches your choice of '" + physicsEngineSelection + "'. Check '" + physicsEngineConfigDir + "' for options.");
System.exit(1);
}
smooth();
frameRate(FRAME_RATE);
// init data structures
nodes = new HashMap<String,FileNode>();
edges = new HashMap<Pair<FileNode, PersonNode>, Edge>();
people = new HashMap<String,PersonNode>();
history = new LinkedList<ColorBins>();
peopleHistory = new LinkedList<Integer>();
if (isInputSorted) {
//If the input is sorted, we only need to store the next few events
eventsQueue = new ArrayBlockingQueue<FileEvent>(5000);
} else {
//Otherwise we need to store them all at once in a data structure that will sort them
eventsQueue = new PriorityBlockingQueue<FileEvent>();
}
// Init color map
initColors();
loadRepEvents(cfg.getStringProperty(CodeSwarmConfig.INPUT_FILE_KEY)); // event formatted (this is the standard)
while (!finishedLoading && eventsQueue.isEmpty());
prevDate = eventsQueue.peek().date;
SCREENSHOT_FILE = cfg.getStringProperty(CodeSwarmConfig.SNAPSHOT_LOCATION_KEY);
maxFramesSaved = (int) Math.pow(10, SCREENSHOT_FILE.replaceAll("[^#]","").length());
EDGE_LEN = cfg.getIntProperty(CodeSwarmConfig.EDGE_LENGTH_KEY);
if (EDGE_LEN <= 0) {
EDGE_LEN = 25;
}
// Create fonts
String fontName = cfg.getStringProperty(CodeSwarmConfig.FONT_KEY,"SansSerif");
String fontNameBold = cfg.getStringProperty(CodeSwarmConfig.FONT_KEY_BOLD,"SansSerif");
String fontNameInfo = cfg.getStringProperty(CodeSwarmConfig.FONT_KEY_INFO,"SansSerif");
Integer fontSize = cfg.getIntProperty(CodeSwarmConfig.FONT_SIZE, 10);
Integer fontSizeBold = cfg.getIntProperty(CodeSwarmConfig.FONT_SIZE_BOLD, 14);
Integer fontSizeInfo = cfg.getIntProperty(CodeSwarmConfig.FONT_SIZE_INFO, 20);
font = createFont(fontName, fontSize);
boldFont = createFont(fontNameBold, fontSizeBold);
infoFont = createFont(fontNameInfo,fontSizeInfo);
textFont(font);
String SPRITE_FILE = cfg.getStringProperty(CodeSwarmConfig.SPRITE_FILE_KEY);
// Create the file particle image
sprite = loadImage(SPRITE_FILE);
// Add translucency (using itself in this case)
sprite.mask(sprite);
}
/**
* Load a colormap
*/
public void initColors() {
colorAssigner = new ColorAssigner();
int i = 1;
String property;
while ((property = cfg.getColorAssignProperty(i)) != null) {
ColorTest ct = new ColorTest();
ct.loadProperty(property);
colorAssigner.addRule(ct);
i++;
}
// Load the default.
ColorTest ct = new ColorTest();
ct.loadProperty(CodeSwarmConfig.DEFAULT_COLOR_ASSIGN);
colorAssigner.addRule(ct);
}
/**
* Main loop
*/
public void draw() {
long start = System.currentTimeMillis();
background(background); // clear screen with background color
this.update(); // update state to next frame
// Draw edges (for debugging only)
if (showEdges) {
for (Edge edge : edges.values()) {
edge.draw();
}
}
// Surround names with aura
// Then blur it
if (drawNamesHalos) {
drawPeopleNodesBlur();
}
// Then draw names again, but sharp
if (drawNamesSharp) {
drawPeopleNodesSharp();
}
// Draw file particles
for (FileNode node : livingNodes) {
node.draw();
}
textFont(font);
// Show the physics engine name
if (showEngine) {
drawEngine();
}
// help, legend and debug information are exclusive
if (showHelp) {
// help override legend and debug information
drawHelp();
}
else if (showDebug) {
// debug override legend information
drawDebugData();
}
else if (showLegend) {
// legend only if nothing "more important"
drawLegend();
}
if (showPopular) {
drawPopular();
}
if (showHistogram) {
drawHistory();
}
if (showActivity) {
drawPeopleHistory();
}
if (showDate) {
drawDate();
}
if (takeSnapshots) {
dumpFrame();
}
// Stop animation when we run out of data AND all nodes are dead
if (eventsQueue.isEmpty()) {
coolDown = true;
if ( !isThereLife() ) {
// noLoop();
backgroundExecutor.shutdown();
try {
backgroundExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
} catch (InterruptedException e) { /* Do nothing, just exit */}
exit();
}
}
long end = System.currentTimeMillis();
lastDrawDuration = end - start;
}
/**
* The Physics engines may need access to the nodes
* in calls other than onUpdate*() and onRelax*()
*/
public LinkedList<PersonNode> getLivingPeople() {
return livingPeople;
}
public LinkedList<Edge> getLivingEdges() {
return livingEdges;
}
public LinkedList<FileNode> getLivingNodes() {
return livingNodes;
}
/**
* Surround names with aura
*/
public void drawPeopleNodesBlur() {
colorMode(HSB);
// First draw the name
for (PersonNode p : livingPeople) {
fill(hue(p.flavor), 64, 255, p.life);
p.draw();
}
// Then blur it
filter(BLUR, 3);
}
/**
* Draw person's name
*/
public void drawPeopleNodesSharp() {
colorMode(RGB);
for (PersonNode p : livingPeople) {
fill(lerpColor(p.flavor, color(255), 0.5f), max(p.life - 50, 0));
p.draw();
}
}
/**
* Draw date in lower-right corner
*/
public void drawDate() {
fill(255);
String dateText = formatter.format(prevDate);
textFont(infoFont);
textAlign(RIGHT, BASELINE);
textSize(infoFont.size);
if (coolDown)
dateText = "End of history: " + dateText;
text(dateText, width - 1 - 10, height - textDescent() - 10);
}
/**
* Draw histogram in lower-left
*/
public void drawHistory() {
int x = 0;
int heightMinusThree = height - 3;
rectMode(CORNERS);
for (ColorBins cb : history) {
if (cb.totalCount > 0) {
int startY = 0;
int endY = 0;
for (int k = 0; k < cb.keyCount; ++k) {
int color = cb.keys[k];
endY += cb.colorMap.get(color);
stroke(color, 255); // 200 (nicer) vs. 255 (faster)
rect(x, heightMinusThree - startY, x, heightMinusThree - endY);
startY = endY;
}
}
x++;
}
}
/**
* Draw active committers at bottom
*/
public void drawPeopleHistory() {
int x = width / 2 - peopleHistory.size() / 2 ;
int heightMinusThree = height - 3;
rectMode(CORNERS);
for (int people : peopleHistory) {
// clamp people to 1 to 500 as a reasonable interval for most projects
// ln x where x is 1 to 500
// ln 1 = 0
// ln 500 = 6.215
//
// Graph height wanted: 20 to 250
// 230 / 6.215 = 37.01
// 20 + ln x * 37.01
final int GRAPH_HEIGHT_MIN = 10;
final int GRAPH_HEIGHT_MAX = 70;
final int MIN_PEOPLE = 1;
final int MAX_PEOPLE = 500;
final double LOG_SCALE = ( GRAPH_HEIGHT_MAX - GRAPH_HEIGHT_MIN ) / (double)Math.log( MAX_PEOPLE );
int peopleNormalized = max( MIN_PEOPLE, people, min( people, MAX_PEOPLE ) );
int heightLog = GRAPH_HEIGHT_MIN * max( min( 1, people ), 0 ) + (int)( Math.log( peopleNormalized ) * LOG_SCALE );
//System.out.print( "people: " + people + " norm people: " + peopleNormalized + " heightLog " + heightLog + "\n" );
int startY = heightMinusThree - heightLog;
int endY = heightMinusThree;
int color = new Color( 128, 128, 128 ).getRGB();
stroke(color, 255); // 200 (nicer) vs. 255 (faster)
rect(x, startY, x, endY);
x++;
}
}
/**
* Show the Loading screen.
*/
public void drawLoading() {
noStroke();
textFont(font, 20);
textAlign(LEFT, TOP);
fill(255, 200);
text(loadingMessage, 0, 0);
}
/**
* Show color codings
*/
public void drawLegend() {
noStroke();
textFont(infoFont);
textAlign(LEFT, TOP);
fill(255, 200);
text("Legend:", 10, 10);
for (int i = 0; i < colorAssigner.tests.size(); i++) {
ColorTest t = colorAssigner.tests.get(i);
fill(t.c1, 200);
text(t.label, 10 + infoFont.size, (i + 1) * infoFont.size + 10);
}
}
/**
* Show physics engine name
*/
public void drawEngine() {
fill(255);
textAlign(RIGHT, BASELINE);
textSize(10);
text(physicsEngineSelection, width-1, height - (textDescent() * 5));
}
/**
* Show short help on available commands
*/
public void drawHelp() {
int line = 0;
noStroke();
textFont(font);
textAlign(LEFT, TOP);
fill(255, 200);
text("Help on keyboard commands:", 0, 10*line++);
text("space bar : pause", 0, 10*line++);
text(" a : show name halos", 0, 10*line++);
text(" b : show debug", 0, 10*line++);
text(" d : show date", 0, 10*line++);
text(" e : show edges", 0, 10*line++);
text(" E : show physics engine name", 0, 10*line++);
text(" f : draw files fuzzy", 0, 10*line++);
text(" h : show histogram", 0, 10*line++);
text(" j : draw files jelly", 0, 10*line++);
text(" l : show legend", 0, 10*line++);
text(" p : show popular", 0, 10*line++);
text(" q : quit code_swarm", 0, 10*line++);
text(" s : draw names sharp", 0, 10*line++);
text(" S : draw files sharp", 0, 10*line++);
text(" minus : previous physics engine", 0, 10*line++);
text(" plus : next physics engine", 0, 10*line++);
text(" ? : show help", 0, 10*line++);
}
/**
* Show debug information about all drawable objects
*/
public void drawDebugData() {
noStroke();
textFont(font);
textAlign(LEFT, TOP);
fill(255, 200);
text("Nodes: " + nodes.size(), 0, 0);
text("People: " + people.size(), 0, 10);
text("Queue: " + eventsQueue.size(), 0, 20);
text("Last render time: " + lastDrawDuration, 0, 30);
}
/**
* TODO This could be made to look a lot better.
*/
public void drawPopular() {
CopyOnWriteArrayList <FileNode> al=new CopyOnWriteArrayList<FileNode>();
noStroke();
textFont(font);
textAlign(RIGHT, TOP);
fill(255, 200);
text("Popular Nodes (touches):", width-120, 0);
for (FileNode fn : nodes.values()) {
if (fn.qualifies()) {
// Insertion Sort
if (al.size() > 0) {
int j = 0;
for (; j < al.size(); j++) {
if (fn.compareTo(al.get(j)) <= 0) {
continue;
} else {
break;
}
}
al.add(j,fn);
} else {
al.add(fn);
}
}
}
int i = 1;
ListIterator<FileNode> it = al.listIterator();
while (it.hasNext()) {
FileNode n = it.next();
// Limit to the top 10.
if (i <= 10) {
text(n.name + " (" + n.touches + ")", width-100, 10 * i++);
} else if (i > 10) {
break;
}
}
}
/**
* @param name
* @return physics engine instance
*/
@SuppressWarnings("unchecked")
public PhysicsEngine getPhysicsEngine(String name) {
PhysicsEngine pe = null;
try {
Class<PhysicsEngine> c = (Class<PhysicsEngine>)Class.forName(name);
Constructor<PhysicsEngine> peConstructor = c.getConstructor();
pe = peConstructor.newInstance();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
return pe;
}
/**
* Take screenshot
*/
public void dumpFrame() {
if (frameCount < maxFramesSaved) {
final String outputFileName = insertFrame(SCREENSHOT_FILE);
final PImage image = get();
backgroundExecutor.execute(new Runnable() {
public void run() {
image.save(new File(outputFileName).getAbsolutePath());
}
});
// saveFrame(SCREENSHOT_FILE);
}
}
/**
* Update the particle positions
*/
public void update() {
// Create a new histogram line
ColorBins cb = new ColorBins();
history.add(cb);
// Create a new people histogram line
peopleHistory.add(new Integer(livingPeople.size()));
nextDate = new Date(prevDate.getTime() + UPDATE_DELTA);
currentEvent = eventsQueue.peek();
while (currentEvent != null && currentEvent.date.before(nextDate)) {
if (finishedLoading) {
currentEvent = eventsQueue.poll();
if (currentEvent == null)
return;
}
else {
try {
currentEvent = eventsQueue.take();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("Interrupted while fetching current event from eventsQueue");
e.printStackTrace();
continue;
}
}
FileNode n = findNode(currentEvent.path + currentEvent.filename);
if (n == null) {
n = new FileNode(currentEvent);
nodes.put(currentEvent.path + currentEvent.filename, n);
livingNodes.addLast(n);
} else {
if (!n.isAlive())
livingNodes.addLast(n);
n.freshen();
}
// add to histogram
cb.add(n.nodeHue);
PersonNode p = findPerson(currentEvent.author);
if (p == null) {
p = new PersonNode(currentEvent.author);
people.put(currentEvent.author, p);
livingPeople.addLast(p);
} else {
if (!p.isAlive())
livingPeople.addLast(p);
p.freshen();
}
p.addColor(n.nodeHue);
Edge ped = findEdge(n, p);
if (ped == null) {
ped = new Edge(n, p);
edges.put(new Pair<FileNode,PersonNode>(n,p), ped);
livingEdges.addLast(ped);
} else {
if (!ped.isAlive())
livingEdges.addLast(ped);
ped.freshen();
}
/*
* if ( currentEvent.date.equals( prevDate ) ) { Edge e = findEdge( n, prevNode
* ); if ( e == null ) { e = new Edge( n, prevNode ); edges.add( e ); } else {
* e.freshen(); } }
*/
// prevDate = currentEvent.date;
prevNode = n;
if (finishedLoading)
currentEvent = eventsQueue.peek();
else {
while (eventsQueue.isEmpty());
currentEvent = eventsQueue.peek();
}
}
if ( !coolDown )
prevDate = nextDate;
// sort colorbins
cb.sort();
// restrict history to drawable area
while (history.size() > 320)
history.remove();
// restrict people history to drawable area
while (peopleHistory.size() > 200)
peopleHistory.remove();
// Do not allow toggle Physics Engine yet.
safeToToggle = false;
// Init frame:
mPhysicsEngine.initializeFrame();
// update velocity
livingEdges = mPhysicsEngine.onRelaxEdges(livingEdges);
// update velocity
livingNodes = mPhysicsEngine.onRelaxNodes(livingNodes);
// update velocity
livingPeople = mPhysicsEngine.onRelaxPeople(livingPeople);
// update position
livingEdges = mPhysicsEngine.onUpdateEdges(livingEdges);
// update position
livingNodes = mPhysicsEngine.onUpdateNodes(livingNodes);
// update position
livingPeople = mPhysicsEngine.onUpdatePeople(livingPeople);
// Finalize frame:
mPhysicsEngine.finalizeFrame();
safeToToggle = true;
if (wantToToggle == true) {
switchPhysicsEngine(toggleDirection);
}
}
/**
* Checks the node list for signs of life.
* @return Does life exist?
*/
public boolean isThereLife() {
return !livingNodes.isEmpty();
}
/**
* Searches the nodes array for a given name
* @param name
* @return FileNode with matching name or null if not found.
*/
public FileNode findNode(String name) {
return nodes.get(name);
}
/**
* Searches the nodes array for a given name
* @param n1 From
* @param n2 To
* @return Edge connecting n1 to n2 or null if not found
*/
public Edge findEdge(FileNode n1, PersonNode n2) {
return edges.get(new Pair<FileNode, PersonNode>(n1,n2));
}
/**
* Searches the people array for a given name.
* @param name
* @return PersonNode for given name or null if not found.
*/
public PersonNode findPerson(String name) {
return people.get(name);
}
/**
* Load the standard event-formatted file.
* @param filename
*/
public void loadRepEvents(String filename) {
final String fullFilename = filename;
Runnable eventLoader = new XMLQueueLoader(fullFilename, eventsQueue, isInputSorted);