26 August 2008

Changing Selection Indicator in Datagrid

add Tween, alpha, shape, etc.

So I was recently working on a project, and the client wanted to have a different effect on the DataGrid's selection indicator. He said that he wanted the indicator to fade rather than the default "hard" appear.  After doing some digging, I found a post about changing the List Selection indicator which was pretty close to what I needed, but this was for a TileList.  The good news is that DataGrid inherits from the List base class, and the List Selection indicator methods are in there as well. Sound easy! 

The two methods in question are the drawSelectionIndicator() and drawHighlightIndicator() in the ListBase.as class.  If you override these two methods, you can create some pretty cool effects.  Here is an example of implementing a Tweener fade, and setting the final alpha to 50%:

   override protected function drawSelectionIndicator(
      indicator:Sprite, x:Number, y:Number, width:Number,
      height:Number, color:uint,
      itemRenderer:IListItemRenderer):void{
      var g:Graphics = Sprite(indicator).graphics;
      g.clear();
      g.beginFill(color,.25);
      g.drawRect(0,0,width,height);
      g.endFill();
     
      indicator.x = x;
      indicator.y = y;
     
 }
 override protected function drawHighlightIndicator(
  indicator:Sprite, x:Number, y:Number, width:Number,
  height:Number, color:uint,
  itemRenderer:IListItemRenderer):void{
  var g:Graphics = Sprite(indicator).graphics;
  g.clear();
  g.beginFill(color,.5);
  g.drawRect(0,0,width,height);
  g.endFill();
  
  indicator.x = x;
        indicator.y = y;
        indicator.alpha = 0;
        Tweener.addTween(indicator,({alpha:.5, time:1, transition:"easeOutCubic"})); }

As the code above shows, you can just override the draw and fill methods with your custom settings.  One neat thing is that you can even replace the drawRect method with a drawCircle and get a completely different shape for the indicator.  Below is an extended DataGrid class that changes the hightlight indicator's alpha to 50% and adds a fade-in effect using Tweener:

package com.swfflex
{
 import caurina.transitions.*;
 
 import flash.display.Graphics;
 import flash.display.Sprite;
 
 import mx.controls.DataGrid;
 import mx.controls.listClasses.IListItemRenderer; public class DataGridEx extends DataGrid
 { override protected function drawSelectionIndicator(
      indicator:Sprite, x:Number, y:Number, width:Number,
      height:Number, color:uint,
      itemRenderer:IListItemRenderer):void{
      var g:Graphics = Sprite(indicator).graphics;
      g.clear();
      g.beginFill(color,.25);
      g.drawRect(0,0,width,height);
      g.endFill();
     
      indicator.x = x;
      indicator.y = y;
     
 }
 override protected function drawHighlightIndicator(
  indicator:Sprite, x:Number, y:Number, width:Number,
  height:Number, color:uint,
  itemRenderer:IListItemRenderer):void{
  var g:Graphics = Sprite(indicator).graphics;
  g.clear();
  g.beginFill(color,.5);
  g.drawRect(0,0,width,height);
  g.endFill();
  
  indicator.x = x;
        indicator.y = y;
        indicator.alpha = 0;
        Tweener.addTween(indicator,({alpha:.5, time:1, transition:"easeOutCubic"})); }
 override protected function clearHighlightIndicator(indicator:Sprite, itemRenderer:IListItemRenderer):void{
  if(highlightIndicator)
   Tweener.addTween(indicator,({alpha:0, time:1, transition:"easeOutCubic", onComplete:function():void{Sprite(highlightIndicator).graphics.clear()}}));
 } }  

-Christopher Keeler
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

No comments found.

Name:   Required
Email:   Required your email address will not be publicly displayed.

Want to receive notifications when new comments are added? Login/Register for an account.

Anti-spam key

Type in the text that you see in the above image:

Your comment:

Sorry, no HTML allowed!