DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Actionscript _Draw Class

02.13.2007
| 4571 views |
  • submit to reddit
        These still need tweaking, but essentially I just wanted a repeatable way to draw boxes via script.  This includes the ability to add rounded corners, gradients, and drop shadows (could it really be any other way? :)  The Balloon function was to fulfill a specific goal, obviously, but since these things are more and more popular I figured it would be a good idea to have it somewhat configurable for future usage.  Right now, for drop shadows you just have to make two boxes, one blurry, one not.  Perhaps it would be better to consolidate the balloon script into the box script, but for now, this works.

import flash.filters.BlurFilter;
dynamic class _Draw {
	
	static function box(targetMC,targetDepth,w,h,c,r,gradientType){
		var targetDepth = (targetDepth != undefined && targetDepth != null) ? targetDepth : targetMC.getNextHighestDepth();
		var newBox:MovieClip = targetMC.createEmptyMovieClip("newBox", targetDepth);
		var g:Object = new Object();
		
		if (c.length != undefined){
			g = getGradientObject(w,h,gradientType);
			newBox.beginGradientFill(g.fillType, c, g.alphas, g.ratios, g.matrix, g.spreadMethod, g.interpolationMethod, g.focalPointRatio);
		} else {
			newBox.beginFill(c);
		}
		
		if (r == undefined) var r = 0;
		
		with (newBox){
			lineStyle(0,0x000000,0);
			moveTo(r, 0);
			lineTo(w-r, 0);
			curveTo(w,0,w,r);
			lineTo(w, h-r);
			curveTo(w,h,w-r,h);			
			lineTo(r, h);
			curveTo(0,h,0,h-r);
			lineTo(0, r);
			curveTo(0,0,r,0);
			endFill();
		}
		
		return newBox;
	}

	static function balloon(targetMC,targetDepth,w,h,c,r,gradientType,arrowBaseOffset,arrowBaseWidth,arrowLength,arrowPointOffset){
		var targetDepth = (targetDepth != undefined && targetDepth != null) ? targetDepth : targetMC.getNextHighestDepth();
		var newBalloon:MovieClip = targetMC.createEmptyMovieClip("newBalloon", targetDepth);
		var g:Object = new Object();
		
		if (c.length != undefined){
			g = getGradientObject(w,h,gradientType);
			newBalloon.beginGradientFill(g.fillType, c, g.alphas, g.ratios, g.matrix, g.spreadMethod, g.interpolationMethod, g.focalPointRatio);
		} else {
			newBalloon.beginFill(c);
		}
		
		if (r == undefined) var r = 0;
		
		with (newBalloon){
			lineStyle(0,0x000000,0);
			moveTo(r, 0);
			lineTo(w-r, 0);
			curveTo(w,0,w,r);
			lineTo(w, h-r);
			curveTo(w,h,w-r,h);
			
			// start arrow			
			lineTo(r + arrowBaseOffset + arrowBaseWidth, h);
			lineTo(r + arrowBaseOffset + (arrowBaseWidth/2) + arrowPointOffset, h + arrowLength);
			lineTo(r + arrowBaseOffset, h);
			// end arrow
			
			lineTo(r, h);
			curveTo(0,h,0,h-r);
			lineTo(0, r);
			curveTo(0,0,r,0);
			endFill();
		}
		
		return newBalloon;
	}

	static function getGradientObject(w,h,gradientType){
		var gradientObj:Object = new Object();
		
		gradientObj.fillType = "linear"
		gradientObj.alphas = [100, 100];
		gradientObj.ratios = [0, 0xFF];
		gradientObj.spreadMethod = "pad";
		gradientObj.interpolationMethod = "RGB";
		gradientObj.focalPointRatio = 1;
		
		if (gradientType == "vGradient") {
			gradientObj.matrix = {a:0, b:w, c:0, d:-h, e:0, f:0, g:0,h:h/2, i:1};
		} else {
			gradientObj.matrix = {a:w, b:0, c:0, d:0, e:w, f:0, g:w/2, h:0, i:1};
		}
		
		return gradientObj;
	}
	
	static function blur(mc,blurAmount,blurOpacity){
		blurAmount = (blurAmount == undefined) ? 10 : blurAmount;
		blurOpacity = (blurOpacity == undefined) ? 100 : blurOpacity;
		var filter:BlurFilter = new BlurFilter(blurAmount, blurAmount, 2);
		var filterArray:Array = new Array();
		filterArray.push(filter);
		mc.filters = filterArray;
		mc._alpha = blurOpacity
	}
}