Flash (CS3) SimpleButton with Disabled State

Yet another difference between Flex  and Flash components… in Flash the SimpleButton doesn’t support the disabled state so here is a class to implement it:

package {
    import flash.display.DisplayObject;
    import flash.display.SimpleButton;

    /**
     * Flash SimpleButton class extension to handle the disabled state.
     *
     * usage
     *
     * using the constructor
     *
     * var yourCustomButton:CustomSimpleButton = new CustomSimpleButton(yourUpState,
     *                                                                                                                 yourOverState,
     * 	                                                                                                                yourDownState,
     *     	                                                                                                        yourHitTestState,
     *	                                                                                                                yourDisabledState);
     *
     * using the init method
     *
     * var yourCustomButton:CustomSimpleButton = new CustomSimpleButton();
     * yourCustomButton.init(yourUpState,
     *	                                   yourOverState,
     * 	                                   yourDownState,
     * 	                                   yourHitTestState,
     * 	                                   yourDisabledState);
     *
     *
     * by setting the hideWhenDisabled = true your button will be invisible
     * when disabled and the disabledState won't be visible.
     *
     * @author Simone Vicentini
     *
     */
    public class CustomSimpleButton extends SimpleButton
    {
        /** reference to the original upState */
        protected var upStateBackup:DisplayObject;

        /** reference to the original overState */
        protected var overStateBackup:DisplayObject;

        /** disabled state */
        protected var disabledState_:DisplayObject;

        /** Hide the button when disabled? */
        protected var hideWhenDisabled_:Boolean = false;

        /**
         * Class constructor
         *
         * This has the same interface as the SimpleButton to keep it compatible.
         *
         * @param upState DisplayObject to represent the up state
         * @param overState DisplayObject to represent the over state
         * @param downState DisplayObject to represent the down state
         * @param hitTestState DisplayObject to represent the hitTest area
         * @param disabledState DisplayObject to represent the disabled state
         */
        public function CustomSimpleButton(upState:DisplayObject = null,
                                           overState:DisplayObject = null,
                                           downState:DisplayObject = null,
                                           hitTestState:DisplayObject = null,
                                           disabledState:DisplayObject = null)
        {
            init(upState, overState, downState, hitTestState, disabledState);
        }

        /**
         * Initialize the CustomSimpleButton
         *
         * @param upState DisplayObject to represent the up state
         * @param overState DisplayObject to represent the over state
         * @param downState DisplayObject to represent the down state
         * @param hitTestState DisplayObject to represent the hitTest area
         * @param disabledState DisplayObject to represent the disabled state
         */
        public function init(up:DisplayObject = null,
                             over:DisplayObject = null,
                             down:DisplayObject = null,
                             hit:DisplayObject = null,
                             disabled:DisplayObject = null):void
        {
            /* up state */
            upState = up;
            upStateBackup = up;

            /* down state */
            if (down == null)
                down = up;
            downState = down;

            /* over state */
            if (over == null)
                over = up;
            overState = over;
            overStateBackup = over;

            /* hit state */
            if (hit == null)
                hit = up;
            hitTestState = hit;

            /* disabled state */
            disabledState = disabled;
        }

        /**
         * Sets if we want to hide the button when set to disabled
         *
         * @param value True hide the button when disabled, false otherwise.
         */
        public function set hideWhenDisabled(value:Boolean):void
        {
            hideWhenDisabled_ = value;
        }

        /**
         * Enables/Disables the button
         *
         * @param value True enables the button, false disables it
         */
        override public function set enabled(value:Boolean):void
        {
            super.enabled = value;
            if (!value)
            {
                this.visible = hideWhenDisabled_;
                if (disabledState_ != null)
                {
                    upState = disabledState_;
                    overState = disabledState_;
                }
            }
            else
            {
                this.visible = true;
                if (disabledState_ != null)
                {
                    upState = upStateBackup;
                    overState = overStateBackup;
                }
            }

        }

        /**
         * Gets the upState object
         *
         * @return The upState DisplayObject
         */
        override public function get upState():DisplayObject
        {
            return upStateBackup;
        }

        /**
         * Sets the upState object
         *
         * @param value The new upState DisplayObject
         */
        override public function set upState(value:DisplayObject):void
        {
            super.upState = value;
            if (value != disabledState_)
                upStateBackup = value;
        }

        /**
         * Gets the disabledState object
         *
         * @return The disabledState DisplayObject
         */
        public function get disabledState():DisplayObject
        {
            return disabledState_;
        }

        /**
         * Sets the disabledState object
         *
         * @param value The new disabledState DisplayObject
         */
        public function set disabledState(value:DisplayObject):void
        {
            disabledState_ = value;
        }
    }
}
This entry was posted in AS3, components, Flash and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
blog comments powered by Disqus