/*
To add the SWC:
Project->Properties->ActionScript Build Path->Library Path->Add SWC
*/
package org.pv3d.examples
{
import caurina.transitions.Tweener;
import caurina.transitions.Equations;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import org.papervision3d.materials.MovieMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.view.BasicView;
public class CubeNav extends BasicView
{
private var cube:Cube;
private var materialsArray:Array;
public function CubeNav(viewportWidth:Number=640, viewportHeight:Number=320, scaleToStage:Boolean=true, interactive:Boolean=false, cameraType:String="CAMERA3D")
{
super(640, 480, true, true);
createObjects();
}
private function createObjects():void
{
var materialsList:MaterialsList = new MaterialsList();
var frontMaterial:MovieMaterial;
var backMaterial:MovieMaterial;
var leftMaterial:MovieMaterial;
var rightMaterial:MovieMaterial;
var topMaterial:MovieMaterial;
var bottomMaterial:MovieMaterial;
//The following 20 lines or so represents me being lazy
materialsArray = [frontMaterial, backMaterial, leftMaterial, rightMaterial, topMaterial, bottomMaterial];
var materialNames:Array = ["front", "back", "left", "right", "top", "bottom"];
var clickFunctions:Array = [onFrontButtonClick, onBackButtonClick, onLeftButtonClick, onRightButtonClick, onTopButtonClick, onBottomButtonClick];
for(var i:Number = 0; i<materialsArray.length; i++)
{
var movieClipFromSWC:MovieClip = new MovieClipFromSWC();
movieClipFromSWC.sideTextField.text = materialNames[i];
movieClipFromSWC.background.graphics.beginFill(Math.random() * 0xffffff);
movieClipFromSWC.background.graphics.drawRect(10, 10, 480, 480);
movieClipFromSWC.background.graphics.endFill();
for(var j:Number = 0; j<materialNames.length; j++)
{
movieClipFromSWC[materialNames[j]].addEventListener(MouseEvent.CLICK, clickFunctions[j]);
movieClipFromSWC[materialNames[j]].addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
movieClipFromSWC[materialNames[j]].addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
}
materialsArray[i] = new MovieMaterial(movieClipFromSWC, false, true, true);
materialsArray[i].interactive = true;
materialsArray[i].precise = true;
materialsArray[i].smooth = true;
materialsList.addMaterial(materialsArray[i], materialNames[i]);
}
cube = new Cube(materialsList);
cube.scale = 1.9;
scene.addChild(cube);
startRender();
}
private function onMouseOver(e:MouseEvent):void
{
viewport.buttonMode = true;
}
private function onMouseOut(e:MouseEvent):void
{
viewport.buttonMode = false;
}
private function onFrontButtonClick(e:MouseEvent):void
{
unsmooth();
trace("onFrontClick");
Tweener.addTween(cube,
{
rotationX: 0,
rotationY: 180,
rotationZ: 0,
onComplete: smooth,
transition: "easeinoutelastic",
time: 3
}
);
}
private function onBackButtonClick(e:MouseEvent):void
{
unsmooth();
trace("onBackClick");
Tweener.addTween(cube,
{
rotationX: 0,
rotationY: 0,
rotationZ: 0,
onComplete: smooth,
transition: "easeinoutelastic",
time: 4
}
);
}
private function onLeftButtonClick(e:MouseEvent):void
{
unsmooth();
trace("onLeftClick");
Tweener.addTween(cube,
{
rotationX: 0,
rotationY: -90,
rotationZ: 0,
onComplete: smooth,
transition: "easeinoutelastic",
time: 4
}
);
}
private function onRightButtonClick(e:MouseEvent):void
{
unsmooth();
trace("onRightClick");
Tweener.addTween(cube,
{
rotationX: 0,
rotationY: 90,
rotationZ: 0,
onComplete: smooth,
transition: "easeinoutelastic",
time: 4
}
);
}
private function onTopButtonClick(e:MouseEvent):void
{
unsmooth();
trace("onTopClick");
Tweener.addTween(cube,
{
rotationX: 90,
rotationY: 0,
rotationZ: 0,
onComplete: smooth,
transition: "easeinoutelastic",
time: 4
}
);
}
private function onBottomButtonClick(e:MouseEvent):void
{
unsmooth();
trace("onBottomClick");
Tweener.addTween(cube,
{
rotationX: -90,
rotationY: 0,
rotationZ: 180,
onComplete: smooth,
transition: "easeinoutelastic",
time: 4
}
);
}
private function unsmooth():void
{
for(var i:Number = 0; i<materialsArray.length; i++)
{
materialsArray[i].smooth = false;
}
}
private function smooth():void
{
for(var i:Number = 0; i<materialsArray.length; i++)
{
trace(i);
materialsArray[i].smooth = true;
}
}
private function startRender():void
{
addEventListener(Event.ENTER_FRAME, render);
}
private function render(e:Event):void
{
singleRender();
}
}
}