Posted By

mcorlan on 07/09/11


Tagged


Versions (?)

Tile Item Renderer for Section Tile List


 / Published in: ActionScript 3
 

URL: http://corlan.org/?p=2987

  1. /**
  2.  * Section Title font size
  3.  */
  4. [Style(name="sectionFontSize", type="Number", format="Length", inherit="no")]
  5.  
  6. public class TileItemRenderer extends LabelItemRenderer {
  7.  
  8. private static var _imageCache:ContentCache;
  9.  
  10. public function TileItemRenderer() {
  11. super();
  12. if (_imageCache == null) {
  13. _imageCache = new ContentCache();
  14. _imageCache.enableCaching = true;
  15. _imageCache.maxCacheEntries = 100;
  16. }
  17. }
  18.  
  19. private var _backgroundSection:Number = 0xDDDDDD;
  20.  
  21. public function set backgroundSection(value:Number):void {
  22. _backgroundSection = value;
  23. }
  24.  
  25. public function set sectionFontSize(value:int):void {
  26. setStyle("sectionFontSize", value);
  27. }
  28.  
  29. public function set fontSize(value:int):void {
  30. setStyle("fontSize", value);
  31. }
  32.  
  33. private var _backgroundRegular:Number = 0xF4DD06;
  34. private var _backgroundLabel:Number = 0xEAEAE8;
  35.  
  36. private var _normalLabelField:String = "label";
  37.  
  38. public function set normalLabelField(value:String):void {
  39. _normalLabelField = value;
  40. }
  41.  
  42. private var _sectionLabel:String = "section";
  43.  
  44. public function set sectionLabel(value:String):void {
  45. if (value == _sectionLabel)
  46. return;
  47.  
  48. _sectionLabel = value;
  49. invalidateProperties();
  50. }
  51.  
  52. private var _iconField:String;
  53.  
  54. /**
  55. * The name of the field in the data item to display as the icon.
  56. * By default <code>iconField</code> is <code>null</code>, and the item renderer
  57. * does not display an icon.
  58. *
  59. * @default null
  60. *
  61. * @langversion 3.0
  62. * @playerversion AIR 2.5
  63. * @productversion Flex 4.5
  64. */
  65. public function get iconField():String {
  66. return _iconField;
  67. }
  68.  
  69. /**
  70. * @private
  71. */
  72. public function set iconField(value:String):void {
  73. if (value == _iconField)
  74. return;
  75.  
  76. _iconField = value;
  77. invalidateProperties();
  78. }
  79.  
  80. /**
  81. * Change the style based on the data: section item or regular item
  82. */
  83. override public function set data(value:Object):void {
  84. if (value) {
  85. if (value[_sectionLabel]) {
  86. if (image)
  87. image.visible = false;
  88. if (labelBg)
  89. labelBg.visible = false;
  90. label = value[_sectionLabel];
  91. labelDisplay.setStyle("textAlign", "center");
  92. labelDisplay.setStyle("fontWeight", "bold");
  93. labelDisplay.setStyle("fontSize", getStyle("sectionFontSize"));
  94. } else {
  95. if (_iconField && value[_iconField]) {
  96. if (image)
  97. image.visible = true;
  98. image.source = value[_iconField];
  99. } else {
  100. if (image) {
  101. image.visible = false;
  102. image.source = "";
  103. }
  104. }
  105. if (labelBg)
  106. labelBg.visible = true;
  107. label = value[_normalLabelField];
  108. labelDisplay.setStyle("fontSize", getStyle("fontSize"));
  109. labelDisplay.setStyle("textAlign", "left");
  110. labelDisplay.setStyle("fontWeight", "normal");
  111. }
  112. }
  113. super.data = value;
  114. }
  115.  
  116. //destroyIconDisplay() todo;
  117. override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
  118. // clear the graphics before calling super.updateDisplayList()
  119. graphics.clear();
  120. super.updateDisplayList(unscaledWidth, unscaledHeight);
  121. //the following methods are called in super.updateDisplayList();
  122. //drawBackground(unscaledWidth, unscaledHeight);
  123. //layoutContents(unscaledWidth, unscaledHeight);
  124. }
  125.  
  126. private var image:Image;
  127. private var labelBg:Sprite;
  128. private var drawn:Boolean;
  129.  
  130. override protected function createChildren():void {
  131. if (!image) {
  132. image = new Image();
  133. image.smooth = true;
  134. image.scaleMode = BitmapScaleMode.STRETCH;
  135. image.fillMode = BitmapFillMode.SCALE;
  136. image.contentLoader = _imageCache;
  137. addChild(image);
  138. }
  139. //create the background for label
  140. if (!labelBg) {
  141. labelBg = new Sprite();
  142. addChild(labelBg);
  143. }
  144. super.createChildren();
  145. }
  146.  
  147. /**
  148. * Change the background color for section items
  149. */
  150. override protected function drawBackground(unscaledWidth:Number, unscaledHeight:Number):void {
  151. super.drawBackground(unscaledWidth, unscaledHeight);
  152.  
  153. if (data[_sectionLabel]) {
  154. graphics.beginFill(_backgroundSection, 1);
  155. graphics.lineStyle();
  156. graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
  157. graphics.endFill();
  158. } else {
  159. //add a vertical line to the right of the item
  160. var rightSeparatorColor:uint = 0x000000;
  161. var rightSeparatorAlpha:Number = .3;
  162. graphics.beginFill(rightSeparatorColor, rightSeparatorAlpha);
  163. graphics.drawRect(unscaledWidth - 1, 0, 1, unscaledHeight);
  164. graphics.endFill();
  165. //draw a rounded corner place holder for text and icon
  166. //let a padding around for seeing through the selection
  167. graphics.beginFill(_backgroundRegular, 1);
  168. graphics.lineStyle();
  169. graphics.drawRoundRect(3, 3, unscaledWidth - 6, unscaledHeight - 6, 10, 10);
  170. graphics.endFill();
  171. }
  172. }
  173.  
  174. override protected function layoutContents(unscaledWidth:Number, unscaledHeight:Number):void {
  175. super.layoutContents(unscaledWidth, unscaledHeight);
  176.  
  177. //position the image
  178. if (!data[_sectionLabel] && _iconField && data[_iconField]) {
  179. setElementPosition(image, 5, 5);
  180. setElementSize(image, unscaledWidth - 10, unscaledWidth - 10);
  181. }
  182.  
  183. if (!data[_sectionLabel] && labelDisplay) {
  184. labelDisplay.commitStyles();
  185. var h:Number = labelDisplay.height;
  186.  
  187. //draw for holding the text
  188. if (!drawn) {
  189. drawn = true;
  190. labelBg.graphics.clear();
  191. labelBg.graphics.beginFill(_backgroundLabel, 1);
  192. labelBg.graphics.lineStyle();
  193. labelBg.graphics.drawRoundRect(3, unscaledHeight - 10 - h, unscaledWidth - 6, h + 6, 10, 10);
  194. labelBg.graphics.endFill();
  195. // setElementPosition(labelBg, 0, unscaledHeight - 10 - h);
  196. }
  197. var paddingLeft:Number = getStyle("paddingLeft");
  198. //reposition the label at the bottom of the item
  199. setElementPosition(labelDisplay, paddingLeft, unscaledHeight - h);
  200. }
  201. }
  202.  
  203. }

Report this snippet  

You need to login to post a comment.