PageRenderTime 6060ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/custardbelly/as3flobile/controls/label/Label.as

http://github.com/bustardcelly/as3flobile
ActionScript | 356 lines | 202 code | 42 blank | 112 comment | 33 complexity | 31ddc7aacc236f5d349623c0ea384877 MD5 | raw file
  1. /**
  2. * <p>Original Author: toddanderson</p>
  3. * <p>Class File: Label.as</p>
  4. * <p>Version: 0.4</p>
  5. *
  6. * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:</p>
  12. *
  13. * <p>The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.</p>
  15. *
  16. * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.</p>
  23. *
  24. * <p>Licensed under The MIT License</p>
  25. * <p>Redistributions of files must retain the above copyright notice.</p>
  26. */
  27. package com.custardbelly.as3flobile.controls.label
  28. {
  29. import com.custardbelly.as3flobile.controls.core.AS3FlobileComponent;
  30. import com.custardbelly.as3flobile.controls.label.renderer.ILabelRenderer;
  31. import com.custardbelly.as3flobile.controls.label.renderer.MultilineLabelRenderer;
  32. import com.custardbelly.as3flobile.controls.label.renderer.TruncationLabelRenderer;
  33. import flash.display.DisplayObject;
  34. import flash.display.Sprite;
  35. import flash.events.Event;
  36. import flash.geom.Rectangle;
  37. import flash.text.TextFormatAlign;
  38. import flash.text.engine.BreakOpportunity;
  39. import flash.text.engine.ElementFormat;
  40. import flash.text.engine.FontDescription;
  41. import flash.text.engine.TextBlock;
  42. import flash.text.engine.TextElement;
  43. import flash.text.engine.TextLine;
  44. /**
  45. * Label is a textual base component utilizing the Flash Text Engine to render a single or multiple line of text.
  46. * @author toddanderson
  47. */
  48. public class Label extends AS3FlobileComponent
  49. {
  50. protected var _textElement:TextElement;
  51. protected var _text:String;
  52. protected var _format:ElementFormat;
  53. protected var _truncate:Boolean = true;
  54. protected var _truncationText:String = "...";
  55. protected var _autosize:Boolean;
  56. protected var _multiline:Boolean;
  57. protected var _textAlign:String;
  58. protected var _renderer:ILabelRenderer;
  59. protected var _truncationRenderer:ILabelRenderer;
  60. protected var _multilineRenderer:ILabelRenderer;
  61. protected var _rendererWidth:int;
  62. protected var _rendererHeight:int;
  63. protected var _measuredWidth:int;
  64. protected var _measuredHeight:int;
  65. protected var _hasRequestForInvalidationOnDisplay:Boolean;
  66. /**
  67. * Constructor.
  68. */
  69. public function Label() { super(); }
  70. /**
  71. * @inherit
  72. */
  73. override protected function initialize():void
  74. {
  75. super.initialize();
  76. _width = 100;
  77. _height = 20;
  78. _format = new ElementFormat( new FontDescription("DroidSans") );
  79. _format.breakOpportunity = BreakOpportunity.NONE;
  80. _format.fontSize = 14;
  81. _textElement = new TextElement();
  82. _truncationText = "...";
  83. _textAlign = TextFormatAlign.LEFT;
  84. _truncationRenderer = new TruncationLabelRenderer( this );
  85. _multilineRenderer = new MultilineLabelRenderer( this );
  86. _renderer = _truncationRenderer;
  87. _renderer.truncationText = _truncationText;
  88. _renderer.textAlign = _textAlign;
  89. }
  90. /**
  91. * @inheritDoc
  92. */
  93. override protected function invalidate( method:Function, args:Array = null ):void
  94. {
  95. // Override to limit invalidate of text display to one time per frame as it is requsted to be invoked through many poroperties.
  96. var hasArgs:Boolean = ( args && args.length > 0 );
  97. var isRequestForInvalidationOnDisplay:Boolean = ( method == invalidateTextDisplay );
  98. var addInvalidationToQueue:Boolean = !isRequestForInvalidationOnDisplay;
  99. if( isRequestForInvalidationOnDisplay && !_hasRequestForInvalidationOnDisplay )
  100. {
  101. _hasRequestForInvalidationOnDisplay = true;
  102. addInvalidationToQueue = true;
  103. }
  104. if( addInvalidationToQueue )
  105. {
  106. if( hasArgs ) super.invalidate( method, args );
  107. else super.invalidate( method );
  108. }
  109. }
  110. /**
  111. * @inheritDoc
  112. */
  113. override protected function render(evt:Event=null):void
  114. {
  115. // Flip flag for invalidate on text display. Limit is once per frame.
  116. _hasRequestForInvalidationOnDisplay = false;
  117. super.render( evt );
  118. }
  119. /**
  120. * @private
  121. *
  122. * Validates the textual content and its formatting.
  123. */
  124. protected function invalidateTextDisplay():void
  125. {
  126. _textElement.text = _text;
  127. _textElement.elementFormat = _format;
  128. _rendererWidth = ( _autosize && !_multiline ) ? 0 : _width;
  129. _rendererHeight = ( _autosize ) ? 0 : _height;
  130. _renderer.render( _textElement, _rendererWidth, _rendererHeight );
  131. if( numChildren > 0 )
  132. {
  133. // Updates measured bounds.
  134. var bounds:Rectangle = getBounds(this);
  135. var lastChild:TextLine = getChildAt( numChildren - 1 ) as TextLine;
  136. _measuredWidth = bounds.width;
  137. _measuredHeight = bounds.height + ( lastChild.ascent - ( lastChild.descent * 2 ) );
  138. }
  139. }
  140. /**
  141. * @private
  142. *
  143. * Validate the scrollable area of the content.
  144. */
  145. override protected function invalidateSize():void
  146. {
  147. _measuredWidth = _width;
  148. _measuredHeight = _height;
  149. invalidateTextDisplay();
  150. }
  151. /**
  152. * @private
  153. *
  154. * Updates the renderer state used for display layout.
  155. * @param renderer ILabelRenderer
  156. */
  157. protected function setRendererState( renderer:ILabelRenderer ):void
  158. {
  159. _renderer = renderer;
  160. }
  161. /**
  162. * @inherit
  163. */
  164. override public function dispose():void
  165. {
  166. super.dispose();
  167. while( numChildren > 0 )
  168. removeChildAt( 0 );
  169. _format = null;
  170. _truncationRenderer.dispose();
  171. _truncationRenderer = null;
  172. _renderer = null;
  173. }
  174. /**
  175. * Retruns the measured width of this instance based on autosize flag.
  176. * If autosize is set to true the measured width of this control is based on the content.
  177. * @return int
  178. */
  179. public function get measuredWidth():int
  180. {
  181. return _measuredWidth;
  182. }
  183. /**
  184. * Returns the measured height of this instance based on autosize flag.
  185. * If autosize is set to true and multiline set to true the measured height of this control is based on the content.
  186. * @return int
  187. */
  188. public function get measuredHeight():int
  189. {
  190. return _measuredHeight;
  191. }
  192. /**
  193. * @inherit
  194. *
  195. * Override to return the determined measured width based on content when autosize is specified.
  196. */
  197. override public function get width():Number
  198. {
  199. return ( _autosize ) ? _measuredWidth : _width;
  200. }
  201. /**
  202. * @inherit
  203. *
  204. * Override to return the determined measured height based on content when autosize is specified.
  205. */
  206. override public function get height():Number
  207. {
  208. return ( _autosize ) ? _measuredHeight : _height;
  209. }
  210. /**
  211. * Accessor/Modifier for the textual content to display.
  212. * @return String
  213. */
  214. public function get text():String
  215. {
  216. return _text;
  217. }
  218. public function set text( value:String ):void
  219. {
  220. if( _text == value ) return;
  221. _text = value;
  222. invalidate( invalidateTextDisplay );
  223. }
  224. /**
  225. * Accessor/Modifier for the element formatting of the textual content.
  226. * @return ElementFormat
  227. */
  228. public function get format():ElementFormat
  229. {
  230. return _format;
  231. }
  232. public function set format(value:ElementFormat):void
  233. {
  234. if( _format == value ) return;
  235. _format = value;
  236. if( _text != null ) invalidate( invalidateTextDisplay );
  237. }
  238. /**
  239. * Accessor/Modifier for the textual content represented within truncation. Default is '...'
  240. * @return String
  241. */
  242. public function get truncationText():String
  243. {
  244. return _truncationText;
  245. }
  246. public function set truncationText( value:String ):void
  247. {
  248. if( _truncationText == value ) return;
  249. _truncationText = value;
  250. _truncationRenderer.truncationText = _truncationText;
  251. if( _truncate ) invalidate( invalidateTextDisplay );
  252. }
  253. /**
  254. * Accessor/Modifier flag to truncate text on render.
  255. * @return Boolean
  256. */
  257. public function get truncate():Boolean
  258. {
  259. return _truncate;
  260. }
  261. public function set truncate(value:Boolean):void
  262. {
  263. if( _truncate == value ) return;
  264. _truncate = value;
  265. setRendererState( _truncationRenderer );
  266. if( _text != null ) invalidate( invalidateTextDisplay );
  267. }
  268. /**
  269. * Accessor/Modifier of autosize flag for render.
  270. * @return Boolean
  271. */
  272. public function get autosize():Boolean
  273. {
  274. return _autosize;
  275. }
  276. public function set autosize( value:Boolean ):void
  277. {
  278. if( _autosize == value ) return;
  279. _autosize = value;
  280. if( _text != null ) invalidate( invalidateTextDisplay );
  281. }
  282. /**
  283. * Accessor/Modifier flag to display multiple lines.
  284. * @return Boolean
  285. */
  286. public function get multiline():Boolean
  287. {
  288. return _multiline;
  289. }
  290. public function set multiline( value:Boolean ):void
  291. {
  292. if( _multiline == value ) return;
  293. _multiline = value;
  294. setRendererState( ( value ) ? _multilineRenderer : _truncationRenderer );
  295. if( _text != null ) invalidate( invalidateTextDisplay );
  296. }
  297. /**
  298. * Accessor/Modifier for the alignment of text within a single or multiline Label.
  299. * @return String Value values are from flash.text.TextFormatAlign
  300. */
  301. public function get textAlign():String
  302. {
  303. return _textAlign;
  304. }
  305. public function set textAlign( value:String ):void
  306. {
  307. if( _textAlign == value ) return;
  308. _textAlign = value;
  309. _truncationRenderer.textAlign = _textAlign;
  310. _multilineRenderer.textAlign = _textAlign;
  311. if( _text != null ) invalidate( invalidateTextDisplay );
  312. }
  313. }
  314. }