PageRenderTime 67ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/nickro/greensock/text/FlexSplitTextField.as

http://my-project-nickro.googlecode.com/
ActionScript | 371 lines | 215 code | 29 blank | 127 comment | 50 complexity | 2fea8c56d741a07a0a670feb60bdd303 MD5 | raw file
  1. /**
  2. * VERSION: 0.6
  3. * DATE: 1/6/2010
  4. * AS3
  5. * UPDATES AND DOCUMENTATION AT: http://blog.greensock.com
  6. **/
  7. package org.nickro.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 org.nickro.greensock.text.FlexSplitTextField;
  36. import org.nickro.greensock.TweenMax;
  37. import org.nickro.greensock.easing.Elastic;
  38. import org.nickro.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 2009, 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.6;
  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. tf.setTextFormat(format, j, j + 1);
  197. }
  198. tf.x = x;
  199. tf.y = y;
  200. 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.
  201. if (ascentDiff) {
  202. tf.y += ascentDiff;
  203. }
  204. x += tf.textWidth;
  205. if (segments[i] != " ") {
  206. fields[cnt++] = tf;
  207. }
  208. }
  209. y += lineMetrics.height;
  210. }
  211. i = fields.length;
  212. while (i--) {
  213. container.addChildAt(TextField(fields[i]), index);
  214. }
  215. if (source.parent) {
  216. source.parent.removeChild(source);
  217. }
  218. return fields;
  219. }
  220. /** @private searches the UIComponent for a TextField instance and if it finds one, it sets the _sourceTF and _extraOffset values **/
  221. private function findSourceTF():void {
  222. var i:int = _source.numChildren;
  223. var obj:DisplayObject;
  224. while (i--) {
  225. obj = _source.getChildAt(i);
  226. if (obj is TextField) {
  227. _sourceTF = obj as TextField;
  228. break;
  229. }
  230. }
  231. if (_sourceTF == null) {
  232. throw new Error("FlexSplitTextField error: the source does not contain a valid TextField to split apart.");
  233. }
  234. }
  235. /** When a FlexSplitTextField is activated, it takes the place of the source's original TextField in the display list. **/
  236. public function activate():void {
  237. this.activated = true;
  238. }
  239. /**
  240. * When a FlexSplitTextField is activated, it swaps the source's original TextField back into the display list.
  241. * This makes it simple to animate the FlexSplitTextField's contents with TweenLite/Max and then use
  242. * an onComplete to call disable() which will swap the original (source) TextField back into place.
  243. **/
  244. public function deactivate():void {
  245. this.activated = false;
  246. }
  247. /**
  248. * Deactivates the FlexSplitTextField (swapping the original TextField back into place) and
  249. * deletes all child TextFields that resulted from the split operation, and nulls references to
  250. * the source so that it's eligible for garbage collection.
  251. **/
  252. public function destroy():void {
  253. this.activated = false;
  254. clear();
  255. _sourceTF = null;
  256. _source = null;
  257. }
  258. /** @private **/
  259. private function update():void {
  260. clear();
  261. if (_sourceTF.parent) {
  262. var p:DisplayObjectContainer = _sourceTF.parent;
  263. p.addChildAt(this, p.getChildIndex(_sourceTF));
  264. p.removeChild(_sourceTF);
  265. }
  266. var m:Matrix = _sourceTF.transform.matrix;
  267. if (_regOffsetX != 0 || _regOffsetY != 0) {
  268. var point:Point = m.transformPoint(new Point(_regOffsetX, _regOffsetY));
  269. m.tx = point.x;
  270. m.ty = point.y;
  271. }
  272. this.transform.matrix = m;
  273. this.textFields = split(_sourceTF, _splitType, this, new Point(-_regOffsetX, -_regOffsetY));
  274. }
  275. /** @private **/
  276. private function clear():void {
  277. var i:int = this.textFields.length;
  278. while (i--) {
  279. this.removeChild(this.textFields[i]);
  280. }
  281. this.textFields = [];
  282. }
  283. /** The source UIComponent whose TextField gets split apart. **/
  284. public function get source():UIComponent {
  285. return _source;
  286. }
  287. public function set source(src:UIComponent):void {
  288. _source = src;
  289. if (_source) {
  290. findSourceTF();
  291. }
  292. update();
  293. }
  294. /** 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. **/
  295. public function get splitType():String {
  296. return _splitType;
  297. }
  298. public function set splitType(type:String):void {
  299. _splitType = type;
  300. update();
  301. }
  302. /** 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. **/
  303. public function get regOffsetX():Number {
  304. return _regOffsetX;
  305. }
  306. public function set regOffsetX(x:Number):void {
  307. _regOffsetX = x;
  308. update();
  309. }
  310. /** 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. **/
  311. public function get regOffsetY():Number {
  312. return _regOffsetY;
  313. }
  314. public function set regOffsetY(y:Number):void {
  315. _regOffsetY = y;
  316. update();
  317. }
  318. /** 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 **/
  319. public function get activated():Boolean {
  320. return Boolean(this.parent != null);
  321. }
  322. public function set activated(b:Boolean):void {
  323. if (_sourceTF == null) {
  324. return;
  325. }
  326. if (_sourceTF.parent && b) {
  327. _sourceTF.parent.addChildAt(this, _sourceTF.parent.getChildIndex(_sourceTF));
  328. _sourceTF.parent.removeChild(_sourceTF);
  329. } else if (this.parent && !b) {
  330. this.parent.addChildAt(_sourceTF, this.parent.getChildIndex(this));
  331. this.parent.removeChild(this);
  332. }
  333. }
  334. }
  335. }