Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import flash.display.GradientType;
- import flash.display.Sprite;
- import flash.geom.Matrix;
- import flash.text.TextField;
- import flash.text.TextFormat;
- /********************************************
- * *
- * Progress Bar v 1.0 *
- * @author Martin René Andersen *
- * *
- *******************NOTES********************
- * A progress bar which in this case will *
- * show a box which will getting filled with*
- * a color from left to right, and will be *
- * able to show a text field in the center. *
- * *
- * If a 2nd color is defined, a gradient is *
- * made *
- ****************CHANGE LOG******************
- * 1.0 - First release *
- * *
- ********************************************
- */
- public class ProgressBar extends Sprite
- {
- ////////////////////////////////////
- var myFont = new visitor();// UPDATE THIS DEPENDING ON THE FONT YOU WANT TO USE
- ////////////////////////////////////
- //Determinates how far the progress bar is
- var travel:Number = 0;
- var thickness:Number;
- var color:uint;
- var colorLineStyle:uint;
- var w:Number;
- var h:Number;
- var maxValue:Number;//amount of value needed to fill the bar
- var endText:String;
- var startText:String;
- var bgColor:uint;
- var percentCal:Number;
- var valueTotal:Number = 0;
- var percent:Boolean;
- var outof:Boolean;
- //a boolean to check if the bar is filled.
- var done:Boolean;
- var colors:Array;
- var alphas:Array = [1,1];
- var ratios:Array = [0,255];
- var mat:Matrix = new Matrix();
- var myFormat:TextFormat = new TextFormat();
- var myText:TextField = new TextField();
- public function ProgressBar(w:Number, h:Number, color:uint, maxValue:Number = 225, startText:String = "", fontSize:uint = 24, endText:String = "", color2:uint = 0, bgColor:uint = 0xFFFFFF, colorLineStyle:uint = 0x000000, colorText:uint = 0, thickness:Number = 1)
- {
- if (colorText == 0)
- {
- colorText = colorLineStyle;
- }
- if (color2 == 0)
- {
- color2 = color;
- }
- if (endText == "")
- {
- endText = startText;
- }
- if (startText.toLowerCase() == "percent")
- {
- if (endText == startText)
- {
- endText = "100%";
- }
- percent = true;
- startText = "0%";
- }
- if (startText.toLowerCase() == "outof")
- {
- if (endText == startText)
- {
- endText = maxValue + " out of " + maxValue;
- }
- outof = true;
- startText = Math.round(valueTotal).toString() + " out of " + maxValue;
- }
- mat.createGradientBox(w,h,toRad(-90));
- colors = [color, color2];
- this.thickness = thickness;
- this.color = color;
- this.colorLineStyle = colorLineStyle;
- this.h = h;
- this.w = w;
- this.maxValue = maxValue;
- this.endText = endText;
- this.startText = startText;
- this.bgColor = bgColor;
- update();
- myFormat.size = fontSize;
- myFormat.font = myFont.fontName;
- myFormat.align = "center";
- with (myText)
- {
- defaultTextFormat = myFormat;
- text = startText;
- embedFonts = true;
- width = w;
- textColor = colorText;
- wordWrap = true;
- selectable = false;
- y = h / 2 - myText.textHeight / 2;
- }
- addChild(myText);
- }
- public function update(value:Number = 0)
- {
- valueTotal = valueTotal + value;
- if (valueTotal >= maxValue)
- {
- value = (value - (valueTotal - maxValue));
- valueTotal = maxValue;
- done = true;
- }
- else
- {
- done = false;
- }
- travel += ((w - thickness) / maxValue) * value;
- if (done == false)
- {
- if (percent == true)
- {
- percentCal = Math.round((travel / (w - thickness)) * 100);
- myText.text = percentCal.toString() + "%";
- }
- if (outof == true)
- {
- myText.text = Math.round(valueTotal).toString() + " out of " + maxValue;
- }
- }
- else
- {
- myText.text = endText;
- }
- draw();
- }
- public function reset():void
- {
- travel = 0;
- done = false;
- valueTotal = 0;
- if (percent == true)
- {
- percentCal = Math.round((travel / (w - thickness)) * 100);
- myText.text = percentCal.toString() + "%";
- }
- else if (outof == true)
- {
- myText.text = Math.round(valueTotal).toString() + " out of " + maxValue;
- }
- else
- {
- myText.text = startText;
- }
- draw();
- }
- function draw():void
- {
- with (this.graphics)
- {
- clear();
- lineStyle(thickness, colorLineStyle);
- beginFill(bgColor);
- drawRect(0, 0, w, h);
- endFill();
- beginGradientFill(GradientType.LINEAR,colors,alphas,ratios,mat);
- lineStyle();
- drawRect(thickness,thickness,travel,h-thickness);
- endFill();
- }
- }
- function toRad(a:Number):Number
- {
- return a*Math.PI/180;
- }
- public function getValue():Number
- {
- return valueTotal;
- }
- }
- }
Add Comment
Please, Sign In to add comment