Some Flex components that might be useful, Indexed RadioButton Group, Flex MovieClip

In my last project I made some possibly useful Flex classes that I’ll briefly explain and share here. Maybe I should set up a GitHub with them soon or something.

The first one is IndexedRadioButtonGroup which improves the spark(Flex4) RadioButtonGroup by adding methods that help you keep track and control the index of which radio button is selected. The main use for this is using radio buttons for navigation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// IndexedRadioButtonGroup.as
package com.designoidgames.ui
{
    import flash.events.Event;
    import flash.utils.getTimer;
 
    import mx.core.IFlexDisplayObject;
 
    import spark.components.RadioButtonGroup;
 
    public class IndexedRadioButtonGroup extends RadioButtonGroup
    {
 
        public function IndexedRadioButtonGroup(document:IFlexDisplayObject=null)
        {
            super(document);
        }
 
        public function getCurrentIndex():int{
            for(var i:int=0;i
<this.numRadioButtons;i++){
                if(getRadioButtonAt(i) == this.selection){
                    return i;
                }
            }
 
            return -1;
        }
 
        public function selectIndex(index:int):void{
            selection = getRadioButtonAt(index);
            dispatchEvent(new Event(Event.CHANGE));
        }
 
        public function moveLeft(ignoreEnabled:Boolean=false):void{
            var index:int = getCurrentIndex();
            index = Math.max(0, index-1);
            if(getRadioButtonAt(index).enabled || ignoreEnabled){
                selectIndex(index);
            }
        }
 
        public function moveRight(ignoreEnabled:Boolean=false):void{
            var index:int = getCurrentIndex();
            index = Math.min(numRadioButtons-1, index+1);
            if(getRadioButtonAt(index).enabled || ignoreEnabled){
                selectIndex(index);
            }
        }
 
    }
}
// IndexedRadioButtonGroup.as
package com.designoidgames.ui
{
	import flash.events.Event;
	import flash.utils.getTimer;

	import mx.core.IFlexDisplayObject;

	import spark.components.RadioButtonGroup;

	public class IndexedRadioButtonGroup extends RadioButtonGroup
	{

		public function IndexedRadioButtonGroup(document:IFlexDisplayObject=null)
		{
			super(document);
		}

		public function getCurrentIndex():int{
			for(var i:int=0;i
<this.numRadioButtons;i++){
				if(getRadioButtonAt(i) == this.selection){
					return i;
				}
			}

			return -1;
		}

		public function selectIndex(index:int):void{
			selection = getRadioButtonAt(index);
			dispatchEvent(new Event(Event.CHANGE));
		}

		public function moveLeft(ignoreEnabled:Boolean=false):void{
			var index:int = getCurrentIndex();
			index = Math.max(0, index-1);
			if(getRadioButtonAt(index).enabled || ignoreEnabled){
				selectIndex(index);
			}
		}

		public function moveRight(ignoreEnabled:Boolean=false):void{
			var index:int = getCurrentIndex();
			index = Math.min(numRadioButtons-1, index+1);
			if(getRadioButtonAt(index).enabled || ignoreEnabled){
				selectIndex(index);
			}
		}

	}
}

The next two classes are the MovieClipButton and a static skin class for it. This is useful when you want to use embedded MovieClips, like when you have a lot of material inside the Flash IDE and you export it as an .SWC file into your project. The MovieClipButton class is extremely simple, in fact it only adds one method to the spark Button called 'source' (other spark components like Image use the same).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// MovieClipButton.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Button xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"
          xmlns:mx="library://ns.adobe.com/flex/mx" useHandCursor="true" buttonMode="true">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
 
    <fx:Script>
        <![CDATA[
            public var source:Class;
        ]]>
    </fx:Script>
 
</s:Button>
// MovieClipButton.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Button xmlns:fx="http://ns.adobe.com/mxml/2009"
		  xmlns:s="library://ns.adobe.com/flex/spark"
		  xmlns:mx="library://ns.adobe.com/flex/mx" useHandCursor="true" buttonMode="true">
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>

	<fx:Script>
		<![CDATA[
			public var source:Class;
		]]>
	</fx:Script>

</s:Button>

Then you supply a skin class for the button, this is the simplest one. It only tries to set the current frame of the MovieClip to a frame with the same name as the current state. Also if the MovieClip has a text label like most buttons do, you can change the text through MXML. Just make sure it's called "label" inside the MovieClip.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// StaticButtonSkin.mxml
<?xml version="1.0" encoding="utf-8"?>
 
<!--- The default wireframe skin class for the Spark Button component.
        Skin classes in the wireframe package are useful for using as a simple base for a custom skin.
 
       @see spark.components.Button
 
      @langversion 3.0
      @playerversion Flash 10
      @playerversion AIR 1.5
      @productversion Flex 4
-->
<s:SparkButtonSkin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
                    creationComplete="creationCompleteHandler(event)" alpha.disabled="0">
    <fx:Metadata>
        [HostComponent("com.designoidgames.ui.MovieClipButton")]
    </fx:Metadata>
 
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
 
            private var mc:MovieClip;
 
            override protected function stateChanged(oldState:String, newState:String, recursive:Boolean):void{
                super.stateChanged(oldState,newState,recursive);
 
                if(mc==null) return;
 
                mc.gotoAndStop(newState);
            }
 
            protected function creationCompleteHandler(event:FlexEvent):void{
                mc = new (hostComponent as MovieClipButton).source();
                mcHolder.addChild(mc);
 
                mc.gotoAndStop(currentState);
 
                if(hostComponent.label) mc.label.text = hostComponent.label;
            }
 
        ]]>
    </fx:Script>
 
    <s:states>
        <s:State name="up" />
        <s:State name="over" />
        <s:State name="down" />
        <s:State name="disabled" />
    </s:states>
 
    <s:SpriteVisualElement id="mcHolder"/>
 
</s:SparkButtonSkin>
// StaticButtonSkin.mxml
<?xml version="1.0" encoding="utf-8"?>

<!--- The default wireframe skin class for the Spark Button component.
        Skin classes in the wireframe package are useful for using as a simple base for a custom skin.

       @see spark.components.Button

      @langversion 3.0
      @playerversion Flash 10
      @playerversion AIR 1.5
      @productversion Flex 4
-->
<s:SparkButtonSkin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
    				creationComplete="creationCompleteHandler(event)" alpha.disabled="0">
	<fx:Metadata>
		[HostComponent("com.designoidgames.ui.MovieClipButton")]
	</fx:Metadata>

	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;

			private var mc:MovieClip;

			override protected function stateChanged(oldState:String, newState:String, recursive:Boolean):void{
				super.stateChanged(oldState,newState,recursive);

				if(mc==null) return;

				mc.gotoAndStop(newState);
			}

			protected function creationCompleteHandler(event:FlexEvent):void{
				mc = new (hostComponent as MovieClipButton).source();
				mcHolder.addChild(mc);

				mc.gotoAndStop(currentState);

				if(hostComponent.label) mc.label.text = hostComponent.label;
			}

		]]>
	</fx:Script>

    <s:states>
        <s:State name="up" />
        <s:State name="over" />
        <s:State name="down" />
        <s:State name="disabled" />
    </s:states>

	<s:SpriteVisualElement id="mcHolder"/>

</s:SparkButtonSkin>

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*