package com.rabenauge.parandroid;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLU;
import android.util.FloatMath;
import com.rabenauge.demo.*;
import com.rabenauge.gl.*;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.opengles.GL11;

public class IntroBlink extends EffectManager {
    private static final int WIDTH=800, HEIGHT=480;
    private static final short[] JOINTS={
        269, 93,  231,131,  213,131,
        251,299,  269,299,
        185,357,
        403, 53,  403, 72,  421, 72,
        438,128,  438,147,
        335,339,  335,357,  335,376,  354,376,  373,376,
        479,288,  461,323,  461,341,
        531, 46,  549, 65,  549, 83,  549,100,
        556,397,  573,397,  573,378
    };
    private static final float POINT_SIZE=7.0f;

    private PointSprite dot;
    private int[] colors;

    private class BlinkJoints extends Effect {
        public void onStart(GL11 gl) {
            dot.setSize(POINT_SIZE);
            gl.glColorPointer(4, GL11.GL_FIXED, 0, IntBuffer.wrap(colors));
        }

        public void onRender(GL11 gl, long t, long e, float s) {
            // Fade the colors.
            for (int i=3; i<colors.length; i+=4) {
                colors[i]=(int)((1.0f-FloatMath.cos(DemoMath.PI*s*i))/2.0f*65536);
            }

            // Set OpenGL states that differ from the concurrently running fading part.
            Helper.toggleState(gl, GL11.GL_POINT_SPRITE_OES, true);
            gl.glEnableClientState(GL11.GL_COLOR_ARRAY);

            // Set the projection to match the joint coordinates.
            gl.glMatrixMode(GL11.GL_PROJECTION);
            gl.glPushMatrix();
            gl.glLoadIdentity();
            GLU.gluOrtho2D(gl, 0, WIDTH, HEIGHT, 0);

            dot.makeCurrent();

            gl.glVertexPointer(2, GL11.GL_SHORT, 0, ShortBuffer.wrap(JOINTS));
            gl.glDrawArrays(GL11.GL_POINTS, 0, JOINTS.length/2);

            // Restore OpenGL states for the fading part.
            gl.glPopMatrix();

            gl.glDisableClientState(GL11.GL_COLOR_ARRAY);
            Helper.toggleState(gl, GL11.GL_POINT_SPRITE_OES, false);

            // Yes, using color arrays seems to modify the color state!
            gl.glColor4f(1, 1, 1, 1);
        }
    }

    public IntroBlink(Demo demo, GL11 gl) {
        super(gl);

        // Load the dot texture.
        Bitmap bitmap;

        bitmap=BitmapFactory.decodeResource(demo.getActivity().getResources(), R.drawable.dot);
        dot=new PointSprite(gl);
        dot.setData(bitmap);
        bitmap.recycle();

        colors=new int[JOINTS.length/2*4];
        for (int i=0; i<colors.length; i+=4) {
            colors[i  ]=(int)(1.0f*65536);
            colors[i+1]=0;
            colors[i+2]=0;
            // Set alpha when rendering.
        }

        // Schedule the effects in this part.
        add(new BlinkJoints(), Demo.DURATION_PART_INTRO-IntroFade.DURATION_EFFECT_FADEOUT);
    }
}

@@ -488,29 +488,64 @@
     
-269, 93,
+// First %22column%22%0D%0A        270, 93,%0D%0A      
   2
-3
 1
+3
 ,131,  2
 13,1
@@ -540,18 +540,18 @@
 ,131,  2
-1
 3
+2
 ,131,%0D%0A 
@@ -594,16 +594,46 @@
 85,357,%0D
+%0A%0D%0A        // Second %22column%22%0D
 %0A       
@@ -655,17 +655,17 @@
  72,  42
-1
+0
 , 72,%0D%0A 
@@ -673,17 +673,17 @@
       43
-8
+9
 ,128,  4
@@ -683,17 +683,17 @@
 128,  43
-8
+9
 ,147,%0D%0A 
@@ -705,17 +705,17 @@
   335,33
-9
+8
 ,  335,3
@@ -715,17 +715,17 @@
   335,35
-7
+6
 ,  335,3
@@ -749,24 +749,26 @@
 3,376,%0D%0A
+%0D%0A
         
 479,288,
@@ -763,36 +763,25 @@
     
-479,288,  461,323,  461,341,
+// Third %22column%22
 %0D%0A  
@@ -787,18 +787,18 @@
        5
-31
+29
 , 46,  5
@@ -798,17 +798,17 @@
  46,  54
-9
+8
 , 65,  5
@@ -808,17 +808,17 @@
  65,  54
-9
+8
 , 83,  5
@@ -818,17 +818,17 @@
  83,  54
-9
+8
 ,100,%0D%0A 
@@ -838,11 +838,49 @@
     
+480,288,  461,322,  461,341,%0D%0A        5
 55
-6
 ,397
@@ -888,23 +888,23 @@
   57
-3
+4
 ,3
-9
 7
+8
 ,  57
-3
+4
 ,3
+9
 7
-8
 %0D%0A  

Optimize the joint coordinates once more for the Milestone