Return to Snippet

Revision: 24868
at March 13, 2010 10:51 by LeeProbert


Initial Code
package com.game
{
	import caurina.transitions.Tweener;
	
	import com.app.data.AppFlags;
	import com.app.data.Config;
	import com.app.data.Data;
	import com.game.utils.GameUtils;
	import com.robot.comm.DispatchManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;
	
	import swingpants.effect.MagnifyingGlass;

	public class ViewfinderController extends EventDispatcher
	{
		public static const VIEWFINDER_CLICKED:String = "viewfinderClicked";
		
		protected const FRICTION:Number = 0.1;
		protected const REFRACTION:Number=0.5;
		protected const RADIUS:Number = 80;
		
		protected var isFirstKeyPress:Boolean = true;
		//----------------------------------------------------------------
		protected var _viewfinder_mc:Sprite;
		public function set viewfinder_mc(v:Sprite):void { _viewfinder_mc = v; };
		public function get viewfinder_mc():Sprite { return _viewfinder_mc; };
		//----------------------------------------------------------------
		protected var _magnifying_glass:MagnifyingGlass;
		public function set magnifying_glass(v:MagnifyingGlass):void { _magnifying_glass = v; };
		public function get magnifying_glass():MagnifyingGlass { return _magnifying_glass; };
		//----------------------------------------------------------------
		protected var _mousePoint:Point;
		public function set mousePoint(v:Point):void { _mousePoint = v; };
		public function get mousePoint():Point { return _mousePoint; };
		//----------------------------------------------------------------
		protected var _magPoint:Point;
		public function set magPoint(v:Point):void { _magPoint = v; };
		public function get magPoint():Point { return _magPoint; };
		//----------------------------------------------------------------
		public function ViewfinderController(target:IEventDispatcher=null)
		{
			super(target);
			
			DispatchManager.addEventListener(AppFlags.FREEZE, freeze );
			DispatchManager.addEventListener(AppFlags.UNFREEZE, unfreeze );
		}
		//---------------------------------------------------
		public function addViewfinderSprite(mc:Sprite,mapBmpData:BitmapData):void
		{
			viewfinder_mc = mc;
			mousePoint = new Point(0,0);
			GameManager.getInstance().gameSprite.addChild(viewfinder_mc);
			viewfinder_mc.alpha = 0;
			viewfinder_mc.y = Config.O_H/2;
			viewfinder_mc.x = Config.O_W/2;
			
			magnifying_glass = new MagnifyingGlass(REFRACTION,RADIUS, new Point(viewfinder_mc.x , viewfinder_mc.y), mapBmpData);
			viewfinder_mc.addChildAt(magnifying_glass,0);
		}
		//---------------------------------------------------
		public function enable():void
		{
			//Mouse.hide();
			Tweener.addTween( viewfinder_mc, { alpha:1, time:1, transition:'easeOutCubic'} ) ;
			
			viewfinder_mc.addEventListener(Event.ENTER_FRAME,moveViewfinder);
			viewfinder_mc.addEventListener(MouseEvent.CLICK,click);
			
			viewfinder_mc.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
			viewfinder_mc.stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
			
			viewfinder_mc.focusRect = false;
			viewfinder_mc.stage.focus = viewfinder_mc;
		}
		//---------------------------------------------------
		public function disable():void
		{
			//Mouse.show();
			Tweener.addTween( viewfinder_mc, { alpha:0, time:1, transition:'easeOutCubic'} ) ;
			
			viewfinder_mc.removeEventListener(Event.ENTER_FRAME,moveViewfinder);
			viewfinder_mc.removeEventListener(MouseEvent.CLICK,click);
			
			viewfinder_mc.stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
			viewfinder_mc.stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
			
			viewfinder_mc.tabEnabled = false;
		}
		//----------------------------------------------------------------
		
		
		
		//-------------------------------------------------------------------------------------
		protected function click(e:Event = null):void
		{
			if(!GameManager.getInstance().debug) viewfinder_mc.removeEventListener(MouseEvent.CLICK,click);
			
			if(!magPoint) magPoint = new Point();
			magPoint.x = viewfinder_mc.x;
			magPoint.y = viewfinder_mc.y;
			
			//disable();
			
			GameManager.getInstance().gameMain.playSound("click");
			dispatchEvent(new Event(VIEWFINDER_CLICKED,true));
		}
		//-------------------------------------------------------------------------------------
		protected function keyDownHandler(e:KeyboardEvent):void
		{
			/*
			Constants only available with the AIR runtime Keyboard class.
			
			W = 87
			A = 65
			S = 83
			D = 68
			X = 88
			*/
			
			if(e.keyCode == 65 || e.keyCode == 83 || e.keyCode == 87 || e.keyCode == 68 || e.keyCode == 88)
			{
				var isShift:Boolean = e.shiftKey;
				
				viewfinder_mc.removeEventListener(MouseEvent.MOUSE_MOVE,resumeMouseControl);
				viewfinder_mc.removeEventListener(Event.ENTER_FRAME,moveViewfinder);
				
				if(e.keyCode == 87) mousePoint.y -= isFirstKeyPress? isShift? 10 : 1 : isShift? 20 : 10;
				if(e.keyCode == 83) mousePoint.y += isFirstKeyPress? isShift? 10 : 1 : isShift? 20 : 10;
				if(e.keyCode == 65) mousePoint.x -= isFirstKeyPress? isShift? 10 : 1 : isShift? 20 : 10;
				if(e.keyCode == 68) mousePoint.x += isFirstKeyPress? isShift? 10 : 1 : isShift? 20 : 10;
				
				viewfinder_mc.x = mousePoint.x;
				viewfinder_mc.y = mousePoint.y;
				
				magnifying_glass.magnifyingGlassPosition = new Point(viewfinder_mc.x, viewfinder_mc.y);
				magnifying_glass.update();
				
				if(e.keyCode == 88)
				{
					click();
				}
				
				isFirstKeyPress = false;
			}
		}
		//-------------------------------------------------------------------------------------
		protected function keyUpHandler(e:KeyboardEvent):void
		{
			isFirstKeyPress = true;
			if(e.keyCode == 65 || e.keyCode == 83 || e.keyCode == 87 || e.keyCode == 68 || e.keyCode == 88)
			{
				viewfinder_mc.addEventListener(MouseEvent.MOUSE_MOVE,resumeMouseControl);
			}
		}
		//-------------------------------------------------------------------------------------
		protected function resumeMouseControl(e:MouseEvent):void
		{
			viewfinder_mc.removeEventListener(MouseEvent.MOUSE_MOVE,resumeMouseControl);
			viewfinder_mc.addEventListener(Event.ENTER_FRAME,moveViewfinder);
		}
		//-------------------------------------------------------------------------------------
		protected function moveViewfinder(e:Event):void
		{
			mousePoint.x = GameManager.getInstance().gameSprite.mouseX;
			mousePoint.y = GameManager.getInstance().gameSprite.mouseY;
			
			var revertX:int = viewfinder_mc.x;
			var revertY:int = viewfinder_mc.y;
			
			viewfinder_mc.x = viewfinder_mc.x+((mousePoint.x-viewfinder_mc.x)*FRICTION);
			viewfinder_mc.y = viewfinder_mc.y+((mousePoint.y-viewfinder_mc.y)*FRICTION);
			
			if(viewfinder_mc.x <= 0 || viewfinder_mc.x >= Config.O_W) 
				viewfinder_mc.x = revertX;
			
			if(viewfinder_mc.y <= 0 || viewfinder_mc.y >= Config.O_H) 
				viewfinder_mc.y = revertY;
			
			magnifying_glass.magnifyingGlassPosition = new Point(viewfinder_mc.x, viewfinder_mc.y);
			magnifying_glass.update();
		}
		//-------------------------------------------------------------------------------------
		protected function freeze(e:Event):void
		{
			trace("freeze viewfinder");
			disable();
		}
		//-------------------------------------------------------------------------------------
		protected function unfreeze(e:Event):void
		{
			trace("unfreeze viewfinder");
			enable();
		}
	}
}

Initial URL


Initial Description


Initial Title
Class used for object following mouse with elasticity including keyboard control capture

Initial Tags
textmate

Initial Language
Other