/src/sg/camo/scrollables/ScrollMask2Rect.as

https://github.com/stewymac07/SG-Camo-Collections
ActionScript | 185 lines | 135 code | 34 blank | 16 comment | 0 complexity | 4348f2d3f3fd0f36a90585e6dc67fecd MD5 | raw file
  1. package sg.camo.scrollables
  2. {
  3. import flash.display.DisplayObject;
  4. import flash.display.Shape;
  5. import flash.geom.Rectangle;
  6. import flash.display.Sprite;
  7. import sg.camo.interfaces.IScrollProxy;
  8. import sg.camo.interfaces.IScrollable;
  9. /**
  10. * Converts any current IScrollProxy implementation which involves scrolling content physically
  11. * within masks to scrolling by scrollRect instead.
  12. * <br/>
  13. * This behaviour is automatically used by <code>ScrollableBehaviour</code> if it is found that
  14. * an actual <code>scrollRect</code> is used as the "scrollMask" for the scrollContent.
  15. *
  16. * @see sg.camo.behaviour.ScrollableBehaviour
  17. *
  18. * @author Glenn Ko
  19. */
  20. public class ScrollMask2Rect implements IScrollable
  21. {
  22. protected var _dummyRef:DummyReference = new DummyReference();
  23. protected var _target:IScrollable;
  24. /**
  25. * Constructor
  26. * @param iScroll An IScrollProxy reference to convert.
  27. */
  28. public function ScrollMask2Rect(iScroll:IScrollProxy)
  29. {
  30. _target = iScroll.target;
  31. _dummyRef.width = iScroll.scrollContent.width;
  32. _dummyRef.height = iScroll.scrollContent.height;
  33. _dummyRef.disp = iScroll.scrollContent;
  34. //_scrollRectProxy = new ScrollRectProxy(iScroll.scrollContent);
  35. iScroll.target = this;
  36. }
  37. public function get x():Number {
  38. return scrollMask.x;
  39. }
  40. public function get y():Number {
  41. return scrollMask.y;
  42. }
  43. public function get width():Number {
  44. return scrollContent.scrollRect.width;
  45. }
  46. public function set width(val:Number):void {
  47. var rect:Rectangle = scrollContent.scrollRect;
  48. rect.width = val;
  49. scrollContent.scrollRect = rect;
  50. }
  51. public function get height():Number {
  52. return scrollContent.scrollRect.height;
  53. }
  54. public function set height(val:Number):void {
  55. var rect:Rectangle = scrollContent.scrollRect;
  56. rect.height = val;
  57. scrollContent.scrollRect = rect;
  58. }
  59. protected function getDestScrollH (ratio:Number):Number {
  60. var w:Number = _dummyRef.width - scrollMask.width;
  61. return (ratio * w);
  62. }
  63. protected function getDestScrollV(ratio:Number):Number {
  64. var h:Number = _dummyRef.height - scrollMask.height;
  65. return (ratio * h);
  66. }
  67. public function set scrollH (ratio:Number):void {
  68. _dummyRef.x = getDestScrollH(ratio);
  69. }
  70. public function set scrollV (ratio:Number):void {
  71. _dummyRef.y = getDestScrollV(ratio);
  72. }
  73. public function get scrollContent():DisplayObject {
  74. return _dummyRef;
  75. }
  76. public function resetScroll():void {
  77. _dummyRef.x = 0;
  78. _dummyRef.y = 0;
  79. }
  80. public function get scrollContainer ():Sprite {
  81. return _target.scrollContainer;
  82. }
  83. public function get scrollMask ():* {
  84. return scrollContent.scrollRect;
  85. }
  86. public function get itemLength ():Number {
  87. return _target.itemLength;
  88. }
  89. public function set itemLength (val:Number):void {
  90. _target.itemLength = val;
  91. }
  92. public function destroy():void {
  93. _target = null;
  94. _dummyRef = null;
  95. }
  96. public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
  97. _target.addEventListener(type, listener, useCapture, priority, useWeakReference);
  98. }
  99. public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
  100. _target.removeEventListener(type, listener, useCapture);
  101. }
  102. }
  103. }
  104. import flash.display.DisplayObject;
  105. import flash.display.Shape;
  106. import flash.geom.Rectangle;
  107. internal class DummyReference extends Shape {
  108. private var _disp:DisplayObject;
  109. private var _scrollRect:Rectangle;
  110. private var _width:Number;
  111. private var _height:Number;
  112. public function DummyReference(width:Number=0, height:Number=0, disp:DisplayObject=null) {
  113. this.disp = disp;
  114. _width = width;
  115. _height = height;
  116. }
  117. public function set disp(d:DisplayObject):void {
  118. _disp = d;
  119. }
  120. override public function set x(val:Number):void {
  121. var rect:Rectangle = _disp.scrollRect;
  122. rect.x = -val;
  123. _disp.scrollRect = rect;
  124. }
  125. override public function get x():Number {
  126. return -_disp.scrollRect.x;
  127. }
  128. override public function get scrollRect():Rectangle {
  129. return _disp.scrollRect;
  130. }
  131. override public function set y(val:Number):void {
  132. var rect:Rectangle = _disp.scrollRect;
  133. rect.y = -val;
  134. _disp.scrollRect = rect;
  135. }
  136. override public function get y():Number {
  137. return -_disp.scrollRect.y;
  138. }
  139. override public function set width(val:Number):void {
  140. _width = val;
  141. }
  142. override public function set height(val:Number):void {
  143. _height = val;
  144. }
  145. override public function get width():Number {
  146. return _width;
  147. }
  148. override public function get height():Number {
  149. return _height;
  150. }
  151. }