/*Copyright (c) 2009 Anna Filina Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ package utils { import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Point; import flash.text.TextField; import org.papervision3d.core.proto.CameraObject3D; public class pv3dCameraControl extends Sprite { protected var _camera:CameraObject3D; protected var _btnX:MovieClip; protected var _btnY:MovieClip; protected var _btnZ:MovieClip; protected var _btnRotX:MovieClip; protected var _btnRotY:MovieClip; protected var _btnRotZ:MovieClip; protected var _btnZoom:MovieClip; protected var _changingX:Boolean = false; protected var _changingY:Boolean = false; protected var _changingZ:Boolean = false; protected var _changingRotX:Boolean = false; protected var _changingRotY:Boolean = false; protected var _changingRotZ:Boolean = false; protected var _changingZoom:Boolean = false; protected var _moveOrigin:Point = new Point(0,0); protected var _valueOrigin:Number; public function pv3dCameraControl(camera:CameraObject3D) { super(); _camera = camera; // position _btnX = createButton('x: '+_camera.x); _btnX.addEventListener(MouseEvent.MOUSE_DOWN, function():void {_changingX = true; _valueOrigin = _camera.x; _moveOrigin = new Point(stage.mouseX, stage.mouseY)}); _btnY = createButton('y: '+_camera.y); _btnY.addEventListener(MouseEvent.MOUSE_DOWN, function():void {_changingY = true; _valueOrigin = _camera.y; _moveOrigin = new Point(stage.mouseX, stage.mouseY)}); _btnY.y = 20; _btnZ = createButton('z: '+_camera.z); _btnZ.addEventListener(MouseEvent.MOUSE_DOWN, function():void {_changingZ = true; _valueOrigin = _camera.z; _moveOrigin = new Point(stage.mouseX, stage.mouseY)}); _btnZ.y = 40; // rotation _btnRotX = createButton('rot x: '+_camera.rotationX); _btnRotX.addEventListener(MouseEvent.MOUSE_DOWN, function():void {_changingRotX = true; _valueOrigin = _camera.rotationX; _moveOrigin = new Point(stage.mouseX, stage.mouseY)}); _btnRotX.x = 100; _btnRotY = createButton('rot y: '+_camera.rotationY); _btnRotY.addEventListener(MouseEvent.MOUSE_DOWN, function():void {_changingRotY = true; _valueOrigin = _camera.rotationY; _moveOrigin = new Point(stage.mouseX, stage.mouseY)}); _btnRotY.y = 20; _btnRotY.x = 100; _btnRotZ = createButton('rot z: '+_camera.rotationZ); _btnRotZ.addEventListener(MouseEvent.MOUSE_DOWN, function():void {_changingRotZ = true; _valueOrigin = _camera.rotationZ; _moveOrigin = new Point(stage.mouseX, stage.mouseY)}); _btnRotZ.y = 40; _btnRotZ.x = 100; // zoom _btnZoom = createButton('zoom: '+_camera.zoom); _btnZoom.addEventListener(MouseEvent.MOUSE_DOWN, function():void {_changingZoom = true; _valueOrigin = _camera.zoom; _moveOrigin = new Point(stage.mouseX, stage.mouseY)}); _btnZoom.x = 200; addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler, false, 0, true); } protected function addedToStageHandler(e:Event):void { stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); } protected function mouseMoveHandler(e:MouseEvent):void { if (!e.buttonDown) { _changingX = false; _changingY = false; _changingZ = false; _changingRotX = false; _changingRotY = false; _changingRotZ = false; _changingZoom = false; return; } var pt:Point = new Point(stage.mouseX, stage.mouseY); var offset:Point = _moveOrigin.subtract(pt); if (_changingX) { _camera.x = _valueOrigin + offset.y; (_btnX.getChildByName('textfield') as TextField).text = 'x: '+_camera.x; } else if (_changingY) { _camera.y = _valueOrigin + offset.y; (_btnY.getChildByName('textfield') as TextField).text = 'y: '+_camera.y; } else if (_changingZ) { _camera.z = _valueOrigin + offset.y; (_btnZ.getChildByName('textfield') as TextField).text = 'z: '+_camera.z; } else if (_changingRotX) { _camera.rotationX = _valueOrigin + offset.y; (_btnRotX.getChildByName('textfield') as TextField).text = 'rot x: '+_camera.rotationX; } else if (_changingRotY) { _camera.rotationY = _valueOrigin + offset.y; (_btnRotY.getChildByName('textfield') as TextField).text = 'rot y: '+_camera.rotationY; } else if (_changingRotZ) { _camera.rotationZ = _valueOrigin + offset.y; (_btnRotZ.getChildByName('textfield') as TextField).text = 'rot z: '+_camera.rotationZ; } else if (_changingZoom) { _camera.zoom = _valueOrigin + offset.y; (_btnZoom.getChildByName('textfield') as TextField).text = 'zoom: '+_camera.zoom; } } protected function createButton(text:String):MovieClip { var btn:MovieClip = new MovieClip(); var txt:TextField = new TextField(); txt.selectable = false; txt.text = text; txt.name = 'textfield'; btn.addChild(txt); btn.buttonMode = true; btn.useHandCursor = true; this.addChild(btn); return btn; } } }