1  /**
     2   * com.sekati.effects.TextEffects
     3   * @version 1.1.1
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   */
     8  
     9  import com.sekati.effects.AnimHandler;
    10  import com.sekati.utils.StringUtils;
    11  import com.sekati.utils.TextUtils;
    12  /**
    13   * Static class wrapping various text animation effects
    14   */
    15  class com.sekati.effects.TextEffects {
    16  	/**
    17  	 * Count up any positive numeric value in a text field
    18  	 * @param tf (TextField)
    19  	 * @param n (Number)
    20  	 * @param steps (Number)
    21  	 * @param cb (Function)
    22  	 * @return Void
    23  	 */	
    24  	public static function counter(tf:TextField, n:Number, steps:Number, cb:Function):Void {
    25  		tf.text = "";
    26  		var str:String = n.toString( );
    27  		var count:Number = 0;
    28  		var anim:MovieClip = AnimHandler.create( tf );
    29  		anim.onEnterFrame = function():Void {
    30  			if (count >= n) {
    31  				AnimHandler.destroy( tf );
    32  				tf.text = str;
    33  			} else {
    34  				count += steps;
    35  				var tmp:String = "";
    36  				for (var i:Number = 0; i < (str.length - count.toString( ).length) ; i++) tmp += "0";
    37  				tf.text = tmp + count.toString( );
    38  			}
    39  		};
    40  	}
    41  	/**
    42  	 * Easing alpha type in text animation
    43  	 * @param tf (TextField) target text
    44  	 * @param str (String) string to animate in
    45  	 * @param cb (Function) callback
    46  	 * @return Void
    47  	 */
    48  	public static function atype(tf:TextField, str:String, cb:Function):Void {
    49  		tf.html = true;
    50  		tf._alpha = 0;
    51  		var anim:MovieClip = AnimHandler.create( tf );
    52  		anim.cursor = 0;
    53  		anim.dLen = StringUtils.stripTags( str ).length;
    54  		anim.onEnterFrame = function():Void {
    55  			if (anim.dLen > 30) {
    56  				anim.cursor += 16 + random( 16 );
    57  			} else if (anim.dLen > 20) {
    58  				anim.cursor += 3 + random( 4 );
    59  			} else {
    60  				anim.cursor += 1;
    61  			}
    62  			tf.htmlText = str.substr( 0, anim.cursor );
    63  			anim.dLen = StringUtils.stripTags( str ).length - tf.text.length;
    64  			tf._alpha = int( tf.text.length * 100 / str.length );
    65  			//
    66  			if (anim.dLen <= 1) {
    67  				anim.destroy( );
    68  				tf.htmlText = str;
    69  				tf._alpha = 100;
    70  				cb( );
    71  			}
    72  		};
    73  	}
    74  	/**
    75  	 * easing alpha type out  text animation
    76  	 * @param tf (TextField) target text
    77  	 * @param cb (Function) callback
    78  	 * @return Void
    79  	 */
    80  	public static function uatype(tf:TextField, cb:Function):Void {
    81  		var sLen:Number = tf.htmlText.length;
    82  		trace( "uatype " + sLen );
    83  		// create anim handler                
    84  		var anim:MovieClip = AnimHandler.create( tf );
    85  		anim.cursor = tf.htmlText.length;
    86  		anim.onEnterFrame = function():Void {
    87  			if (anim.cursor == 0) {
    88  				//trace ("uatype done");
    89  				anim.destroy( );
    90  				tf._alpha = 100;
    91  				if (cb) {
    92  					cb( );
    93  				}
    94  			} else {
    95  				//trace ("running: " + anim.cursor);
    96  				anim.cursor -= (40 + random( 25 ));
    97  				tf._alpha = int( tf.htmlText.length * 100 / sLen );
    98  				if (anim.cursor < 0) {
    99  					anim.cursor = 0;
   100  					tf._alpha = 0;
   101  				}
   102  				tf.htmlText = tf.htmlText.substr( 0, anim.cursor );
   103  			}
   104  		};
   105  	}
   106  	/**
   107  	 * text animation pushes text into view
   108  	 * @param tf (TextField) target text
   109  	 * @param str (String) string to animate in
   110  	 * @param ms (Number) miliseconds
   111  	 * @param htm (Boolean) support htmlText
   112  	 * @param cb (Function) callback
   113  	 * @return Void
   114  	 */
   115  	public static function pushtype(tf:TextField, str:String, ms:Number, htm:Boolean, cb:Function):Void {
   116  		if (htm == true) {
   117  			var hstr:String = str;
   118  			str = StringUtils.stripTags( str );
   119  			tf.html = true;
   120  		}
   121  		if (tf.pushing != true) {
   122  			tf.pushing = true;
   123  			var from:Number = str.length;
   124  			tf.wordWrap = true;
   125  			var cp:Number = (str.length > 250) ? 26 : Math.floor( str.length / 4 );
   126  			if (cp < 1) {
   127  				cp = 1;
   128  			}
   129  			var myInterval:Number = setInterval( function():Void {
   130  				if (from > 0) {
   131  					from -= cp;
   132  					var to:Number = str.length;
   133  					tf.text = str.substring( from, to );
   134  				}
   135  				if (tf.text.length == str.length) {
   136  					clearInterval( myInterval );
   137  					if (htm == true) {
   138  						tf.htmlText = hstr;
   139  					}
   140  					tf.pushing = undefined;
   141  					cb( );
   142  				}
   143  				updateAfterEvent( );
   144  			}, ms );
   145  		}
   146  	}
   147  	/**
   148  	 * text animation "types" text into field
   149  	 * @param tf (TextField) target text
   150  	 * @param str (String) string to animate in
   151  	 * @param cursor (String) cursor character
   152  	 * @param isFocus (Boolean) switch textfield to input type and add caret to end of string
   153  	 * @param alphaStep (Number) alpha fade increment
   154  	 * @param pi (Number) pause intervals
   155  	 * @param ms (Number) miliseconds
   156  	 * @param cb (Function) callback
   157  	 * @return Void
   158  	 */
   159  	public static function itype(tf:TextField, str:String, cursor:String, isFocus:Boolean, alphaStep:Number, pi:Number, ms:Number, cb:Function):Void {
   160  		var anim:String = "$__xtype__", fc:Number = 0, index:Number = 0, dLen:Number = str.length + 1;
   161  		if (tf[anim]) {
   162  			clearInterval( tf[anim] );
   163  		}
   164  		delete tf[anim];
   165  		tf.autoSize = true, 
   166  		tf._alpha = 0, 
   167  		tf.text = "", 
   168  		tf.type = "dynamic";
   169  		tf[anim] = setInterval( function ():Void {
   170  			if (fc >= pi) {
   171  				if (tf._alpha < 100) {
   172  					tf._alpha += alphaStep;
   173  				}
   174  				if (dLen > 26) {
   175  					index += 4 + random( 3 );
   176  				} else if (dLen > 10) {
   177  					index += 2 + random( 2 );
   178  				} else {
   179  					index += 1;
   180  				}
   181  				tf.text = str.substr( 0, index ) + cursor;
   182  				dLen = (str.length + 1) - tf.length;
   183  				if (dLen < 2) {
   184  					clearInterval( tf[anim] );
   185  					delete tf[anim];
   186  					if (isFocus) {
   187  						tf.type = "input";
   188  						TextUtils.caret( tf, str.length );
   189  					}
   190  					tf.text = str;
   191  					tf._alpha = 100;
   192  					cb( );
   193  				}
   194  			} else {
   195  				fc++;
   196  			}
   197  			updateAfterEvent( );
   198  		}, ms );
   199  	}
   200  	/**
   201  	 *  text animation "types" text out of field
   202  	 * @param tf (TextField) target text
   203  	 * @param cursor (String) cursor character
   204  	 * @param isFocus (Boolean) switch textfield to input type and add caret to end of string
   205  	 * @param ms (Number) miliseconds
   206  	 * @param cb (Function) callback
   207  	 * @return Void
   208  	 */
   209  	public static function otype(tf:TextField, cursor:String, isFocus:Boolean, ms:Number, cb:Function):Void {
   210  		var str:String = tf.text, anim:String = "$__xtype__", undex:Number = 0, toc:Number = str.length + 1;
   211  		tf.autoSize = true, 
   212  		tf.type = "dynamic";
   213  		if (tf[anim]) {
   214  			clearInterval( tf[anim] );
   215  		}
   216  		delete tf[anim];
   217  		tf[anim] = setInterval( function ():Void {
   218  			if (toc > str.length) {
   219  				if (toc > 26) {
   220  					undex -= 4 + random( 3 );
   221  				} else if (toc > 10) {
   222  					undex -= 2 + random( 2 );
   223  				} else {
   224  					undex -= 1;
   225  				}
   226  				// old: if(toc>26) undex-=1; else if(toc>10) undex-=2+random(2); else undex-=4+random(3);
   227  				tf.text = str.substr( 0, undex ) + cursor;
   228  				toc = (str.length - 1) + tf.length;
   229  			} else {
   230  				clearInterval( tf[anim] );
   231  				delete tf[anim];
   232  				tf.text = "";
   233  				if (isFocus) {
   234  					tf.type = "input";
   235  					TextUtils.caret( tf, 0 );
   236  				}
   237  				cb( );
   238  			}
   239  			updateAfterEvent( );
   240  		}, ms );
   241  	}
   242  	/**
   243  	 * text animation type text in, pauses and types text out of  field
   244  	 * @param tf (TextField) target text
   245  	 * @param str (String) string to animate in
   246  	 * @param cursor (String) cursor character
   247  	 * @param isFocus (Boolean) switch textfield to input type and add caret to end of string
   248  	 * @param alphaStep (Number) alpha fade increment
   249  	 * @param pi (Number) pause intervals
   250  	 * @param ms (Number) miliseconds
   251  	 * @param cb (Function) callback
   252  	 * @return Void
   253  	 */
   254  	public static function iotype(tf:TextField, str:String, cursor:String, isFocus:Boolean, alphaStep:Number, pauseIndex:Number, ms:Number, cb:Function):Void {
   255  		var anim:String = "$__iotype__", fc:Number = 0, index:Number = 0, undex:Number = 0, dLen:Number = str.length + 1, toc:Number = str.length + 1;
   256  		if (tf[anim]) {
   257  			clearInterval( tf[anim] );
   258  		}
   259  		delete tf[anim];
   260  		tf.autoSize = true, 
   261  		tf._alpha = 0, 
   262  		tf.text = "", 
   263  		tf.type = "dynamic";
   264  		tf[anim] = setInterval( function ():Void {
   265  			if (tf._alpha < 100) {
   266  				tf._alpha += alphaStep;
   267  			}
   268  			if (dLen > 26) {
   269  				index += 4 + random( 3 );
   270  			} else if (dLen > 10) {
   271  				index += 2 + random( 2 );
   272  			} else {
   273  				index += 1;
   274  			}
   275  			tf.text = str.substr( 0, index ) + cursor;
   276  			dLen = (str.length + 1) - tf.length;
   277  			if (dLen < 2) {
   278  				// finished type-in
   279  				tf.text = str;
   280  				tf._alpha = 100;
   281  				if (fc >= pauseIndex) {
   282  					// if pause is finished
   283  					//--------------------------------------
   284  					if (toc > str.length) {
   285  						//--untype start--
   286  						//if(toc>26) undex -= 4+random(3); else if(toc>10) undex -= 2+random(2); else undex -= 1; // old
   287  						if (toc > 26) {
   288  							undex -= 1;
   289  						} else if (toc > 10) {
   290  							undex -= 2 + random( 2 );
   291  						} else {
   292  							undex -= 4 + random( 3 );
   293  						}
   294  						tf.text = str.substr( 0, undex ) + cursor;
   295  						toc = (str.length - 1) + tf.length;
   296  						//--untype stop--
   297  					} else {
   298  						// finished type-out: exit
   299  						clearInterval( tf[anim] );
   300  						delete tf[anim];
   301  						tf.text = "";
   302  						if (isFocus) {
   303  							tf.type = "input";
   304  							TextUtils.caret( tf, str.length );
   305  						}
   306  						cb( );
   307  					}
   308  					//--------------------------------------
   309  				} else {
   310  					fc++;
   311  				}
   312  			}
   313  			updateAfterEvent( );
   314  		}, ms );
   315  	}
   316  	private function TextEffects() {
   317  	}
   318  }