Apat and added acos to spherycolor, not included in color yet but working
[SugarCubes.git] / GranimPattern.pde
index c2f9fc0d0aa4d2c043b41526c9959da50d9e141c..da4c78f3955fbb1831ce6fab4532c20989acf3c4 100644 (file)
@@ -1,6 +1,7 @@
 import java.util.LinkedHashMap;
 class Graphic
 {
+       public boolean changed = false;
        public int position  = 0;
        public ArrayList<Integer> graphicBuffer;
        Graphic()
@@ -16,21 +17,21 @@ class Graphic
 };
 class Granim extends Graphic
 {
-       LinkedHashMap<String,Graphic> displayList;
+       HashMap<String,Graphic> displayList;
        
        Granim()
        {
-               displayList = new LinkedHashMap<String,Graphic>();
+               displayList = new HashMap<String,Graphic>();
        }
        public Graphic addGraphic(String name, Graphic g)
        {
-               graphicBuffer.clear();
                while(width()< g.position+1)
                {
-                               graphicBuffer.add(color(0,0,0));
+                               graphicBuffer.add(lx.hsb(0,0,0));
                }
                drawAll();
                displayList.put(name , g);
+               changed =true;
                return g;
        }
 
@@ -41,15 +42,29 @@ class Granim extends Graphic
 
        public void update()
        {
-
+               
                for(Graphic g : displayList.values())
                {
                        if(g instanceof Granim)
                        {
                                ((Granim) g).update();
+                               
+                       }
+                       changed = changed || g.changed;
+                       if(changed)
+                       {
+                               while(width()< g.position + g.width())
+                               {
+                                       graphicBuffer.add(lx.hsb(0,0,0));
+                               }
+                               if(g.changed)
+                               {
+                                       drawOne(g);
+                                       g.changed =false;
+                               }
                        }
                }
-               drawAll();
+               changed = false;
 
        }
        public void drawOne(Graphic g)
@@ -58,25 +73,16 @@ class Granim extends Graphic
        }
        public void drawAll()
        {
-               graphicBuffer.clear();
-               for(Graphic g : displayList.values())
-               {
-                       while(width()< g.position + g.width())
-                       {
-                               graphicBuffer.add(color(0,0,0));
-                       }
-                       drawOne(g);
-               }
        }
 };
 class GranimPattern extends SCPattern
 {
-       LinkedHashMap<String,Graphic> displayList;
+       HashMap<String,Graphic> displayList;
 
        GranimPattern(GLucose glucose)
        {
                super(glucose);
-               displayList = new LinkedHashMap<String,Graphic>();
+               displayList = new HashMap<String,Graphic>();
        }
 
        public Graphic addGraphic(String name, Graphic g)
@@ -90,11 +96,11 @@ class GranimPattern extends SCPattern
                return displayList.get(name);
        }
 
-       public void run(int deltaMs) 
+       public void run(double deltaMs) 
        {
                drawToPointList();
        }
-
+       private Integer[] gbuffer;
        public void drawToPointList()
        {
                for(Graphic g : displayList.values())
@@ -103,12 +109,16 @@ class GranimPattern extends SCPattern
                        {
                                ((Granim) g).update();
                        }
-                       List<Point> drawList = model.points.subList(g.position, g.position + g.width());
-
+                       List<LXPoint> drawList = model.points.subList(Math.min(g.position,colors.length-1), Math.min(g.position + g.width(),colors.length-1));
+                       //println("drawlistsize "+drawList.size());
+                       
+                       gbuffer = g.graphicBuffer.toArray(new Integer[0]);
+                       
                        for (int i=0; i < drawList.size(); i++)
                        {
-                               colors[drawList.get(i).index] = (int) g.graphicBuffer.get(i);
+                               colors[drawList.get(i).index] = gbuffer[i];
                        }
+                       g.changed = false;
                }
        }
 
@@ -131,7 +141,7 @@ class RedsGraphic extends Graphic
        {
                for(int i = 0; i < len ;i++)
                {
-                       graphicBuffer.add(color(0,255,255));
+                       graphicBuffer.add(lx.hsb(0,255,255));
                }
        }
 };
@@ -178,30 +188,35 @@ class RandomsGranim extends Granim
                _len=len;
                addGraphic("myrandoms", makeGraphic(len));
        }
+       int colorLid=0;
        public Graphic makeGraphic(int len)
        {
+
                int[] colors= new int[len]; 
                for(int i =0;i<len;i++)
                {
-                       colors[i]=(int) Math.round(Math.random()*255);
+                       colors[i]=(int) Math.round(Math.random()*80)+colorLid;
+                       
                }
+               colorLid+=4;
                return new ColorDotsGraphic(colors);
        }
        private int count =1;
        private int instanceCount =0;
        public void update()
        {
-               super.update();
-               if(count % 50==0)
+               
+               if(instanceCount<90 && count % 20==0)
                {
                        instanceCount++;
                        Graphic h=addGraphic("myrandoms_"+instanceCount, makeGraphic(_len));
                        h.position = instanceCount*(_len+100);
-                       println("one more " + instanceCount+" at "+h.position);
+                       //println("one more " + instanceCount+" at "+h.position);
                        count=0;
+                       changed = true;
                }
                count++;
-               
+               super.update();
        }
        
 };
@@ -214,7 +229,8 @@ class ColorDotsGraphic extends Graphic
                super();
                for (int colorVal : colorSequence)
                {
-                       graphicBuffer.add(color(colorVal, 255, 255));
+                       graphicBuffer.add(lx.hsb(colorVal, 255, 255));
                }
+               changed = true;
        }
 };