home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Templates / Flash / flashmo_101_3d_carousel / org / papervision3d / utils / InteractiveSceneManager.as < prev   
Text File  |  2007-07-19  |  7KB  |  179 lines

  1. /**
  2. * ...
  3. * @author John Grden
  4. * @version 0.1
  5. */
  6.  
  7. package org.papervision3d.utils 
  8. {
  9.     import com.blitzagency.xray.logger.XrayLog;
  10.     
  11.     import flash.display.Sprite;
  12.     import flash.events.EventDispatcher;
  13.     import flash.events.MouseEvent;
  14.     import flash.utils.Dictionary;
  15.     
  16.     import org.papervision3d.core.geom.Mesh3D;
  17.     import org.papervision3d.core.proto.CameraObject3D;
  18.     import org.papervision3d.core.proto.SceneObject3D;
  19.     import org.papervision3d.events.InteractiveScene3DEvent;
  20.     import org.papervision3d.objects.DisplayObject3D;
  21.     import flash.events.Event;
  22.  
  23.     public class InteractiveSceneManager extends EventDispatcher
  24.     {
  25.         public static var DEFAULT_SPRITE_ALPHA                        :Number = .0051;
  26.         public static var DEFAULT_FILL_ALPHA                        :Number = .0051;
  27.         public static var DEFAULT_FILL_COLOR                        :Number = 0xFFFFFF;
  28.         
  29.         public var faceDictionary                                    :Dictionary = new Dictionary();
  30.         public var containerDictionary                                :Dictionary = new Dictionary();
  31.         public var container                                        :Sprite = new Sprite();
  32.         public var scene                                            :SceneObject3D;
  33.         
  34.         public var debug                                            :Boolean = false;
  35.         
  36.         protected var dummy                                            :DisplayObject3D = DisplayObject3D.ZERO;
  37.         
  38.         protected var log                                            :XrayLog = new XrayLog();
  39.         
  40.         public function InteractiveSceneManager(p_scene:SceneObject3D):void
  41.         {
  42.             scene = p_scene;
  43.             scene.container.parent.addChild(container);
  44.             container.x = scene.container.x;
  45.             container.y = scene.container.y;
  46.             container.stage.addEventListener(Event.RESIZE, handleResize);
  47.             container.stage.addEventListener(MouseEvent.MOUSE_UP, handleReleaseOutside);
  48.         }
  49.         
  50.         public function addDisplayObject(container3d:DisplayObject3D):void
  51.         {
  52.             if(faceDictionary[container3d] == null) 
  53.             {
  54.                 var icd:InteractiveContainerData = faceDictionary[container3d] = new InteractiveContainerData(container3d);
  55.                 
  56.                 // for reverse lookup when you have the sprite container
  57.                 containerDictionary[icd.container] = container3d;
  58.                 
  59.                 // add mouse events to be captured and passed along
  60.                 icd.container.addEventListener(MouseEvent.MOUSE_DOWN, handleMousePress);
  61.                 icd.container.addEventListener(MouseEvent.MOUSE_UP, handleMouseRelease);
  62.                 icd.container.addEventListener(MouseEvent.CLICK, handleMouseClick);
  63.                 icd.container.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
  64.                 icd.container.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
  65.                 icd.container.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
  66.                 
  67.                 if(debug) log.debug("addDisplayObject id", container3d.id, container3d.name, DEFAULT_SPRITE_ALPHA);
  68.             }
  69.         }
  70.         
  71.         public function drawFace(container3d:DisplayObject3D, x0:Number, x1:Number, x2:Number, y0:Number, y1:Number, y2:Number ):void
  72.         {
  73.             if(faceDictionary[container3d] == null) addDisplayObject(container3d);
  74.             var drawingContainer:InteractiveContainerData = faceDictionary[container3d];
  75.             
  76.             drawingContainer.container.graphics.beginFill(drawingContainer.color, drawingContainer.fillAlpha);
  77.             drawingContainer.container.graphics.moveTo( x0, y0 );
  78.             drawingContainer.container.graphics.lineTo( x1, y1 );
  79.             drawingContainer.container.graphics.lineTo( x2, y2 );
  80.             drawingContainer.container.graphics.endFill();
  81.             drawingContainer.isDrawn = true;
  82.         }
  83.         
  84.         public function getSprite(container3d:DisplayObject3D):Sprite
  85.         {
  86.             return InteractiveContainerData(faceDictionary[container3d]).container;
  87.         }
  88.         
  89.         public function getDisplayObject3D(sprite:Sprite):DisplayObject3D
  90.         {
  91.             return DisplayObject3D(containerDictionary[sprite]);
  92.         }
  93.         
  94.         public function resizeStage():void
  95.         {
  96.             container.x = scene.container.x;
  97.             container.y = scene.container.y;
  98.         }
  99.         
  100.         public function resetFaces():void
  101.         {            
  102.             // clear all triangles/faces that have been drawn
  103.             for each( var item:InteractiveContainerData in faceDictionary)
  104.             {
  105.                 
  106.                 item.container.graphics.clear();
  107.                 item.sort = item.isDrawn;
  108.                 item.isDrawn = false;
  109.             }
  110.             
  111.             // sort the sprite containers
  112.             sortObjects();
  113.         }
  114.         
  115.         protected function sortObjects():void
  116.         {
  117.             var sort:Array = [];
  118.             
  119.             for each( var item:InteractiveContainerData in faceDictionary)
  120.             {
  121.                 if(!item.sort) continue;
  122.                 var distance:Number = item.displayObject3D.screenZ;
  123.                 sort.push({container:item.container, distance:distance});
  124.             }
  125.             
  126.             sort.sortOn("distance", Array.DESCENDING | Array.NUMERIC);
  127.             
  128.             for(var i:Number=0;i<sort.length;i++) container.addChild(sort[i].container);
  129.         }
  130.         
  131.         protected function handleMousePress(e:MouseEvent):void
  132.         {
  133.             if(debug) log.debug("press", DisplayObject3D(containerDictionary[e.target]).name);
  134.             dispatchEvent(new InteractiveScene3DEvent(InteractiveScene3DEvent.OBJECT_PRESS, containerDictionary[e.currentTarget], Sprite(e.currentTarget)));
  135.         }
  136.         
  137.         protected function handleMouseRelease(e:MouseEvent):void
  138.         {
  139.             if(debug) log.debug("release", DisplayObject3D(containerDictionary[e.target]).name);
  140.             dispatchEvent(new InteractiveScene3DEvent(InteractiveScene3DEvent.OBJECT_RELEASE, containerDictionary[e.currentTarget], Sprite(e.currentTarget)));
  141.         }
  142.         
  143.         protected function handleMouseClick(e:MouseEvent):void
  144.         {
  145.             if(debug) log.debug("click", DisplayObject3D(containerDictionary[e.target]).name);
  146.             dispatchEvent(new InteractiveScene3DEvent(InteractiveScene3DEvent.OBJECT_CLICK, containerDictionary[e.currentTarget], Sprite(e.currentTarget)));
  147.         }
  148.         
  149.         protected function handleMouseOver(e:MouseEvent):void
  150.         {
  151.             if(debug) log.debug("Over", DisplayObject3D(containerDictionary[e.target]).name);
  152.             dispatchEvent(new InteractiveScene3DEvent(InteractiveScene3DEvent.OBJECT_OVER, containerDictionary[e.currentTarget], Sprite(e.currentTarget)));
  153.         }
  154.         
  155.         protected function handleMouseOut(e:MouseEvent):void
  156.         {
  157.             if(debug) log.debug("Out", DisplayObject3D(containerDictionary[e.target]).name);
  158.             dispatchEvent(new InteractiveScene3DEvent(InteractiveScene3DEvent.OBJECT_OUT, containerDictionary[e.currentTarget], Sprite(e.currentTarget)));
  159.         }
  160.         
  161.         protected function handleMouseMove(e:MouseEvent):void
  162.         {    
  163.             if(debug) log.debug("Move", DisplayObject3D(containerDictionary[e.target]).name);
  164.             dispatchEvent(new InteractiveScene3DEvent(InteractiveScene3DEvent.OBJECT_MOVE, containerDictionary[e.currentTarget], Sprite(e.currentTarget)));
  165.         }
  166.         
  167.         protected function handleReleaseOutside(e:MouseEvent):void
  168.         {    
  169.             if(debug) log.debug("releaseOutside");
  170.             dispatchEvent(new InteractiveScene3DEvent(InteractiveScene3DEvent.OBJECT_RELEASE_OUTSIDE));
  171.         }
  172.         
  173.         protected function handleResize(e:Event):void
  174.         {
  175.             resizeStage();
  176.         }
  177.     }
  178. }
  179.