PageRenderTime 550ms CodeModel.GetById 413ms app.highlight 10ms RepoModel.GetById 120ms app.codeStats 0ms

/src/away3d/tools/utils/ColorHitMap.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 210 lines | 123 code | 30 blank | 57 comment | 10 complexity | 897b31cfdbcdf267acfaf42cd1027fa3 MD5 | raw file
  1package away3d.tools.utils
  2{
  3	import flash.display.BitmapData;
  4	import flash.events.Event;
  5	import flash.events.EventDispatcher;
  6	
  7	public class ColorHitMap extends EventDispatcher
  8	{
  9		
 10		private var _colorMap:BitmapData;
 11		private var _colorObjects:Vector.<ColorObject>;
 12		private var _scaleX:Number;
 13		private var _scaleY:Number;
 14		private var _offsetX:Number;
 15		private var _offsetY:Number;
 16		
 17		/**
 18		 * Creates a new <code>ColorHitMap</code>
 19		 *
 20		 * @param    bitmapData        The bitmapdata with color regions to act as trigger.
 21		 * @param    scaleX                [optional]    A factor scale along the X axis. Default is 1.
 22		 * @param    scaleY                [optional]    A factor scale along the Y axis. Default is 1.
 23		 *
 24		 * Note that by default the class is considered as centered, like a plane at 0,0,0.
 25		 * Also by default coordinates offset are set. If a map of 256x256 is set, and no custom offsets are set.
 26		 * The read method using the camera position -128 x and -128 z would return the color value found at 0,0 on the map.
 27		 */
 28		public function ColorHitMap(bitmapData:BitmapData, scaleX:Number = 1, scaleY:Number = 1)
 29		{
 30			_colorMap = bitmapData;
 31			_scaleX = scaleX;
 32			_scaleY = scaleY;
 33			
 34			_offsetX = _colorMap.width*.5;
 35			_offsetY = _colorMap.height*.5;
 36		}
 37		
 38		/**
 39		 * If at the given coordinates a color is found that matches a defined color event, the color event will be triggered.
 40		 *
 41		 * @param    x            X coordinate on the source bmd
 42		 * @param    y            Y coordinate on the source bmd
 43		 */
 44		public function read(x:Number, y:Number):void
 45		{
 46			if (!_colorObjects)
 47				return;
 48			
 49			var color:uint = _colorMap.getPixel((x/_scaleX) + _offsetX, (y/_scaleY) + _offsetY);
 50			
 51			var co:ColorObject;
 52			for (var i:uint = 0; i < _colorObjects.length; ++i) {
 53				co = ColorObject(_colorObjects[i]);
 54				if (co.color == color) {
 55					fireColorEvent(co.eventID);
 56					break;
 57				}
 58			}
 59		}
 60		
 61		/**
 62		 * returns the color at x,y coordinates.
 63		 * This method is made to test if the color is indeed the expected one, (the one you set for an event), as due to compression
 64		 * for instance using the Flash IDE library, compression might have altered the color values.
 65		 *
 66		 * @param    x            X coordinate on the source bitmapData
 67		 * @param    y            Y coordinate on the source bitmapData
 68		 *
 69		 * @return        A uint, the color value at coordinates x, y
 70		 * @see        plotAt
 71		 */
 72		public function getColorAt(x:Number, y:Number):uint
 73		{
 74			return _colorMap.getPixel((x/_scaleX) + _offsetX, (y/_scaleY) + _offsetY);
 75		}
 76		
 77		/**
 78		 * Another method for debug, if you addChild your bitmapdata on screen, this method will colour a pixel at the coordinates
 79		 * helping you to visualize if your scale factors or entered coordinates are correct.
 80		 * @param    x            X coordinate on the source bitmapData
 81		 * @param    y            Y coordinate on the source bitmapData
 82		 */
 83		public function plotAt(x:Number, y:Number, color:uint = 0xFF0000):void
 84		{
 85			_colorMap.setPixel((x/_scaleX) + _offsetX, (y/_scaleY) + _offsetY, color);
 86		}
 87		
 88		/**
 89		 * Defines a color event for this class.
 90		 * If read method is called, and the target pixel color has the same value as a previously set listener, an event is triggered.
 91		 *
 92		 * @param    color            A color Number
 93		 * @param    eventID        A string to identify that event
 94		 * @param    listener        The function  that must be triggered
 95		 */
 96		public function addColorEvent(color:Number, eventID:String, listener:Function):void
 97		{
 98			if (!_colorObjects)
 99				_colorObjects = new Vector.<ColorObject>();
100			
101			var colorObject:ColorObject = new ColorObject();
102			colorObject.color = color;
103			colorObject.eventID = eventID;
104			colorObject.listener = listener;
105			
106			_colorObjects.push(colorObject);
107			
108			addEventListener(eventID, listener, false, 0, false);
109		}
110		
111		/**
112		 * removes a color event by its id
113		 *
114		 * @param  eventID  The Event id
115		 */
116		public function removeColorEvent(eventID:String):void
117		{
118			if (!_colorObjects)
119				return;
120			
121			var co:ColorObject;
122			for (var i:uint = 0; i < _colorObjects.length; ++i) {
123				co = ColorObject(_colorObjects[i]);
124				if (co.eventID == eventID) {
125					if (hasEventListener(eventID))
126						removeEventListener(eventID, co.listener);
127					
128					_colorObjects.splice(i, 1);
129					break;
130				}
131			}
132		}
133		
134		/**
135		 * The offsetX, offsetY
136		 * by default offsetX and offsetY represent the center of the map.
137		 */
138		public function set offsetX(value:Number):void
139		{
140			_offsetX = value;
141		}
142		
143		public function set offsetY(value:Number):void
144		{
145			_offsetY = value;
146		}
147		
148		public function get offsetX():Number
149		{
150			return _offsetX;
151		}
152		
153		public function get offsetY():Number
154		{
155			return _offsetY;
156		}
157		
158		/**
159		 * defines the  scaleX and scaleY. Defines the ratio map to the 3d world
160		 */
161		public function set scaleX(value:Number):void
162		{
163			_scaleX = value;
164		}
165		
166		public function set scaleY(value:Number):void
167		{
168			_scaleY = value;
169		}
170		
171		public function get scaleX():Number
172		{
173			return _scaleX;
174		}
175		
176		public function get scaleY():Number
177		{
178			return _scaleY;
179		}
180		
181		/**
182		 * The source bitmapdata uses for colour readings
183		 */
184		public function set bitmapData(map:BitmapData):void
185		{
186			_colorMap = map;
187			
188			_offsetX = _colorMap.width*.5;
189			_offsetY = _colorMap.height*.5;
190		}
191		
192		public function get bitmapData():BitmapData
193		{
194			return _colorMap;
195		}
196		
197		private function fireColorEvent(eventID:String):void
198		{
199			dispatchEvent(new Event(eventID));
200		}
201	
202	}
203}
204
205class ColorObject
206{
207	public var color:uint;
208	public var eventID:String;
209	public var listener:Function;
210}