PDA

View Full Version : really weird stuff (Yahoo ASTRA Tree - CellRenderer)


SirDuke
04-23-2008, 06:35 PM
I wanted to change the color of the rolled-over node on the tree component so i used:

mainMenu.myTree.addEventListener(ListEvent.ITEM_CL ICK, MenuClickHandler);
mainMenu.myTree.addEventListener(ListEvent.ITEM_RO LL_OVER, MenuRollOverHandler);
mainMenu.myTree.addEventListener(ListEvent.ITEM_RO LL_OUT, MenuRollOutHandler);

function MenuRollOverHandler(e:ListEvent){
var cr:TreeCellRenderer = mainMenu.myTree.itemToCellRenderer(e.item) as TreeCellRenderer;
var tf:TextFormat = cr.getStyle("textFormat") as TextFormat;
tf.color = _menuOverColor; //_menuOverColor = "0x000000"
}
function MenuRollOutHandler(e:ListEvent){
var cr:TreeCellRenderer = mainMenu.myTree.itemToCellRenderer(e.item) as TreeCellRenderer;
var tf:TextFormat = cr.getStyle("textFormat") as TextFormat;
tf.color = _menuColor; //_menuColor = "0x888888"
}
And it works fine!

Now i want to change the color of an item when CLICKED so i use:

function MenuClickHandler(e:ListEvent):void {
var cr:TreeCellRenderer = mainMenu.myTree.itemToCellRenderer(e.item) as TreeCellRenderer;
var tf:TextFormat = cr.getStyle("textFormat") as TextFormat;
tf.color = _menuOverColor;
}

But this just won't work!
I trace "tf.color" in each of those function. The first two return some number (like 8764948) but the last one (click handler) returns always zero (0)...

I check and see that in both cases i get the same textFormat Object! But in the click event handler i just can't change it...

Can anyone see why this could be caused?:eek:

Thanks

allenrabinovich
04-24-2008, 11:28 PM
This is very odd indeed; I am not sure why the behavior differs among the three event listeners. Somehow, it appears, that in the item_click listener, a copy of the TextFormat object is created, rather than a direct pointer.

While I investigate further, there's an easy workaround. After assigning the needed color value to the tf object, call:

cr.setStyle("textFormat", tf);

That will ensure that tf is copied to the textFormat style of the renderer.

Let me know how this goes.

SirDuke
04-25-2008, 10:49 AM
well i didn't check for your answer sooner and in the meantime i found another (more complex) workaround. I created a var called "isClicked" in the CellRenderer and inside the TreeCellRenderer i have an if statement that renders a different color based on this boolean var. So in my click handler i just do:

var cr:TreeCellRenderer = mainMenu.myTree.itemToCellRenderer(e.item) as TreeCellRenderer;
cr.isClicked = true;

then store in a variable the last node clicked and in the next click i set the previousNode.isClicked to false and the new node to true...

I figured it had to be with a variable inside the cellRenderer cause no matter what color i set it to, in the next redraw (click) it was reset to the cellRenderer's default.

As far as i'm concerned this is done for me. It might be overkill, but since it works, it's okay for this project. However if you need anything else feel free to ask.Thanx for your attention and of course for the component :) (next version: animation)