<?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" minWidth="955" minHeight="600" 
               xmlns:components="com.scianski.components.*"
               initialize="initializeApplicationHandler(event)" viewSourceURL="srcview/index.html">
    
    <s:layout>
        <s:HorizontalLayout paddingLeft="10" paddingRight="10" paddingTop="10" paddingBottom="10"/>
    </s:layout>
    
    <fx:Script>
        <![CDATA[
            import away3d.containers.View3D;
            import away3d.core.base.Mesh;
            import away3d.core.base.Object3D;
            import away3d.loaders.Md2;
            import away3d.materials.BitmapMaterial;
            
            import mx.collections.ArrayList;
            import mx.controls.Alert;
            import mx.events.FlexEvent;
            
            import spark.events.IndexChangeEvent;
            
            // Embeded model textures
            [Embed("assets/models/textures/car.png")]
            private var _CarTexture : Class;
            [Embed("assets/models/textures/van.png")]
            private var _VanTexture : Class;
            [Embed("assets/models/textures/fighter.png")]
            private var _FighterTexture : Class;
            
            // Embeded 3D models
            [Embed("assets/models/car.md2", mimeType="application/octet-stream")]
            private var _CarMD2 : Class;
            [Embed("assets/models/van.md2", mimeType="application/octet-stream")]
            private var _VanMD2 : Class;
            [Embed("assets/models/fighter.md2", mimeType="application/octet-stream")]
            private var _FighterMD2 : Class;
            
            // File loader for the MD2 file format
            private var _md2: Md2;
            
            // Constants for model names
            public static const CAR:String = "Car";
            public static const VAN:String = "Van";
            public static const FIGHTER:String = "Fighter";
            
            // Array for 3D models
            [Bindable]private var _models:ArrayList;
            
            private var _lastAddedModelName:String;

            protected function initializeApplicationHandler(event:FlexEvent):void
            {
                away3DComponent.view = new View3D();
                                
                _models = new ArrayList();
                _models.addItem(parseModel(_CarMD2, _CarTexture, CAR));
                _models.addItem(parseModel(_VanMD2, _VanTexture, VAN));
                _models.addItem(parseModel(_FighterMD2, _FighterTexture, FIGHTER));
            }
            
            protected function list_changeHandler(event:IndexChangeEvent):void
            {
                if(_lastAddedModelName) away3DComponent.view.scene.removeChildByName(_lastAddedModelName);
                var model:Object3D = modelList.selectedItem as Object3D;
                _lastAddedModelName = model.name;
                away3DComponent.view.scene.addChild(model);
            }
            
            protected function parseModel(md2Model:Class, texture:Class, name:String):Mesh
            {        
                _md2 = new Md2();
                
                var _model:Mesh = _md2.parseGeometry(md2Model) as Mesh;
                _model.ownCanvas = true;
                
                // Adjusts the pivot point of the object so that it lies at the center of it's geoemtry
                _model.centerPivot();
                
                // Setting zero position for model
                _model.x = _model.z = _model.y = 0;
                
                var bitmapBodyMaterial:BitmapMaterial = new BitmapMaterial(new texture().bitmapData);
                bitmapBodyMaterial.smooth = true;
                
                _model.material = bitmapBodyMaterial;
                _model.name = name;
                
                return _model;
            }
        ]]>
    </fx:Script>
    
    <components:Away3DComponent id="away3DComponent"/>
    
    <s:Panel width="190" id="panel" title="Select model" 
             mouseOver="{away3DComponent.isMouseOnScene3D = false}"
             mouseOut="{away3DComponent.isMouseOnScene3D = true}">
        
        <s:layout>
            <s:VerticalLayout paddingLeft="0" paddingRight="0" paddingTop="0" paddingBottom="0"/>
        </s:layout>
        
        <s:List id="modelList" dataProvider="{_models}" 
                labelField="name"
                width="100%" 
                change="list_changeHandler(event)" borderVisible="false"/>
    
    </s:Panel>
    
    <s:RichText text="Use mouse wheel to zoom in / out. To move camera around model, press and move left mouse button." width="508" height="13"/>
    
</s:Application>