ReflectionManager.as
上传用户:jqyjoan
上传日期:2018-02-02
资源大小:1773k
文件大小:6k
源码类别:

Ajax

开发平台:

Flex

  1. /********************************************
  2.  title   : Reflection Manager
  3.  version : 1.0
  4.  author  : Wietse Veenstra
  5.  website : http://www.wietseveenstra.nl
  6.  date    : 2007-05-29
  7. ********************************************/
  8. package nl.wv.extenders.panel {
  9. import flash.display.Bitmap;
  10. import flash.display.BitmapData;
  11. import flash.display.BlendMode;
  12. import flash.display.GradientType;
  13. import flash.display.Graphics;
  14. import flash.display.Shape;
  15. import flash.events.Event;
  16. import flash.events.MouseEvent;
  17. import flash.filters.BitmapFilter;
  18. import flash.filters.BlurFilter;
  19. import flash.geom.Matrix;
  20. import flash.geom.Point;
  21. import flash.geom.Rectangle;
  22. import mx.core.UIComponent;
  23. import mx.events.DragEvent;
  24. import mx.events.FlexEvent;
  25. import mx.events.MoveEvent;
  26. import mx.events.ResizeEvent;
  27. import mx.states.RemoveChild;
  28. import mx.controls.Alert;
  29. public class ReflectionManager extends UIComponent {
  30. private static const _rPoint:Point = new Point(0, 0);
  31. private static const _rMatrix:Matrix = new Matrix();
  32. private var _bitmap:Bitmap = new Bitmap(new BitmapData(1, 1, true, 0));
  33. private var _targetBMData:BitmapData;
  34. private var _alphaGrBMData:BitmapData;
  35. private var _combinedBMData:BitmapData;
  36. private var _alphaGradient:Matrix = new Matrix();
  37. private var _blur:Number = 0;
  38. private var _blurFilter:BitmapFilter;
  39. private var _fadeFrom:Number = 0.3;
  40. private var _fadeTo:Number = 0;
  41. private var _matrix:Matrix;
  42. private var _point:Point;
  43. private var _rectangle:Rectangle;
  44. private var _shape:Shape;
  45. private var _target:UIComponent;
  46. // this offset is needed because of the drop shadow
  47. private var _yshadowOffset:Number = -14;
  48. public function ReflectionManager():void {
  49. this.addChild(this._bitmap);
  50. this.invalidateDisplayList();
  51. }
  52. public function get fadeFrom():Number {
  53. return this._fadeFrom;
  54. }
  55. public function set fadeFrom(value:Number):void {
  56. this._fadeFrom = value;
  57. this.invalidateDisplayList();
  58. }
  59. public function get fadeTo():Number {
  60. return this._fadeFrom;
  61. }
  62. public function set fadeTo(value:Number):void {
  63. this._fadeTo = value;
  64. this.invalidateDisplayList();
  65. }
  66. public function get blur():Number {
  67. return this._blur;
  68. }
  69. public function set blur(value:Number):void {
  70. this._blur = value;
  71. this.invalidateDisplayList();
  72. }
  73. public function set target(value:UIComponent):void {
  74. if (this._target != null) {
  75. this._target.removeEventListener(FlexEvent.UPDATE_COMPLETE, targetUpdateHandler, true);
  76. this._target.removeEventListener(Event.REMOVED_FROM_STAGE, targetRemovedHandler);
  77. this._target.removeEventListener(MouseEvent.MOUSE_DOWN, targetMouseDownHandler);
  78. this._target.removeEventListener(ResizeEvent.RESIZE, targetResizeHandler);
  79. this.clearBMData();
  80. }
  81. this._target = value;
  82. if (this._target) {
  83. this._target.addEventListener(FlexEvent.UPDATE_COMPLETE, targetUpdateHandler, true);
  84. this._target.addEventListener(Event.REMOVED_FROM_STAGE, targetRemovedHandler);
  85. this._target.addEventListener(MouseEvent.MOUSE_DOWN, targetMouseDownHandler);
  86. this._target.addEventListener(ResizeEvent.RESIZE, targetResizeHandler);
  87. this.invalidateDisplayList();
  88. }
  89. }
  90. public function targetUpdateHandler(event:FlexEvent):void {
  91. this.invalidateDisplayList();
  92. }
  93. private function targetMouseDownHandler(event:MouseEvent):void {
  94. this._target.addEventListener(MouseEvent.MOUSE_MOVE, targetMoveHandler);
  95. }
  96. private function targetMoveHandler(event:MouseEvent):void {
  97. this._bitmap.x = this._target.x;
  98. this._bitmap.y = this._target.y + this._target.height + _yshadowOffset;
  99. }
  100. private function targetResizeHandler(event:ResizeEvent):void {
  101. this.clearBMData();
  102. this.width = _target.width;
  103. this.height = (_target.height / 100) * 50; //this.parentDocument.distance.value;
  104. this.invalidateDisplayList();
  105. }
  106. private function targetRemovedHandler(event:Event):void {
  107. this.parent.removeChild(this);
  108. }
  109. override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
  110. if (_target != null) {
  111. this.drawReflection(_target);
  112. this._bitmap.bitmapData.dispose();
  113. this._bitmap.bitmapData = this._combinedBMData;
  114. // do it here
  115. this._bitmap.x = _target.x;
  116. this._bitmap.y = _target.y + _target.height + _yshadowOffset;  
  117. } else {
  118. this.clearBMData();
  119. }
  120. }
  121. private function drawReflection(target:UIComponent):void { 
  122. if (this.width > 0 && this.height > 0) {
  123. this._matrix = new Matrix(1, 0, 0, -1, 0, target.height);
  124. this._point = this._matrix.transformPoint(new Point(0, target.height));
  125. this._matrix.tx = this._point.x * -1 ;
  126. this._matrix.ty = (this._point.y - target.height) * -1;
  127. this._targetBMData = new BitmapData(this.width, this.height, true, 0);
  128. this._targetBMData.draw(target, this._matrix);
  129. this._rectangle = new Rectangle(0, 0, this.width, this.height);
  130. if (this._blur > 0) {
  131. this._blurFilter = new BlurFilter(this._blur * 5, this._blur * 10, 1.0);
  132. this._targetBMData.applyFilter(this._targetBMData, this._rectangle, this._point, this._blurFilter);
  133. }
  134. if (this._alphaGrBMData == null) {
  135. this._alphaGradient.createGradientBox(this.width, this.height, Math.PI / 2);
  136. this._shape = new Shape();
  137. this._shape.graphics.beginGradientFill(GradientType.LINEAR, new Array(0, 0), new Array(this._fadeFrom, this._fadeTo), new Array(0, 0xFF), this._alphaGradient);
  138. this._shape.graphics.drawRect(0, 0, this.width, this.height);
  139. this._shape.graphics.endFill();
  140. this._alphaGrBMData = new BitmapData(this.width, this.height, true, 0);
  141. this._alphaGrBMData.draw(this._shape, _rMatrix);
  142. }
  143. this._combinedBMData = new BitmapData(this.width, this.height, true, 0);
  144. this._combinedBMData.copyPixels(this._targetBMData, this._rectangle, _rPoint, this._alphaGrBMData);
  145. }
  146. }
  147. public function clearBMData():void {
  148. this._targetBMData = null;
  149. this._alphaGrBMData = null;
  150. this._combinedBMData = null;
  151. }
  152. }
  153. }