Change to list clearing
[SugarCubes.git] / GranimPattern.pde
1 import java.util.LinkedHashMap;
2 class Graphic
3 {
4 public int position = 0;
5 public ArrayList<Integer> graphicBuffer;
6 Graphic()
7 {
8 graphicBuffer = new ArrayList<Integer>();
9 }
10 public int width()
11 {
12 return graphicBuffer.size();
13 }
14
15
16 };
17 class Granim extends Graphic
18 {
19 LinkedHashMap<String,Graphic> displayList;
20
21 Granim()
22 {
23 displayList = new LinkedHashMap<String,Graphic>();
24 }
25 public Graphic addGraphic(String name, Graphic g)
26 {
27 //graphicBuffer.clear();
28 while(width()< g.position+1)
29 {
30 graphicBuffer.add(color(0,0,0));
31 }
32 drawAll();
33 displayList.put(name , g);
34 return g;
35 }
36
37 public Graphic getGraphicByName(String name)
38 {
39 return displayList.get(name);
40 }
41
42 public void update()
43 {
44
45 for(Graphic g : displayList.values())
46 {
47 if(g instanceof Granim)
48 {
49 ((Granim) g).update();
50 }
51 }
52 drawAll();
53
54 }
55 public void drawOne(Graphic g)
56 {
57 graphicBuffer.addAll(g.position,g.graphicBuffer);
58 }
59 public void drawAll()
60 {
61 graphicBuffer.clear();
62 for(Graphic g : displayList.values())
63 {
64 while(width()< g.position + g.width())
65 {
66 graphicBuffer.add(color(0,0,0));
67 }
68 drawOne(g);
69 }
70 }
71 };
72 class GranimPattern extends SCPattern
73 {
74 LinkedHashMap<String,Graphic> displayList;
75
76 GranimPattern(GLucose glucose)
77 {
78 super(glucose);
79 displayList = new LinkedHashMap<String,Graphic>();
80 }
81
82 public Graphic addGraphic(String name, Graphic g)
83 {
84 displayList.put(name,g);
85 return g;
86 }
87
88 public Graphic getGraphicByName(String name)
89 {
90 return displayList.get(name);
91 }
92
93 public void run(int deltaMs)
94 {
95 drawToPointList();
96 }
97
98 public void drawToPointList()
99 {
100 for(Graphic g : displayList.values())
101 {
102 if(g instanceof Granim)
103 {
104 ((Granim) g).update();
105 }
106 List<Point> drawList = model.points.subList(g.position, g.position + g.width());
107
108 for (int i=0; i < drawList.size(); i++)
109 {
110 colors[drawList.get(i).index] = (int) g.graphicBuffer.get(i);
111 }
112 }
113 }
114
115 };
116
117 class RedsGraphic extends Graphic
118 {
119 RedsGraphic()
120 {
121 super();
122 drawit(10);
123 }
124 RedsGraphic(int len)
125 {
126 super();
127 drawit(len);
128
129 }
130 void drawit(int len)
131 {
132 for(int i = 0; i < len ;i++)
133 {
134 graphicBuffer.add(color(0,255,255));
135 }
136 }
137 };
138
139 class RedsGranim extends Granim
140 {
141 RedsGranim()
142 {
143 super();
144 addGraphic("myreds", new RedsGraphic(10));
145 }
146 RedsGranim(int len)
147 {
148 super();
149 addGraphic("myreds", new RedsGraphic(len));
150 }
151 public float count = 0.0;
152 public void update()
153 {
154 Graphic g=getGraphicByName("myreds");
155 g.position = Math.round(sin(count)*20)+100;
156 count+= 0.1;
157 if(count>Math.PI*2)
158 {
159 count=0;
160 }
161 super.update();
162 }
163
164 };
165
166 class RandomsGranim extends Granim
167 {
168 private int _len =0 ;
169 RandomsGranim()
170 {
171 super();
172 _len =100;
173 addGraphic("myrandoms", makeGraphic(_len));
174 }
175 RandomsGranim(int len)
176 {
177 super();
178 _len=len;
179 addGraphic("myrandoms", makeGraphic(len));
180 }
181 public Graphic makeGraphic(int len)
182 {
183 int[] colors= new int[len];
184 for(int i =0;i<len;i++)
185 {
186 colors[i]=(int) Math.round(Math.random()*255);
187 }
188 return new ColorDotsGraphic(colors);
189 }
190 private int count =1;
191 private int instanceCount =0;
192 public void update()
193 {
194 super.update();
195 if(count % 50==0)
196 {
197 instanceCount++;
198 Graphic h=addGraphic("myrandoms_"+instanceCount, makeGraphic(_len));
199 h.position = instanceCount*(_len+100);
200 println("one more " + instanceCount+" at "+h.position);
201 count=0;
202 }
203 count++;
204
205 }
206
207 };
208
209
210 class ColorDotsGraphic extends Graphic
211 {
212 ColorDotsGraphic(int[] colorSequence)
213 {
214 super();
215 for (int colorVal : colorSequence)
216 {
217 graphicBuffer.add(color(colorVal, 255, 255));
218 }
219 }
220 };