1  /**
     2   * com.sekati.draw.RoundRectangle
     3   * @version 1.0.5
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   */
     8  
     9  import com.sekati.geom.Point;
    10  
    11  /**
    12   * Rounded rectangle drawing utility.
    13   */
    14  class com.sekati.draw.RoundRectangle {
    15  
    16  	/**
    17  	 * Draw a rectangle in an existing clip
    18  	 * @param mc (Movie	Clip) target clip to draw in
    19  	 * @param topLeft (Point)
    20  	 * @param bottomRight (Point)
    21  	 * @param fillColor (Number) hex fill, if undefined rectangle will not be filled
    22  	 * @param fillAlpha (Number) fill alpha transparency [default: 100]
    23  	 * @param strokeWeight (Number) border line width, if undefined no border will be drawn
    24  	 * @param strokeColor (Number) hex border color 
    25  	 * @param strokeAlpha (Number) stroke alpha transparancy [default: 100]
    26  	 * @return Void
    27  	 * {@code Usage:
    28  	 * 	var box:MovieClip = this.createEmptyMovieClip ("box", this.getNextHighestDepth ());
    29  	 * 	RoundRectangle.draw(rbox, new Point(150, 150), new Point(250, 250), 12, 0xff00ff, 100, 1, 0x00fffff, 100);
    30  	 * }
    31  	 */
    32  	public static function draw(mc:MovieClip, topLeft:Point, bottomRight:Point, cornerRadius:Number, fillColor:Number, fillAlpha:Number, strokeWeight:Number, strokeColor:Number, strokeAlpha:Number):Void {
    33  		var sw:Number = (!strokeWeight) ? undefined : strokeWeight;
    34  		var sc:Number = (isNaN( strokeColor )) ? 0x000000 : strokeColor;
    35  		var sa:Number = (isNaN( strokeAlpha )) ? 100 : strokeAlpha;
    36  		var fa:Number = (isNaN( fillAlpha )) ? 100 : fillAlpha;
    37  
    38  		var x:Number = topLeft.x;
    39  		var y:Number = topLeft.y;
    40  		var w:Number = bottomRight.x - topLeft.x;
    41  		var h:Number = bottomRight.y - topLeft.y;
    42  
    43  		var theta:Number = Math.PI / 4;  
    44  		// 45 degrees in radians
    45  		var angle:Number = -Math.PI / 2; 
    46  		// 90 degrees in radians
    47  		
    48  		// w + h are larger than 2*cornerRadius
    49  		cornerRadius = (cornerRadius > Math.min( w, h ) / 2) ? cornerRadius = Math.min( w, h ) / 2 : cornerRadius;
    50  		
    51  		mc.clear( );
    52  		mc.lineStyle( sw, sc, sa, true, "none", "round", "round", 8 );
    53  		if (!isNaN( fillColor )) {
    54  			mc.beginFill( fillColor, fa );
    55  		}
    56  
    57  		// draw top line
    58  		mc.moveTo( x + cornerRadius, y );
    59  		mc.lineTo( x + w - cornerRadius, y );
    60  		
    61  		for(var i:Number = 0; i < 8 ; i++) {
    62  			var calcul:Number, cx:Number, cy:Number, px:Number, py:Number;
    63  			
    64  			calcul = Math.cos( angle + (theta / 2) ) * cornerRadius / Math.cos( theta / 2 );
    65  			cx = (i == 0 || i == 1 || i == 2 || i == 3) ? x + w - cornerRadius + calcul : x + cornerRadius + calcul;
    66  			
    67  			calcul = Math.sin( angle + (theta / 2) ) * cornerRadius / Math.cos( theta / 2 );
    68  			cy = (i == 0 || i == 1 || i == 6 || i == 7) ? y + cornerRadius + calcul : y + h - cornerRadius + calcul;
    69  			
    70  			calcul = Math.cos( angle + theta ) * cornerRadius;
    71  			px = (i == 0 || i == 1 || i == 2 || i == 3) ? x + w - cornerRadius + calcul : x + cornerRadius + calcul;
    72  			
    73  			calcul = Math.sin( angle + theta ) * cornerRadius;
    74  			py = (i == 0 || i == 1 || i == 6 || i == 7) ? y + cornerRadius + calcul : y + h - cornerRadius + calcul;
    75  			
    76  			mc.curveTo( cx, cy, px, py );
    77  			
    78  			//straight lines
    79  			if(i == 1) mc.lineTo( x + w, y + h - cornerRadius );
    80  			if(i == 3) mc.lineTo( x + cornerRadius, y + h );
    81  			if(i == 5) mc.lineTo( x, y + cornerRadius );
    82  			
    83  			angle += theta;
    84  		}
    85  		if (!isNaN( fillColor )) {
    86  			mc.endFill( );
    87  		}
    88  	}
    89  
    90  	private function RoundRectangle() {
    91  	}
    92  }