Guest User

Untitled

a guest
May 21st, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.display.GradientType;
  4.     import flash.display.Sprite;
  5.     import flash.geom.Matrix;
  6.     import flash.text.TextField;
  7.     import flash.text.TextFormat;
  8.  
  9.     /********************************************
  10.      *                                          *
  11.      * Progress Bar v 1.0                       *
  12.      * @author Martin RenĂ© Andersen             *
  13.      *                                          *
  14.      *******************NOTES********************
  15.      * A progress bar which in this case will   *
  16.      * show a box which will getting filled with*  
  17.      * a color from left to right, and will be  *
  18.      * able to show a text field in the center. *
  19.      *                                          *
  20.      * If a 2nd color is defined, a gradient is *
  21.      * made                                     *
  22.      ****************CHANGE LOG******************
  23.      *  1.0 - First release                     *
  24.      *                                          *
  25.      ********************************************
  26.      */
  27.     public class ProgressBar extends Sprite
  28.     {
  29.         ////////////////////////////////////
  30.         var myFont = new visitor();// UPDATE THIS DEPENDING ON THE FONT YOU WANT TO USE
  31.         ////////////////////////////////////
  32.  
  33.         //Determinates how far the progress bar is
  34.         var travel:Number = 0;
  35.  
  36.         var thickness:Number;
  37.         var color:uint;
  38.         var colorLineStyle:uint;
  39.         var w:Number;
  40.         var h:Number;
  41.         var maxValue:Number;//amount of value needed to fill the bar
  42.         var endText:String;
  43.         var startText:String;
  44.         var bgColor:uint;
  45.         var percentCal:Number;
  46.         var valueTotal:Number = 0;
  47.  
  48.         var percent:Boolean;
  49.         var outof:Boolean;
  50.  
  51.         //a boolean to check if the bar is filled.
  52.         var done:Boolean;
  53.  
  54.  
  55.         var colors:Array;
  56.         var alphas:Array = [1,1];
  57.         var ratios:Array = [0,255];
  58.         var mat:Matrix   = new Matrix();
  59.  
  60.  
  61.         var myFormat:TextFormat = new TextFormat();
  62.         var myText:TextField = new TextField();
  63.  
  64.         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)
  65.         {
  66.             if (colorText == 0)
  67.             {
  68.                 colorText = colorLineStyle;
  69.             }
  70.  
  71.             if (color2 == 0)
  72.             {
  73.                 color2 = color;
  74.             }
  75.  
  76.             if (endText == "")
  77.             {
  78.                 endText = startText;
  79.             }
  80.  
  81.             if (startText.toLowerCase() == "percent")
  82.             {
  83.                 if (endText == startText)
  84.                 {
  85.                     endText = "100%";
  86.                 }
  87.  
  88.                 percent = true;
  89.                 startText = "0%";
  90.  
  91.             }
  92.  
  93.             if (startText.toLowerCase() == "outof")
  94.             {
  95.                 if (endText == startText)
  96.                 {
  97.                     endText = maxValue + " out of " + maxValue;
  98.                 }
  99.                
  100.                 outof = true;
  101.                 startText = Math.round(valueTotal).toString() + " out of " + maxValue;
  102.             }
  103.  
  104.             mat.createGradientBox(w,h,toRad(-90));
  105.             colors = [color, color2];
  106.            
  107.             this.thickness = thickness;
  108.             this.color = color;
  109.             this.colorLineStyle = colorLineStyle;
  110.             this.h = h;
  111.             this.w = w;
  112.             this.maxValue = maxValue;
  113.             this.endText = endText;
  114.             this.startText = startText;
  115.             this.bgColor = bgColor;
  116.  
  117.             update();
  118.  
  119.             myFormat.size = fontSize;
  120.             myFormat.font = myFont.fontName;
  121.             myFormat.align = "center";
  122.             with (myText)
  123.             {
  124.                 defaultTextFormat = myFormat;
  125.                 text = startText;
  126.                 embedFonts = true;
  127.                 width = w;
  128.                 textColor = colorText;
  129.                 wordWrap = true;
  130.                 selectable = false;
  131.                 y = h / 2 - myText.textHeight / 2;
  132.             }
  133.             addChild(myText);
  134.         }
  135.  
  136.         public function update(value:Number = 0)
  137.         {
  138.             valueTotal = valueTotal + value;
  139.  
  140.             if (valueTotal >= maxValue)
  141.             {
  142.                 value = (value - (valueTotal - maxValue));
  143.                 valueTotal = maxValue;
  144.                 done = true;
  145.             }
  146.             else
  147.             {
  148.                 done = false;
  149.             }
  150.  
  151.  
  152.             travel += ((w - thickness) / maxValue) * value;
  153.  
  154.             if (done == false)
  155.             {
  156.                 if (percent == true)
  157.                 {
  158.                     percentCal = Math.round((travel / (w - thickness)) * 100);
  159.                     myText.text = percentCal.toString() + "%";
  160.                 }
  161.  
  162.                 if (outof == true)
  163.                 {
  164.                     myText.text = Math.round(valueTotal).toString() + " out of " + maxValue;
  165.                 }
  166.             }
  167.             else
  168.             {
  169.                 myText.text = endText;
  170.             }
  171.            
  172.             draw();
  173.            
  174.         }
  175.  
  176.         public function reset():void
  177.         {
  178.             travel = 0;
  179.             done = false;
  180.             valueTotal = 0;
  181.            
  182.             if (percent == true)
  183.             {
  184.                 percentCal = Math.round((travel / (w - thickness)) * 100);
  185.                 myText.text = percentCal.toString() + "%";
  186.             }
  187.             else if (outof == true)
  188.             {
  189.                 myText.text = Math.round(valueTotal).toString() + " out of " + maxValue;
  190.             }
  191.             else
  192.             {
  193.                 myText.text = startText;
  194.             }
  195.            
  196.             draw();
  197.         }
  198.        
  199.         function draw():void
  200.         {
  201.             with (this.graphics)
  202.             {
  203.                 clear();
  204.                 lineStyle(thickness, colorLineStyle);
  205.                 beginFill(bgColor);
  206.                 drawRect(0, 0, w, h);
  207.                 endFill();
  208.                 beginGradientFill(GradientType.LINEAR,colors,alphas,ratios,mat);
  209.                 lineStyle();
  210.                 drawRect(thickness,thickness,travel,h-thickness);
  211.                 endFill();
  212.             }
  213.         }
  214.  
  215.         function toRad(a:Number):Number
  216.         {
  217.             return a*Math.PI/180;
  218.         }
  219.  
  220.         public function getValue():Number
  221.         {
  222.             return valueTotal;
  223.         }
  224.     }
  225. }
Add Comment
Please, Sign In to add comment