| 1 | /** |
|---|
| 2 | * Manages scrolling of a designated MovieClip, automatic or with scrollbar. |
|---|
| 3 | * |
|---|
| 4 | * @example |
|---|
| 5 | * var myScroller = new com.jeroenwijering.utils.Scroller(myMovie,myMask); |
|---|
| 6 | * myscroller.scrollTo(200); |
|---|
| 7 | * |
|---|
| 8 | * @author Jeroen Wijering |
|---|
| 9 | * @version 1.8 |
|---|
| 10 | **/ |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | import com.jeroenwijering.utils.Animations; |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | class com.jeroenwijering.utils.Scroller { |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | /** Movieclip that should be scrolled **/ |
|---|
| 20 | private var targetClip:MovieClip; |
|---|
| 21 | /** Mask of the movieclip **/ |
|---|
| 22 | private var maskClip:MovieClip; |
|---|
| 23 | /** Use automatic scroling, defaults to false **/ |
|---|
| 24 | private var autoScroll:Boolean = false; |
|---|
| 25 | /** scrollbar front color **/ |
|---|
| 26 | private var frontColor:Number = 0x000000; |
|---|
| 27 | /** scrollbar highlighting color **/ |
|---|
| 28 | private var lightColor:Number = 0x000000; |
|---|
| 29 | /** size ratio clip:mask **/ |
|---|
| 30 | private var sizeRatio:Number; |
|---|
| 31 | /** scroll interval id for autoscroller and dragging of scrollbar **/ |
|---|
| 32 | private var scrollInterval:Number; |
|---|
| 33 | /** corrent scroll index **/ |
|---|
| 34 | private var currentScroll:Number = 0; |
|---|
| 35 | /** autoscroll multiplier **/ |
|---|
| 36 | private var AUTOSCROLL_SPEED:Number = 0.25; |
|---|
| 37 | /** Movieclip the scrollbar is drawn into **/ |
|---|
| 38 | private var SCROLLER_CLIP:MovieClip; |
|---|
| 39 | /** Color object of the scrollbar back **/ |
|---|
| 40 | private var SCROLLER_BACK_COLOR:Color; |
|---|
| 41 | /** Color object of the scrollbar front **/ |
|---|
| 42 | private var SCROLLER_FRONT_COLOR:Color; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | /** Sets up scrolling behaviour and scrollbar **/ |
|---|
| 46 | function Scroller(tgt:MovieClip,msk:MovieClip,asc:Boolean, |
|---|
| 47 | fcl:Number,hcl:Number) { |
|---|
| 48 | targetClip = tgt; |
|---|
| 49 | maskClip = msk; |
|---|
| 50 | arguments.length > 2 ? autoScroll = asc: null; |
|---|
| 51 | arguments.length > 3 ? frontColor = fcl: null; |
|---|
| 52 | arguments.length > 4 ? lightColor = hcl: null; |
|---|
| 53 | sizeRatio = maskClip._height/targetClip._height; |
|---|
| 54 | if(autoScroll == false) { |
|---|
| 55 | drawScrollbar(); |
|---|
| 56 | } else { |
|---|
| 57 | scrollInterval = setInterval(this,"doAutoscroll",50); |
|---|
| 58 | } |
|---|
| 59 | if(System.capabilities.os.toLowerCase().indexOf("mac") == -1) { |
|---|
| 60 | Mouse.addListener(this); |
|---|
| 61 | } |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | /** Draw the scrollbar. **/ |
|---|
| 66 | private function drawScrollbar() { |
|---|
| 67 | targetClip._parent.createEmptyMovieClip("scrollbar", |
|---|
| 68 | targetClip._parent.getNextHighestDepth()); |
|---|
| 69 | SCROLLER_CLIP = targetClip._parent.scrollbar; |
|---|
| 70 | SCROLLER_CLIP._x = maskClip._x+maskClip._width - 1; |
|---|
| 71 | SCROLLER_CLIP._y = maskClip._y+3; |
|---|
| 72 | SCROLLER_CLIP.createEmptyMovieClip("back",0); |
|---|
| 73 | SCROLLER_CLIP.back._alpha = 0; |
|---|
| 74 | SCROLLER_CLIP.back._y = -3; |
|---|
| 75 | drawSquare(SCROLLER_CLIP.back,12,maskClip._height,frontColor); |
|---|
| 76 | SCROLLER_CLIP.createEmptyMovieClip("bar",1); |
|---|
| 77 | SCROLLER_CLIP.bar._x = 4; |
|---|
| 78 | SCROLLER_CLIP.bar._alpha = 50; |
|---|
| 79 | drawSquare(SCROLLER_CLIP.bar,4,maskClip._height-5,frontColor); |
|---|
| 80 | SCROLLER_CLIP.createEmptyMovieClip("front",2); |
|---|
| 81 | SCROLLER_CLIP.front._x = 3; |
|---|
| 82 | drawSquare(SCROLLER_CLIP.front,6, |
|---|
| 83 | SCROLLER_CLIP.bar._height*sizeRatio,frontColor); |
|---|
| 84 | SCROLLER_CLIP.front.createEmptyMovieClip("bg",1); |
|---|
| 85 | SCROLLER_CLIP.front.bg._x = -3; |
|---|
| 86 | SCROLLER_CLIP.front.bg._alpha = 0; |
|---|
| 87 | drawSquare(SCROLLER_CLIP.front.bg,12, |
|---|
| 88 | SCROLLER_CLIP.front._height,frontColor); |
|---|
| 89 | SCROLLER_FRONT_COLOR = new Color(SCROLLER_CLIP.front); |
|---|
| 90 | setScrollbarEvents(); |
|---|
| 91 | }; |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | /** Set use of mousewheel to scroll playlist. **/ |
|---|
| 95 | public function onMouseWheel(dta:Number) { |
|---|
| 96 | scrollTo(currentScroll-dta*20); |
|---|
| 97 | }; |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | /** Set autoscroll events. **/ |
|---|
| 101 | private function doAutoscroll() { |
|---|
| 102 | if (maskClip._xmouse>0 && maskClip._xmouse<maskClip._width/ |
|---|
| 103 | (maskClip._xscale/100) && maskClip._ymouse>0 && |
|---|
| 104 | maskClip._ymouse<maskClip._height/(maskClip._yscale/100)) { |
|---|
| 105 | var dif:Number = |
|---|
| 106 | maskClip._ymouse*(maskClip._yscale/100)-maskClip._height/2; |
|---|
| 107 | scrollTo(currentScroll+Math.floor(dif*AUTOSCROLL_SPEED)); |
|---|
| 108 | } |
|---|
| 109 | }; |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | /** All scrollbar mouse events grouped together. **/ |
|---|
| 113 | private function setScrollbarEvents():Void { |
|---|
| 114 | var instance:Scroller = this; |
|---|
| 115 | SCROLLER_CLIP.front.onRollOver = |
|---|
| 116 | SCROLLER_CLIP.back.onRollOver = function() { |
|---|
| 117 | instance.SCROLLER_FRONT_COLOR.setRGB(instance.lightColor); |
|---|
| 118 | }; |
|---|
| 119 | SCROLLER_CLIP.front.onRollOut = |
|---|
| 120 | SCROLLER_CLIP.back.onRollOut = function() { |
|---|
| 121 | instance.SCROLLER_FRONT_COLOR.setRGB(instance.frontColor); |
|---|
| 122 | }; |
|---|
| 123 | SCROLLER_CLIP.back.onRelease = function() { |
|---|
| 124 | if(this._ymouse > this._parent.front._y + |
|---|
| 125 | this._parent.front._height) { |
|---|
| 126 | instance.scrollTo(instance.currentScroll + |
|---|
| 127 | instance.maskClip._height/2); |
|---|
| 128 | } else if (this._ymouse < this._parent.front._y) { |
|---|
| 129 | instance.scrollTo(instance.currentScroll - |
|---|
| 130 | instance.maskClip._height/2); |
|---|
| 131 | } |
|---|
| 132 | }; |
|---|
| 133 | SCROLLER_CLIP.front.onPress = function() { |
|---|
| 134 | this.startDrag(false,3,0,3,instance.SCROLLER_CLIP.bar._height - |
|---|
| 135 | this._height); |
|---|
| 136 | instance.scrollInterval = setInterval(instance,"scrollTo",100); |
|---|
| 137 | }; |
|---|
| 138 | SCROLLER_CLIP.front.onRelease = |
|---|
| 139 | SCROLLER_CLIP.front.onReleaseOutside = function() { |
|---|
| 140 | this.stopDrag(); |
|---|
| 141 | clearInterval(instance.scrollInterval); |
|---|
| 142 | }; |
|---|
| 143 | scrollTo(maskClip._y - targetClip._y); |
|---|
| 144 | }; |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | /** Scroll the MovieClip to a given Y position. **/ |
|---|
| 148 | public function scrollTo(yps:Number):Void { |
|---|
| 149 | if(arguments.length == 0 && autoScroll == false) { |
|---|
| 150 | yps = SCROLLER_CLIP.front._y*maskClip._height / |
|---|
| 151 | SCROLLER_CLIP.front._height; |
|---|
| 152 | } |
|---|
| 153 | if(yps<5) { |
|---|
| 154 | yps=0; |
|---|
| 155 | } else if (yps>targetClip._height-maskClip._height-5) { |
|---|
| 156 | yps = targetClip._height - maskClip._height; |
|---|
| 157 | } |
|---|
| 158 | Animations.easeTo(targetClip,targetClip._x,maskClip._y - yps); |
|---|
| 159 | SCROLLER_CLIP.front._y = yps*SCROLLER_CLIP.front._height / |
|---|
| 160 | maskClip._height; |
|---|
| 161 | currentScroll = yps; |
|---|
| 162 | }; |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | /** Remove the scrollbar from stage **/ |
|---|
| 166 | public function purgeScrollbar() { |
|---|
| 167 | clearInterval(scrollInterval); |
|---|
| 168 | Mouse.removeListener(this); |
|---|
| 169 | scrollTo(0); |
|---|
| 170 | SCROLLER_CLIP.removeMovieClip(); |
|---|
| 171 | }; |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | /** Draw a square in a given movieclip. **/ |
|---|
| 175 | private function drawSquare(tgt:MovieClip,wth:Number,hei:Number, |
|---|
| 176 | clr:Number) { |
|---|
| 177 | tgt.clear(); |
|---|
| 178 | tgt.beginFill(clr,100); |
|---|
| 179 | tgt.moveTo(0,0); |
|---|
| 180 | tgt.lineTo(wth,0); |
|---|
| 181 | tgt.lineTo(wth,hei); |
|---|
| 182 | tgt.lineTo(0,hei); |
|---|
| 183 | tgt.lineTo(0,0); |
|---|
| 184 | tgt.endFill(); |
|---|
| 185 | }; |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | } |
|---|