import flash.geom.ColorTransform;
import flash.geom.Transform;
class com.onebyonedesign.utils.OBO_FullScrollBar {
private static var OBO_FSB:OBO_FullScrollBar;
private var _content:MovieClip;
private var _scrollValue:Number;
private var _easeAmount:Number;
private var _scrollBar:MovieClip;
private var _scroller:MovieClip;
private var _scrollTrack:MovieClip;
private var _trackColor:Number;
private var _scrollerColor:Number;
private var _position:String;
private var _useScrollWheel:Boolean;
// events
public var onScrollDown:Function;
public var onScrollUp:Function;
// dummy properties to avoid senseless compiler errors from scrollContent method
private var parent:Object;
private var onEnterFrame:Object;
private var _y:Object;
/**
*
* @author Devon O. Wolfgang
* @date 4/12/2007 7:30 PM
* @description creates a full screen scroll bar for use with full browser flash applications
*
* @param trackColor Number hexadecimal color of scrollbar track
* @param scrollerColor Number hexadecimal color of scrollbar scroller
* @param content MovieClip the content that will be scrolled
* @param easeAmount Number how much easing applied to scrolled content (1 = no ease, Number.POSITIVE_INFINITY = way too much ease)
* @param position String either "left" or "right" - determines which side of browser scrollbar appears on
* @param useScrollWheel Boolean OPTIONAL specify whether or not scrollbar is mouse wheel enabled - default is false
*
* @usage var myScrollBar = OBO_FullScrollBar.createScrollBar(0x000000, 0x535353, scrollContent_mc, 3, "right", true);
*/
private function OBO_FullScrollBar(trackColor:Number, scrollerColor:Number, content:MovieClip, easeAmount:Number, position:String, useScrollWheel:Boolean) {
if (position == undefined || (position != "right" && position != "left")) throw new Error("OBO_FullScrollBar error: constructor method requires five arguments. Two colors in hexadecimal number format, a content MovieClip, an amount of ease greater than zero, and a position, either \"left\" or \"right\". A useScrollWheel Boolean is optional.");
_trackColor = trackColor;
_scrollerColor = scrollerColor;
_content = content;
_easeAmount = (easeAmount > 0) ? Math.ceil(easeAmount) : 1;
_position = position;
_useScrollWheel = useScrollWheel || false;
initScroller();
}
private function initScroller():Void {
makeScrollBar();
adjustSize();
var sb:OBO_FullScrollBar = this;
_scroller.parent = sb;
_scroller.useHandCursor = false;
_scroller.onMouseWheel = function(d:Number) {
if (d > 0) {
if ((this._y - (d * 3)) >= 0){
this._y -= d * 3;
} else {
this._y = 0;
}
if (this.onEnterFrame == undefined) this.onEnterFrame = this.parent.scrollContent;
} else {
if (((this._y + this._height) + (Math.abs(d) * 3)) <= Stage.height){
this._y += Math.abs(d) * 3;
} else {
this._y = Stage.height - this._height;
}
if (this.onEnterFrame == undefined) this.onEnterFrame = this.parent.scrollContent;
}
}
if (_useScrollWheel) Mouse.addListener(_scroller);
_scroller.onPress = function() {
this.parent.onScrollDown();
this.startDrag(false, 0, 0, 0, Stage.height - this._height);
this.onMouseMove = function() {
updateAfterEvent();
if (this.onEnterFrame == undefined) this.onEnterFrame = sb.scrollContent;
}
}
_scroller.onRelease = _scroller.onReleaseOutside = function() {
this.parent.onScrollUp();
this.stopDrag();
delete this.onMouseMove;
}
}
private function scrollContent():Void {
var ty:Number = -((this.parent._content._height - Stage.height) * (this._y / this.parent._scrollValue));
var dist:Number = ty - this.parent._content._y;
var moveAmount:Number = dist / this.parent._easeAmount;
this.parent._content._y += moveAmount;
if (Math.abs(this.parent._content._y - ty) < .5) {
delete this.onEnterFrame;
this.parent._content._y = Math.round(ty);
}
}
private function makeScrollBar() {
_scrollBar = _root.createEmptyMovieClip("sb"+Math.round(Math.ra ndom()*1000), _root.getNextHighestDepth());
_scrollTrack = createTrack();
_scroller = createScroller();
}
private function createTrack():MovieClip {
var t:MovieClip = _scrollBar.createEmptyMovieClip("track", _scrollBar.getNextHighestDepth());
t.beginFill(_trackColor);
t.moveTo(0, 0);
t.lineTo(16, 0);
t.lineTo(16, 16);
t.lineTo(0, 16);
t.endFill();
return t;
}
private function createScroller():MovieClip {
var s:MovieClip = _scrollBar.createEmptyMovieClip("scroller", _scrollBar.getNextHighestDepth());
s.beginFill(_scrollerColor);
s.moveTo(0, 0);
s.lineTo(16, 0);
s.lineTo(16, 16);
s.lineTo(0, 16);
s.endFill();
return s;
}
/*
*
* public methods
*
*/
// singleton instantiation
public static function createScrollBar(tc:Number, sc:Number, c:MovieClip, e:Number, pos:String, sw:Boolean):OBO_FullScrollBar {
if (OBO_FSB == undefined) OBO_FSB = new OBO_FullScrollBar(tc, sc, c, e, pos, sw);
return OBO_FSB;
}
// adusts and repositions scrollbar. call this when stage or content movieclip is resized.
public function adjustSize():Void {
_scrollBar._x = (_position == "left") ? 0 : Stage.width - 16;
_scrollTrack._height = Stage.height;
_scroller._height = Math.ceil((Stage.height / _content._height) * Stage.height);
if ((_scroller._y + _scroller._height) > Stage.height) _scroller._y = Stage.height - _scroller._height;
_scroller._y = (_content._height < Stage.height) ? 0 : _scroller._y;
_scroller._visible = (_content._height < Stage.height) ? false : true;
_scroller.enabled = (_content._height < Stage.height) ? false : true;
_scrollValue = Stage.height - _scroller._height;
_content._y = -((_content._height - Stage.height) * (_scroller._y / _scrollValue));
}
// use just like MovieClip.swapDepths()
public function swapDepths(o:Object):Void {
_scrollBar.swapDepths(o);
}
// allows or disallows mouse wheel scrolling
public function set useScrollWheel(b:Boolean) {
if (b) {
Mouse.addListener(_scroller);
} else {
Mouse.removeListener(_scroller);
}
}
public function get useScrollWheel():Boolean {
return _useScrollWheel;
}
// sets the movie clip that will be scrolled
public function set scrollTarget(targ:MovieClip):Void {
_content = targ;
adjustSize();
}
public function get scrollTarget():MovieClip {
return _content;
}
// sets the amout of ease to use
public function set easeAmount(e:Number):Void {
_easeAmount = (e <=0 ) ? 1 : Math.ceil(e);
}
public function get easeAmount():Number {
return _easeAmount;
}
// sets the color of the scrollbar track
public function set trackColor(col:Number):Void {
var tColor_cxf:ColorTransform = new ColorTransform();
var trans_xf:Transform = new Transform(_scrollTrack);
_trackColor = col;
tColor_cxf.rgb = _trackColor;
trans_xf.colorTransform = tColor_cxf;
}
public function get trackColor():Number {
return _trackColor;
}
// sets the color of the scrollbar scroller
public function set scrollerColor(col:Number):Void {
var tColor_cxf:ColorTransform = new ColorTransform();
var trans_xf:Transform = new Transform(_scroller);
_scrollerColor = col;
tColor_cxf.rgb = _scrollerColor;
trans_xf.colorTransform = tColor_cxf;
}
public function get scrollerColor():Number {
return _scrollerColor;
}
}