After dealing with the frustrations of the Resize effect for too long
I decided to write my own replacement to it that works the way I would
expect Resize should.

Major distinctions are support for resizing with percentages and
supports the ability to resize one direction (width or height) with a
different type (pixel or percentage) than the other.

Here's the code:

package jsl {
        import mx.effects.IEffectInstance;
        import mx.effects.TweenEffect;
        
        public class Resize extends TweenEffect {
                public function Resize(target:Object = null) {
                        super(target);
                        instanceClass = ResizeInstance;
                }
                
                [Inspectable(defaultValue=null)]
                public var widthFrom:Number;
                
                [Inspectable(defaultValue=null)]
                public var heightFrom:Number;
                
                [Inspectable(defaultValue=null)]
                public var widthTo:Number;
                
                [Inspectable(defaultValue=null)]
                public var heightTo:Number;
                
                [Inspectable(defaultValue=null)]
                public var widthType:String;
                
                [Inspectable(defaultValue=null)]
                public var heightType:String;
                
                override protected function
initInstance(instance:IEffectInstance):void {
                        super.initInstance(instance);
                        var effectInstance:ResizeInstance = 
ResizeInstance(instance);
                        effectInstance.widthFrom = widthFrom;
                        effectInstance.heightFrom = heightFrom;
                        effectInstance.widthTo = widthTo;
                        effectInstance.heightTo = heightTo;
                        effectInstance.widthType = widthType;
                        effectInstance.heightType = heightType;
                }
        }
}

package jsl {
        import mx.effects.effectClasses.AnimatePropertyInstance;
        import mx.effects.effectClasses.TweenEffectInstance;
        
        public class ResizeInstance extends TweenEffectInstance {
                public var widthFrom:Number;
                public var heightFrom:Number;
                public var widthTo:Number;
                public var heightTo:Number;
                public var widthType:String;
                public var heightType:String;
                
                private var widthEffect:AnimatePropertyInstance;
                private var heightEffect:AnimatePropertyInstance;
                
                public function ResizeInstance(target:Object) {
                        super(target);
                }
                
                override public function play():void {
                        super.play();
                        initializeProperties();
                        if (widthEffect != null) widthEffect.play();
                        if (heightEffect != null) heightEffect.play();
                }
                
                protected function initializeProperties():void {
                        if ((!isNaN(widthTo)) && (widthType != 'auto')) {
                                widthEffect = new 
AnimatePropertyInstance(target);
                                widthEffect.duration = duration;
                                widthEffect.repeatCount = repeatCount;
                                widthEffect.repeatDelay = repeatDelay;
                                widthEffect.startDelay = startDelay;
                                widthEffect.easingFunction = easingFunction;
                                if (widthType == 'pixel') {
                                        widthEffect.property = 'width';
                                } else if (widthType == 'percentage') {
                                        widthEffect.property = 'percentWidth';
                                } else {
                                        Logger.log('Unknown Width Property: ' + 
widthType);
                                }
                                if (!isNaN(widthFrom)) {
                                        widthEffect.fromValue = widthFrom;
                                } else if ((widthType == 'percentage') &&
(isNaN(target['percentWidth']))) {
                                        // Trying to change percentage but a 
percentage is not currently
determined
                                        var pW:Object = target['parent'];
                                        if (pW != null) {
                                                widthEffect.fromValue = 
(target['width'] / pW['width']) * 100;
                                        }
                                }
                                widthEffect.toValue = widthTo;
                        }
                        if ((!isNaN(heightTo)) && (heightType != 'auto')) {
                                heightEffect = new 
AnimatePropertyInstance(target);
                                heightEffect.duration = duration;
                                heightEffect.repeatCount = repeatCount;
                                heightEffect.repeatDelay = repeatDelay;
                                heightEffect.startDelay = startDelay;
                                heightEffect.easingFunction = easingFunction;
                                if (heightType == 'pixel') {
                                        heightEffect.property = 'height';
                                } else if (heightType == 'percentage') {
                                        heightEffect.property = 'percentHeight';
                                } else {
                                        Logger.log('Unknown Height Property: ' 
+ heightType);
                                }
                                if (!isNaN(heightFrom)) {
                                        heightEffect.fromValue = heightFrom;
                                } else if ((heightType == 'percentage') &&
(isNaN(target['percentHeight']))) {
                                        // Trying to change percentage but a 
percentage is not currently
determined
                                        var pH:Object = target['parent'];
                                        if (pH != null) {
                                                heightEffect.fromValue = 
(target['height'] / pH['height']) * 100;
                                        }
                                }
                                heightEffect.toValue = heightTo;
                        }
                }
        }
}

Reply via email to