home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Templates / Flash / flashmo_097_3d_sphere / org / papervision3d / scenes / FlexScene3D.as next >
Text File  |  2007-07-18  |  5KB  |  137 lines

  1. package org.papervision3d.scenes
  2. {
  3.     import flash.display.Sprite;
  4.     import mx.containers.Canvas;
  5.     import org.papervision3d.objects.DisplayObject3D;
  6.     import flash.utils.Dictionary;
  7.     import flash.utils.getTimer;
  8.     import mx.core.UIComponent;
  9.  
  10.     public class FlexScene3D extends Scene3D
  11.     {
  12.         private var containerList : Array;
  13.         private var spriteList : Dictionary;
  14.         
  15.         public function FlexScene3D(container:Sprite)
  16.         {
  17.             super(container);
  18.             
  19.             this.containerList = new Array();
  20.             spriteList = new Dictionary();
  21.         }
  22.         
  23.         // ___________________________________________________________________ A D D C H I L D
  24.         //
  25.         //   AA   DDDDD  DDDDD   CCCC  HH  HH II LL     DDDDD
  26.         //  AAAA  DD  DD DD  DD CC  CC HH  HH II LL     DD  DD
  27.         // AA  AA DD  DD DD  DD CC     HHHHHH II LL     DD  DD
  28.         // AAAAAA DD  DD DD  DD CC  CC HH  HH II LL     DD  DD
  29.         // AA  AA DDDDD  DDDDD   CCCC  HH  HH II LLLLLL DDDDD
  30.     
  31.         /**
  32.         * Adds a child DisplayObject3D instance to the scene.
  33.         *
  34.         * If you add a GeometryObject3D symbol, a new DisplayObject3D instance is created.
  35.         * 
  36.         * CanvasScene3D is a fix for Flex2 - Its Flex2's version of MovieScene3D.  If you want your objects separated into sprite containers, use CanvasScene3D.
  37.         * I DID try using pure Canvas objects where sprite was used, but I kept getting "index out of range errors" and just kept getting internal Flex errors.
  38.         *
  39.         * [TODO: If you add a child object that already has a different display object container as a parent, the object is removed from the child list of the other display object container.]
  40.         *
  41.         * @param    child    The GeometryObject3D symbol or DisplayObject3D instance to add as a child of the scene.
  42.         * @param    name    An optional name of the child to add or create. If no name is provided, the child name will be used.
  43.         * @return    The DisplayObject3D instance that you have added or created.
  44.         */
  45.         public override function addChild( child :DisplayObject3D, name :String=null ):DisplayObject3D
  46.         {
  47.             child = super.addChild( child, name );
  48.             
  49.             // for Flex2 we need to create a UIComponent
  50.             var uiDumby:UIComponent = new UIComponent();
  51.             
  52.             // now create the sprite container
  53.             child.container = new Sprite();
  54.             
  55.             // add the sprite to the UIComponent - which is legal, but adding a sprite to a Canvas or any other sub classed UIComponent is not
  56.             uiDumby.addChild(child.container);
  57.             
  58.             // now add the uiDumby to the canvas container
  59.             container.addChild( uiDumby );
  60.             
  61.             // push for use in the renderer
  62.             this.containerList.push( child.container );
  63.             spriteList[child] = child.container;
  64.             return child;
  65.         }
  66.         
  67.         public function getSprite(child:DisplayObject3D):Canvas
  68.         {
  69.             return spriteList[child];
  70.         }
  71.         
  72.         // ___________________________________________________________________ R E N D E R   C A M E R A
  73.         //
  74.         // RRRRR  EEEEEE NN  NN DDDDD  EEEEEE RRRRR
  75.         // RR  RR EE     NNN NN DD  DD EE     RR  RR
  76.         // RRRRR  EEEE   NNNNNN DD  DD EEEE   RRRRR
  77.         // RR  RR EE     NN NNN DD  DD EE     RR  RR
  78.         // RR  RR EEEEEE NN  NN DDDDD  EEEEEE RR  RR CAMERA
  79.     
  80.         /**
  81.         * Generates an image from the camera's point of view and the visible models of the scene.
  82.         *
  83.         * @param    camera        camera to render from.
  84.         */
  85.         protected override function renderObjects( sort:Boolean ):void
  86.         {
  87.             var objectsLength :Number = this.objects.length;
  88.     
  89.             // Clear object container
  90.             var gfx                                  :Sprite;
  91.             var containerList                         :Array = this.containerList;
  92.             var i                                    :Number = 0;
  93.     
  94.             // Clear all known object
  95.             while( gfx = containerList[i++] ) gfx.graphics.clear();
  96.     
  97.             // Render
  98.             var p                                   :DisplayObject3D;
  99.             var objects                             :Array  = this.objects;
  100.             i = objects.length;
  101.     
  102.             if( sort )
  103.             {
  104.                 while( p = objects[--i] )
  105.                 {
  106.                     if( p.visible )
  107.                     {
  108.                         // this keeps the memory consumption stable.  Otherwise, there was a slight memory leak with each render
  109.                         if(container.getChildByName("container")) container.removeChild(container.getChildByName("container"));
  110.                         
  111.                         // same trick: add sprite to a UIComponent stand-in
  112.                         var uiDumby:UIComponent = new UIComponent();
  113.                         uiDumby.name = "container";
  114.                         
  115.                         uiDumby.addChild(p.container);
  116.                         container.addChild( uiDumby );
  117.                         p.render( this );
  118.                     }
  119.                 }
  120.             }
  121.             else
  122.             {
  123.                 while( p = objects[--i] )
  124.                 {
  125.                     if( p.visible )
  126.                     {
  127.                         p.render( this );
  128.                     }
  129.                 }
  130.             }
  131.     
  132.             // Update stats
  133.             var stats:Object  = this.stats;
  134.             stats.performance = getTimer() - stats.performance;
  135.         }        
  136.     }
  137. }