In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

GUI

Get Adobe Flash player
by Shazypro 15 Sep 2011
package {
    import flash.display.Sprite;

    public class newProject extends Sprite {
        public function newProject () {
            var life :LifeBox = new LifeBox();
            addChild(life);
        }
    }
}




// LifeBox Class
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.MouseEvent;

internal class LifeBox extends Sprite {
    // Private variables, for use only within LifeBox
    
    // Public variables for use to the extendee
    public var container :Sprite = new Sprite();
    
    public function LifeBox() {
        // A simple gradient rectangle
        var container:Sprite = Draw.gradientRect({
            "width"   : 100,
            "height"  : 400,
            "colors"  : [0x3381FF, 0x2154A6],
            "alphas"  : [1, 1],
            "radius"  : 10, 
            "filters" : [new GlowFilter(0x2154A6, 1.0, 10, 10, 2, 5, false, false)] 
        });
        
        container.x = 50; container.y = 10;
        this.addChild(container);
        
        
        // A simple progress bar. Still a work in progress -
        // ... for some reason borders (lines) are not
        // working correctly.
        var progress :ProgressBar = new ProgressBar({
            "width"   : 100,
            "height"  : 10,
            "radius"  : 5
            //,"lineThick" : 5       
        }, {"colors" : [0xFFEA00, 0x2154A6]});
                        
        progress.y += 5;
        container.addChild(progress);        
        progress.setPercentage(10);
        
        
        var button1 :TextButton = Draw.textButton({
            "width"  : 100,
            "height" : 20,
            "text"   : "Testing",
            "textSize" : 11,
            "radius" : 10   
        });
        
        container.addChild(button1);
               
        this.addEventListener(MouseEvent.CLICK, function (event:MouseEvent) :void {
            progress.setProgress("add", 2, 2);
        });
    }
}



// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// GUI Class
import flash.display.Sprite;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.display.InterpolationMethod;              
import flash.geom.Matrix;
import flash.filters.*;

internal class Draw {
    // Private variables
    
    // Public variables
    
    public function Draw() {
        throw ("Use a method of GUI instead of calling it directly");       
    }
    
    public static function replaceObjectWithDefaults (props :Object, f :String = null) :Object {
        if (!f || f === "shape") {
            props.alpha      ||= 1.0;               props.width     ||= 0;
            props.height     ||= 0;                 props.x         ||= 0;
            props.y          ||= 0;                 props.radius    ||= 0;
            props.filters    ||= [];                props.lineThick ||= null;
            props.lineColor  ||= 0x000000;          props.lineAlpha ||= 1.0;
            props.fillColor  ||= 0x000000;          props.fillAlpha ||= 1.0;
            props.colors     ||= null;              props.alphas    ||= [1, 1];
        }
        
        if (f === "textbutton") {
            props = replaceObjectWithDefaults(props, "shape");
            
            props.text       ||= null;              props.textColor ||= 0xFFF;
            props.textSize   ||= 10;                props.form      ||= null;
            props.font       ||= "Arial";           props.bold      ||= false;
            
            props.down       ||= null;              props.over      ||= null;
        }
        
        return props as Object; 
    }
    
    /**
     * Changes the default values for certain methods to change the theme.
     *
     * <p>This is still a work in progress and currently does not function right now.</p>
     *
     * @param name Name of the theme to use.
     */
    public static function setTheme (name :String) :void {
        return;
    }
            
    /**
     * A shortcut to draw a basic rectangle.
     *
     * @return Sprite The rectangle drawn based on the props given.
     *
     * @see basicRectangle
     */
    public static function rect (props :Object) :Sprite {
        return new basicRectangle(props);                                
    }
            
    /**
     * A shortcut to draw a rectangle with a gradient fill.
     *
     * @return Sprite The rectangle drawn based on the props given.
     *
     * @see gradientRectangle
     */
    public static function gradientRect (props :Object) :Sprite {
       return new gradientRectangle(props);        
    }
    
    /**
     * A shortcut to draw a ProgressBar
     *
     * @return ProgressBar The ProgressBar class.
     *
     * @see ProgressBar
     */
    public static function progress (... args) :ProgressBar {
       return new ProgressBar(args[0], args[1], args[2], args[3]);        
    }
    
    /**
     * A shortcut to draw a simple button with text.
     *
     * @return TextButton The TextButton class.
     *
     * @see TextButton
     */
    public static function textButton (props :Object) :TextButton {
       return new TextButton(props);        
    }
    
}


import flash.display.Sprite;                
import flash.geom.*;
import flash.filters.*;

import caurina.transitions.Tweener;

internal class ProgressBar extends Sprite {
    // Private variables
    private static var total          :Number, progress        :Number = 0;
    
    // Public variables
    public static var backgroundProps :Object, foregroundProps :Object;        
    public static var bgBar           :Sprite, fgMaskBar       :Sprite, fgBar           :Sprite;
    
    public function ProgressBar (bgProps:Object, fgProps:Object, progress:Number = 0, percentage:Number = 100) {
        // Replace the received Object's props with Defaults
        // if they are not supplied.
        bgProps         = Draw.replaceObjectWithDefaults(bgProps, "shape");
        fgProps         = Draw.replaceObjectWithDefaults(fgProps, "shape");
        
        // The foreground (the actual "bar") should be
        // the same width and height of the background
        // and should also have the same radius
        fgProps.width   = bgProps.width;
        fgProps.height  = bgProps.height;
        fgProps.radius  = bgProps.radius;
        fgProps.lineThick = bgProps.lineThick;
    
        // Draw both the foreground and the background
        // then set the given props as our class
        // props... If that makes any sense. ;)
        bgBar           = (bgProps.colors ? Draw.gradientRect(bgProps) : Draw.rect(bgProps));
        backgroundProps = bgProps;
        
        fgMaskBar       = Draw.rect(bgProps);
        
        fgBar           = (fgProps.colors ? Draw.gradientRect(fgProps) : Draw.rect(fgProps));
        foregroundProps = fgProps;
        
        // Add the bars to our extendee
        //addChild(bgBar);        
        addChild(bgBar);
        addChild(fgMaskBar);
        bgBar.addChild(fgBar);
        
        bgBar.mask      =  fgMaskBar;
        
        // Set the limit for the progress. Default
        // is set at 100 (100%);
        setPercentage(percentage);
        
        // Let's set the current progress if we are given
        // any and if not, default is 0.
        setProgress("set", progress);    
    }
    
    public function setPercentage (p:int = 0) :void {
        if (p !== total) {
            total = p;
            
            // Only need to change any widths if our
            // progress is greater than 0.    
            if (getProgress() > 0) {
               setProgress("set", progress);    
            }
        }
    }
    
    // TODO: Introduce animation
    public function setProgress (method:String, amount:Number = 0, time:Number = 1) :void {
        var progressExpression :Number;
    
        method === "set" ? progress =  amount : null;
        method === "add" ? progress += amount : null;
        method === "min" ? progress -= amount : null;

        progress >=  total ? progress =  total  : null; 
        progress <=  0     ? progress =  0      : null;
        
        progressExpression = (progress / total) * bgBar.width;
        
        //Tweener.addTween(fgBar, {"width": (progress / total) * bgBar.width, "time": time, "transition": "easeIn"});
        fgBar.width = progressExpression;               
    }
    
    public function getProgress () :Number { 
        return progress; 
    }
    
    public function getMax () :Number { 
        return total;    
    }
   
}






    import flash.display.Shape;
    import flash.display.SimpleButton;
    import flash.display.Sprite;
    import flash.display.GradientType;
    import flash.display.SpreadMethod;
    import flash.display.InterpolationMethod;              
    import flash.geom.Matrix;

    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;

    internal class TextButton extends SimpleButton {
    
        public function TextButton (props :Object) {
            props     = Draw.replaceObjectWithDefaults(props, "textbutton");
            
            upState   = makeFace(props,      "up");
            overState = makeFace(props.over, "over");
            downState = makeFace(props.down, "down");
                        
            hitTestState = upState;
        }
        
        protected function makeFace (props :Object, fr :String) :Sprite {
            var face  :Sprite    = new Sprite();
            var labell :TextField = new TextField();
            
            if (fr !== "up") {
                //props[f0r] = props;
                //props[f0r].fillColor = ColorMod.lighten(props.fillColor, 0.5);        
            }

            
            if (props.color) {
                face = Draw.gradientRect(props);
            } else {
                face = Draw.rect(props);
            }

            // If we are given any text
            if (props.text) {
     
                labell.textColor = props.textColor;
                labell.width = face.width;
                labell.height = face.height;
                
                if (props.form) {
                    labell.defaultTextFormat = props.form;
                } else {
                    labell.defaultTextFormat = new TextFormat(props.font, props.textSize, props.color, props.bold);
                }
                
                labell.text = props.text;
                labell.mouseEnabled = false;
                
                face.addChild(labell);
                labell.x = face.width/2  - labell.textWidth/2 - 2;
                labell.y = face.height/2 - labell.textHeight/2 - 3;
                
            }
            
            return face;        
        }

    }
    
    
    
    
    
    
    
    import flash.display.Sprite;
    import flash.display.GradientType;
    import flash.display.SpreadMethod;
    import flash.display.InterpolationMethod;              
    import flash.geom.Matrix;
    import flash.filters.*;

    internal class gradientRectangle extends Sprite {
        
        public function gradientRectangle (props :Object) {
            var temp   :Sprite = new Sprite(),
                matrix :Matrix = new Matrix();
                
            props = Draw.replaceObjectWithDefaults(props);
                        
            // If we are given a line thickness, then proceed to give
            // the new sprite a border.    
            if (props.lineThick) {
                temp.graphics.lineStyle(props.lineThick, props.lineColor, props.lineAlpha);
            }
            
            matrix = new Matrix();
            matrix.createGradientBox(props.width, props.height, Math.PI/2);
            
            temp.graphics.beginGradientFill(GradientType.LINEAR, props.colors, props.alphas, [0, 255], matrix, 
            SpreadMethod.PAD, InterpolationMethod.LINEAR_RGB);
            temp.graphics.drawRoundRect(props.x, props.y, props.width, props.height, props.radius);
            temp.graphics.endFill();
            temp.filters = props.filters;
            
            addChild(temp);                     
        }
                
    }
    
    
    
    
    import flash.display.Sprite;
    import flash.filters.*;

    internal class basicRectangle extends Sprite {
        
        public function basicRectangle (props :Object) {
            var temp :Sprite = new Sprite();

            props = Draw.replaceObjectWithDefaults(props);
                        
            // If we are given a line thickness, then proceed to give
            // the new sprite a border.    
            if (props.lineThick) {
                temp.graphics.lineStyle(props.lineThick, props.lineColor, props.lineAlpha);
            }

            temp.graphics.beginFill(props.fillColor, props.fillAlpha);            
            temp.graphics.drawRoundRect(props.x, props.y, props.width, props.height, props.radius);
            temp.graphics.endFill();            
            temp.alpha   = props.alpha;
            temp.filters = props.filters;

            addChild(temp)                     
        }
                
    }
    
    
    
    internal class ColorMod {
    
        public function ColorMod () {
            throw ("ColorMod is a static class so no need to call it directly.");
        }
        
         // Convert Hex (uint) to RGB (array or object)
        public static function hex2rgb (hex :uint, returnObject :Boolean = false) :* {
            var rgb:Array = [(hex >> 16) & 0xFF, (hex >> 8) & 0xFF, hex & 0xFF];
            
            if (returnObject) {
                return {
                    "r"    : rgb[0],
                    "g"    : rgb[1],
                    "b"    : rgb[2]
                } as Object;
            }
            
            return rgb as Array;
        }
        
        // Convert RGB (r,g,b, or "r,g,b" or [r, g, b]) to hex (uint)
        public static function rgb2dec (props :*, g:uint = NaN, b:uint = NaN) :uint {
            var rgb :Array;
        
            if (props is String) {
                rgb = props.split(",");
            } else if (props is Array) {
                rgb = props;
            } else if (props is uint) {
                rgb = [props, g, b];
            }
            
            return uint((rgb[0] << 16) | (rgb[1] << 8) | rgb[2]);
        }
        
        // Just about the same as rgb2dec but prepends a "0x" to the number.
        public static function rgb2hex (... args) :uint {
            return uint("0x" + Number(rgb2dec(args)).toString(16).toUpperCase());
        }
        
        // lighten up the color
        public static function lighten (originalColor :uint, percent: Number = 0.5) :uint {
            var rgb :Array = hex2rgb(originalColor);
            
            for (var i:int = 0; i < 3; i++) {
                rgb[i] = Math.round(rgb[i] * percent) + Math.round(255 * (1-percent));                
                rgb[i] = rgb[i] > 255 ? 255 : rgb[i];
            }
            
            return rgb2dec(rgb);
        }
        
        // darken down the color
        public static function darken (originalColor :uint, percent: Number = -0.5) :uint {
            var rgb :Array = hex2rgb(originalColor);
            
            for (var i:int = 0; i < 3; i++) {
                rgb[i] = Math.round(rgb[i] * (percent - (percent*2))) + Math.round(0 * (1-(percent - (percent*2))));                
                rgb[i] = rgb[i] > 255 ? 255 : rgb[i];
            }
            
            return rgb2dec(rgb);
        }

    }