Friday, February 27, 2009

ImageGallery 1

package blueroad.todo.imagegallery1 { //blueroad.todo.imagegallery1.ImageGallery1
import blueroad.base.PaperBase;
import caurina.transitions.Tweener;
import com.pixelfumes.reflect.ReflectBitmap;
import flash.display.Bitmap;
import flash.display.MovieClip;
import org.papervision3d.core.utils.InteractiveSceneManager;
import org.papervision3d.events.FileLoadEvent;
import org.papervision3d.events.InteractiveScene3DEvent;
import org.papervision3d.materials.BitmapFileMaterial;
import org.papervision3d.materials.BitmapMaterial;
import org.papervision3d.objects.primitives.Plane;

/**
* ...
* @author binhdocco
*/
public class ImageGallery1 extends PaperBase {

private var num:uint = 0;
private var totalThumbs:uint = 0;
private var planes: Array;
private var currentIndex: int = 0;
public var isShowingLargeImage: Boolean = false;
//PLANE STATUS
public const STATUS_HOVER: String = "STATUS_HOVER";
public const STATUS_NORMAL: String = "STATUS_NORMAL";
public const STATUS_ACTIVE: String = "STATUS_ACTIVE";
public const STATUS_TWEENING: String = "STATUS_TWEENING";
public function ImageGallery1() {
this.init();
}

override protected function init2d():void {
super.init2d();
totalThumbs = Global_ImageGallery1.resourceManager.total;
planes = new Array();
}

override protected function init3d():void {
//super.init3d();
current_camera.zoom = 10;
current_camera.z = 1000;
current_camera.x = 8000;

createThumb();
}

private function createThumb():void {
num = 0;
for (var i:int = 0; i < totalThumbs; i++) {
var thumbXML: XML = Global_ImageGallery1.resourceManager.imageXMLList[i];
var thumb: String = Global_ImageGallery1.resourceManager.sourceImagePath + "/" + thumbXML.@image_icon;
var image: String = Global_ImageGallery1.resourceManager.sourceImagePath + "/" + thumbXML.@image;

var mat: BitmapFileMaterial = new BitmapFileMaterial(thumb as String);
mat.updateBitmap();
//mat.doubleSided = true;
mat.smooth = true;
mat.interactive = true;
mat.extra = {id: i, image: image, index: i };
mat.addEventListener(FileLoadEvent.LOAD_COMPLETE, createPlane);
}
}

private function onOutPlane(e:InteractiveScene3DEvent):void {
if (isShowingLargeImage) return;
current_viewport.buttonMode = false;
var plane: Plane = e.displayObject3D as Plane;
if (plane.extra.status == STATUS_HOVER) {
if (!Tweener.isTweening(current_camera)) {
var x = plane.extra.pos.x;
var z = plane.extra.pos.z;
var refPlane: Plane = current_scene.getChildByName("refPlane" + plane.extra.id) as Plane;
Tweener.addTween(plane, { x:x, z:z, time:1, transition:"easeOutCubic" } );
Tweener.addTween(refPlane, { x:x, z:z, time:1, transition:"easeOutCubic" } );
}
plane.extra.status = STATUS_NORMAL;
}

}

private function onOverPlane(e:InteractiveScene3DEvent):void {
if (isShowingLargeImage) return;
current_viewport.buttonMode = true;
var plane: Plane = e.displayObject3D as Plane;
if (plane.extra.status == STATUS_NORMAL) {
if (!Tweener.isTweening(current_camera)) {
var x = plane.extra.pos.x + 200 * Math.sin(55 * (Math.PI / 180));
var z = plane.extra.pos.z - 200 * Math.cos(55 * (Math.PI / 180));

var refPlane: Plane = current_scene.getChildByName("refPlane" + plane.extra.id) as Plane;

Tweener.addTween(plane, { x:x, z:z, time:1, transition:"easeOutCubic" } );
Tweener.addTween(refPlane, { x:x, z:z, time:1, transition:"easeOutCubic" } );
}
plane.extra.status = STATUS_HOVER;
}
}

private function onPressPlane(e:InteractiveScene3DEvent):void {
if (isShowingLargeImage) return;
var plane: Plane = e.displayObject3D as Plane;
//if (plane.extra.index != currentIndex) {
if (plane.extra.status != STATUS_ACTIVE) {
currentIndex = plane.extra.index;
arrangeThumbAt(currentIndex);
} else {
var imageDisplay: ImageDisplay = MovieClip(this.parent).imageLoader_mc as ImageDisplay;
imageDisplay.loadImage(plane.extra.image);
}
//}
}

private function createPlane(e: FileLoadEvent):void {
var mat: BitmapFileMaterial = e.target as BitmapFileMaterial;
var plane: Plane = new Plane(mat, mat.bitmap.width, mat.bitmap.height, 4, 4);
plane.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, onOverPlane);
plane.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, onOutPlane);
plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onPressPlane);
plane.extra = mat.extra;
plane.extra.status = STATUS_NORMAL;
planes.push(plane);
//reflect
var refBitmap: Bitmap = ReflectBitmap.getReflectBitmap(mat.bitmap, 100, 0.5);
refBitmap.cacheAsBitmap = true;
var refMat: BitmapMaterial = new BitmapMaterial(refBitmap.bitmapData);
refMat.smooth = true;
//refMat.doubleSided = true;
var refPlane: Plane = new Plane(refMat, refMat.bitmap.width, refMat.bitmap.height, 4, 4);
refPlane.extra = { };
//
default_scene.addChild(plane, "plane" + plane.extra.id);
default_scene.addChild(refPlane, "refPlane" + plane.extra.id);
//num ++;
/*if (num >= totalThumbs - 1) */arrangeThumb();
}

private function arrangeThumb():void {
for (var i:int = 0; i < planes.length; i++) {
var plane: Plane = planes[i] as Plane;
var rotationY: Number = 0;
if (i > 0)
{
rotationY = 35;
} else {
plane.extra.status = STATUS_ACTIVE;
}

var x = -100 + Math.cos(55 * (Math.PI / 180)) * 200 * i;
var z = Math.sin(55 * (Math.PI / 180)) * 200 * i;
var y = 0;
plane.extra.pos = { x: x, z: z };
var refPlane: Plane = current_scene.getChildByName("refPlane" + plane.extra.id) as Plane;
refPlane.extra.pos = { x: x, z: z };
Tweener.addTween(plane, { rotationY: rotationY, x: x, y: y, z: z, time: .5 , transition:"easeOutBack" } );
Tweener.addTween(refPlane, { rotationY: rotationY, x: x, y: y - refPlane.material.bitmap.height - 2, z: z, time: .5 , transition:"easeOutBack" } );
}
if (i == (totalThumbs - 1)) {
Tweener.addTween(current_camera, { z: -1000, x:0, time:6, delay:0.3, transition:"easeOutBack" } );
}
}

//
private function arrangeThumbAt(index: int):void {
for (var i:int = 0; i < planes.length; i++) {
var plane: Plane = planes[i] as Plane;
var dis: int = plane.extra.index - index;
var rotationY: Number = 0;

if (dis != 0)
{
rotationY = 35;
plane.extra.status = STATUS_TWEENING;
} else {
plane.extra.status = STATUS_ACTIVE;
}
var pos: int = 0;
if (dis > 0) {
pos = dis;
} else if (dis < 0) {
pos = planes.length + dis;
}

var x = -100 + Math.cos(55 * (Math.PI / 180)) * 200 * pos;
var z = Math.sin(55 * (Math.PI / 180)) * 200 * pos;
var y = 0;
plane.extra.pos = { x: x, z: z};
plane.extra.index = pos;

var refPlane: Plane = current_scene.getChildByName("refPlane" + plane.extra.id) as Plane;
refPlane.extra.pos = { x: x, z: z };

Tweener.removeTweens(plane);
Tweener.removeTweens(refPlane);
if (dis > 0) {
Tweener.addTween(plane, { rotationY: rotationY , x: x, z: z, time: 1.5 , transition:"easeOutBack", onComplete: turnNormal, onCompleteParams: [plane] } );
Tweener.addTween(refPlane, { rotationY: rotationY , x: x, z: z, time: 1.5 , transition:"easeOutBack"} );
} else if (dis < 0) {
Tweener.addTween(plane, {rotationY: rotationY , x: plane.x - 2500, z: plane.z - 2500, time: .8 , transition:"easeOutBack", onComplete: continue1, onCompleteParams: [plane]} );
Tweener.addTween(refPlane, {rotationY: rotationY , x: refPlane.x - 2500, z: refPlane.z - 2500, time: .8 , transition:"easeOutBack", onComplete: continue1, onCompleteParams: [refPlane]} );
} else {
Tweener.addTween(plane, { rotationY: rotationY , x: x, z: z, time: 1.5 , transition:"easeOutBack"} );
Tweener.addTween(refPlane, { rotationY: rotationY , x: x, z: z, time: 1.5 , transition:"easeOutBack"} );
}
}
}

private function continue1(plane: Plane):void {
plane.x = plane.extra.pos.x + 1000;
plane.z = plane.extra.pos.z + 1000;

Tweener.addTween(plane, {x: plane.extra.pos.x, z: plane.extra.pos.z, time: .7 , transition:"easeOutBack", onComplete: turnNormal, onCompleteParams: [plane]} );
}

private function turnNormal(plane: Plane):void {
plane.extra.status = STATUS_NORMAL;
}
}

}javascript:void(0)

No comments: