Archive for the tag 'scrolling'

AS3: Double AxisScroller

View Preview
Download Example Files
Download AxisScroller

I've been asked a couple of times to show a demonstration of the AxisScroller being used on both the X and Y axis. Here is an example of it in AS3. For some reason I haven't been able to replicate this behavior with the AS2 version but I'll continue to look into it further and see if I can come up with a solution. If anyone else wants to take a shot at it, by all means please do so.

Actionscript:
  1. import flash.events.MouseEvent;
  2. import fl.transitions.Tween;
  3. import fl.transitions.easing.*;
  4. import com.reintroducing.ui.AxisScroller;
  5.  
  6. stop();
  7.  
  8. var optionalObjV:Object = new Object(
  9. {
  10.     scrollType: "easing",
  11.     isTrackClickable: true,
  12.     useArrows: true,
  13.     upArrow: content_mc.up_btn,
  14.     downArrow: content_mc.down_btn,
  15.     continuousScroll: false,
  16.     easeFunc: Regular.easeOut,
  17.     duration: .25,
  18.     arrowMove: 50,
  19.     scaleScroller: true,
  20.     autoHideControls: true
  21. });
  22.  
  23. var optionalObjH:Object = new Object(
  24. {
  25.     scrollType: "easing",
  26.     isTrackClickable: true,
  27.     useArrows: true,
  28.     upArrow: content_mc.left_btn,
  29.     downArrow: content_mc.right_btn,
  30.     continuousScroll: false,
  31.     easeFunc: Regular.easeOut,
  32.     duration: .25,
  33.     arrowMove: 50,
  34.     scaleScroller: true,
  35.     autoHideControls: true
  36. });
  37.  
  38. var scrollerV:AxisScroller = new AxisScroller(stage, content_mc, content_mc.scrollerV_mc, content_mc.movie_mc, content_mc.trackV_mc, content_mc.mask_mc, "y", optionalObjV);
  39. var scrollerH:AxisScroller = new AxisScroller(stage, content_mc, content_mc.scrollerH_mc, content_mc.movie_mc, content_mc.trackH_mc, content_mc.mask_mc, "x", optionalObjH);
  40.  
  41. reset_btn.addEventListener(MouseEvent.MOUSE_DOWN, resetScroller);
  42.  
  43. function resetScroller($evt:MouseEvent):void
  44. {
  45.     scrollerV.reset();
  46.     scrollerH.reset();
  47. }

Tags: , ,

AS3: AxisScroller v1.0

View Example
View Documentation
Download Class & Example Files

AxisScroller is a scroller class that can be used in any of your AS3 projects. It's an update to the original AS2 AxisScroller and it is versioned at v1.0. The syntax for usage is almost identical except that I added a first parameter, the stage reference, so that the keyboard would work in AS3 as the keys are bound to the stage. This version also includes all the new stuff that was introduced in v2.0 of AxisScroller for AS2 and will be updated in the future simultaneously.

Actionscript:
  1. /**
  2. * 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.
  3. *
  4. * @usage
  5. * <code>
  6. * <pre>
  7. import flash.events.MouseEvent;
  8. import fl.transitions.Tween;
  9. import fl.transitions.easing.*;
  10. import com.reintroducing.ui.AxisScroller;
  11. stop();
  12. var optionalObj:Object = new Object(
  13. {
  14.     scrollType: "easing",
  15.     isTrackClickable: true,
  16.     useArrows: true,
  17.     upArrow: content_mc.up_btn,
  18.     downArrow: content_mc.down_btn,
  19.     continuousScroll: false,
  20.     easeFunc: Regular.easeOut,
  21.     duration: .25,
  22.     arrowMove: 50,
  23.     scaleScroller: true,
  24.     autoHideControls: true
  25. });
  26. var scroller:AxisScroller = new AxisScroller(stage, content_mc, content_mc.scroller_mc, content_mc.movie_mc, content_mc.track_mc, content_mc.mask_mc, "y", optionalObj);
  27. reset_btn.addEventListener(MouseEvent.MOUSE_DOWN, resetScroller);
  28. function resetScroller($evt:MouseEvent):void
  29. {
  30.     scroller.reset();
  31. }
  32. * </pre>
  33. * </code>
  34. *
  35. * @author Matt Przybylski [http://www.reintroducing.com]
  36. * @version 1.0
  37. */
  38.  
  39. package com.reintroducing.ui
  40. {
  41.     import flash.display.MovieClip;
  42.     import flash.display.Stage;
  43.     import flash.events.Event;
  44.     import flash.events.KeyboardEvent;
  45.     import flash.events.MouseEvent;
  46.     import flash.events.TimerEvent;
  47.     import flash.utils.Timer;
  48.    
  49.     import fl.transitions.Tween;
  50.     import fl.transitions.easing.*;
  51.    
  52.     public class AxisScroller
  53.     {
  54. //- PRIVATE & PROTECTED VARIABLES -------------------------------------------------------------------------
  55.  
  56.         private var _stage:Stage;
  57.         private var _changeProp:String;
  58.         private var _mouseAxis:String;
  59.         private var _changeScale:String;
  60.         private var _nextPosition:int;
  61.         private var _arrowMove:Number;
  62.         private var _scaleScroller:Boolean;
  63.         private var _autoHideControls:Boolean;
  64.         private var _mouseIsPressedDown:Boolean;
  65.         private var _trackStart:Number;
  66.         private var _trackEnd:Number;
  67.         private var _tween:Tween;
  68.         private var _arrow1Tween:Tween;
  69.         private var _arrow2Tween:Tween;
  70.         private var _scrollerTween:Tween;
  71.         private var _trackTween:Tween;
  72.         private var _easeFunc:Function;
  73.         private var _duration:Number;
  74.         private var _holder:MovieClip;
  75.         private var _scroller:MovieClip;
  76.         private var _toBeScrolled:MovieClip;
  77.         private var _track:MovieClip;
  78.         private var _mask:MovieClip;
  79.         private var _axis:String;
  80.         private var _scrollType:String;
  81.         private var _isTrackClickable:Boolean;
  82.         private var _arrows:Boolean;
  83.         private var _upArrow:MovieClip;
  84.         private var _downArrow:MovieClip;
  85.         private var _continuousScroll:Boolean;
  86.         private var _arrowTimer:Timer;
  87.         private var _arrowPressed:String;
  88.        
  89. //- PUBLIC & INTERNAL VARIABLES ---------------------------------------------------------------------------
  90.        
  91.         public static const DEFAULT_NAME:String = "com.reintroducing.ui.AxisScroller";
  92.        
  93. //- CONSTRUCTOR -------------------------------------------------------------------------------------------
  94.    
  95.         /**
  96.          * Creates a new instance of the AxisScroller class.
  97.          *
  98.          * <p>
  99.          * The $optionalParams parameter takes in a couple of values.
  100.          * <ul>
  101.          * <li>scrollType: A string representing the scroll type, "instant" or "easing" (default: "easing")</li>
  102.          * <li>isTrackClickable: A boolean value that makes the track clickable so that you can scroll directly to a certain spot (default: true)</li>
  103.          * <li>useArrows: A boolean value that allows the use of arrow buttons (default: false)</li>
  104.          * <li>upArrow: If useArrows, a movie clip that represents the up arrow button (default: null)</li>
  105.          * <li>downArrow: If useArrows, a movie clip that represents the down arrow button (default: null)</li>
  106.          * <li>continuousScroll: If useArrows, a boolean that specifies if when pressing the arrows the scrolling should happen continuously or not (default: false)</li>
  107.          * <li>easeFunc: A function representing the ease type you'd like to use, as per the Tween class easing functions (default: Regular.easeOut)</li>
  108.          * <li>duration: An integer (in seconds) representing the time that the scrollable content will take to move when easing is used (default: .25)</li>
  109.          * <li>arrowMove: An integer (in pixels) that the scrollable content will move onMouseWheel/Key/Arrow movement (default: 100)</li>
  110.          * <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>
  111.          * <li>scaleScroller *: A boolean value that allows automatic scaling of the scroll bar (default: false)</li>
  112.          * </ul>
  113.          * * 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
  114.          * scaling it automatically for you.  If you AREN'T using this parameter, please size your scroll bar accordingly manually.
  115.          * </p>
  116.          *
  117.          * @usage <pre><code>var scroller:AxisScroller = new AxisScroller($stage, $holder, $scroller, $toBeScrolled, $track, $mask, $axis, $optionalParams);</code></pre>
  118.          *
  119.          * @param $stage The stage object to use for keyboard input
  120.          * @param $holder The movie clip (or timeline) that holds all of the other clips corresponding to the scroller
  121.          * @param $scroller The movie clip to be used as the scroller
  122.          * @param $toBeScrolled The movie clip where the scrollable content resides
  123.          * @param $track The movie clip that is used as the track (measures extremes)
  124.          * @param $mask The movie clip that is used as the mask for the scrollable content area
  125.          * @param $axis A string value of either "x" or "y" that determines what axis the content is scrolled along
  126.          * @param $optionalParams Optional parameters to further control the scroller
  127.          */
  128.        
  129.         public function AxisScroller($stage:Stage, $holder:MovieClip, $scroller:MovieClip, $toBeScrolled:MovieClip, $track:MovieClip, $mask:MovieClip, $axis:String, $optionalParams:Object)
  130.         {
  131.             this._stage      = $stage;
  132.             this._holder                = $holder;
  133.             this._scroller      = $scroller;
  134.             this._toBeScrolled     = $toBeScrolled;
  135.             this._track                     = $track;
  136.             this._mask       = $mask;
  137.             this._axis       = $axis;
  138.            
  139.             this._scrollType             = ($optionalParams.scrollType == null) ? "easing" : $optionalParams.scrollType;
  140.             this._isTrackClickable    = ($optionalParams.isTrackClickable == null) ? true : $optionalParams.isTrackClickable;
  141.             this._arrows                = ($optionalParams.useArrows == null) ? false : $optionalParams.useArrows;
  142.             this._upArrow           = ($optionalParams.upArrow == null) ? null : $optionalParams.upArrow;
  143.             this._downArrow                 = ($optionalParams.downArrow == null) ? null : $optionalParams.downArrow;
  144.             this._continuousScroll      = ($optionalParams.continuousScroll == null) ? false : $optionalParams.continuousScroll;
  145.             this._easeFunc      = ($optionalParams.easeFunc == null) ? Regular.easeOut : $optionalParams.easeFunc;
  146.             this._duration      = ($optionalParams.duration == null) ? .25 : $optionalParams.duration;
  147.             this._arrowMove                 = ($optionalParams.arrowMove == null) ? 100 : $optionalParams.arrowMove;
  148.             this._autoHideControls      = ($optionalParams.autoHideControls == null) ? false : $optionalParams.autoHideControls;
  149.             this._scaleScroller    = ($optionalParams.scaleScroller == null) ? false : $optionalParams.scaleScroller;
  150.            
  151.             this.manageAxis(this._axis);
  152.             this.init();
  153.         }
  154.        
  155. //- PRIVATE & PROTECTED METHODS ---------------------------------------------------------------------------
  156.        
  157.         // initiates listeners and other events
  158.         private function init():void
  159.         {
  160.             this._holder.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
  161.             this._holder.addEventListener(Event.ENTER_FRAME, manageScrolling);
  162.             this._stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
  163.            
  164.             if (this._continuousScroll)
  165.             {
  166.                 this._arrowTimer = new Timer(80);
  167.                 this._arrowTimer.addEventListener(TimerEvent.TIMER, manageContinuousScroll);
  168.             }
  169.            
  170.             this.activateButtonEvents();
  171.         }
  172.        
  173.         // re-activates the button events
  174.         private function activateButtonEvents():void
  175.         {
  176.             this._scroller.addEventListener(MouseEvent.MOUSE_DOWN, manageMouseDown);
  177.             this._scroller.buttonMode = true;
  178.            
  179.             if (this._isTrackClickable)
  180.             {
  181.                 this._track.addEventListener(MouseEvent.MOUSE_UP, manageTrack);
  182.                 this._track.buttonMode = true;
  183.             }
  184.            
  185.             if (this._arrows)
  186.             {
  187.                 this._upArrow.addEventListener(MouseEvent.MOUSE_DOWN, manageUpArrow);
  188.                 this._upArrow.addEventListener(MouseEvent.MOUSE_UP, resetArrowTimer);
  189.                
  190.                 this._downArrow.addEventListener(MouseEvent.MOUSE_DOWN, manageDownArrow);
  191.                 this._downArrow.addEventListener(MouseEvent.MOUSE_UP, resetArrowTimer);
  192.                
  193.                 this._downArrow.buttonMode = this._upArrow.buttonMode = true;
  194.             }
  195.         }
  196.        
  197.         // destroys the events on the buttons
  198.         private function killButtonEvents():void
  199.         {
  200.             this._scroller.removeEventListener(MouseEvent.MOUSE_DOWN, manageMouseDown);
  201.             this._scroller.buttonMode = false;
  202.            
  203.             if (this._isTrackClickable)
  204.             {
  205.                 this._track.removeEventListener(MouseEvent.MOUSE_UP, manageTrack);
  206.                 this._track.buttonMode = false;
  207.             }
  208.            
  209.             if (this._arrows)
  210.             {
  211.                 this._upArrow.removeEventListener(MouseEvent.MOUSE_UP, manageUpArrow);
  212.                 this._upArrow.removeEventListener(MouseEvent.MOUSE_DOWN, manageUpArrow);
  213.                
  214.                 this._downArrow.removeEventListener(MouseEvent.MOUSE_UP, manageDownArrow);
  215.                 this._downArrow.removeEventListener(MouseEvent.MOUSE_DOWN, manageDownArrow);
  216.                
  217.                 this._downArrow.buttonMode = this._upArrow.buttonMode = false;
  218.             }
  219.         }
  220.        
  221.         // sets the axis at which the scroller will function against
  222.         private function manageAxis($axis:String):void
  223.         {
  224.             if ($axis == "x")
  225.             {
  226.                 this._changeProp          = "width";
  227.                 this._mouseAxis             = "mouseX";
  228.                 this._changeScale         = "scaleX";
  229.             }
  230.             else if ($axis == "y")
  231.             {
  232.                 this._changeProp          = "height";
  233.                 this._mouseAxis             = "mouseY";
  234.                 this._changeScale         = "scaleY";
  235.             }
  236.         }
  237.        
  238.         // calculates values and responds to changes by the scroller
  239.         private function manageScrolling($evt:Event):void
  240.         {
  241.             var trackSize:Number          = this._track[this._changeProp];
  242.             var trackStart:Number       = this._track[this._axis];
  243.             var trackEnd:Number             = this._track[this._axis] + trackSize - (this._scroller[this._changeProp]);
  244.             var trackRange:Number       = trackEnd - trackStart;
  245.             var maskSize:Number             = this._mask[this._changeProp];
  246.             var movieSize:Number          = this._toBeScrolled[this._changeProp];
  247.             var minPosition:Number    = this._mask[this._axis] + 2;
  248.             var maxPosition:Number    = minPosition + movieSize - maskSize + 4;
  249.             var onePercentChange:Number     = (maxPosition - minPosition) / 100;
  250.             var percent:int                 = Math.round(((this._scroller[this._axis] - trackStart) / trackRange) * 100);
  251.             var targetPosition:int     = Math.round(minPosition - (onePercentChange * percent));
  252.            
  253.             this._trackStart             = trackStart;
  254.             this._trackEnd      = trackEnd;
  255.            
  256.             if (this._scrollType == "instant")
  257.             {
  258.                 this._toBeScrolled[this._axis] = targetPosition;
  259.             }
  260.             else if (this._scrollType == "easing")
  261.             {
  262.                 this._toBeScrolled[this._axis] += Math.round((targetPosition - this._toBeScrolled[this._axis]) / 5);
  263.             }
  264.    
  265.             if (movieSize <maskSize)
  266.             {
  267.                 onePercentChange = 0;
  268.             }
  269.            
  270.             if (this._mouseIsPressedDown)
  271.             {
  272.                 this._scroller[this._axis] = Math.round(this._holder[this._mouseAxis] - (this._scroller[this._changeProp] / 2));
  273.             }
  274.            
  275.             if (this._scroller[this._axis] <trackStart)
  276.             {
  277.                 this._scroller[this._axis] = Math.round(trackStart);
  278.             }
  279.             else if (this._scroller[this._axis]> trackEnd)
  280.             {
  281.                 this._scroller[this._axis] = Math.round(trackEnd);
  282.             }
  283.            
  284.             if (this._scaleScroller) this.manageScrollScaling(movieSize, maskSize);
  285.            
  286.             this.manageControls(this._autoHideControls, movieSize, maskSize);
  287.         }
  288.        
  289.         // regulates mouse pressed down
  290.         private function manageMouseDown($evt:MouseEvent):void
  291.         {
  292.             this._mouseIsPressedDown = true;