選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Cover.as 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. package {
  2. import flash.events.Event;
  3. import flash.display.Loader;
  4. import flash.display.DisplayObject;
  5. import flash.system.LoaderContext;
  6. import flash.display.Bitmap;
  7. import flash.display.BitmapData;
  8. import flash.net.URLRequest;
  9. import mx.controls.Alert;
  10. import flash.system.Security;
  11. import flash.system.ApplicationDomain;
  12. import mx.controls.Image;
  13. import mx.core.UIComponent;
  14. import flash.geom.Matrix;
  15. import flash.display.Sprite;
  16. import flash.display.Shape;
  17. import flash.display.Graphics;
  18. import flash.display.GradientType;
  19. import flash.geom.Rectangle;
  20. import flash.geom.Point;
  21. import sandy.util.*;
  22. public class Cover extends UIComponent {
  23. // Cover information
  24. private var _caption:String;
  25. private var _uri:String;
  26. private var _imageLoaded:Boolean = false;
  27. // Actual content information
  28. private var _bitmapData:BitmapData;
  29. private var _bitmap:Bitmap;
  30. private var _img:Image;
  31. private var _distortedShape:Shape;
  32. private var _reflectionBitmap:Bitmap;
  33. private var _reflectionShape:Shape;
  34. // Distort information
  35. private var _realAngle:Number = -1;
  36. private var _realScale:Number = -1;
  37. private var _angle:Number = 0;
  38. private var _scale:Number = 1;
  39. private static const perspectiveConstant:Number = .15;
  40. // Position information
  41. private var _x:int;
  42. private var _y:int;
  43. /**
  44. * Constructor
  45. */
  46. public function Cover(caption:String, uri:String) {
  47. super();
  48. // Set this element's size to 100% x 100%
  49. super.percentHeight = 100;
  50. super.percentWidth = 100;
  51. // Store input data
  52. this._caption = caption;
  53. this._uri = uri;
  54. // Initialize default image
  55. this._img = new Image();
  56. this._img.percentHeight = 100;
  57. this._img.percentWidth = 100;
  58. this._bitmapData = new BitmapData(100,100,false, 0xffff0000);
  59. _distortedShape = new Shape();
  60. addChild(_distortedShape);
  61. _reflectionShape = new Shape();
  62. addChild(_reflectionShape);
  63. // Create the default image
  64. this._img.source = this.getBitmap();
  65. addChildAt(_img, 0);
  66. }
  67. /**
  68. * Load an image if it hasn't been loaded yet
  69. */
  70. public function loadImage():void {
  71. if(!_imageLoaded) {
  72. // Create a loader to load the image
  73. var request:URLRequest = new URLRequest(this._uri);
  74. var imageLoader:Loader = new Loader();
  75. // Check for restrictions
  76. var imgLdrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
  77. imgLdrContext.checkPolicyFile = true;
  78. // Set an event listener
  79. imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
  80. // Load the image
  81. imageLoader.load(request, imgLdrContext);
  82. }
  83. }
  84. /**
  85. * Event handler for image load completion
  86. * @param Event event
  87. */
  88. private function imgLoaded(event:Event):void {
  89. // Check if we got the image
  90. try {
  91. removeChild(_img);
  92. this._bitmapData = Bitmap(event.currentTarget.content).bitmapData;
  93. // Set new source for the image
  94. this._img.source = this.getBitmap();
  95. addChildAt(_img, 0);
  96. _imageLoaded = true;
  97. invalidateDisplayList();
  98. }
  99. catch(error:Error) {
  100. // An error occured
  101. Alert.show(error.toString());
  102. }
  103. }
  104. /**
  105. * Measure the size of the image
  106. */
  107. override protected function measure():void {
  108. if(_img != null)
  109. {
  110. measuredHeight = _img.getExplicitOrMeasuredHeight();
  111. measuredWidth = _img.getExplicitOrMeasuredWidth();
  112. }
  113. }
  114. /**
  115. * Update the image on the display
  116. */
  117. override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
  118. // Do this if and only iff the angle or the scale has changed. Otherwise we do not
  119. // want to re-draw the image, only move it.
  120. if(_img && (_realAngle != _angle || _realScale != _scale))
  121. {
  122. var contentWidth:Number = _img.getExplicitOrMeasuredWidth();
  123. var contentHeight:Number= _img.getExplicitOrMeasuredHeight();
  124. _img.setActualSize(contentWidth,contentHeight);
  125. _realAngle = _angle;
  126. _realScale = _scale;
  127. // Distort the image
  128. distortImage(_bitmap, _distortedShape, false);
  129. // Create a reflection
  130. createReflectionBitmap(_bitmap);
  131. }
  132. }
  133. /**
  134. * This function distorts the target according to its angle
  135. * @param Bitmap bm - The bitmap of the object which is being distorted
  136. * @param Shape target - The target where the distorted image is stored
  137. * @param Boolean refelection - Is this target a reflection?
  138. */
  139. private function distortImage(bm:Bitmap, target:Shape, reflection:Boolean):void {
  140. // Turn the angle into a value between 0 and 1
  141. var k:Number = (Math.abs(_angle)/90);
  142. k = Math.sqrt(k);
  143. var vShear:Number = (_angle >= 0)? k*-perspectiveConstant : k*perspectiveConstant;
  144. var verticalOffset:Number;
  145. // How much is the width of the image scaled
  146. var hScale:Number = 1 - k;
  147. // How much lower/higher are the corners "further away" than the one "closer"
  148. verticalOffset = (_img.getExplicitOrMeasuredHeight()*vShear);
  149. // Is this image a reflection? If yes, then the vertical offset distortion is made in another direction
  150. if(reflection)
  151. verticalOffset *= -1;
  152. // Initialize the distortion class
  153. var distort:DistortImage = new DistortImage();
  154. distort.container = target;
  155. distort.target = bm;
  156. distort.smooth = true;
  157. distort.initialize( 5, 5, null );
  158. // Distort the images to the left
  159. if(_angle > 0) {
  160. distort.setTransform(
  161. 0,0,
  162. _img.getExplicitOrMeasuredWidth()*hScale,-verticalOffset,
  163. _img.getExplicitOrMeasuredWidth()*hScale,_img.getExplicitOrMeasuredHeight()+verticalOffset,
  164. 0,_img.getExplicitOrMeasuredHeight()
  165. );
  166. }
  167. // Distort the images to the right
  168. else if(_angle < 0) {
  169. distort.setTransform(
  170. 0,verticalOffset,
  171. _img.getExplicitOrMeasuredWidth()*hScale,0,
  172. _img.getExplicitOrMeasuredWidth()*hScale,_img.getExplicitOrMeasuredHeight(),
  173. 0,_img.getExplicitOrMeasuredHeight()-verticalOffset
  174. );
  175. }
  176. distort.render();
  177. // We use the original image as a reference, but do not want to show it on the screen
  178. _img.visible = false;
  179. // Align the distorted image correctly
  180. var m:Matrix = target.transform.matrix;
  181. m.tx = 0 - _img.width/2* hScale;
  182. target.transform.matrix = m;
  183. }
  184. /**
  185. * This function creates a reflection of the target object
  186. * @param DisplayObject target
  187. */
  188. private function createReflectionBitmap(target:DisplayObject):void {
  189. // Size of the fade
  190. var fadeSize:Number = 0.4;
  191. // Create a rectangle
  192. var box:Rectangle = new Rectangle(0, 0, target.width, target.height);
  193. // Create a matrix for the gradient
  194. var gradientMatrix: Matrix = new Matrix();
  195. // Create a shape object for the gradient
  196. var gradientShape: Shape = new Shape();
  197. // Apply a gradient on the matrix
  198. gradientMatrix.createGradientBox(target.width, target.height * fadeSize, Math.PI/2, 0, target.height * (1.0 - fadeSize));
  199. // Fill the shape with the gradient
  200. gradientShape.graphics.beginGradientFill(GradientType.LINEAR, [0xFFFFFF, 0xFFFFFF], [0, 1], [0, 255], gradientMatrix);
  201. gradientShape.graphics.drawRect(0, target.height * (1.0 - fadeSize), target.width, target.height * fadeSize);
  202. gradientShape.graphics.endFill();
  203. // Bitmap representation of the gradient
  204. var gradientBm:BitmapData = new BitmapData(target.width, target.height, true, 0x00000000);
  205. gradientBm.draw(gradientShape, new Matrix());
  206. var targetBm:BitmapData = new BitmapData(target.width, target.height, true, 0x00000000);
  207. targetBm.fillRect(box, 0x00000000);
  208. targetBm.draw(target, new Matrix());
  209. var reflectionData:BitmapData = new BitmapData(target.width, target.height, true, 0x00000000);
  210. reflectionData.fillRect(box, 0x00000000);
  211. reflectionData.copyPixels(targetBm, box, new Point(), gradientBm);
  212. // Store the reflection
  213. _reflectionBitmap = new Bitmap(reflectionData);
  214. // Move the reflection to its correct position
  215. var m:Matrix = _distortedShape.transform.matrix;
  216. m.d = -1;
  217. m.ty = _distortedShape.height*2;
  218. _reflectionShape.transform.matrix = m;
  219. // Distort the reflection
  220. distortImage(_reflectionBitmap, _reflectionShape, true);
  221. // Set a transparancy for the reflection
  222. _reflectionShape.alpha = 0.4;
  223. }
  224. /**
  225. * Get name
  226. * @return String
  227. */
  228. public function get caption():String {
  229. return this._caption;
  230. }
  231. /**
  232. * Set name
  233. * @param String name
  234. */
  235. public function set caption(caption:String):void {
  236. this._caption = caption;
  237. }
  238. /**
  239. * Get uri
  240. * @return String
  241. */
  242. public function get uri():String {
  243. return this._uri;
  244. }
  245. /**
  246. * Set uri
  247. * @param String uri
  248. */
  249. public function set uri(uri:String):void {
  250. this._uri = uri;
  251. }
  252. /**
  253. * Get the bitmap
  254. * @return Bitmap
  255. */
  256. public function getBitmap():Bitmap {
  257. this._bitmap = new Bitmap(this._bitmapData);
  258. return this._bitmap;
  259. }
  260. /**
  261. * Get the bitmap data
  262. * @return BitmapData
  263. */
  264. public function getBitmapData():BitmapData {
  265. return this._bitmapData;
  266. }
  267. /**
  268. * Set the angle of the cover
  269. * @param Number angle
  270. */
  271. public function set angle(angle:Number):void {
  272. this._angle = angle;
  273. invalidateDisplayList();
  274. }
  275. /**
  276. * Get the angle of the cover
  277. * @return Number
  278. */
  279. public function get angle():Number {
  280. return this._angle;
  281. }
  282. /**
  283. * Set the x position of the cover
  284. * @param Number x
  285. */
  286. public function set xPos(x:Number):void {
  287. this._x = x;
  288. }
  289. /**
  290. * Get the x position of the cover
  291. * @return Number
  292. */
  293. public function get xPos():Number {
  294. return this._x;
  295. }
  296. /**
  297. * Set the y position of the cover
  298. * @param Number y
  299. */
  300. public function set yPos(y:Number):void {
  301. this._y = y;
  302. }
  303. /**
  304. * Get the y position of the cover
  305. * @return Number
  306. */
  307. public function get yPos():Number {
  308. return this._y;
  309. }
  310. /**
  311. * Set the scale of the cover
  312. * @param Number scale
  313. */
  314. public function set scale(scale:Number):void {
  315. this._scale = scale;
  316. }
  317. /**
  318. * Get the scale position of the cover
  319. * @return Number
  320. */
  321. public function get scale():Number {
  322. return this._scale;
  323. }
  324. }
  325. }