-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimatedRenderer.pde
166 lines (146 loc) · 3.88 KB
/
AnimatedRenderer.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
class AnimatedRenderer implements Drawable {
private SortingMachine m;
private int pc = -1;
private List<InstructionDrawable> instructionDrawables;
private ArrayDrawable arrayDrawable;
private List<ArrayDrawable> snapshots;
private List<Instruction> program;
Layout sheetLayout;
private AtomicBoolean sendInstruction = new AtomicBoolean(false);
AnimatedRenderer(SortingMachine m) {
this.m = m;
new ScheduledThreadPoolExecutor(1).scheduleAtFixedRate(
new Runnable() {
public void run() {
sendInstruction.set(true);
}
}, 500L, 500L, TimeUnit.MILLISECONDS);
}
public void setup() {
System.out.println("configuring standard view");
m.setup();
size(m.getWindow().getWidth(), m.getWindow().getHeight());
sheetLayout = new Layout(m.getWindow().getLeftMargin(), m.getWindow().getTopMargin(), width/2, height);
float cursorX = 0f, cursorY = 0f;
SortJob job = m.getSortJobs().get(0);
DataSet data = job.getData();
program = job.getInstructions();
layoutSheetMusic(data.getValues());
arrayDrawable =
new ArrayDrawable(
data.getValues(), // values
width/2f, 130,
10, 10, 10, 10, color(0, 0, 0, 30)
);
arrayDrawable.place(
new Point(
width/2f, // x
m.getWindow().getTopMargin() // y
));
arrayDrawable.setup();
takeSnapshot();
// throwaway debug thing that makes sure that the sorting instructions work
int[] a = copy(data.getValues());
println("before -->\n" + join(a, ", ") + "\n");
for(Instruction i : program) {
i.exec(a);
}
println("after -->\n" + join(a, ", ") + "\n");
}
public void layoutSheetMusic(int[] a) {
instructionDrawables = Lists.newArrayListWithCapacity(program.size());
for(Instruction instruction : program) {
InstructionDrawable d =
new InstructionDrawable(
instruction,
m.getNoteHeight(), m.getStepWidth(),
a
);
d.setup();
instructionDrawables.add(d);
sheetLayout.place(d);
}
}
private void takeSnapshot() {
if(snapshots == null) {
snapshots = Lists.newArrayList();
}
ArrayDrawable s = arrayDrawable.clone();
s.setup();
s.setColor(0, 0, 0, 0);
snapshots.add(s);
}
public void tick() {
if(sendInstruction.get()) {
nextInstruction();
sendInstruction.set(false);
}
for(int i = 0; i < instructionDrawables.size(); i++) {
InstructionDrawable d = instructionDrawables.get(i);
d.tick();
// set 'executing' flag if necessary
d.setExecuting(i == pc);
}
arrayDrawable.tick();
layoutSnapshots();
for(Drawable s : snapshots) {
s.tick();
}
}
public void draw() {
background(255,255,255);
for(int i = 0; i < instructionDrawables.size(); i++) {
Drawable d = instructionDrawables.get(i);
d.draw();
}
arrayDrawable.draw();
for(ArrayDrawable s : snapshots) {
s.draw();
}
/*saveFrame("/tmp/sorting-machine/b-####.tif");*/
}
private void nextInstruction() {
pc++;
if(pc > program.size() - 1) {
pc = program.size() - 1;
//noLoop();
return;
}
Instruction i = program.get(pc);
arrayDrawable.execute(i);
// record the state of the array after the swap
if(i.swap()) {
takeSnapshot();
}
}
private void layoutSnapshots() {
Layout snapshotLayout =
new Layout(
width/2f,
arrayDrawable.x.value + arrayDrawable.getHeight(),
width,
0f
);
float visibleWidth = width/2f - m.getWindow().getLeftMargin() - m.getWindow().getRightMargin();
float sx = width/2f;
float sy = arrayDrawable.y.target + arrayDrawable.dheight.target;
float swidth = visibleWidth/7f;
float sheight = swidth * 7f / 16f;
for(ArrayDrawable d : snapshots) {
d.reposition(sx, sy, swidth, sheight, 8f, 8f, 8f, 8f);
d.targetColor(0, 0, 0, 100);
sx += swidth;
if((sx+swidth) > (width-m.getWindow().getRightMargin())) {
sx = width/2f;
sy += sheight;
}
}
}
public void place(Point p) { }
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
}