[AS3] ScrollBar

package {
import __AS3__.vec.Vector;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;

public class BasicScrollBar extends MovieClip {
protected var _mcUpArrow:MovieClip;
protected var _mcDownArrow:MovieClip;
protected var _mcScroller:MovieClip; // the scroller is the thing that you can drag to scroll
protected var _mcScrollArea:MovieClip; // the area under the scroller
protected var _mcScrolled:MovieClip; // the area that is actually scrolled
protected var _mcScrolledMask:MovieClip; // mask for scrolled

protected var _scrollTrack:Rectangle; // the scroller track (limits the movement)

protected var _nScrollerLocation:Number; // a 0~1 value

protected var _nScrollSpeed:Number;

public function BasicScrollBar() {
init();
initListeners();
}

public function cleanUp():void {
_mcUpArrow.removeEventListener(MouseEvent.CLICK, onUpArrow);
_mcDownArrow.removeEventListener(MouseEvent.CLICK, onDownArrow);
_mcScroller.removeEventListener(MouseEvent.MOUSE_DOWN, onScroll);
}

private function init():void {
_mcUpArrow = getChildByName(“mcUpArrow”) as MovieClip;
_mcDownArrow = getChildByName(“mcDownArrow”) as MovieClip;
_mcScroller = getChildByName(“mcScroller”) as MovieClip;
_mcScrollArea = getChildByName(“mcScrollArea”) as MovieClip;
_mcScrolled = getChildByName(“mcScrolled”) as MovieClip;
_mcScrolledMask = getChildByName(“mcScrolledMask”) as MovieClip;

_mcScrolledMask.width = Math.abs(_mcScrolled.x);
_mcScrolledMask.height = _mcUpArrow.height + _mcScrollArea.height + _mcDownArrow.height;
_mcScrolled.mask = _mcScrolledMask;

_scrollTrack = new Rectangle(0, _mcScroller.height, 0, _mcScrollArea.height – _mcScroller.height); // scroll track

_nScrollSpeed = 10;
}

private function initListeners():void {
_mcUpArrow.buttonMode = true;
_mcDownArrow.buttonMode = true;
//            _mcScroller.buttonMode = true;

_mcUpArrow.addEventListener(MouseEvent.CLICK, onUpArrow);
_mcDownArrow.addEventListener(MouseEvent.CLICK, onDownArrow);
_mcScroller.addEventListener(MouseEvent.MOUSE_DOWN, onScroll);
}

private function onUpArrow(e:MouseEvent):void { // clicked up arrow
if ( _mcScroller.y > _mcUpArrow.height ) {
_mcScroller.y = _mcScroller.y – _nScrollSpeed;

if (_mcScroller.y < _mcUpArrow.height) { _mcScroller.y = _mcUpArrow.height; }
}

_nScrollerLocation = – ((_mcUpArrow.height – _mcScroller.y) / (_mcScrollArea.height – _mcScroller.height));
setScrolled( _nScrollerLocation );
}

private function onDownArrow(e:MouseEvent):void { // clicked down arrow
if ( _mcScroller.y < _mcScrollArea.height ) {
_mcScroller.y = _mcScroller.y + _nScrollSpeed;

if ( _mcScroller.y > _mcScrollArea.height ) { _mcScroller.y = _mcScrollArea.height; }
}

_nScrollerLocation = – ((_mcUpArrow.height – _mcScroller.y) / (_mcScrollArea.height – _mcScroller.height));
setScrolled( _nScrollerLocation );
}

private function onScroll(e:MouseEvent):void {
_mcScroller.startDrag(false, _scrollTrack); // move the scrollbar

stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
}

private function onMouseMoveHandler(e:MouseEvent):void {
_nScrollerLocation = – ((_mcUpArrow.height – _mcScroller.y) / (_mcScrollArea.height – _mcScroller.height));

setScrolled( _nScrollerLocation );
}

private function onMouseUpHandler(e:MouseEvent):void {
_mcScroller.stopDrag();

stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
}

private function setScrolled(nLocation:Number):void { // takes values between 0~1
var nScrollBarTotalHeight:Number = _mcUpArrow.height + _mcScrollArea.height + _mcDownArrow.height;
var nScrollAmountTotal:Number = _mcScrolled.height – nScrollBarTotalHeight;
var nNewPosition:Number = -nLocation * nScrollAmountTotal;

if (nNewPosition <= 0) {
_mcScrolled.y = nNewPosition;
}
}
}
}

Advertisement

~ by rotaercz on 2010-August-10.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.