Add hue cycle and rotation buttons
[SugarCubes.git] / DanHorwitz.pde
index ca829ee6e893c31abe9514d432e09f9b557bbb99..856c579a2e8ac7c7a14ce809b378abea3e4980d9 100644 (file)
@@ -134,7 +134,7 @@ public class Noise extends DPat
 
                        b +=    n.den/100 -.4 + pDensity.Val() -1;
                        b +=    transAdd;
-                       c =     blendColor(c,lx.hsb(n.hue,100*n.sat,c1c(b)),ADD);
+                       c =     blendColor(c,color(n.hue,100*n.sat,c1c(b)),ADD);
                }
                return c;
        }
@@ -184,9 +184,9 @@ public class Play extends DPat
            pRadius             = addParam("Rad"        , .1    );
                pBounce         = addParam("Bnc"        , .2    );
            pAmp                = addParam("Amp"        , .2    );
-               pTempoMult      = addPick ("TMult"      , 0 , 5         , new String[] {"1x", "2x", "4x", "8x", "16x", "Rand"   }       );
-               pTimePattern= addPick ("TPat"   , 6 , 7         , new String[] {"Bounce", "Sin", "Roll", "Quant", "Accel", "Deccel", "Slide", "Rand"}   );
-               pShape          = addPick ("Shape"      , 3 , 15        , new String[] {"Line", "Tap", "V", "RandV",
+               pTempoMult      = addPick ("TMult"      , 5 , 5         , new String[] {"1x", "2x", "4x", "8x", "16x", "Rand"   }       );
+               pTimePattern= addPick ("TPat"   , 7 , 7         , new String[] {"Bounce", "Sin", "Roll", "Quant", "Accel", "Deccel", "Slide", "Rand"}   );
+               pShape          = addPick ("Shape"      , 7 , 15        , new String[] {"Line", "Tap", "V", "RandV",
                                                                                                                                        "Pyramid", "Wings", "W2", "Clock",
                                                                                                                                        "Triangle", "Quad", "Sphere", "Cone",
                                                                                                                                        "Noise", "Wave", "?", "?"}                                              );
@@ -354,218 +354,194 @@ public class Play extends DPat
 // 12- TRB, B (r), BRB, R (b)          // Back, Down, Fwd , Up
 // 1->7, 15->9
 
-class dBolt {
-       dStrip v, h;
-       int    vpos, hpos;
-       dBolt(dStrip _v, dStrip _h, int _vpos, int _hpos) {
-               v=_v; h=_h; vpos=_vpos; hpos=_hpos;
-               if (v.b0 == null) { v.b0=this; h.b0=this; } 
-               else                      { v.b1=this; h.b1=this; }
-       }
-}
-
-class dVertex {
-       dStrip  s1      , s2    ;
-       int     dir1, dir2      ;
-       dVertex(dStrip s, int d) {
-               int _a = (s.iS%4==1)? (d==1? 5: 3) :
-                                (s.iS%4==3)? (d==1? 9:11) :
-                                (d==1)         ? (s.Top()?4:12)   : (s.Top()?12:4);
-               dir1 = d * (s.isVert() ? -1 : 1);
-               dir2 = d;
-               s1       = DL_.DS[s.iCube() + ((s.iS+_a) % 16)];
-               s2       = DL_.DS[d == 1 ?      (s.idx == s.iFace()+3 ? s.idx-3 : s.idx+1):
-                                                               (s.idx == s.iFace()   ? s.idx+3 : s.idx-1)];
-               swapout(1 , 6);
-               swapout(15,-6);
-       }
-       void swapout(int a, int b) {
-               if (s1.iS == a) { s1 = DL_.DS[s1.idx + b]; dir1 = -dir1; }
-               if (s2.iS == a) { s2 = DL_.DS[s2.idx + b]; dir2 = -dir2; }
-       }
-
-}
+int randDir() { return round(random(1))*2-1; }
+//----------------------------------------------------------------------------------------------------------------------------------
+boolean dDebug = true;
+class dCursor {
+       dVertex vCur, vNext, vDest;
+       float   destSpeed;
+       int     posStop, pos,posNext;   // 0 - 65535
+       color   clr;
 
-class dStrip  { // THIS WAS SUCH A PAIN!
-       int row, col, ci, idx, iS, axis;        // 1-y, 2-left, 3-right
-       Strip s; 
-       boolean bTop;   // direction: top ccw, bottom cw.
-
-       boolean Top   (){       return axis!=1 &&  bTop ;               }
-       boolean Bottom(){       return axis!=1 && !bTop;                }
-       boolean isVert(){       return axis==1;                                 }
-       boolean isHorz(){       return axis!=1;                                 }
-       int     iCube (){       return 16*floor(idx/16);                }
-       int     iFace (){       return iCube() + 4*floor(iS/4); }
-
-       void    init(Strip _s, int _i, int _row, int _col)  {
-               idx = _i; row = _row; col = _col; s = _s;
-               iS      = idx%16; bTop = (iS%4==0);
-               ci  = s.points.get(0).index;
-               DL_.DQ[col][row] = iCube();
-               switch (iS) {
-                       case 4: case 6 : case 12: case 14:      axis=2; break;
-                       case 0: case 2 : case 8 : case 10:      axis=3; break;
-                       default:                                                        axis=1; break;
+       dCursor() {}
+
+       boolean isDone  ()                                                                      { return pos==posStop;                                                                           }
+       boolean atDest  ()                                                                      { return vCur.s==vDest.s || 
+                                                                                                                                PointDist(vCur.getPoint(0), vDest.getPoint(0)) < 12 || 
+                                                                                                                                PointDist(vCur.getPoint(0), vDest.getPoint(15))< 12;}
+       void    setCur  (dVertex _v, int _p)                            { p2=null; vCur=_v; pos=_p; PickNext();                                          }
+       void    setCur  (dPixel  _p)                                            { setCur(_p.v, _p.pos);                                                                          }
+       void    setNext (dVertex _v, int _p, int _s)            { vNext = _v; posNext = _p<<12; posStop = _s<<12;                        }
+       void    setDest (dVertex _v, float _speed)                      { vDest = _v; destSpeed = _speed;                                                        }
+       void    onDone  ()                                                                      { setCur(vNext, posNext); PickNext();                                            }
+
+       float   minDist;
+       int     nTurns;
+       boolean bRandEval;
+
+       void    Evaluate(dVertex v, int p, int s) {
+               if (v == null) return; ++nTurns;
+               if (bRandEval) {
+                       if (random(nTurns) < 1) setNext(v,p,s); return; }
+               else {
+                       float d = PointDist(v.getPoint(15), vDest.getPoint(0));
+                       if (d <  minDist)                                       { minDist=d; setNext(v,p,s); }
+                       if (d == minDist && random(2)<1)        { minDist=d; setNext(v,p,s); }
                }
        }
 
-       void addBolts() {
-               v0 = new dVertex(this, 1);
-               v1 = new dVertex(this,-1);
+       void    EvalTurn(dTurn t) { 
+               if (t == null || t.pos0<<12 <= pos) return; 
+               Evaluate(t.v    ,    t.pos1, t.pos0);
+               Evaluate(t.v.opp, 16-t.pos1, t.pos0);
+       }
 
+       void    PickNext()      {
+               bRandEval = random(.05+destSpeed) < .05; minDist=500; nTurns=0;
+               Evaluate(vCur.c0, 0, 16);       Evaluate(vCur.c1, 0, 16);
+               EvalTurn(vCur.t0);                      EvalTurn(vCur.t1);
+       }
 
-               if (iS == 7 && col != 0 && row != 0)                                                                    // left bottom
-                       new dBolt(this, DL_.GetStrip(row-1,col-1,(col % 2 == 1) ? 8 : 12),  
-                                                       4, (col % 2 == 1) ? 6 : 9);     
+       Point   p1, p2; int i2;
 
-               if (iS == 7 && col != 0 && row < MaxCubeHeight*2-2)                                     // left top
-                       new dBolt(this, DL_.GetStrip(row+1,col-1,(col % 2 == 1) ? 10 : 14), 
-                                                       11, (col % 2 == 1) ? 9 : 6);
+       int draw(int nAmount, SCPattern pat) {
+               int nFrom       = (pos    ) >> 12;
+               int     nMv     = min(nAmount, posStop-pos);
+               int     nTo     = min(15,(pos+nMv) >> 12);
+               dVertex v       = vCur;
 
-               if (iS == 9 && col < NumBackTowers-1 && row < MaxCubeHeight*2-2)                // right top
-                       new dBolt(this, DL_.GetStrip(row+1,col+1,(col % 2 == 1) ? 6 : 2), 
-                                                       4, (col % 2 == 1) ? 6 : 9);
+               if (dDebug) {   p1 = v.getPoint(nFrom); float d = (p2 == null ? 0 : PointDist(p1,p2)); if (d>5) { println("too wide! quitting: " + d); exit(); }}
+                                                               for (int i = nFrom; i <= nTo; i++) { pat.getColors()[v.ci          + v.dir*i     ] = clr; }
+               if (v.same != null)             for (int i = nFrom; i <= nTo; i++) { pat.getColors()[v.same.ci + v.same.dir*i] = clr; }
 
-               if (iS == 9 && col < NumBackTowers-1 && row != 0)                                               // right bottom
-                       new dBolt(this, DL_.GetStrip(row-1,col+1,(col % 2 == 1) ? 4 : 0), 
-                                                       11, (col % 2 == 1) ? 9 : 6);
-       }
+               if (dDebug) {   p2 = v.getPoint(nTo); i2 = nTo; }
 
-       dBolt   b0, b1;
-       dVertex v0, v1;
+               pos += nMv; return nAmount - nMv;
+       }       
 }
 
-class dCursor {
-       dStrip  s, sNext;
-       int     nLast,pos,posNext,end;  // 0 - 65535
-       int     dir;                    // 1 or -1
-       color   clr;
-       
-       dCursor(color _c) { clr=_c;}
-
-       boolean isDone()                                        { return pos==end; }
-       void    set(dStrip _s, int _dir)        { 
-                       s=_s; dir=_dir; pos = 0; end=65536; nLast=-1; sNext=null;
+//----------------------------------------------------------------------------------------------------------------------------------
+class Worms extends SCPattern {
+       float   StripsPerSec    = 10;
+       float   TrailTime               = 3000;
+       int     numCursors              = 50;
+       ArrayList<dCursor> cur  = new ArrayList<dCursor>(30);
+
+       private GraphicEQ eq = null;
+
+       private BasicParameter pBeat      = new BasicParameter("BEAT",  0);
+       private BasicParameter pSpeed     = new BasicParameter("FAST", .2);
+       private BasicParameter pBlur      = new BasicParameter("BLUR", .3);
+       private BasicParameter pWorms     = new BasicParameter("WRMS", .3);
+       private BasicParameter pConfusion = new BasicParameter("CONF", .1);
+       private BasicParameter pEQ        = new BasicParameter("EQ"  ,  0);
+       private BasicParameter pSpawn     = new BasicParameter("DIR" ,  0);
+
+       int     zMidLat = 82;
+       float   nConfusion;
+       private final Click moveChase = new Click(1000);
+
+       xyz     middle;
+       int     AnimNum() { return floor(pSpawn.getValuef()*(4-.01)); }
+       float   randX() { return random(model.xMax-model.xMin)+model.xMin; }
+       float   randY() { return random(model.yMax-model.yMin)+model.yMin; }
+       xyz     randEdge() { 
+               return random(2) < 1 ?  new xyz(random(2)<1 ? model.xMin:model.xMax, randY(), zMidLat)  :
+                                                               new xyz(randX(), random(2)<1 ? model.yMin:model.yMax, zMidLat)  ;
        }
 
-       boolean MakeTurn(dBolt b) {
-               int nEnd=       (s.isVert() ? b.vpos : b.hpos) <<12;
-                       nEnd=   (dir==1 ? nEnd : 65536-nEnd);
-               if (nEnd < pos) return false;
-               if (s.isVert()) { sNext = b.h; posNext = b.hpos<<12; end = nEnd; }
-               else                    { sNext = b.v; posNext = b.vpos<<12; end = nEnd; }
-               return true;
+       Worms(GLucose glucose) {
+               super(glucose); 
+           addModulator(moveChase).start();
+           addParameter(pBeat);    addParameter(pSpeed);
+           addParameter(pBlur);    addParameter(pWorms);
+           addParameter(pEQ);      addParameter(pConfusion);
+               addParameter(pSpawn);
+           middle = new xyz(model.cx, model.cy, 71);
+               if (lattice == null) lattice = new dLattice();
+               for (int i=0; i<numCursors; i++) { dCursor c = new dCursor(); reset(c); cur.add(c); }
+               onParameterChanged(pEQ); setNewDest();
        }
 
-       void    PickNext()      {
-               if (sNext != null) {
-                       if (end == 65536) exit();
-                       end                     = 65536;        
-                       pos                     = posNext;
-                       dir                     = randDir(); 
-                       if (dir<0) pos = end-pos;
-                       s                       = sNext; sNext = null;
-                       nLast           = -1;
-                       return;// could switch again!!
-               } else {
-                       dVertex v = (dir == 1 ? s.v0 : s.v1);
-                       int r = floor(random(2));
-                       set(r==0 ? v.s1 : v.s2,r==0 ? v.dir1 : v.dir2);
+       void onParameterChanged(LXParameter parameter) {
+               nConfusion = 1-pConfusion.getValuef();
+               for (int i=0; i<numCursors; i++) {
+                       if (parameter==pSpawn) reset(cur.get(i));
+                       cur.get(i).destSpeed = nConfusion;
                }
-
-               // plan to turn the corner
-               if (random(6)<1 && s.b0 != null && MakeTurn(s.b0)) return;
-               if (random(6)<1 && s.b1 != null && MakeTurn(s.b1)) return;
        }
-}
 
-int randDir() { return round(random(1))*2-1; }
+       void reset(dCursor c) {
+               switch(AnimNum()) {
+                       case 0: c.clr = lx.hsb(135,100,100);                    // middle to edges
+                                       c.setDest(lattice.getClosest(randEdge()).v, nConfusion);
+                                       c.setCur (lattice.getClosest(middle));
+                                       break;
 
-class dLattice {
-       int             iTowerStrips=-1;
-       dStrip[]        DS = new dStrip[glucose.model.strips.size()];
-       int[][]         DQ = new int[NumBackTowers][MaxCubeHeight*2];
-
-       int             nStrips() { return iTowerStrips; }
-       dStrip GetStrip (int row, int col, int off) { return DS[DQ[col][row]+off]; }
-       dLattice() {
-               DL_=this;
-               int   col = 0, row = -2, i=-1;
-               for (Strip strip : glucose.model.strips  ) { i++; 
-                       if (i % 16 == 0) row+=2;
-                       if (row >= MaxCubeHeight*2-1) { col++; row = (col%2==1)?1:0; }
-                       if (col >= NumBackTowers) continue;
-                       iTowerStrips++  ;
-                       dStrip s = DS[iTowerStrips] = new dStrip();
-                       s.init(strip, iTowerStrips, row, col);
-               }
+                       case 1: c.clr = lx.hsb(135,0,100);                              // top to bottom
+                                       float xLin = randX();
+                                       c.setDest(lattice.getClosest(new xyz(xLin, 0         , zMidLat)).v, nConfusion);
+                                       c.setCur (lattice.getClosest(new xyz(xLin, model.yMax, zMidLat)));
+                                       break;
 
-               for (int j=0; j<iTowerStrips; j++) DS[j].addBolts();
-       }
-       dStrip  rand()                          { return DS[floor(random(iTowerStrips))];               }
-       void    setRand(dCursor c)      { c.set(rand(),randDir());                      }
-}
+                       case 2: c.clr = lx.hsb(300,0,100); break;               // chase a point around
 
-dLattice DL_;
-//----------------------------------------------------------------------------------------------------------------------------------
-class Worms extends SCPattern {
+                       case 3: boolean bLeft = random(2)<1;
+                                       c.clr = lx.hsb(135+random(120),100,100);                                // sideways
+                                       float yLin = randX();
+                                       c.setDest(lattice.getClosest(new xyz(bLeft ? 0 : model.xMax,yLin,zMidLat)).v, nConfusion);
+                                       c.setCur (lattice.getClosest(new xyz(bLeft ? model.xMax : 0,yLin,zMidLat)));
+                                       break;
+               }
+               if (pBlur.getValuef() == 1 && random(2)<1) c.clr = lx.hsb(0,0,0);
+       }
 
-       int draw(dCursor c, int nAmount) {
-               int nFrom       = max(c.nLast+1,c.pos >> 12);
-               int     nTo     = min(15,(c.pos+nAmount) >> 12); c.nLast=nTo;
-               int     nMv     = min(nAmount, c.end-c.pos);
-                       c.pos   += nMv;
-               for (int i = nFrom; i <= nTo; i++) {
-                       int n = c.s.ci + (c.dir>0 ? i : 15-i);
-                       colors[n] = c.clr;
+       void setNewDest() {
+               if (AnimNum() != 2) return;
+               xyz dest = new xyz(randX(), randY(), zMidLat);
+               for (int i=0; i<numCursors; i++) {
+                       cur.get(i).setDest(lattice.getClosest(dest).v, nConfusion);
+                       cur.get(i).clr = lx.hsb(0,100,100);     // chase a point around
                }
-               return nAmount - nMv;
        }
 
-       float   StripsPerSec    = 6;
-       float   TrailTime               = 1500;
-       int     Cursors                 = 40;
-       dCursor cur[] = new dCursor[Cursors];
+       void run(double deltaMs) { 
+               if (deltaMs > 100) return;
+           if (moveChase.click()) setNewDest();
 
-       Worms(GLucose glucose) { 
-               super(glucose); 
-               if (DL_ == null) DL_ = new dLattice();
-               for (int i=0; i<Cursors; i++) { cur[i] = new dCursor(lx.hsb(random(360),50+random(50),50+random(50))); DL_.setRand(cur[i]); }
-       }
+           float fBass=0, fTreble=0;
+           if (pEQ.getValuef()>0) {            // EQ
+                   eq.run(deltaMs);
+                   fBass       = eq.getAverageLevel(0, 4);
+                   fTreble = eq.getAverageLevel(eq.numBands-7, 7);
+               }
 
-       void run(double deltaMs) {
-                       //Test Joints
-               if (false) {
-                       for (int j=0; j<DL_.nStrips(); j++) {
-                                               dStrip s =DL_.DS[j]; dBolt d = s.b0;
-                                               if (d != null) {for (int i=0;i<16;i++) {
-                                                       if (s == d.v && i <= d.vpos) colors[d.v.ci+i]   = lx.hsb(0,0,30);
-                                                       if (s == d.h && i <= d.hpos) colors[d.h.ci+i]   = lx.hsb(0,0,30);
-                                               }}
-
-                                               d = s.b1; 
-                                               if (d != null) {for (int i=0;i<16;i++) {
-                                                       if (s == d.v && i >= d.vpos) colors[d.v.ci+i]   = lx.hsb(0,0,30);
-                                                       if (s == d.h && i >= d.hpos) colors[d.h.ci+i]   = lx.hsb(0,0,30);
-                                               }}
-                       }
-               } else {
-                       for (int i=0; i<Cursors; i++) {
-                               int nLeft = floor((float)deltaMs*.001*StripsPerSec * 65536);
-                               while(nLeft > 0) {      
-                                       nLeft = draw(cur[i], nLeft);
-                                       if (cur[i].isDone()) cur[i].PickNext(); 
-                               }
+               if (pBlur.getValuef() < 1) {    // trails
+                       for (int i=0,s=model.points.size(); i<s; i++) {
+                               color c = colors[i]; float b = brightness(c); 
+                               if (b>0) colors[i] = color(hue(c), saturation(c), (float)(b-100*deltaMs/(pBlur.getValuef()*TrailTime)));
                        }
+               }
 
-                       for (int i=0,s=model.points.size(); i<s; i++) {
-                               color c = colors[i];
-                               float b = lx.b(c);
-                               if (b>0) colors[i] = lx.hsb(lx.h(c), lx.s(c), 
-                                               max(0, (float)(b-100*deltaMs/TrailTime)));
+               int nWorms = floor(pWorms.getValuef() * numCursors * 
+                                        map(pEQ.getValuef(),0,1,1,constrain(2*fTreble,0,1)));
+
+               for (int i=0; i<nWorms; i++) {
+                       dCursor c = cur.get(i);
+                       int nLeft = floor((float)deltaMs*.001*StripsPerSec * 65536 * (5*pSpeed.getValuef()));
+                       nLeft *= (1 - lx.tempo.rampf()*pBeat.getValuef());
+                       while(nLeft > 0) { 
+                               nLeft = c.draw(nLeft,this); if (!c.isDone()) continue;
+                               c.onDone(); if (c.atDest()) reset(c);
                        }
                }
        }
+
+
+       public void onActive() { if (eq == null) {
+               eq = new GraphicEQ(lx, 16);             eq.slope.setValue(0.6);
+               eq.level.setValue(0.65);                eq.range.setValue(0.35);
+               eq.release.setValue(0.4);
+       }}
 }
 //----------------------------------------------------------------------------------------------------------------------------------