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