Posted By

tommybaggett on 08/05/11


Tagged

mobile Flex LabelItemRenderer


Versions (?)

LabelItemRenderer drawBackground function


 / Published in: ActionScript 3
 

  1. protected function drawBackground(unscaledWidth:Number,
  2. unscaledHeight:Number):void
  3. {
  4. // figure out backgroundColor
  5. var backgroundColor:*;
  6. var downColor:* = getStyle("downColor");
  7. var drawBackground:Boolean = true;
  8. var opaqueBackgroundColor:* = undefined;
  9.  
  10. if (down && downColor !== undefined)
  11. {
  12. backgroundColor = downColor;
  13. }
  14. else if (selected)
  15. {
  16. backgroundColor = getStyle("selectionColor");
  17. }
  18. else if (hovered)
  19. {
  20. backgroundColor = getStyle("rollOverColor");
  21. }
  22. else if (showsCaret)
  23. {
  24. backgroundColor = getStyle("selectionColor");
  25. }
  26. else
  27. {
  28. var alternatingColors:Array;
  29. var alternatingColorsStyle:Object = getStyle("alternatingItemColors");
  30.  
  31. if (alternatingColorsStyle)
  32. alternatingColors = (alternatingColorsStyle is Array) ? (alternatingColorsStyle as Array) : [alternatingColorsStyle];
  33.  
  34. if (alternatingColors && alternatingColors.length > 0)
  35. {
  36. // translate these colors into uints
  37. styleManager.getColorNames(alternatingColors);
  38.  
  39. backgroundColor = alternatingColors[itemIndex % alternatingColors.length];
  40. }
  41. else
  42. {
  43. // don't draw background if it is the contentBackgroundColor. The
  44. // list skin handles the background drawing for us.
  45. drawBackground = false;
  46. }
  47.  
  48. }
  49.  
  50. // draw backgroundColor
  51. // the reason why we draw it in the case of drawBackground == 0 is for
  52. // mouse hit testing purposes
  53. graphics.beginFill(backgroundColor, drawBackground ? 1 : 0);
  54. graphics.lineStyle();
  55. graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
  56. graphics.endFill();
  57.  
  58. var topSeparatorColor:uint;
  59. var topSeparatorAlpha:Number;
  60. var bottomSeparatorColor:uint;
  61. var bottomSeparatorAlpha:Number;
  62.  
  63. // Selected and down states have a gradient overlay as well
  64. // as different separators colors/alphas
  65. if (selected || down)
  66. {
  67. var colors:Array = [0x000000, 0x000000 ];
  68. var alphas:Array = [.2, .1];
  69. var ratios:Array = [0, 255];
  70. var matrix:Matrix = new Matrix();
  71.  
  72. // gradient overlay
  73. matrix.createGradientBox(unscaledWidth, unscaledHeight, Math.PI / 2, 0, 0 );
  74. graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
  75. graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
  76. graphics.endFill();
  77. }
  78. else if (drawBackground)
  79. {
  80. // If our background is a solid color, use it as the opaqueBackground property
  81. // for this renderer. This makes scrolling considerably faster.
  82. opaqueBackgroundColor = backgroundColor;
  83. }
  84.  
  85. // separators are a highlight on the top and shadow on the bottom
  86. topSeparatorColor = 0xFFFFFF;
  87. topSeparatorAlpha = .3;
  88. bottomSeparatorColor = 0x000000;
  89. bottomSeparatorAlpha = .3;
  90.  
  91.  
  92. // draw separators
  93. // don't draw top separator for down and selected states
  94. if (!(selected || down))
  95. {
  96. graphics.beginFill(topSeparatorColor, topSeparatorAlpha);
  97. graphics.drawRect(0, 0, unscaledWidth, 1);
  98. graphics.endFill();
  99. }
  100.  
  101. graphics.beginFill(bottomSeparatorColor, bottomSeparatorAlpha);
  102. graphics.drawRect(0, unscaledHeight - (isLastItem ? 0 : 1), unscaledWidth, 1);
  103. graphics.endFill();
  104.  
  105.  
  106. // add extra separators to the first and last items so that
  107. // the list looks correct during the scrolling bounce/pull effect
  108. // top
  109. if (itemIndex == 0)
  110. {
  111. graphics.beginFill(bottomSeparatorColor, bottomSeparatorAlpha);
  112. graphics.drawRect(0, -1, unscaledWidth, 1);
  113. graphics.endFill();
  114. }
  115.  
  116. // bottom
  117. if (isLastItem)
  118. {
  119. // we want to offset the bottom by 1 so that we don't get
  120. // a double line at the bottom of the list if there's a
  121. // border
  122. graphics.beginFill(topSeparatorColor, topSeparatorAlpha);
  123. graphics.drawRect(0, unscaledHeight + 1, unscaledWidth, 1);
  124. graphics.endFill();
  125. }
  126.  
  127. opaqueBackground = opaqueBackgroundColor;
  128. }

Report this snippet  

You need to login to post a comment.