/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

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