AS2: AxisScroller v2.0

View Example
View Documentation
Download Class & Example Files

This is the latest update to the AxisScroller. This update adds a new optionalParameter, continuousScroll, which allows you to scroll (if the arrows are active) continuously by just pressing down one of the arrows. There is also a new method, reset(), that resets the scroller and content movie clips to its original positions. Lastly, some of the code was condensed to promote reuse. If you're using the AxisScroller, definitely upgrade to this version. The syntax for usage was all kept in tact so you should have no problems just replacing this in a project (nothing should be broken because of it).

Actionscript:
  1. import mx.utils.Delegate;
  2. import mx.transitions.easing.*;
  3. import mx.transitions.Tween;
  4.  
  5. /**
  6. * Creates a scroller that allows for scrolling in the x or y axis, using the mouse wheel/arrow keys, different easing functions and types, and some other miscellaneous cool things.
  7. *
  8. * @usage
  9. * <code>
  10. * <pre>
  11. import com.reintroducing.ui.AxisScroller;
  12. stop();
  13. var optionalObj:Object = new Object(
  14. {
  15.     scrollType: "easing",
  16.     isTrackClickable: true,
  17.     useArrows: true,
  18.     upArrow: content_mc.up_btn,
  19.     downArrow: content_mc.down_btn,
  20.     continuousScroll: true,
  21.     easeFunc: Regular.easeOut,
  22.     duration: .25,
  23.     arrowMove: 50,
  24.     scaleScroller: true,
  25.     autoHideControls: true
  26. });
  27. var scroller:AxisScroller = new AxisScroller(content_mc, content_mc.scroller_mc, content_mc.movie_mc, content_mc.track_mc, content_mc.mask_mc, "y", optionalObj);
  28. * </pre>
  29. * </code>
  30. *
  31. * @author Matt Przybylski [http://www.reintroducing.com]
  32. * @version 2.0
  33. */
  34.  
  35. class com.reintroducing.ui.AxisScroller
  36. {
  37. //- PRIVATE VARIABLES -------------------------------------------------------------------------------------
  38.    
  39.     private var _changeProp:String;
  40.     private var _mouseAxis:String;
  41.     private var _changeScale:String;
  42.     private var _nextPosition:Number;
  43.     private var _arrowMove:Number;
  44.     private var _scaleScroller:Boolean;
  45.     private var _autoHideControls:Boolean;
  46.     private var _mouseIsPressedDown:Boolean;
  47.     private var _trackOffset:Number;
  48.     private var _trackStart:Number;
  49.     private var _trackEnd:Number;
  50.     private var _tween:Tween;
  51.     private var _arrow1Tween:Tween;
  52.     private var _arrow2Tween:Tween;
  53.     private var _scrollerTween:Tween;
  54.     private var _trackTween:Tween;
  55.     private var _easeFunc:Function;
  56.     private var _duration:Number;
  57.     private var _holder:MovieClip;
  58.     private var _scroller:MovieClip;
  59.     private var _toBeScrolled:MovieClip;
  60.     private var _track:MovieClip;
  61.     private var _mask:MovieClip;
  62.     private var _axis:String;
  63.     private var _scrollType:String;
  64.     private var _isTrackClickable:Boolean;
  65.     private var _arrows:Boolean;
  66.     private var _upArrow:MovieClip;
  67.     private var _downArrow:MovieClip;
  68.     private var _continuousScroll:Boolean;
  69.     private var _arrowTimer:Number;
  70.     private var _arrowPressed:String;
  71.    
  72. //- PUBLIC VARIABLES --------------------------------------------------------------------------------------
  73.    
  74.     public static var DEFAULT_NAME:String = "com.reintroducing.ui.AxisScroller";
  75.  
  76. //- CONSTRUCTOR -------------------------------------------------------------------------------------------
  77.    
  78.     /**
  79.      * Creates a new instance of the AxisScroller class.
  80.      *
  81.      * <p>
  82.      * The $optionalParams parameter takes in a couple of values.
  83.      * <ul>
  84.      * <li>scrollType: A string representing the scroll type, "instant" or "easing" (default: "easing")</li>
  85.      * <li>isTrackClickable: A boolean value that makes the track clickable so that you can scroll directly to a certain spot (default: true)</li>
  86.      * <li>useArrows: A boolean value that allows the use of arrow buttons (default: false)</li>
  87.      * <li>upArrow: If useArrows, a movie clip that represents the up arrow button (default: null)</li>
  88.      * <li>downArrow: If useArrows, a movie clip that represents the down arrow button (default: null)</li>
  89.      * <li>continuousScroll: If useArrows, a boolean that specifies if when pressing the arrows the scrolling should happen continuously or not (default: false)</li>
  90.      * <li>easeFunc: A function representing the ease type you'd like to use, as per the Tween class easing functions (default: Regular.easeOut)</li>
  91.      * <li>duration: An integer (in seconds) representing the time that the scrollable content will take to move when easing is used (default: .25)</li>
  92.      * <li>arrowMove: An integer (in pixels) that the scrollable content will move onMouseWheel/Key/Arrow movement (default: 100)</li>
  93.      * <li>autoHideControls: A boolean value that hides the scroller/track/arrows (if applicable) if the scroll content is too small to scroll (default: false)</li>
  94.      * <li>scaleScroller *: A boolean value that allows automatic scaling of the scroll bar (default: false)</li>
  95.      * </ul>
  96.      * * If you use the scaleScroller optional parameter, please make the size of the scroll bar the same size as the track and this will take care of
  97.      * scaling it automatically for you.  If you AREN'T using this parameter, please size your scroll bar accordingly manually.
  98.      * </p>
  99.      *
  100.      * @usage <pre><code>var scroller:AxisScroller = new AxisScroller($holder, $scroller, $toBeScrolled, $track, $mask, $axis, $optionalParams);</code></pre>
  101.      *
  102.      * @param $holder The movie clip (or timeline) that holds all of the other clips corresponding to the scroller
  103.      * @param $scroller The movie clip to be used as the scroller
  104.      * @param $toBeScrolled The movie clip where the scrollable content resides
  105.      * @param $track The movie clip that is used as the track (measures extremes)
  106.      * @param $mask The movie clip that is used as the mask for the scrollable content area
  107.      * @param $axis A string value of either "x" or "y" that determines what axis the content is scrolled along
  108.      * @param $optionalParams Optional parameters to further control the scroller
  109.      */
  110.    
  111.     public function AxisScroller($holder:MovieClip, $scroller:MovieClip, $toBeScrolled:MovieClip, $track:MovieClip, $mask:MovieClip, $axis:String, $optionalParams:Object)
  112.     {
  113.         this._holder                = $holder;
  114.         this._scroller      = $scroller;
  115.         this._toBeScrolled     = $toBeScrolled;
  116.         this._track                     = $track;
  117.         this._mask       = $mask;
  118.         this._axis       = "_" + $axis;
  119.        
  120.         this._scrollType             = ($optionalParams.scrollType == undefined) ? "easing" : $optionalParams.scrollType;
  121.         this._isTrackClickable    = ($optionalParams.isTrackClickable == undefined) ? true : $optionalParams.isTrackClickable;
  122.         this._arrows                = ($optionalParams.useArrows == undefined) ? false : $optionalParams.useArrows;
  123.         this._upArrow           = ($optionalParams.upArrow == undefined) ? null : $optionalParams.upArrow;
  124.         this._downArrow                 = ($optionalParams.downArrow == undefined) ? null : $optionalParams.downArrow;
  125.         this._continuousScroll      = ($optionalParams.continuousScroll == undefined) ? false : $optionalParams.continuousScroll;
  126.         this._easeFunc      = ($optionalParams.easeFunc == undefined) ? Regular.easeOut : $optionalParams.easeFunc;
  127.         this._duration      = ($optionalParams.duration == undefined) ? .25 : $optionalParams.duration;
  128.         this._arrowMove                 = ($optionalParams.arrowMove == undefined) ? 100 : $optionalParams.arrowMove;
  129.         this._autoHideControls      = ($optionalParams.autoHideControls == undefined) ? false : $optionalParams.autoHideControls;
  130.         this._scaleScroller    = ($optionalParams.scaleScroller == undefined) ? false : $optionalParams.scaleScroller;
  131.        
  132.         this.manageAxis(this._axis);
  133.         this.init();
  134.     }
  135.    
  136. //- PRIVATE METHODS ---------------------------------------------------------------------------------------
  137.    
  138.     // initiates listeners and other events
  139.     private function init():Void
  140.     {
  141.         Mouse.addListener(this);
  142.         Key.addListener(this);
  143.        
  144.         this._holder.onEnterFrame       = Delegate.create(this, manageScrolling);
  145.        
  146.         this.activateButtonEvents();
  147.     }
  148.    
  149.     // re-activates the button events
  150.     private function activateButtonEvents():Void
  151.     {
  152.         this._scroller.onPress     = Delegate.create(this, manageMouseDown);
  153.        
  154.         this._scroller.onRelease = this._scroller.onReleaseOutside = Delegate.create(this, manageMouseUp);
  155.         this._scroller.useHandCursor = true;
  156.        
  157.         if (this._isTrackClickable)
  158.         {
  159.             this._track.onRelease       = Delegate.create(this, manageTrack);
  160.             this._track.useHandCursor      = true;
  161.         }
  162.        
  163.         if (this._arrows)
  164.         {
  165.             this._upArrow.onPress        = Delegate.create(this, manageUpArrow);
  166.             this._upArrow.onRelease         = Delegate.create(this, resetArrowTimer);
  167.            
  168.             this._downArrow.onPress         = Delegate.create(this, manageDownArrow);
  169.             this._downArrow.onRelease     = Delegate.create(this, resetArrowTimer);
  170.            
  171.             this._downArrow.useHandCursor = this._upArrow.useHandCursor = true;
  172.         }
  173.     }
  174.    
  175.     // destroys the events on the buttons
  176.     private function killButtonEvents():Void
  177.     {
  178.         this._scroller.onPress = this._scroller.onRelease = this._scroller.onReleaseOutside = null;
  179.         this._scroller.useHandCursor = false;
  180.        
  181.         if (this._isTrackClickable)
  182.         {
  183.             this._track.onRelease = null;
  184.             this._track.useHandCursor = false;
  185.         }
  186.        
  187.         if (this._arrows)
  188.         {
  189.             this._upArrow.onPress = this._downArrow.onPress = null;
  190.             this._upArrow.onRelease = this._downArrow.onRelease = null;
  191.             this._upArrow.useHandCursor = this._downArrow.useHandCursor = false;
  192.         }
  193.     }
  194.    
  195.     // sets the axis at which the scroller will function against
  196.     private function manageAxis($axis:String):Void
  197.     {
  198.         if ($axis == "_x")
  199.         {
  200.             this._changeProp          = "_width";
  201.             this._mouseAxis             = "_xmouse";
  202.             this._changeScale         = "_xscale";
  203.         }
  204.         else if ($axis == "_y")
  205.         {
  206.             this._changeProp          = "_height";
  207.             this._mouseAxis             = "_ymouse";
  208.             this._changeScale         = "_yscale";
  209.         }
  210.        
  211.         this._trackOffset         = - (this._scroller[this._changeProp] / 2);
  212.     }
  213.    
  214.     // calculates values and responds to changes by the scroller
  215.     private function manageScrolling():Void
  216.     {
  217.         var trackSize:Number          = this._track[this._changeProp];
  218.         var trackStart:Number       = this._track[this._axis];
  219.         var trackEnd:Number             = this._track[this._axis] + trackSize - (this._scroller[this._changeProp]);
  220.         var trackRange:Number       = trackEnd - trackStart;
  221.         var maskSize:Number             = this._mask[this._changeProp];
  222.         var movieSize:Number          = this._toBeScrolled[this._changeProp];
  223.         var minPosition:Number    = this._mask[this._axis] + 2;
  224.         var maxPosition:Number    = minPosition + movieSize - maskSize + 4;
  225.         var onePercentChange:Number     = (maxPosition - minPosition) / 100;
  226.         var percent:Number     = Math.round(((this._scroller[this._axis] - trackStart) / trackRange) * 100);
  227.         var targetPosition:Number     = Math.round(minPosition - (onePercentChange * percent));
  228.        
  229.         this._trackStart             = trackStart;
  230.         this._trackEnd      = trackEnd;
  231.        
  232.         if (this._scrollType == "instant")
  233.         {
  234.             this._toBeScrolled[this._axis] = targetPosition;
  235.         }
  236.         else if (this._scrollType == "easing")
  237.         {
  238.             this._toBeScrolled[this._axis] += Math.round((targetPosition - this._toBeScrolled[this._axis]) / 5);
  239.         }
  240.  
  241.         if (movieSize <maskSize)
  242.         {
  243.             onePercentChange = 0;
  244.         }
  245.        
  246.         if (this._mouseIsPressedDown)
  247.         {
  248.             this._scroller[this._axis] = Math.round(this._holder[this._mouseAxis] - (this._scroller[this._changeProp] / 2));
  249.         }
  250.        
  251.         if (this._scroller[this._axis] <trackStart)
  252.         {
  253.             this._scroller[this._axis] = Math.round(trackStart);
  254.         }
  255.         else if (this._scroller[this._axis]> trackEnd)
  256.         {
  257.             this._scroller[this._axis] = Math.round(trackEnd);
  258.         }
  259.        
  260.         if (this._scaleScroller) this.manageScrollScaling(movieSize, maskSize);
  261.        
  262.         this.manageControls(this._autoHideControls, movieSize, maskSize);
  263.     }
  264.    
  265.     // regulates mouse pressed down
  266.     private function manageMouseDown():Void
  267.     {
  268.         this._mouseIsPressedDown = true;
  269.     }
  270.    
  271.     // regulates mouse released
  272.     private function manageMouseUp():Void
  273.     {
  274.         this._mouseIsPressedDown = false;
  275.     }
  276.    
  277.     // moves the scroller up when the up arrow is clicked
  278.     private function moveUp():Void
  279.     {
  280.         if (this._scrollType == "instant")
  281.         {
  282.             this._nextPosition = Math.round(this._scroller[this._axis] - this._arrowMove);
  283.            
  284.             if (this._nextPosition <this._trackStart)
  285.             {
  286.                 this._scroller[this._axis] = this._trackStart;
  287.             }
  288.             else
  289.             {
  290.                 this._scroller[this._axis] = this._nextPosition;
  291.             }
  292.         }
  293.         else if (this._scrollType == "easing")
  294.         {
  295.             this._nextPosition = Math.round(this._scroller[this._axis] - this._arrowMove);
  296.            
  297.             if (this._nextPosition <this._trackStart)
  298.             {
  299.                 this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._trackStart, this._duration, true);
  300.             }
  301.             else
  302.             {
  303.                 this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._nextPosition, this._duration, true);
  304.             }
  305.         }
  306.     }
  307.    
  308.     // moves the scroller down when the down arrow is clicked
  309.     private function moveDown():Void
  310.     {
  311.         if (this._scrollType == "instant")
  312.         {
  313.             this._nextPosition = Math.round(this._scroller[this._axis] + this._arrowMove);
  314.            
  315.             if (this._nextPosition> this._trackEnd)
  316.             {
  317.                 this._scroller[this._axis] = this._trackEnd;
  318.             }
  319.             else
  320.             {
  321.                 this._scroller[this._axis] = this._nextPosition;
  322.             }
  323.         }
  324.         else if (this._scrollType == "easing")
  325.         {
  326.             this._nextPosition = Math.round(this._scroller[this._axis] + this._arrowMove);
  327.            
  328.             if (this._nextPosition> this._trackEnd)
  329.             {
  330.                 this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._trackEnd, this._duration, true);
  331.             }
  332.             else
  333.             {
  334.                 this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._nextPosition, this._duration, true);
  335.             }
  336.         }
  337.     }
  338.    
  339.     // regulates the movement of the up arrow, if applicable
  340.     private function manageUpArrow():Void
  341.     {
  342.         if (this._continuousScroll)
  343.         {
  344.             this._arrowPressed = "up";
  345.             this._arrowTimer = setInterval(this, "manage