<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        xmlns:al="com.almirun.common.view.graph3d.*" 
        layout="absolute" width="500" height="420" creationComplete="init()"
        backgroundGradientAlphas="[1.0, 0.71]"
        backgroundGradientColors="[#010101, #030303]" viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            import org.papervision3d.materials.ColorMaterial;
            import org.papervision3d.objects.primitives.PaperPlane;
            import org.papervision3d.objects.primitives.Plane;
            import org.papervision3d.materials.utils.MaterialsList;
            import org.papervision3d.objects.primitives.Cube;
            import org.papervision3d.objects.primitives.Sphere;
            import com.almirun.common.controllers.PapervisionCameraController;
            import com.almirun.common.view.graph3d.ConcreteGridTerrain;
            import com.almirun.common.view.graph3d.GridTerrain;
            import org.papervision3d.materials.WireframeMaterial;
            import org.papervision3d.view.Viewport3D;
            import com.almirun.common.view.graph3d.PV3DGraph;
            
            private var graph:PV3DGraph;
            private var done:Boolean = false;
            
            public function init():void {
                var frameMaterial:WireframeMaterial = new WireframeMaterial(0x666666, 1, 1);
                frameMaterial.doubleSided = true;
                
                graph = graphContainer.graph;
                graph.wireframeColour = 0xFFFFFF;
                graph.createFrame(frameMaterial, frameMaterial, frameMaterial);
                
                graph.camera.x = 410;
                graph.camera.y = 174;
                graph.camera.z = -277;
                graph.camera.rotationX = -20;
                graph.camera.rotationY = -55;
                graph.camera.rotationZ = 2;
                graph.camera.zoom = 4;
                                
                addEventListener(Event.ENTER_FRAME, handleEnterFrameEvent);
                
                populateGraph();
            }
            
            private function populateGraph():void {
                var terrain:GridTerrain = createArbitraryTerrain();
                graph.useGridTerrain(terrain, 2, 2);
            }
            
            private function createArbitraryTerrain():GridTerrain {
                var terrain:ConcreteGridTerrain = new ConcreteGridTerrain();
                var k:Number = 4;
                
                for (var i:uint = 0; i < 80; i++) {
                    k += Math.random() * 2 - 1;
                    
                    for (var j:uint = 0; j < 40; j++) {
                        terrain.setHeightAt(i, j, k + Math.random() * 4 + 2);
                    }
                }
                
                return terrain;
            }
            
            private function handleEnterFrameEvent(event:Event):void {
                if (!done) {
                    new PapervisionCameraController(graph.camera).stage = stage;
                    done = true;
                }
                
                graph.container.rotationY -= rotationSlider.value;
                
                //removeEventListener(Event.ENTER_FRAME, handleEnterFrameEvent);
            }
            
        ]]>
    </mx:Script>
    <al:PV3DGraphComponent x="10" y="10" width="480" height="376" 
        graphWidth="500" graphHeight="200" graphDepth="300" id="graphContainer" />
    <mx:Button x="10" y="394" label="Regenerate" click="populateGraph()" id="regenerateButton" />
    <mx:HSlider x="330" y="394" minimum="-5" maximum="5" enabled="true" id="rotationSlider"
        allowTrackClick="true" value="-1"  liveDragging="true"/>
    <mx:Label x="234" y="397" text="Rotation speed:" color="#B8B8B8" fontWeight="bold"/>
</mx:Application>