/*********************************************************
SandyCamera is a demo application for Camera3D in Sandy,
the 3D Flash libray and API by Thomas Pfeiffer.
Sandy address : http://www.flashsandy.org/
Sandy version : 1.1
AS version : 2.0
Author : Clas Le Petit
Author adress : http://petitpub.com/labs
Year : 2006
License : Use at your own discretion
: Please give me credit ;)
: If you find a bug, please holler!
*********************************************************/
import mx.data.binding.*;
//import mx.controls.TextField;
import mx.managers.DepthManager;
import sandy.core.data.*;
import sandy.core.group.*;
import sandy.primitive.*;
import sandy.view.*;
import sandy.core.*;
import sandy.skin.*;
import sandy.util.*;
import sandy.core.transform.*;
import sandy.events.*;
// =================== Camera Control ==========================
// Start positions for moving demo cams
var upStart = upDemo._y;
var sideStart = sideDemo._x;
var forStart = forDemo._y;
var startPos:Vector;
// Initial camera values in camera system
var tilt:Number = 0;
var roll:Number = 0;
var pan:Number = 0;
var up:Number = 0;
var forw:Number = 0;
var side:Number = 0;
// Initial values in the global system
var xPos:Number = 0;
var yPos:Number = 0;
var zPos:Number = -500;
var xRot:Number = 0;
var yRot:Number = 0;
var zRot:Number = 0;
var helpPane:TextField;
// Initiate the camera controller
function initControl(){
// Get preset camera values
startPos = camera.getPosition();
xPosSlider.value = startPos.x;
yPosSlider.value = startPos.y;
zPosSlider.value = startPos.z;
// Tilt Roll and Pan
tiltSlider.value = tilt;
rollSlider.value = roll;
panSlider.value = pan;
lookAtX.text = lookAtY.text = lookAtZ.text = "0";
// ViewFinder with crosshair toggler
viewFinder.crossCheck.addEventListener("click", crossHairToggler);
function crossHairToggler( evt ){
viewFinder.crossHair._visible = viewFinder.crossCheck.selected;
}
// Create tooltips
createToolTip();
// Create a help pane
createHelp();
}
// Create tool tips for camera demos
function createToolTip(){
// A tooltip component from http://www.frogstyle.ch/go.cfm?tooltip_component&language=en
// Thanks indeed ;)
panDemo.onRollOver = function(){
toolTip.show("pan( angle ) rotates the camera around its local y-axis by angle degree.",200,40,_xmouse,_ymouse,"top");
}
rollDemo.onRollOver = function(){
toolTip.show("roll( angle ) rotates the camera around its local z-axis by angle degree.",200,40,_xmouse,_ymouse,"top");
}
tiltDemo.onRollOver = function(){
toolTip.show("tilt( angle ) rotates the camera around its local x-axis by angle degree.",200,40,_xmouse,_ymouse,"top");
}
upDemo.onRollOver = function(){
toolTip.show("moveUpwards( distance ) moves the camera along its local y-axis.",200,40,_xmouse,_ymouse,"top");
}
forDemo.onRollOver = function(){
toolTip.show("moveForwards( distance ) moves the camera along its local z-axis.",200,40,_xmouse,_ymouse,"top");
}
sideDemo.onRollOver = function(){
toolTip.show("moveSideways( distance ) moves the camera along its local x-axis.",200,40,_xmouse,_ymouse,"top");
}
focalDemo.onRollOver = function(){
toolTip.show("setFocal( number ) sets the camera's focal distance to number.",200,40,_xmouse,_ymouse,"top");
}
panDemo.onRollOut = rollDemo.onRollOut = tiltDemo.onRollOut =
upDemo.onRollOut = forDemo.onRollOut = sideDemo.onRollOut =
focalDemo.onRollOut = function(){
toolTip.hide();
}
}
// Create the help pane
function createHelp(){
helpPane=this.createTextField("helpPane", this.getNextHighestDepth(), 28, 30, 410, 340);
this.createClassObject(mx.controls.UIScrollBar, "scrollBar", 20);
scrollBar.setScrollTarget(helpPane);
scrollBar.setSize(16,helpPane._height);
scrollBar.move(helpPane._x + helpPane._width, helpPane._Y);
// Apply a CSS style sheet to the text field
var helpStyle:TextField.StyleSheet = new TextField.StyleSheet();
helpStyle.load("camhelp.css");
helpPane.styleSheet = helpStyle;
helpPane.multiline= true;
helpPane.wordWrap = true;
helpPane.html = true;
// Don't show the help pane by default
helpPane._visible = false;
scrollBar._visible = false;
helpButton.onRelease = showHelp;
// Add the help page
var story:XML = new XML();
story.ignoreWhite = true;
story.load("camhelp.html");
story.onLoad = function () {
helpPane.htmlText = story.toString();
}
}
function showHelp(){
// Take care of this later
if(helpPane._visible){
screen_mc._visible = true;
helpPane._visible = false;
scrollBar._visible = false;
}
else{
screen_mc._visible = false;
helpPane._visible = true;
scrollBar._visible = true;
}
}
// Reset controller values
resetButton.addEventListener("click",resetControls);
function resetControls(){
tilt = tiltSlider.value = 0;
roll = rollSlider.value = 0;
pan = panSlider.value = 0;
up = upSlider.value = 0;
side = sideSlider.value = 0;
forw = forSlider.value = 0;
xRot = yRot = zRot = 0;
rotXSlider.value = rotYSlider.value = rotZSlider.value = 0;
xPosSlider.value = startPos.x;
yPosSlider.value = startPos.y;
zPosSlider.value = startPos.z;
lookAtX.text = lookAtY.text = lookAtZ.text = "0";
camera.lookAt(0,0,0);
}
// Set focal length / nodal distance
focalSlider.changeHandler = function(){
camView._height = 9000/this.value;
camera.setFocal(this.value);
}
// ----------- Movements in the camera coordinate system ------------
tiltSlider.changeHandler = function(){
tiltDemo._rotation = -this.value;
camera.tilt( this.value - tilt );
tilt = this.value;
}
rollSlider.changeHandler = function(){
rollDemo._rotation = this.value;
camera.roll( this.value - roll);
roll = this.value;
}
panSlider.changeHandler = function(){
panDemo._rotation = this.value;
camera.pan( this.value - pan);
pan = this.value;
}
// ----------- Movements in the global coordinate system ------------
// Linear movements in the camera coordinates
upSlider.changeHandler = function(){
upDemo._y = upStart - this.value/4;
camera.moveUpwards( this.value-up );
up = this.value;
// updateControlPosition();
}
sideSlider.changeHandler = function(){
sideDemo._x = sideStart + this.value/4;
camera.moveSideways( this.value-side );
side = this.value;
// updateControlPosition();
}
forSlider.changeHandler = function(){
forDemo._y = forStart - this.value/4;
camera.moveForward( this.value-forw );
forw = this.value;
// updateControlPosition();
}
// Rotations around axes parallell to the global coordinate axes
rotXSlider.changeHandler = function(){
camera.rotateX(this.value - xRot);
xRot = this.value;
}
rotYSlider.changeHandler = function(){
camera.rotateY(this.value - yRot);
yRot = this.value;
}
rotZSlider.changeHandler = function(){
camera.rotateZ(this.value - zRot);
zRot = this.value;
}
// Linear movements along axes parallell to the global coordinate axes
xPosSlider.changeHandler = setPosition;
yPosSlider.changeHandler = setPosition;
zPosSlider.changeHandler = setPosition;
// Set the camera position in golbal coordinates
function setPosition(){
camera.setPosition( xPosSlider.value, yPosSlider.value, zPosSlider.value);
}
// Look at a specific position in the global coordiante system
lookAtButton.addEventListener("click",lookAtHandler);
function lookAtHandler(){
camera.lookAt(Number(lookAtX.text),Number(lookAtY.text),Number(lokAtZ.text));
}
// Global position controller/view bindings between sliders and text
var xPosStart:Object = {component: xPosSlider, property: "value", event:"change"};
var xPosFinish:Object = {component: camPosX, property: "text", event:"change"};
var xPosBinding:Binding = new Binding(xPosStart,xPosFinish,null,true);
var yPosStart:Object = {component: yPosSlider, property: "value", event:"change"};
var yPosFinish:Object = {component: camPosY, property: "text",event:"change"};
var yPosBinding:Binding = new Binding(yPosStart,yPosFinish,null,true);
var zPosStart:Object = {component: zPosSlider, property: "value", event:"change"};
var zPosFinish:Object = {component: camPosZ, property: "text",event:"change"};
var zPosBinding:Binding = new Binding(zPosStart,zPosFinish,null,true);
/* =================================================================
The Sandy world
================================================================= */
/*
==============================================================================
PyramidTest demonstrates how to build and present a World3D using Sandy
It also presents a Sandy coordinate system for reference
===============================================================================
*/
// Imports above - This should all be OO of course
// - and relax bindings between world and CameraControl
var camera:Camera3D;
var screen:ClipScreen; // Presentation surface
var world:World3D = World3D.getInstance(); // Our 3D world
function init( Void ):Void {
// Create a clipScreen as presentation surface for the World3D to draw on
screen = new ClipScreen( this.createEmptyMovieClip('screen_mc', 1), 480, 400 );
// Create a camera, so we can see our world.
// Give it a focal distanse ( equivalent ) and a reference to the viewing screen.
camera = new Camera3D( 700, screen );
// Position the camera at a negative distance along the z axis
// By default it is looking at the origin of the world coordinate system [0,0,0]
camera.setPosition(0,0,-500);
camera.lookAt(0,0,0);
// Attach the camera to the world
world.addCamera( camera );
// Create the root group, the banch node for all objects in our world
var bg:Group = new Group();
// and attach it to the world. this node is responisble
// for drawing all its ( possibly transformed child nodes
world.setRootGroup( bg );
// Create the scen and attach it to the rootGroup
createScene( bg );
// Finally we start rendering the world
world.render();
}
function createScene( bg:Group ):Void {
// Create the coordinate system.
createCoordinateSystem(bg, true, 1);
// Create the Pyramid
var pyramid = new Pyramid( 90, 40, 40);
pyramid.setSkin( skin=new MixedSkin(0xFEFE4E, 80, 0, 20, 1));
//pyramid.setSkin( skin=new SimpleColorSkin(0xFEFE4E, 60));
skin.setLightingEnable(true);
bg.addChild( pyramid );
}
// Create the coordinate system.
// doPlanes=false excludes coordinate planes
// grid sets the number of divisions ( 1 - 10 ) ( quality in Sandy parlance )
function createCoordinateSystem( bg:Group, doPlanes:Boolean, grid:Number ):Void {
// Create the coordinate axes
var xAxis:Object3D = new Line3D( new Vector(-150,0,0), new Vector(150,0,0) );
var yAxis:Object3D = new Line3D( new Vector(0,-150,0), new Vector(0,150,0) );
var zAxis:Object3D = new Line3D( new Vector(0,0,-150), new Vector(0,0,150) );
// Give the axes distinct colors
xAxis.setSkin( new SimpleLineSkin( 1, 0xff0000, 60 ) ); // red
yAxis.setSkin( new SimpleLineSkin( 1, 0x00ff00, 60 ) ); // green
zAxis.setSkin( new SimpleLineSkin( 1, 0x0000ff, 60 ) ); // blue
// Create the axes group
var axes:Group = new Group();
axes.addChild(xAxis);
axes.addChild(yAxis);
axes.addChild(zAxis);
// Add the coordinate axes to the rootGroup
bg.addChild( axes );
// Create the coordiante planes if so ordered
if( doPlanes){
var xyPlane:Object3D = new Plane3D(100,100,grid,'quad');
xyPlane.setSkin( new MixedSkin(0x00FF00, 10, 0, 15, 1));
var yzPlane:Object3D = new Plane3D(100,100,grid,'quad');
yzPlane.setSkin( new MixedSkin(0x0000FF, 10, 0, 15, 1));
var zxPlane:Object3D = new Plane3D(100,100,grid,'quad');
zxPlane.setSkin( new MixedSkin(0xFF0000, 10, 0, 15, 1));
// Create rotations for the xy plane and the yz plane
// The zx plane doesn't need any transformation
var tg1:TransformGroup = new TransformGroup();
var rot1:Transform3D = new Transform3D();
rot1.rot(90,0,0);
tg1.setTransform( rot1 );
tg1.addChild( xyPlane );
var tg2:TransformGroup = new TransformGroup();
var rot2:Transform3D = new Transform3D();
rot2.rot(0,0,90);
tg2.setTransform( rot2 );
tg2.addChild( yzPlane );
// Add the transformed coordinate planes to the rootGroup
bg.addChild( tg1 );
bg.addChild( tg2 );
bg.addChild( zxPlane );
}
}
// Start creating the world
init();
// Initiate the camera controls
initControl();