topical media & game development
student-powerrail-main.ax
student-powerrail-main.ax
[swf]
[flash]
flex
package {
import caurina.transitions.*;
import com.flexcomps.utils.CustomEvent;
import com.flexcomps.utils.XMLLoaderUtil;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.filters.BlurFilter;
import flash.filters.ConvolutionFilter;
import flash.filters.GlowFilter;
import flash.net.NetStream;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.text.TextField;
import flash.utils.Timer;
import mx.utils.StringUtil;
import org.papervision3d.cameras.*;
import org.papervision3d.events.InteractiveScene3DEvent;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.render.BasicRenderEngine;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;
[SWF(width="800", height="600", backgroundColor="0x000000")]
public class @ax-student-powerrail-main extends Sprite {
/* Viewports */
public var viewport:Viewport3D;
/* Rendering engine */
public var renderer:BasicRenderEngine;
/* Scenes */
public var scene:Scene3D;
/* Cameras */
public var camera:Camera3D;
/* PowerRailViews and PowerRailSettings */
public var views:Array = new Array();
public var currentView:student_powerrail_PowerRailView;
public var settings:student_powerrail_PowerRailSettings;
/* animationList for animated objects */
public var animationList:Array = new Array();
/* Input keys state */
public var upKeyDown:Boolean = false;
public var downKeyDown:Boolean = false;
/* Debug Textfield for debug information */
public var debugMode:Boolean = true;
public var text:TextField;
public var filterToggle:Boolean = false;
public var myTimer:Timer;
public var tourStarted:Boolean;
public var tourCounter:Number;
/* Constructor */
public function @ax-student-powerrail-main() {
//Display Debug information on top left side of screen if enabled:
debugDisplay();
myTimer = new Timer(1000); // 1 second
myTimer.addEventListener(TimerEvent.TIMER, tick);
myTimer.start();
init();
}
/* Debug display displays information used for debugging */
public function debugDisplay():void {
text = new TextField();
text.width = 500;
text.addEventListener(MouseEvent.CLICK, startTour);
addChild(text);
}
public function startTour(evt:MouseEvent):void {
//trace(">> " + currentView.tourTimeout);
this.tourCounter = currentView.tourTimeout as Number;
tourStarted = true;
}
public function tick(event:TimerEvent):void {
if(tourStarted && settings.guidedTour) {
if (tourCounter==0) {
goToNext();
} else {
tourCounter = tourCounter - 1;
}
}
//trace(tourStarted + "-" + settings.guidedTour + "-" + tourCounter);// + "-" + getTimer());
}
public function init(vpWidth:Number = 800, vpHeight:Number = 600):void {
initPapervision(vpWidth, vpHeight); // Initialise papervision
initXML(); // Initialise XML parsing
}
public function initPapervision(vpWidth:Number, vpHeight:Number):void {
viewport = new Viewport3D(vpWidth, vpHeight,false,true);
addChild(viewport);
/* //Working glowfilter for all objects in viewport (this. refers to viewport)
var glow:GlowFilter = new GlowFilter;
glow.color = 0x9c9c9c;
glow.blurX = 17;
glow.blurY = 17;
this.filters = [glow];
*/
renderer = new BasicRenderEngine();
scene = new Scene3D();
camera = new Camera3D();
}
/* Initialise the XML loader and load the PowerRail XML file from the assets folder */
public function initXML():void {
var xmlLoader:XMLLoaderUtil = new XMLLoaderUtil();
xmlLoader.addEventListener(CustomEvent.ONLOADED, onXMLLoaded);
// xmlLoader.load("assets/powerrail_presentatie.xml");
xmlLoader.load("student-powerrail-ae.xml");
}
/* Handles the XML after loading */
public function onXMLLoaded(evt:CustomEvent):void {
var xmlData:XML = evt.data as XML;
settings = new student_powerrail_PowerRailSettings(xmlData.children()[0]); //load settings
var viewsList:XMLList = xmlData.views.children(); //load views
for (var i:uint=0; i < viewsList.length(); i++) {
var view:student_powerrail_PowerRailView = new student_powerrail_PowerRailView(viewsList[i]);
view.id = i;
views.push(view);
}
/* Load the rest of the init statements after the XML is loaded */
init2d(); // Initialize the interface
init3d(); // Initialise 3d objects
initEvents(); // Set up the event listeners
}
/* This function creates 2d items layered on top of the papervision 3d scene */
public function init2d():void {
/*
//Brandmarking, moet nog even gefixed worden
var brandmark:TextField;
brandmark = new TextField;
brandmark.text = "PowerRail ©2008"
brandmark.setTextFormat(new TextFormat("Arial", xml.attribute("fontsize"), 0xFFFFFF,false,false,false,"http://www.power-rail.net/"));
brandmark.x = 700;
brandmark.y = 0;
scene.addChild(brandmark)*/
}
/* This function sets up the papervision 3d stage */
public function init3d():void {
/* Add all the objects listed in the XML to the scene and views list */
for (var i:uint = 0 ; i<views.length ; i++) {
var objectList:XMLList = views[i].xml.objects.children();
for (var j:uint = 0 ; j<objectList.length() ; j++) {
var objType:String = objectList[j].attribute("type");
var obj:Object;
/* Check the objects type and call the corresponding load function */
switch (objType) {
case "image":
obj = student_powerrail_PowerRailObjectLoader.loadImageObject(objectList[j], views[i]);
break;
case "text":
obj = student_powerrail_PowerRailObjectLoader.loadTextObject(objectList[j], views[i]);
break;
case "html":
obj = student_powerrail_PowerRailObjectLoader.loadHTMLObject(objectList[j], views[i]);
break;
case "dae":
obj = student_powerrail_PowerRailObjectLoader.loadDaeObject(objectList[j], views[i]);
break;
case "swf":
obj = student_powerrail_PowerRailObjectLoader.loadSWFObject(objectList[j], views[i]);
break;
case "youtube":
obj = student_powerrail_PowerRailObjectLoader.loadYoutubeObject(objectList[j], views[i]);
break;
case "movie":
obj = student_powerrail_PowerRailObjectLoader.loadMovieObject(objectList[j], views[i]);
break;
case "webcam":
obj = student_powerrail_PowerRailObjectLoader.loadWebcamObject(objectList[j], views[i]);
break;
}
/* Add the object to the viewslist and the scene */
views[i].objects.push(obj);
scene.addChild(obj as DisplayObject3D);
//click attribute
var clickParam:String = objectList[j].attribute("click");
if (clickParam) {
var parsed:Array = clickParam.substring(0,clickParam.length-1).split("(");
var clickType:String = parsed[0];
var clickArgs:Array = parsed[1].split(",");
for (var k:uint; k<clickArgs.length ; k++) {
clickArgs[k] = StringUtil.trim(clickArgs[k]);
}
obj.clickType = clickType;
obj.clickArgs = clickArgs;
switch (clickType) {
case "Internal":
obj.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, onInternalLink);
break;
case "External":
obj.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, onExternalLink);
break;
case "Filter":
obj.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, onGlowLink);
break;
}
if (settings.glowOnMouseOver) {
//TODO Glow evt.
obj.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, onLinkMouseOver);
obj.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, onLinkMouseOut);
}
}
//animate atrribute
var animationParam:String = objectList[j].attribute("animation");
if (animationParam) {
var parsed2:Array = animationParam.substring(0,animationParam.length-1).split("(");
var animationType:String = parsed2[0];
var animationArgs:Array = parsed2[1].split(",");
for (var n:uint; n<animationArgs.length ; n++) {
animationArgs[n] = StringUtil.trim(animationArgs[n]);
}
obj.animationType = animationType;
obj.animationArgs = animationArgs;
animationList.push(obj);
}
}
}
/* If a first view node is specified in XML set that one as the first view */
if(settings.firstView && getView(settings.firstView)) {
currentView = getView(settings.firstView);
} else {
currentView = views[0];
}
camera.x = currentView.x;
camera.y = currentView.y;
camera.z = currentView.z;
camera.rotationX = currentView.camrotationX;
camera.rotationY = currentView.camrotationY;
startVideos(currentView);
startTour(null);
/* DEBUG: display the current view name in debug information */
if (debugMode) {
text.text = currentView.name;
}
}
public function startVideos(v:student_powerrail_PowerRailView):void {
for (var t:uint = 0 ; t<v.objects.length ; t++) {
if (v.objects[t].stream) {
v.objects[t].stream.play("../assets/student/powerrail/" + v.objects[t].xml.attribute("source"));
}
}
}
public function stopVideos(v:student_powerrail_PowerRailView):void {
for (var t:uint = 0 ; t<v.objects.length ; t++) {
if (v.objects[t].stream) {
v.objects[t].stream.pause();
}
}
}
/* If an internal link is clicked, change current view to the passed name */
public function onInternalLink(evt:InteractiveScene3DEvent):void {
stopVideos(currentView);
currentView = getView(evt.target.clickArgs[0]);
startVideos(currentView);
if (debugMode) {
text.text = currentView.name;
}
}
/* If an internal link is clicked, open the passed url */
public function onExternalLink(evt:InteractiveScene3DEvent):void {
var request:URLRequest = new URLRequest(evt.target.clickArgs[0]);
try {
navigateToURL(request, settings.openLinksInNewWindow);
} catch (e:Error) {
trace("Error occurred!");
}
}
/* If an internal link is clicked, open the passed url */
public function onGlowLink(evt:InteractiveScene3DEvent):void {
if (!filterToggle) {
switch (evt.target.clickArgs[0]) {
case "glow":
var glow:GlowFilter = new GlowFilter;
glow.color = 0x9c9c9c;
glow.blurX = evt.target.clickArgs[1];
glow.blurY = evt.target.clickArgs[1];
this.filters = [glow];
filterToggle = true;
break;
case "convolution":
var convolution:ConvolutionFilter = new ConvolutionFilter;
convolution.matrix = [0, 0, 1,
0, 1, 0,
1, 1, 1];
convolution.color = 0x9c9c9c;
this.filters = [convolution];
filterToggle = true;
break;
case "blur":
var blur:BlurFilter = new BlurFilter;
blur.blurX = evt.target.clickArgs[1];
blur.blurY = evt.target.clickArgs[1];
this.filters = [blur];
filterToggle = true;
break;
}
} else {
this.filters = [];
filterToggle = false;
}
}
/* Change the cursor into a hand when hovering over a clickable object */
public function onLinkMouseOver(evt:InteractiveScene3DEvent):void {
/*
//TODO Glow evt.?
var glow:GlowFilter = new GlowFilter;
glow.color = 0x9c9c9c;
glow.blurX = 17;
glow.blurY = 17;
evt.target.filters = [glow];
*/
viewport.buttonMode = true;
}
/* Change the cursor back to normal when moving away from clickable object */
public function onLinkMouseOut(evt:InteractiveScene3DEvent):void {
viewport.buttonMode = false;
}
/* This function sets up all the possible events that can occur */
public function initEvents():void {
/* Create eventlistener that gets called when every frame is entered */
addEventListener(Event.ENTER_FRAME, onEnterFrame);
/* These eventlisteners handle the keyboard events */
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
/* Process each frame individually */
public function processFrame():void {
/* animate objects */
for (var i:uint = 0 ; i<animationList.length ; i++) {
switch (animationList[i].animationType) {
case "RotateX":
animationList[i].rotationX += Number(animationList[i].animationArgs[0]);
break;
case "RotateY":
animationList[i].rotationY += Number(animationList[i].animationArgs[0]);
break;
case "RotateZ":
animationList[i].rotationZ += Number(animationList[i].animationArgs[0]);
break;
}
}
/* If the 'W' or 'UP' key is pressed down, move to the next view */
//if (upKeyDown) {
// goToNext();
//}
/* If the 'S' or 'DOWN' key is pressed down, move to the previous view */
//if (downKeyDown) {
// goToPrevious();
//}
/* Tween the camera to the current view */
if(!Tweener.isTweening(camera)) {
Tweener.addTween(camera, { x:currentView.x,
y:currentView.y,
z:currentView.z,
rotationX:currentView.camrotationX,
rotationY:currentView.camrotationY,
time:currentView.tweenTime,
transition:"linear"});
}
/* Rotate by mouse interaction if enabled in XML settings */
if (settings.mouseViewInteraction==true && currentView.camrotationX==0 && currentView.camrotationY==0) {
camera.rotationY = currentView.camrotationY - 25 + ((mouseX / stage.width) * 50);
camera.rotationX = currentView.camrotationX - 25 + ((mouseY / stage.height) * 50);
}
}
/* This function changes the current view to the next one when called */
public function goToNext():void {
//if(!Tweener.isTweening(camera)) {
/* Check if the current view has a 'next', if not fetch the next view in the list */
if (currentView.next && getView(currentView.next)) {
var newView:student_powerrail_PowerRailView = getView(currentView.next);
stopVideos(currentView);
currentView = (newView ? newView : currentView);
startVideos(currentView);
tourCounter = currentView.tourTimeout;
//een niet bestaande name levert nu een nullpointer op... logisch
} else if (currentView.id<views.length-1) {
stopVideos(currentView);
currentView = views[currentView.id + 1];
startVideos(currentView);
tourCounter = currentView.tourTimeout;
}
/* DEBUG: display the current view name in debug information */
if (debugMode) {
text.text = currentView.name;
}
//}
}
/* This function changes the current view to the previous one when called */
public function goToPrevious():void {
//if(!Tweener.isTweening(camera)) {
/* Check if the current view has a 'previous', if not fetch the previous view in the list */
if (currentView.previous && getView(currentView.previous)) {
stopVideos(currentView);
currentView = getView(currentView.previous);
startVideos(currentView);
tourCounter = currentView.tourTimeout;
} else if (currentView.id>0) {
stopVideos(currentView);
currentView = views[currentView.id - 1];
startVideos(currentView);
tourCounter = currentView.tourTimeout;
}
/* DEBUG: display the current view name in debug information */
if (debugMode) {
text.text = currentView.name;
}
//}
}
/* This function searches the viewlist and returns the object corresponding with the given name */
public function getView(needle:String):student_powerrail_PowerRailView {
for (var i:uint = 0 ; i<views.length ; i++) {
if (views[i].name==needle) {
return views[i];
}
}
return null;
}
/* Process each frame and render them individual */
public function onEnterFrame(evt:Event):void {
processFrame();
renderer.renderScene(scene, camera, viewport);
}
/* When a keyboardkey is pressed down, update values to true */
public function onKeyDown(evt:KeyboardEvent):void {
/* Keycodes for keyboardkeys; W: 87, S: 83, UP: 38, DOWN: 40 */
if (evt.keyCode==87 || evt.keyCode==38) {
goToNext();
upKeyDown = true;
} else if (evt.keyCode==83 || evt.keyCode==40) {
goToPrevious();
downKeyDown = true;
}
}
/* When a keyboardkey is release, update values to false */
public function onKeyUp(evt:KeyboardEvent):void {
switch(evt.keyCode) {
case 87:
upKeyDown = false;
break;
case 83:
downKeyDown = false;
break;
case 38:
upKeyDown = false;
break;
case 40:
downKeyDown = false;
break;
}
}
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.