PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/!src/com/greensock11.2/text/FlexSplitTextField.as

https://github.com/tluczyns/tluczyns-as3-base
ActionScript | 372 lines | 216 code | 29 blank | 127 comment | 51 complexity | b2a5fef2e0e50f4459154130252d6344 MD5 | raw file
  1. /**
  2. * VERSION: 0.62
  3. * DATE: 2010-03-05
  4. * AS3
  5. * UPDATES AND DOCUMENTATION AT: http://www.greensock.com
  6. **/
  7. package com.greensock.text {
  8. import flash.display.DisplayObject;
  9. import flash.display.DisplayObjectContainer;
  10. import flash.display.Sprite;
  11. import flash.geom.Matrix;
  12. import flash.geom.Point;
  13. import flash.geom.Rectangle;
  14. import flash.text.TextField;
  15. import flash.text.TextFieldAutoSize;
  16. import flash.text.TextFormat;
  17. import flash.text.TextFormatAlign;
  18. import flash.text.TextLineMetrics;
  19. import mx.core.UIComponent;
  20. /**
  21. * FlexSplitTextField makes it easy to break apart the TextField in a UIComponent like a Label,
  22. * Text, or TextArea so that each character, word, or line is in its own TextField, making complex
  23. * animation simple. When you create a FlexSplitTextField, it loops through the source's children
  24. * looking for a TextField and when it finds one, it replaces it with the SplitTextField (a Sprite)
  25. * containing these multiple TextFields, all conveniently stored in a "textFields" array that you can,
  26. * for example, feed to a TweenMax.allFrom() or loop through to create unique tweens for each character,
  27. * word or line. The FlexSplitTextField keeps the same scale/rotation/position as the source TextField,
  28. * and you can optionally offset the registration point by a certain number of pixels on its
  29. * local x- or y-axis, which can be useful if, for example, you want to be able to scale the whole
  30. * FlexSplitTextField from its center instead of its upper left corner. Use an onComplete in your
  31. * tween to call the FlexSplitTextField's <code>deactivate()</code> or <code>destroy()</code>
  32. * method which will swap the original TextField back into place. <br /><br />
  33. *
  34. * @example Example AS3 code:<listing version="3.0">
  35. import com.greensock.text.FlexSplitTextField;
  36. import com.greensock.TweenMax;
  37. import com.greensock.easing.Elastic;
  38. import com.greensock.plugins.~~;
  39. import flash.geom.Point;
  40. //split myLabel by characters (the default type of split)
  41. var stf1:FlexSplitTextField = new FlexSplitTextField(myLabel);
  42. //tween each character down from 100 pixels above while fading in, and offset the start times by 0.05 seconds and then swap the original TextField back into place and destroy the split TextFields
  43. TweenMax.allFrom(stf1.textFields, 1, {y:"-100", autoAlpha:0, ease:Elastic.easeOut}, 0.05, stf1.destroy);
  44. //split myLabel2 by words
  45. var stf2:FlexSplitTextField = new FlexSplitTextField(myLabel2, FlexSplitTextField.TYPE_WORDS);
  46. //explode the words outward using the physics2D feature of TweenLite/Max
  47. TweenPlugin.activate([Physics2DPlugin]);
  48. var i:int = stf2.textFields.length;
  49. var explodeOrigin:Point = new Point(stf2.width ~~ 0.4, stf2.height + 100);
  50. while (i--) {
  51. var angle:Number = Math.atan2(stf2.textFields[i].y - explodeOrigin.y, stf2.textFields[i].x - explodeOrigin.x) ~~ 180 / Math.PI;
  52. TweenMax.to(stf2.textFields[i], 2, {physics2D:{angle:angle, velocity:Math.random() ~~ 200 + 150, gravity:400}, scaleX:Math.random() ~~ 4 - 2, scaleY:Math.random() ~~ 4 - 2, rotation:Math.random() ~~ 100 - 50, autoAlpha:0, delay:1 + Math.random()});
  53. }
  54. //split myText by lines
  55. var stf3:FlexSplitTextField = new FlexSplitTextField(myText, FlexSplitTextField.TYPE_LINES);
  56. //slide each line in from the right, fading it in over 1 second and staggering the start times by 0.5 seconds.
  57. TweenMax.allFrom(stf3.textFields, 1, {x:"200", autoAlpha:0}, 0.5, stf3.destroy);
  58. </listing>
  59. *
  60. * <b>NOTES / LIMITATIONS</b><br />
  61. * <ul>
  62. * <li>Does not work perfectly with non-standard antialiasing (like "anti-alias for readability")</li>
  63. * <li>Does not work with htmlText</li>
  64. * <li>Does not recognize custom kerning.</li>
  65. * <li>To improve performance, mouseChildren is set to false by default (you're welcome to set it to true if you need the TextFields to receive MouseEvents)</li>
  66. * <li>To avoid some bugs in the way Flash renders TextFields, when creating TextField instances
  67. * dynamically make sure you set the various properties of the TextField <b>BEFORE</b> adding
  68. * the actual text. For example, set the <code>width, height, embedFonts, autoSize, multiline,</code> and
  69. * other properties before setting the <code>text</code> property.</li>
  70. * <li>Due to rendering delays in the Flex framework, some componenets (like Text) will
  71. * use the default width of 100 for the TextField initially until the component fully
  72. * renders which happens even after the creationComplete event! Therefore, if you try
  73. * to split apart the component's TextField too quickly, the result will look odd (100 pixels wide).</li>
  74. * </ul><br />
  75. *
  76. * FlexSplitTextField is a <a href="http://blog.greensock.com/club/">Club GreenSock</a> membership benefit.
  77. * You must have a valid membership to use this class without violating the terms of use. Visit
  78. * <a href="http://blog.greensock.com/club/">http://blog.greensock.com/club/</a> to sign up or get more details. <br /><br />
  79. *
  80. * <b>Copyright 2010, GreenSock. All rights reserved.</b> This work is subject to the terms in <a href="http://www.greensock.com/terms_of_use.html">http://www.greensock.com/terms_of_use.html</a> or for corporate Club GreenSock members, the software agreement that was issued with the corporate membership.
  81. *
  82. * @author Jack Doyle, jack@greensock.com
  83. */
  84. public class FlexSplitTextField extends Sprite {
  85. /** @private **/
  86. public static const version:Number = 0.62;
  87. /** Split type: characters **/
  88. public static const TYPE_CHARACTERS:String = "characters";
  89. /** Split type: words **/
  90. public static const TYPE_WORDS:String = "words";
  91. /** Split type: lines **/
  92. public static const TYPE_LINES:String = "lines";
  93. /** @private **/
  94. private static const _propNames:Array = ["embedFonts","alpha","antiAliasType","blendMode","filters","focusRect","gridFitType","mouseEnabled","sharpness","selectable","textColor","thickness"];
  95. /** @private **/
  96. private var _splitType:String;
  97. /** @private **/
  98. private var _regOffsetX:Number;
  99. /** @private **/
  100. private var _regOffsetY:Number;
  101. /** @private **/
  102. private var _source:UIComponent;
  103. /** @private **/
  104. private var _sourceTF:TextField;
  105. /** Array of UITextFields resulting from the split (one for each character, word, or line based on the splitType) **/
  106. public var textFields:Array;
  107. /**
  108. * Constructor.
  109. *
  110. * @param source The source UIComponent whose child TextField should be split apart. Remember, its TextField will be replaced in the display list with the FlexSplitTextField (which is essentially a UIComponent containing the various resulting TextFields).
  111. * @param splitType Determines the way in which the TextField is split apart - either by characters, words, or lines. Use the <code>TYPE_CHARACTERS, TYPE_WORDS,</code> and <code>TYPE_LINES</code> constants.
  112. * @param regOffsetX To offset the registration point by a certain number of pixels along its x-axis (according to the FlexSplitTextField's internal coordinate system), use regOffsetX.
  113. * @param regOffsetY To offset the registration point by a certain number of pixels along its y-axis (according to the FlexSplitTextField's internal coordinate system), use regOffsetY.
  114. */
  115. public function FlexSplitTextField(source:UIComponent=null, splitType:String="characters", regOffsetX:Number=0, regOffsetY:Number=0) {
  116. super();
  117. _source = source;
  118. if (source) {
  119. findSourceTF();
  120. }
  121. _splitType = splitType;
  122. _regOffsetX = regOffsetX;
  123. _regOffsetY = regOffsetY;
  124. this.textFields = [];
  125. if (source) {
  126. update();
  127. }
  128. }
  129. /**
  130. * This static method can be used to split apart any TextField and place the resulting
  131. * TextFields into any DisplayObjectContainer. It provides the core functionality of the
  132. * FlexSplitTextField class, but can be used apart from any instance thereof as well.
  133. *
  134. * @param source The source TextField that should be split apart. Remember, this TextField will be replaced in the display list with the FlexSplitTextField (which is essentially a Sprite containing the various resulting TextFields).
  135. * @param splitType Determines the way in which the TextField is split apart - either by characters, words, or lines. Use the <code>TYPE_CHARACTERS, TYPE_WORDS,</code> and <code>TYPE_LINES</code> constants.
  136. * @param container The DisplayObjectContainer into which the new TextFields should be placed.
  137. * @param offset Determines the offset x/y of the new TextFields. By default, the TextFields will be positioned in the container as though the container's registration point was aligned perfectly with the source TextField's. The source TextField's scale, rotation, and x/y coordinates will have no effect whatsoever.
  138. * @return Array of TextFields resulting from the split.
  139. */
  140. public static function split(source:TextField, spitType:String="characters", container:DisplayObjectContainer=null, offset:Point=null):Array {
  141. if (container == null) {
  142. container = source.parent;
  143. }
  144. if (offset == null) {
  145. offset = new Point(0,0);
  146. }
  147. var index:uint = (source.parent == container) ? source.parent.getChildIndex(source) : container.numChildren;
  148. var segments:Array;
  149. var cnt:uint = 0;
  150. var linesTotal:uint = source.numLines;
  151. var bounds:Rectangle = source.getBounds(source);
  152. var y:Number = bounds.y + offset.y;
  153. var fields:Array = [];
  154. var format:TextFormat, tf:TextField, x:Number, line:uint, i:uint, j:int, l:uint, text:String, totalSegments:uint, charOffset:uint, lineMetrics:TextLineMetrics, ascentDiff:Number;
  155. for (line = 0; line < linesTotal; line++) {
  156. text = source.getLineText(line);
  157. charOffset = source.getLineOffset(line);
  158. lineMetrics = source.getLineMetrics(line);
  159. if (source.text.length <= charOffset) { //sometimes Flash adds an extra line to the end unnecessarily
  160. continue;
  161. }
  162. //There are bugs in TextField.getCharBoundaries() that incorrectly returned null results occassionally, so we must figure it out using the align of the line.
  163. format = source.getTextFormat(charOffset, charOffset + 1);
  164. if (format.align == "left") {
  165. x = bounds.x + offset.x;
  166. } else if (format.align == "center") {
  167. x = bounds.x + offset.x - 2 + (source.width - lineMetrics.width) * 0.5;
  168. } else {
  169. x = bounds.x + offset.x - 4 + (source.width - lineMetrics.width);
  170. }
  171. if (spitType == TYPE_CHARACTERS) {
  172. segments = text.split("");
  173. } else if (spitType == TYPE_WORDS) {
  174. segments = text.split(" ").join("~#$ ~#$").split("~#$");
  175. } else {
  176. segments = [text];
  177. }
  178. totalSegments = segments.length;
  179. for (i = 0; i < totalSegments; i++) {
  180. if (segments[i].length == 0) {
  181. continue;
  182. }
  183. tf = new TextField();
  184. j = _propNames.length;
  185. while (j--) {
  186. tf[_propNames[j]] = source[_propNames[j]];
  187. }
  188. tf.autoSize = TextFieldAutoSize.LEFT;
  189. tf.selectable = tf.multiline = tf.wordWrap = false;
  190. tf.text = segments[i];
  191. l = segments[i].length;
  192. for (j = 0; j < l; j++) {
  193. format = source.getTextFormat(charOffset, charOffset + 1);
  194. charOffset += 1;
  195. format.align = TextFormatAlign.LEFT;
  196. format.leading = 0;
  197. tf.setTextFormat(format, j, j + 1);
  198. }
  199. tf.x = x;
  200. tf.y = y;
  201. ascentDiff = lineMetrics.ascent - tf.getLineMetrics(0).ascent; //if different formatting is used on the same line (for example, a 50px letter next to a 12px letter), we must adjust the y position to make the baselines match.
  202. if (ascentDiff) {
  203. tf.y += ascentDiff;
  204. }
  205. x += tf.textWidth;
  206. if (segments[i] != " " || format.underline) {
  207. fields[cnt++] = tf;
  208. }
  209. }
  210. y += lineMetrics.height;
  211. }
  212. i = fields.length;
  213. while (i--) {
  214. container.addChildAt(TextField(fields[i]), index);
  215. }
  216. if (source.parent) {
  217. source.parent.removeChild(source);
  218. }
  219. return fields;
  220. }
  221. /** @private searches the UIComponent for a TextField instance and if it finds one, it sets the _sourceTF and _extraOffset values **/
  222. private function findSourceTF():void {
  223. var i:int = _source.numChildren;
  224. var obj:DisplayObject;
  225. while (i--) {
  226. obj = _source.getChildAt(i);
  227. if (obj is TextField) {
  228. _sourceTF = obj as TextField;
  229. break;
  230. }
  231. }
  232. if (_sourceTF == null) {
  233. throw new Error("FlexSplitTextField error: the source does not contain a valid TextField to split apart.");
  234. }
  235. }
  236. /** When a FlexSplitTextField is activated, it takes the place of the source's original TextField in the display list. **/
  237. public function activate():void {
  238. this.activated = true;
  239. }
  240. /**
  241. * When a FlexSplitTextField is activated, it swaps the source's original TextField back into the display list.
  242. * This makes it simple to animate the FlexSplitTextField's contents with TweenLite/Max and then use
  243. * an onComplete to call disable() which will swap the original (source) TextField back into place.
  244. **/
  245. public function deactivate():void {
  246. this.activated = false;
  247. }
  248. /**
  249. * Deactivates the FlexSplitTextField (swapping the original TextField back into place) and
  250. * deletes all child TextFields that resulted from the split operation, and nulls references to
  251. * the source so that it's eligible for garbage collection.
  252. **/
  253. public function destroy():void {
  254. this.activated = false;
  255. clear();
  256. _sourceTF = null;
  257. _source = null;
  258. }
  259. /** @private **/
  260. private function update():void {
  261. clear();
  262. if (_sourceTF.parent) {
  263. var p:DisplayObjectContainer = _sourceTF.parent;
  264. p.addChildAt(this, p.getChildIndex(_sourceTF));
  265. p.removeChild(_sourceTF);
  266. }
  267. var m:Matrix = _sourceTF.transform.matrix;
  268. if (_regOffsetX != 0 || _regOffsetY != 0) {
  269. var point:Point = m.transformPoint(new Point(_regOffsetX, _regOffsetY));
  270. m.tx = point.x;
  271. m.ty = point.y;
  272. }
  273. this.transform.matrix = m;
  274. this.textFields = split(_sourceTF, _splitType, this, new Point(-_regOffsetX, -_regOffsetY));
  275. }
  276. /** @private **/
  277. private function clear():void {
  278. var i:int = this.textFields.length;
  279. while (i--) {
  280. this.removeChild(this.textFields[i]);
  281. }
  282. this.textFields = [];
  283. }
  284. /** The source UIComponent whose TextField gets split apart. **/
  285. public function get source():UIComponent {
  286. return _source;
  287. }
  288. public function set source(src:UIComponent):void {
  289. _source = src;
  290. if (_source) {
  291. findSourceTF();
  292. }
  293. update();
  294. }
  295. /** Determines the way in which the source TextField is split apart - either by characters, words, or lines. Use the <code>TYPE_CHARACTERS, TYPE_WORDS,</code> and <code>TYPE_LINES</code> constants. **/
  296. public function get splitType():String {
  297. return _splitType;
  298. }
  299. public function set splitType(type:String):void {
  300. _splitType = type;
  301. update();
  302. }
  303. /** To offset the registration point by a certain number of pixels along its x-axis (according to the FlexSplitTextField's internal coordinate system), use regOffsetX. **/
  304. public function get regOffsetX():Number {
  305. return _regOffsetX;
  306. }
  307. public function set regOffsetX(x:Number):void {
  308. _regOffsetX = x;
  309. update();
  310. }
  311. /** To offset the registration point by a certain number of pixels along its y-axis (according to the FlexSplitTextField's internal coordinate system), use regOffsetY. **/
  312. public function get regOffsetY():Number {
  313. return _regOffsetY;
  314. }
  315. public function set regOffsetY(y:Number):void {
  316. _regOffsetY = y;
  317. update();
  318. }
  319. /** When a FlexSplitTextField is activated, it replaces the source's original TextField in the display list. When it is deactivated, it swaps the source's original TextField back into place **/
  320. public function get activated():Boolean {
  321. return Boolean(this.parent != null);
  322. }
  323. public function set activated(b:Boolean):void {
  324. if (_sourceTF == null) {
  325. return;
  326. }
  327. if (_sourceTF.parent && b) {
  328. _sourceTF.parent.addChildAt(this, _sourceTF.parent.getChildIndex(_sourceTF));
  329. _sourceTF.parent.removeChild(_sourceTF);
  330. } else if (this.parent && !b) {
  331. this.parent.addChildAt(_sourceTF, this.parent.getChildIndex(this));
  332. this.parent.removeChild(this);
  333. }
  334. }
  335. }
  336. }