Allow top-to-bottom card order when rowCount>1

Add a member function to CarouselRS.java called setFirstCardTop()
to allow the first card to appear on top instead of the bottom
when rowCount>1. The default is false, meaning the existing
behaviour is not changed for other apps.

Change-Id: I38d666958ebb802e211496895cbe3f91e78f5392
diff --git a/carousel/java/com/android/ex/carousel/CarouselController.java b/carousel/java/com/android/ex/carousel/CarouselController.java
index 40853d5..b722583 100644
--- a/carousel/java/com/android/ex/carousel/CarouselController.java
+++ b/carousel/java/com/android/ex/carousel/CarouselController.java
@@ -91,6 +91,7 @@
             new int[] {0}, 0, 1, 1, 1, Bitmap.Config.ARGB_4444);
     private int mDragModel = CarouselRS.DRAG_MODEL_SCREEN_DELTA;
     private int mFillDirection = CarouselRS.FILL_DIRECTION_CCW;
+    private boolean mFirstCardTop = false;
 
     public CarouselController() {
         boolean useDepthBuffer = true;
@@ -110,6 +111,7 @@
         setPrefetchCardCount(mPrefetchCardCount);
         setRowCount(mRowCount);
         setRowSpacing(mRowSpacing);
+        setFirstCardTop(mFirstCardTop);
         setDetailTextureAlignment(mDetailTextureAlignment);
         setForceBlendCardsWithZ(mForceBlendCardsWithZ);
         setDrawRuler(mDrawRuler);
@@ -235,6 +237,16 @@
         }
     }
 
+     /**
+     * Sets the position of the first card when rowCount > 1 .
+     */
+    public void setFirstCardTop(boolean f) {
+        mFirstCardTop = f;
+        if (mRenderScript != null) {
+            mRenderScript.setFirstCardTop(f);
+        }
+    }
+
     /**
      * Sets how detail textures are aligned with respect to the card.
      *
diff --git a/carousel/java/com/android/ex/carousel/CarouselRS.java b/carousel/java/com/android/ex/carousel/CarouselRS.java
index a5e53d2..ea4be35 100644
--- a/carousel/java/com/android/ex/carousel/CarouselRS.java
+++ b/carousel/java/com/android/ex/carousel/CarouselRS.java
@@ -453,6 +453,10 @@
         mScript.set_rowSpacing(spacing);
     }
 
+    public void setFirstCardTop(boolean first) {
+        mScript.set_firstCardTop(first);
+    }
+
     public void setPrefetchCardCount(int count) {
         mPrefetchCardCount = count;
         mScript.set_prefetchCardCount(count);
diff --git a/carousel/java/com/android/ex/carousel/CarouselView.java b/carousel/java/com/android/ex/carousel/CarouselView.java
index 471e827..572f172 100644
--- a/carousel/java/com/android/ex/carousel/CarouselView.java
+++ b/carousel/java/com/android/ex/carousel/CarouselView.java
@@ -247,6 +247,13 @@
     }
 
     /**
+     * Sets the position of the first card when rowCount > 1.
+     */
+    public void setFirstCardTop(boolean f) {
+        mController.setFirstCardTop(f);
+    }
+
+    /**
      * Set the number of detail textures that can be visible at one time.
      *
      * @param n the number of slots
diff --git a/carousel/java/com/android/ex/carousel/carousel.rs b/carousel/java/com/android/ex/carousel/carousel.rs
index 45dbd21..e35cc1a 100644
--- a/carousel/java/com/android/ex/carousel/carousel.rs
+++ b/carousel/java/com/android/ex/carousel/carousel.rs
@@ -171,6 +171,7 @@
 float4 backgroundColor;
 int rowCount;  // number of rows of cards in a given slot, default 1
 float rowSpacing;  // spacing between rows of cards
+bool firstCardTop; // set true for first card on top row when multiple rows used
 
 int dragModel = DRAG_MODEL_SCREEN_DELTA;
 int fillDirection; // the order in which to lay out cards: +1 for CCW (default), -1 for CW
@@ -275,6 +276,7 @@
     cardCount = 0;
     rowCount = 1;
     rowSpacing = 0.0f;
+    firstCardTop = false;
     fadeInDuration = 250;
     rezInCardCount = 0.0f; // alpha will ramp to 1.0f over this many cards (0.0f means disabled)
     detailFadeRate = 0.5f; // fade details over this many slot positions.
@@ -552,7 +554,11 @@
    }
    const float cardHeight = cardVertices[3].y - cardVertices[0].y;
    const float totalHeight = rowCount * (cardHeight + rowSpacing) - rowSpacing;
-   const float rowOffset = (i % rowCount) * (cardHeight + rowSpacing);
+   if (firstCardTop)
+      i = rowCount - (i % rowCount) - 1;
+   else
+      i = i % rowCount;
+   const float rowOffset = i * (cardHeight + rowSpacing);
    return (cardHeight - totalHeight) / 2 + rowOffset;
 }