// forked from Shazypro's GUI
package {
import flash.display.Sprite;
public class SimpleButtonExample extends Sprite {
public function SimpleButtonExample() {
var life :LifeBox = new LifeBox();
addChild(life);
}
}
}
// LifeBox Class
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.filters.GlowFilter;
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() {
var container:Sprite = GUI.drawGradRect({
"width" : 100,
"height" : 400,
"colors" : [0x3381FF, 0x2154A6],
"alphas" : [1, 0],
"radius" : 10,
"filters" : [new GlowFilter(0x2154A6, 1.0, 10, 10, 2, 5, false, false)]
});
this.addChild(container);
var progress :ProgressBar = new ProgressBar({
"width" : 100,
"height" : 20
}, {"fillColor" : 0xFFFFFF});
this.addChild(progress);
progress.setProgress(10);
}
}
// 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.*;
import flash.geom.*;
import flash.filters.*;
internal class GUI {
// Private variables
// Public variables
public function GUI() {
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 = props.alpha || 1.0;
props.width = props.width || 0,
props.height = props.height || 0,
props.x = props.x || 0,
props.y = props.y || 0,
props.radius = props.radius || 0;
props.filters = props.filters || [];
props.lineThick = props.lineThick || null,
props.lineColor = props.lineColor || 0x000000,
props.lineAlpha = props.lineAlpha || 1.0;
props.fillColor = props.fillColor || 0x000000,
props.fillAlpha = props.fillAlpha || 1.0,
props.colors = props.colors || [0x000000, 0xFFFFFF],
props.alphas = props.alphas || [1, 1];
}
return props as Object;
}
/**************************
SHAPES
**************************/
// Draw a basic rectangle.
public static function drawRect (props :Object) :Sprite {
var temp :Sprite = new Sprite();
props = 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;
return temp as Sprite;
}
// Draw a rectangle with a gradient
public static function drawGradRect (props :Object) :Sprite {
var temp :Sprite = new Sprite(),
matrix :Matrix = new Matrix();
props = 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;
return temp as Sprite;
}
}
import flash.display.Sprite;
import flash.geom.Matrix;
import flash.filters.*;
internal class ProgressBar extends Sprite {
// Private variables
private var total :Number;
private var progress :Number = 0;
private var _maxWidth:Number;
// Public variables
public var backgroundProps :Object;
public var foregroundProps :Object;
public var bgBar:Sprite;
public var fgBar:Sprite;
public function ProgressBar (bgProps:Object, fgProps:Object, progress:Number = 0, percentage:int = 100) {
// Replace the received Object's props with Defaults
// if they are not supplied.
bgProps = GUI.replaceObjectWithDefaults(bgProps, "shape");
fgProps = GUI.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;
_maxWidth = fgProps.width;
// Draw both the foreground and the background
// then set the given props as our class
// props... If that makes any sense. ;)
bgBar = GUI.drawRect(bgProps);
backgroundProps = bgProps;
fgBar = GUI.drawRect(fgProps);
foregroundProps = fgProps;
// Add the bars to our extendee
addChild(bgBar);
bgBar.addChild(fgBar);
fgBar.width = 0;
// ... Then set the progress if we are given it,
// and if not, set it to 0;
setPercentage(percentage);
setProgress(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(progress);
}
}
}
public function setProgress (newProgress:Number):void {
newProgress = Math.max(0, newProgress);
newProgress = Math.min(newProgress, 100);
fgBar.width = _maxWidth * (newProgress / 100);
}
public function getProgress ():Number {
return progress;
}
}