I want to design a custom component that extends Button that will have two new styles: selectedColor and selectedFontSize. These will allow the button to have a different text label color and font size when the button is selected. (When I say 'selected' I mean selected state, not being selected (mouse down).
The approach I've taken is to extend Button, add two new styles, and then override the selected
setter, and then use setStyle on the original color style to change font color to selectedColor (if button is selected) or color (if button is de-selected).
I am using selectedColor for simplicity sake in this example, but I want to be able to do this for selectedFontSize as well.
[Style(name="selectedColor",type="uint", format="Color", inherit="no")]
[Style(name="selectedFontSize",type="Number",inherit="no")]
private var _deselectedColor:uint;
private var _selectedColor:uint;
override public function set selected(value:Boolean):void {
super.selected = value;
_selectedColor = getStyle("selectedColor") as uint;
if (!_deselectedColor)
_deselectedColor = getStyle("color") as uint;
if (selected)
setStyle("color",_selectedColor);
else
setStyle("color",_deselectedColor);
}
I'm storing the original color (de-selected color, that is) because it is overwritten when I setStyle("color",_deselectedColor)
. I need to have some way to retrieve it when the button is de-selected.
There are many problems to this approach. Firstly, the first time the selected setter is called, during object construction, the styles are not yet populated. So I am getting a color
of 0x000000 and selectedColor
of 0x000000. Once I manually change the button state post-construction the styles are able to be read-in correctly, but by then it's too late, I've already stored the wrong value. Also, I need to be able to get style updates when set via mxml, actionscript setStyle method, or through css, both statically and dynamically. This approach obviously doesn't do that.
I've also tried overriding the setStyle
and styleChanged
methods. setStyle
never gets called during construction when initializing the button via mxml, so that's a no-go. styleChanged
gets called, but with nulls as the styleProp, not color like I need.
How do the pros do this?