home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / org / papervision3d / cameras / DebugCamera3D.as < prev    next >
Encoding:
Text File  |  2010-06-23  |  9.9 KB  |  303 lines

  1. package org.papervision3d.cameras
  2. {
  3.    import flash.display.Sprite;
  4.    import flash.display.Stage;
  5.    import flash.events.Event;
  6.    import flash.events.KeyboardEvent;
  7.    import flash.events.MouseEvent;
  8.    import flash.geom.Point;
  9.    import flash.text.TextField;
  10.    import flash.text.TextFormat;
  11.    import flash.ui.Keyboard;
  12.    import org.papervision3d.view.Viewport3D;
  13.    
  14.    public class DebugCamera3D extends Camera3D
  15.    {
  16.       protected var keyLeft:Boolean = false;
  17.       
  18.       protected var targetRotationX:Number = 0;
  19.       
  20.       protected var targetRotationY:Number = 0;
  21.       
  22.       protected var sideFactor:Number = 0;
  23.       
  24.       protected var _propertiesDisplay:Sprite;
  25.       
  26.       protected var viewport3D:Viewport3D;
  27.       
  28.       protected var fovText:TextField;
  29.       
  30.       protected var xText:TextField;
  31.       
  32.       protected var yText:TextField;
  33.       
  34.       protected var zText:TextField;
  35.       
  36.       protected var startPoint:Point;
  37.       
  38.       protected var startRotationX:Number;
  39.       
  40.       protected var startRotationY:Number;
  41.       
  42.       protected var keyBackward:Boolean = false;
  43.       
  44.       protected var farText:TextField;
  45.       
  46.       protected var keyForward:Boolean = false;
  47.       
  48.       protected var rotationXText:TextField;
  49.       
  50.       protected var rotationZText:TextField;
  51.       
  52.       protected var rotationYText:TextField;
  53.       
  54.       protected var forwardFactor:Number = 0;
  55.       
  56.       protected var nearText:TextField;
  57.       
  58.       protected var viewportStage:Stage;
  59.       
  60.       protected var _inertia:Number = 3;
  61.       
  62.       protected var keyRight:Boolean = false;
  63.       
  64.       public function DebugCamera3D(param1:Viewport3D, param2:Number = 90, param3:Number = 10, param4:Number = 5000)
  65.       {
  66.          super(param2,param3,param4,true);
  67.          this.viewport3D = param1;
  68.          this.viewport = param1.sizeRectangle;
  69.          this.focus = this.viewport.height / 2 / Math.tan(param2 / 2 * (Math.PI / 180));
  70.          this.zoom = this.focus / param3;
  71.          this.focus = param3;
  72.          this.far = param4;
  73.          this.displayProperties();
  74.          this.checkStageReady();
  75.       }
  76.       
  77.       protected function keyDownHandler(param1:KeyboardEvent) : void
  78.       {
  79.          switch(param1.keyCode)
  80.          {
  81.             case "W".charCodeAt():
  82.             case Keyboard.UP:
  83.                this.keyForward = true;
  84.                this.keyBackward = false;
  85.                break;
  86.             case "S".charCodeAt():
  87.             case Keyboard.DOWN:
  88.                this.keyBackward = true;
  89.                this.keyForward = false;
  90.                break;
  91.             case "A".charCodeAt():
  92.             case Keyboard.LEFT:
  93.                this.keyLeft = true;
  94.                this.keyRight = false;
  95.                break;
  96.             case "D".charCodeAt():
  97.             case Keyboard.RIGHT:
  98.                this.keyRight = true;
  99.                this.keyLeft = false;
  100.                break;
  101.             case "Q".charCodeAt():
  102.                --rotationZ;
  103.                break;
  104.             case "E".charCodeAt():
  105.                ++rotationZ;
  106.                break;
  107.             case "F".charCodeAt():
  108.                --fov;
  109.                break;
  110.             case "R".charCodeAt():
  111.                ++fov;
  112.                break;
  113.             case "G".charCodeAt():
  114.                near -= 10;
  115.                break;
  116.             case "T".charCodeAt():
  117.                near += 10;
  118.                break;
  119.             case "H".charCodeAt():
  120.                far -= 10;
  121.                break;
  122.             case "Y".charCodeAt():
  123.                far += 10;
  124.          }
  125.       }
  126.       
  127.       public function set inertia(param1:Number) : void
  128.       {
  129.          this._inertia = param1;
  130.       }
  131.       
  132.       protected function setupEvents() : void
  133.       {
  134.          this.viewportStage = this.viewport3D.containerSprite.stage;
  135.          this.viewportStage.addEventListener(MouseEvent.MOUSE_DOWN,this.mouseDownHandler);
  136.          this.viewportStage.addEventListener(MouseEvent.MOUSE_UP,this.mouseUpHandler);
  137.          this.viewportStage.addEventListener(KeyboardEvent.KEY_DOWN,this.keyDownHandler);
  138.          this.viewportStage.addEventListener(KeyboardEvent.KEY_UP,this.keyUpHandler);
  139.          this.viewportStage.addEventListener(Event.ENTER_FRAME,this.onEnterFrameHandler);
  140.       }
  141.       
  142.       protected function displayProperties() : void
  143.       {
  144.          this._propertiesDisplay = new Sprite();
  145.          this._propertiesDisplay.graphics.beginFill(0);
  146.          this._propertiesDisplay.graphics.drawRect(0,0,100,100);
  147.          this._propertiesDisplay.graphics.endFill();
  148.          this._propertiesDisplay.x = 0;
  149.          this._propertiesDisplay.y = 0;
  150.          var _loc1_:TextFormat = new TextFormat("_sans",9);
  151.          this.xText = new TextField();
  152.          this.yText = new TextField();
  153.          this.zText = new TextField();
  154.          this.rotationXText = new TextField();
  155.          this.rotationYText = new TextField();
  156.          this.rotationZText = new TextField();
  157.          this.fovText = new TextField();
  158.          this.nearText = new TextField();
  159.          this.farText = new TextField();
  160.          var _loc2_:Array = [this.xText,this.yText,this.zText,this.rotationXText,this.rotationYText,this.rotationZText,this.fovText,this.nearText,this.farText];
  161.          var _loc3_:int = 10;
  162.          var _loc4_:Number = 0;
  163.          while(_loc4_ < _loc2_.length)
  164.          {
  165.             _loc2_[_loc4_].width = 100;
  166.             _loc2_[_loc4_].selectable = false;
  167.             _loc2_[_loc4_].textColor = 16776960;
  168.             _loc2_[_loc4_].text = "";
  169.             _loc2_[_loc4_].defaultTextFormat = _loc1_;
  170.             _loc2_[_loc4_].y = _loc3_ * _loc4_;
  171.             this._propertiesDisplay.addChild(_loc2_[_loc4_]);
  172.             _loc4_++;
  173.          }
  174.          this.viewport3D.addChild(this._propertiesDisplay);
  175.       }
  176.       
  177.       protected function onEnterFrameHandler(param1:Event) : void
  178.       {
  179.          if(this.keyForward)
  180.          {
  181.             this.forwardFactor += 50;
  182.          }
  183.          if(this.keyBackward)
  184.          {
  185.             this.forwardFactor += -50;
  186.          }
  187.          if(this.keyLeft)
  188.          {
  189.             this.sideFactor += -50;
  190.          }
  191.          if(this.keyRight)
  192.          {
  193.             this.sideFactor += 50;
  194.          }
  195.          var _loc2_:Number = this.rotationX + (this.targetRotationX - this.rotationX) / this._inertia;
  196.          var _loc3_:Number = this.rotationY + (this.targetRotationY - this.rotationY) / this._inertia;
  197.          this.rotationX = Math.round(_loc2_ * 10) / 10;
  198.          this.rotationY = Math.round(_loc3_ * 10) / 10;
  199.          this.forwardFactor += (0 - this.forwardFactor) / this._inertia;
  200.          this.sideFactor += (0 - this.sideFactor) / this._inertia;
  201.          if(this.forwardFactor > 0)
  202.          {
  203.             this.moveForward(this.forwardFactor);
  204.          }
  205.          else
  206.          {
  207.             this.moveBackward(-this.forwardFactor);
  208.          }
  209.          if(this.sideFactor > 0)
  210.          {
  211.             this.moveRight(this.sideFactor);
  212.          }
  213.          else
  214.          {
  215.             this.moveLeft(-this.sideFactor);
  216.          }
  217.          this.xText.text = "x:" + int(x);
  218.          this.yText.text = "y:" + int(y);
  219.          this.zText.text = "z:" + int(z);
  220.          this.rotationXText.text = "rotationX:" + int(_loc2_);
  221.          this.rotationYText.text = "rotationY:" + int(_loc3_);
  222.          this.rotationZText.text = "rotationZ:" + int(rotationZ);
  223.          this.fovText.text = "fov:" + Math.round(fov);
  224.          this.nearText.text = "near:" + Math.round(near);
  225.          this.farText.text = "far:" + Math.round(far);
  226.       }
  227.       
  228.       protected function mouseUpHandler(param1:MouseEvent) : void
  229.       {
  230.          this.viewportStage.removeEventListener(MouseEvent.MOUSE_MOVE,this.mouseMoveHandler);
  231.       }
  232.       
  233.       protected function keyUpHandler(param1:KeyboardEvent) : void
  234.       {
  235.          switch(param1.keyCode)
  236.          {
  237.             case "W".charCodeAt():
  238.             case Keyboard.UP:
  239.                this.keyForward = false;
  240.                break;
  241.             case "S".charCodeAt():
  242.             case Keyboard.DOWN:
  243.                this.keyBackward = false;
  244.                break;
  245.             case "A".charCodeAt():
  246.             case Keyboard.LEFT:
  247.                this.keyLeft = false;
  248.                break;
  249.             case "D".charCodeAt():
  250.             case Keyboard.RIGHT:
  251.                this.keyRight = false;
  252.          }
  253.       }
  254.       
  255.       public function get propsDisplay() : Sprite
  256.       {
  257.          return this._propertiesDisplay;
  258.       }
  259.       
  260.       public function get inertia() : Number
  261.       {
  262.          return this._inertia;
  263.       }
  264.       
  265.       protected function onAddedToStageHandler(param1:Event) : void
  266.       {
  267.          this.setupEvents();
  268.       }
  269.       
  270.       protected function mouseMoveHandler(param1:MouseEvent) : void
  271.       {
  272.          this.targetRotationY = this.startRotationY - (this.startPoint.x - this.viewportStage.mouseX) / 2;
  273.          this.targetRotationX = this.startRotationX + (this.startPoint.y - this.viewportStage.mouseY) / 2;
  274.       }
  275.       
  276.       protected function mouseDownHandler(param1:MouseEvent) : void
  277.       {
  278.          this.viewportStage.addEventListener(MouseEvent.MOUSE_MOVE,this.mouseMoveHandler);
  279.          this.startPoint = new Point(this.viewportStage.mouseX,this.viewportStage.mouseY);
  280.          this.startRotationY = this.rotationY;
  281.          this.startRotationX = this.rotationX;
  282.       }
  283.       
  284.       public function set propsDisplay(param1:Sprite) : void
  285.       {
  286.          this._propertiesDisplay = param1;
  287.       }
  288.       
  289.       private function checkStageReady() : void
  290.       {
  291.          if(this.viewport3D.containerSprite.stage == null)
  292.          {
  293.             this.viewport3D.containerSprite.addEventListener(Event.ADDED_TO_STAGE,this.onAddedToStageHandler);
  294.          }
  295.          else
  296.          {
  297.             this.setupEvents();
  298.          }
  299.       }
  300.    }
  301. }
  302.  
  303.