<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:mx="library://ns.adobe.com/flex/mx"
    width="680" height="500" backgroundColor="0x000000" creationComplete="init();"
    skinClass="skins.AppSkin" viewSourceURL="srcview/index.html">

    <fx:Style source="default.css" />
    
    <fx:Script>
    <![CDATA[
        import away3d.containers.View3D;
        
        import com.polyGeek.drawing3D.TronClock;
        
        /* Away3D */
        private var _view                    : View3D;
        
        
        /* Others */
        private var _tic                     : Timer;
        private var _mouseDownPoint            : Point;
        private var _tronClock                : TronClock;
        private var _wobbleRange            : int             = 25;
        private var _wobbleIndex            : Number        = 0;
        
        private function init():void {
            init3D();
            initScene();
            
            _tic = new Timer( 33 );
            _tic.addEventListener( TimerEvent.TIMER, onTic );
            _tic.start();
            
            // Filling the 'surface' SpriteVisualElement that Away3D is drawn
            // onto with black so that it can recieve mouse clicks no matter 
            // where you click on it.
            surface.graphics.beginFill( 0 );
            surface.graphics.drawRect( 0, 0, 600, 600 );
            surface.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
            
            var date : Date     = new Date();
            monthSlider.value     = date.month;
            dateSlider.value     = date.date
            hourSlider.value    = date.hours;
            minSlider.value        = date.minutes;
            secSlider.value        = date.seconds;
        }
        
        private function init3D():void {
            _view = new View3D();
            // Center the view within the stage.
            _view.x = 345;
            _view.y = 250;
            surface.addChild( _view );
            
            
        }
        
        private function initScene():void {
            // Trident
            /* var trident : Trident = new Trident( 200, true );
            _view.scene.addChild( trident );
            trident.ownCanvas = true; */
            
            _tronClock = new TronClock( null, 250, true );
            // Rotate the scene for the initial pose.
            _view.scene.rotationX = -30;
            _view.scene.rotationY = -36//
            _view.scene.addChild( _tronClock );
        }
        
        private function onTic( e:TimerEvent ):void {
            
            if( autoWobble.selected ) {
                // Make the clock wobble
                _wobbleIndex += 0.01;
                _view.scene.rotationX = -_wobbleRange * Math.cos( _wobbleIndex );
                _view.scene.rotationY = -_wobbleRange * Math.sin( _wobbleIndex );
            }
            
            _view.render();
        }
        
        /*************************************
         *    Mouse Drag Events
         **************************************/
        private function onMouseDown( e:MouseEvent ):void {
            autoWobble.selected = false;
            mouseDragMsg.visible = false;
            _mouseDownPoint = new Point( e.stageX, e.stageY );
            surface.addEventListener( MouseEvent.MOUSE_MOVE, onMouseMove );
            surface.addEventListener( MouseEvent.MOUSE_UP,    onMouseUp );
        }
        
        private function onMouseMove( e:MouseEvent ):void {
            _view.scene.rotationY += ( _mouseDownPoint.x - e.stageX ) / 4;
            _view.scene.rotationX -= ( _mouseDownPoint.y - e.stageY ) / 4;
            trace( "rotation : " + _view.scene.rotationX + " : " + _view.scene.rotationY );
            _mouseDownPoint = new Point( e.stageX, e.stageY );
        }
        
        private function onMouseUp( e:MouseEvent ):void {
            surface.removeEventListener( MouseEvent.MOUSE_MOVE, onMouseMove );
            surface.removeEventListener( MouseEvent.MOUSE_UP, onMouseUp );
            _mouseDownPoint = null;
        }
        
        private function onTimeChange():void {
            clockTickingCB.selected = false;
            _tronClock.isClockTicking = false;
            var date : Date = new Date( null, monthSlider.value, dateSlider.value, hourSlider.value, minSlider.value, secSlider.value );
            _tronClock.time = date;
        }
        
        private function onClockTicking():void {
            _tronClock.isClockTicking = clockTickingCB.selected;
        }
    ]]>
    </fx:Script>
    
    <s:SpriteVisualElement id="surface" />

    <s:VGroup top="5" left="5">
        <s:CheckBox id="autoWobble" label="Auto Wobble on?" selected="false" />
        <s:CheckBox id="clockTickingCB" label="Click Ticking?" change="onClockTicking();" selected="true" />
        
        <s:Label text="month: { monthSlider.value }" />
        <s:HSlider id="monthSlider" minimum="0" maximum="11" change="onTimeChange();" />
        
        <s:Label text="date: { dateSlider.value }" />
        <s:HSlider id="dateSlider" minimum="1" maximum="30" change="onTimeChange();" />
        
        <s:Label text="hour: { hourSlider.value }" />
        <s:HSlider id="hourSlider" minimum="0" maximum="23" change="onTimeChange();" />
        
        <s:Label text="min: { minSlider.value }" />
        <s:HSlider id="minSlider" minimum="0" maximum="59" change="onTimeChange();" />
        
        <s:Label text="sec: { secSlider.value }" />
        <s:HSlider id="secSlider" minimum="0" maximum="59" change="onTimeChange();" />
    </s:VGroup>
    
    <s:Label id="mouseDragMsg" text="Click and drag to rotate view." bottom="10" horizontalCenter="0" />
    
</s:Application>