Merge branch 'integration' of github.com:sugarcubes/SugarCubes into integration
[SugarCubes.git] / TestPatterns.pde
index e7951e3d36c76cfc7b1910a9089a79e165081b36..38ea2296200d31e7f13e370a7d1f3acc35a7df7d 100644 (file)
@@ -74,6 +74,33 @@ class TestZPattern extends SCPattern {
   }
 }
 
+/**
+ * This shows how to iterate over towers, enumerated in the model.
+ */
+class TestTowerPattern extends SCPattern {
+  private final SawLFO towerIndex = new SawLFO(0, model.towers.size(), 1000*model.towers.size());
+  
+  public TestTowerPattern(GLucose glucose) {
+    super(glucose);
+    addModulator(towerIndex).trigger();
+  }
+
+  public void run(int deltaMs) {
+    int ti = 0;
+    for (Tower t : model.towers) {
+      for (Point p : t.points) {
+        colors[p.index] = color(
+          lx.getBaseHuef(),
+          100,
+          max(0, 100 - 80*LXUtils.wrapdistf(ti, towerIndex.getValuef(), model.towers.size()))
+        );
+      }
+      ++ti;
+    }
+  }
+  
+}
+
 /**
  * This is a demonstration of how to use the projection library. A projection
  * creates a mutation of the coordinates of all the points in the model, creating
@@ -112,7 +139,7 @@ class TestProjectionPattern extends SCPattern {
     projection.reset(model)
     
       // Translate so the center of the car is the origin, offset by yPos
-      .translateCenter(0, yPos.getValuef(), 0)
+      .translateCenter(model, 0, yPos.getValuef(), 0)
 
       // Rotate around the origin (now the center of the car) about an X-vector
       .rotate(angle.getValuef(), 1, 0, 0)
@@ -134,12 +161,40 @@ class TestProjectionPattern extends SCPattern {
   } 
 }
 
+class TestCubePattern extends SCPattern {
+  
+  private SawLFO index = new SawLFO(0, Cube.POINTS_PER_CUBE, Cube.POINTS_PER_CUBE*60);
+  
+  TestCubePattern(GLucose glucose) {
+    super(glucose);
+    addModulator(index).start();
+  }
+  
+  public void run(int deltaMs) {
+    for (Cube c : model.cubes) {
+      int i = 0;
+      for (Point p : c.points) {
+        colors[p.index] = color(
+          lx.getBaseHuef(),
+          100,
+          max(0, 100 - 80.*abs(i - index.getValuef()))
+        );
+        ++i;
+      }
+    }
+  }
+}
+
 class MappingTool extends SCPattern {
     
   private int cubeIndex = 0;
   private int stripIndex = 0;
+  private int channelIndex = 0;
 
-  public boolean mappingModeSingleCube = true;
+  public final int MAPPING_MODE_ALL = 0;
+  public final int MAPPING_MODE_CHANNEL = 1;
+  public final int MAPPING_MODE_SINGLE_CUBE = 2;
+  public int mappingMode = MAPPING_MODE_ALL;
 
   public final int CUBE_MODE_ALL = 0;
   public final int CUBE_MODE_SINGLE_STRIP = 1;
@@ -150,8 +205,33 @@ class MappingTool extends SCPattern {
   public boolean channelModeGreen = false;
   public boolean channelModeBlue = false;
   
-  MappingTool(GLucose glucose) {
+  private final int numChannels;
+  
+  private final PandaMapping[] pandaMappings;
+  private PandaMapping activeMapping;
+  private int mappingChannelIndex;
+  
+  MappingTool(GLucose glucose, PandaMapping[] pandaMappings) {
     super(glucose);
+    this.pandaMappings = pandaMappings;
+    numChannels = pandaMappings.length * PandaMapping.CHANNELS_PER_BOARD;
+    setChannel();
+  }
+  
+  private void setChannel() {
+    mappingChannelIndex = channelIndex % PandaMapping.CHANNELS_PER_BOARD;
+    activeMapping = pandaMappings[channelIndex / PandaMapping.CHANNELS_PER_BOARD];
+  }
+  
+  private int cubeInChannel(Cube c) {
+    int i = 1;
+    for (int index : activeMapping.channelList[mappingChannelIndex]) {
+      if (c == model.getCubeByRawIndex(index)) {
+        return i;
+      }
+      ++i;
+    }
+    return 0;
   }
   
   private void printInfo() {
@@ -165,7 +245,7 @@ class MappingTool extends SCPattern {
   }
   
   public void strip(int delta) {
-    int len = Cube.CLIPS_PER_CUBE * Clip.STRIPS_PER_CLIP;
+    int len = Cube.STRIPS_PER_CUBE;
     stripIndex = (len + stripIndex + delta) % len;
     printInfo();
   }
@@ -182,19 +262,36 @@ class MappingTool extends SCPattern {
     
     int ci = 0;
     for (Cube cube : model.cubes) {
-      if (!mappingModeSingleCube || (cubeIndex == ci)) {
-        if (cubeMode == CUBE_MODE_STRIP_PATTERN) {
+      boolean cubeOn = false;
+      int channelIndex = cubeInChannel(cube);
+      switch (mappingMode) {
+        case MAPPING_MODE_ALL: cubeOn = true; break;
+        case MAPPING_MODE_SINGLE_CUBE: cubeOn = (cubeIndex == ci); break;
+        case MAPPING_MODE_CHANNEL: cubeOn = (channelIndex > 0); break;
+      }
+      if (cubeOn) {
+        if (mappingMode == MAPPING_MODE_CHANNEL) {
+          color cc = off;
+          switch (channelIndex) {
+            case 1: cc = r; break;
+            case 2: cc = r|g; break;
+            case 3: cc = g; break;
+            case 4: cc = b; break;
+            case 5: cc = r|b; break;
+          }
+          setColor(cube, cc);
+        } else if (cubeMode == CUBE_MODE_STRIP_PATTERN) {
           int si = 0;
           color sc = off;
           for (Strip strip : cube.strips) {
-            int clipI = si / Clip.STRIPS_PER_CLIP;
-            switch (clipI) {
+            int faceI = si / Face.STRIPS_PER_FACE;
+            switch (faceI) {
               case 0: sc = r; break;
               case 1: sc = g; break;
               case 2: sc = b; break;
               case 3: sc = r|g|b; break;
             }
-            if (si % Clip.STRIPS_PER_CLIP == 2) {
+            if (si % Face.STRIPS_PER_FACE == 2) {
               sc = r|g;
             }
             setColor(strip, sc);
@@ -224,14 +321,27 @@ class MappingTool extends SCPattern {
       cubeIndex += model.cubes.size();
     }
   }
+
+  public void incChannel() {
+    channelIndex = (channelIndex + 1) % numChannels;
+    setChannel();
+  }
+  
+  public void decChannel() {
+    --channelIndex;
+    if (channelIndex < 0) {
+      channelIndex += numChannels;
+    }
+    setChannel();    
+  }
   
   public void incStrip() {
-    int stripsPerCube = Cube.CLIPS_PER_CUBE * Clip.STRIPS_PER_CLIP;
+    int stripsPerCube = Cube.FACES_PER_CUBE * Face.STRIPS_PER_FACE;
     stripIndex = (stripIndex + 1) % stripsPerCube;
   }
   
   public void decStrip() {
-    int stripsPerCube = Cube.CLIPS_PER_CUBE * Clip.STRIPS_PER_CLIP;
+    int stripsPerCube = Cube.FACES_PER_CUBE * Face.STRIPS_PER_FACE;
     --stripIndex;
     if (stripIndex < 0) {
       stripIndex += stripsPerCube;
@@ -240,8 +350,8 @@ class MappingTool extends SCPattern {
   
   public void keyPressed() {
     switch (keyCode) {
-      case UP: incCube(); break;
-      case DOWN: decCube(); break;
+      case UP: if (mappingMode == MAPPING_MODE_CHANNEL) incChannel(); else incCube(); break;
+      case DOWN: if (mappingMode == MAPPING_MODE_CHANNEL) decChannel(); else decCube(); break;
       case LEFT: decStrip(); break;
       case RIGHT: incStrip(); break;
     }