From: Richard K. <ric...@us...> - 2005-05-20 19:59:26
|
Update of /cvsroot/actionstep/actionstep/src/org/actionstep In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16331 Modified Files: ASTheme.as ASDraw.as Log Message: Updated to use new advanced gradient methods for drawing buttons and textfields Index: ASTheme.as =================================================================== RCS file: /cvsroot/actionstep/actionstep/src/org/actionstep/ASTheme.as,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** ASTheme.as 18 May 2005 22:08:00 -0000 1.6 --- ASTheme.as 20 May 2005 19:59:09 -0000 1.7 *************** *** 48,111 **** private static var g_current:ASThemeProtocol; ! private static var g_colorSets = { ! buttonUp: { ! top: 0x82858E, ! top1: 0xDFE2E9, ! top2: 0xEEF2F5, ! top3: 0xD4D8D9, ! bottom: 0xD3D6DB, ! bottom1: 0x858992, ! bottom2: 0x9599A2, ! bottom3: 0xACB1B7, ! left: 0x82858E, ! left1: 0xDFE2E9, ! right: 0xD3D6DB, ! right1: 0x858992, ! middle: 0xC7CAD1}, ! buttonDown: { ! top: 0x6A6E79, ! top1: 0x747883, ! top2: 0x7C838B, ! top3: 0x939AA2, ! bottom: 0xBEC1C6, ! bottom1: 0xCACED1, ! bottom2: 0xDADDE2, ! bottom3: 0xBEC1C6, ! left: 0x6A6E79, ! left1: 0x747883, ! right: 0xBEC1C6, ! right1: 0xCACED1, ! middle: 0xB0B4BD}, ! buttonDisabled: { ! top: 0x81858E, ! top1: 0x858992, ! top2: 0x9599A2, ! top3: 0xABAFB6, ! bottom: 0xD3D6DB, ! bottom1: 0xDDE0E4, ! bottom2: 0xEFF2F6, ! bottom3: 0xD2D5DA, ! left: 0x82858C, ! left1: undefined, ! right: 0x82858A, ! right1: 0xE9ECF1, ! middle: 0xC7CACF}, ! textfield: { ! top: 0x81858E, ! top1: 0x858992, ! top2: 0x9599A2, ! top3: 0xABAFB6, ! bottom: 0xD3D6DB, ! bottom1: 0xDDE0E4, ! bottom2: 0xEFF2F6, ! bottom3: 0xD2D5DA, ! left: 0x82858C, ! left1: undefined, ! right: 0x82858A, ! right1: 0xE9ECF1, ! middle: 0xC7CACF} ! }; - /** * Constructs a new instance of ASTheme. --- 48,56 ---- private static var g_current:ASThemeProtocol; ! static var ANGLE_LEFT_TO_RIGHT = 0; ! static var ANGLE_TOP_TO_BOTTOM = 90; ! static var ANGLE_RIGHT_TO_LEFT = 180; ! static var ANGLE_BOTTOM_TO_TOP = 270; /** * Constructs a new instance of ASTheme. *************** *** 132,183 **** public function drawBorderButtonWithRectInView(rect:NSRect, view:NSView) { ! drawBorderButtonWithRectColorsetInView(rect, "buttonUp", view); } public function drawBorderButtonDisabledWithRectInView(rect:NSRect, view:NSView) { ! drawBorderButtonWithRectColorsetInView(rect, "buttonDisabled", view); } public function drawBezelButtonUpWithRectInView(rect:NSRect, view:NSView) { ! drawBezelButtonWithRectColorsetInView(rect, "buttonUp", view); } public function drawBezelButtonDownWithRectInView(rect:NSRect, view:NSView) { ! drawBezelButtonWithRectColorsetInView(rect, "buttonDown", view); } public function drawBezelButtonDisabledWithRectInView(rect:NSRect, view:NSView) { ! drawBezelButtonWithRectColorsetInView(rect, "buttonDisabled", view); } public function drawTextFieldWithRectInView(rect:NSRect, view:NSView) { ! var x = rect.origin.x; ! var y = rect.origin.y; ! var width = rect.size.width-1; ! var height = rect.size.height-1; ! var mc = view.mcBounds(); ! var colors = g_colorSets["textfield"]; ! ! ASDraw.drawFill(mc, colors.middle, 0, 0, width, height); ! ! //draw top lines ! ASDraw.drawHLine(mc, colors.top , x, width, y); ! ASDraw.drawHLine(mc, colors.top1, x, width, y+1); ! ASDraw.drawHLine(mc, colors.top2, x, width, y+2); ! ASDraw.drawHLine(mc, colors.top3, x, width, y+3); ! ! //draw bottom lines ! ASDraw.drawHLine(mc, colors.bottom , x, width, height ); ! ASDraw.drawHLine(mc, colors.bottom1, x, width, height-1); ! ASDraw.drawHLine(mc, colors.bottom2, x, width, height-2); ! ASDraw.drawHLine(mc, colors.bottom3, x, width, height-3); ! ! //draw left lines ! ASDraw.drawVLine(mc, colors.left , y, height, x ); ! ASDraw.drawVLineEdgeFade(mc, colors.left1, y, height, x+1, 5); ! ! //draw right lines ! ASDraw.drawVLine(mc, colors.right , y, height, width ); ! ASDraw.drawVLineEdgeFade(mc, colors.right1, y, height, width-1, 5); } --- 77,101 ---- public function drawBorderButtonWithRectInView(rect:NSRect, view:NSView) { ! drawBorderButtonUp(view.mcBounds(), rect); } public function drawBorderButtonDisabledWithRectInView(rect:NSRect, view:NSView) { ! drawBorderButtonDown(view.mcBounds(), rect); } public function drawBezelButtonUpWithRectInView(rect:NSRect, view:NSView) { ! drawButtonUp(view.mcBounds(), rect); } public function drawBezelButtonDownWithRectInView(rect:NSRect, view:NSView) { ! drawButtonDown(view.mcBounds(), rect); } public function drawBezelButtonDisabledWithRectInView(rect:NSRect, view:NSView) { ! drawButtonDown(view.mcBounds(), rect); } public function drawTextFieldWithRectInView(rect:NSRect, view:NSView) { ! drawTextfield(view.mcBounds(), rect); } *************** *** 187,190 **** --- 105,118 ---- setImage("NSSwitch", org.actionstep.images.ASSwitchRep); setImage("NSHighlightedSwitch", org.actionstep.images.ASHighlightedSwitchRep); + + setImage("NSScrollerUpArrow", org.actionstep.images.ASScrollerUpArrowRep); + setImage("NSHighlightedScrollerUpArrow", org.actionstep.images.ASHighlightedScrollerUpArrowRep); + setImage("NSScrollerDownArrow", org.actionstep.images.ASScrollerDownArrowRep); + setImage("NSHighlightedScrollerDownArrow", org.actionstep.images.ASHighlightedScrollerDownArrowRep); + + setImage("NSScrollerLeftArrow", org.actionstep.images.ASScrollerLeftArrowRep); + setImage("NSHighlightedScrollerLeftArrow", org.actionstep.images.ASHighlightedScrollerLeftArrowRep); + setImage("NSScrollerRightArrow", org.actionstep.images.ASScrollerRightArrowRep); + setImage("NSHighlightedScrollerRightArrow", org.actionstep.images.ASHighlightedScrollerRightArrowRep); } *************** *** 213,256 **** } ! private function drawBorderButtonWithRectColorsetInView(rect:NSRect, colorSet:String, view:NSView) { var x = rect.origin.x; var y = rect.origin.y; var width = rect.size.width-1; var height = rect.size.height-1; ! var mc = view.mcBounds(); ! var colors = g_colorSets[colorSet]; ! ASDraw.drawFill(mc, colors.middle, x, y, width, height); ASDraw.drawRect(mc, 1, 0, x, y, width, height); } ! ! private function drawBezelButtonWithRectColorsetInView(rect:NSRect, colorSet:String, view:NSView) { var x = rect.origin.x; var y = rect.origin.y; var width = rect.size.width-1; var height = rect.size.height-1; ! var mc = view.mcBounds(); ! var colors = g_colorSets[colorSet]; ! ! //draw top lines ! ASDraw.drawFill(mc, colors.middle, x, y, width, height); ! ASDraw.drawHLine(mc, colors.top , x, x+width, y); ! ASDraw.drawHLine(mc, colors.top1, x, x+width, y+1); ! ASDraw.drawHLine(mc, colors.top2, x, x+width, y+2); ! ASDraw.drawHLine(mc, colors.top3, x, x+width, y+3); ! //draw bottom lines ! ASDraw.drawHLine(mc, colors.bottom , x, x+width, y+height ); ! ASDraw.drawHLine(mc, colors.bottom1, x, x+width, y+height-1); ! ASDraw.drawHLine(mc, colors.bottom2, x, x+width, y+height-2); ! ASDraw.drawHLine(mc, colors.bottom3, x, x+width, y+height-3); ! //draw left lines ! ASDraw.drawVLine(mc, colors.left , y, y+height, x ); ! ASDraw.drawVLineEdgeFade(mc, colors.left1, y, y+height, x+1, 5); ! //draw right lines ! ASDraw.drawVLine(mc, colors.right , y, y+height, x+width ); ! ASDraw.drawVLineEdgeFade(mc, colors.right1, y, y+height, x+width-1, 5); ! } //****************************************************** --- 141,219 ---- } ! private function drawBorderButtonUp(mc:MovieClip, rect:NSRect) { var x = rect.origin.x; var y = rect.origin.y; var width = rect.size.width-1; var height = rect.size.height-1; ! ASDraw.drawFill(mc, 0xC7CAD1, x, y, width, height); ASDraw.drawRect(mc, 1, 0, x, y, width, height); } ! ! private function drawBorderButtonDown(mc:MovieClip, rect:NSRect) { var x = rect.origin.x; var y = rect.origin.y; var width = rect.size.width-1; var height = rect.size.height-1; ! ASDraw.drawFill(mc, 0xB1B5BC, x, y, width, height); ! ASDraw.drawRect(mc, 1, 0, x, y, width, height); ! } ! ! /////////////////////////////// ! // BUTTON DRAW FUNCTIONS ! private static var drawButtonUp_outlineColors:Array = [0x82858E, 0xD3D6DB]; ! private static var drawButtonUp_inlineColors:Array = [0xDFE2E9, 0x858992]; ! private static var drawButtonUp_colors:Array = [0xEEF2F5, 0xC7CAD1, 0xC7CAD1, 0x858992]; ! private static var drawButtonUp_ratios:Array = [ 1, 5, 23, 26]; ! private function drawButtonUp(mc:MovieClip, rect:NSRect) ! { ! drawButtonUpWithoutBorder(mc, rect.insetRect(1,1)); ! ASDraw.outlineRectWithRect( mc, rect, drawButtonUp_outlineColors); ! } ! private function drawButtonUpWithoutBorder(mc:MovieClip, rect:NSRect) ! { ! ASDraw.gradientRectWithRect(mc, rect, ANGLE_TOP_TO_BOTTOM, drawButtonUp_colors, drawButtonUp_ratios); ! ASDraw.outlineRectWithRect( mc, rect, drawButtonUp_inlineColors); ! } ! private static var drawButtonDown_outlineColors:Array = [0x82858E, 0xECEDF0]; ! private static var drawButtonDown_inlineColors:Array = [0x696F79, 0xD4D6DB]; ! private static var drawButtonDown_colors:Array = [0x696F79, 0xB1B5BC, 0xB1B5BC, 0xD9DBDF, 0xC9CDD2]; ! private static var drawButtonDown_ratios:Array = [ 1, 5, 23, 25, 26]; ! private function drawButtonDown(mc:MovieClip, rect:NSRect) ! { ! drawButtonDownWithoutBorder(mc, rect.insetRect(1,1)); ! ASDraw.outlineRectWithRect( mc, rect, drawButtonDown_outlineColors); ! } ! ! private function drawButtonDownWithoutBorder(mc:MovieClip, rect:NSRect) ! { ! ASDraw.gradientRectWithRect(mc, rect, ANGLE_TOP_TO_BOTTOM, drawButtonDown_colors, drawButtonDown_ratios); ! ASDraw.outlineRectWithRect( mc, rect, drawButtonDown_inlineColors); ! } ! ! ! // END BUTTON DRAW FUNCTIONS ! /////////////////////////////// ! ! /////////////////////////////// ! // TEXTFIELD DRAW FUNCTIONS ! private static var drawTextfield_outlineColors:Array = [0x4B4F57, 0xDEE1E6]; ! private static var drawTextfield_colors:Array = [0x80848F, 0xAFB4BA, 0xCACDD2]; ! private static var drawTextfield_ratios:Array = [ 0, 6, 24]; ! private static var drawTextfieldShadow_colors:Array = [0x767A85, 0xB6BBC1]; ! private static var drawTextfieldShadow_alphas:Array = [ 100, 0]; ! private static var drawTextfieldShadow_ratios:Array = [ 0, 5]; ! private function drawTextfield(mc:MovieClip, rect:NSRect) ! { ! var insetRect:NSRect = rect.insetRect(1,1); ! ASDraw.gradientRectWithRect( mc, rect, ANGLE_TOP_TO_BOTTOM, drawTextfield_colors, drawTextfield_ratios); ! ASDraw.gradientRectWithAlphaRect(mc, new NSRect(rect.origin.x, rect.origin.y, 5, rect.size.height), 30, ! drawTextfieldShadow_colors, drawTextfieldShadow_ratios, drawTextfieldShadow_alphas); ! ASDraw.outlineRectWithRect( mc, rect, drawTextfield_outlineColors); ! } ! // END TEXTFIELD DRAW FUNCTIONS ! /////////////////////////////// //****************************************************** Index: ASDraw.as =================================================================== RCS file: /cvsroot/actionstep/actionstep/src/org/actionstep/ASDraw.as,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** ASDraw.as 17 May 2005 03:23:38 -0000 1.5 --- ASDraw.as 20 May 2005 19:59:09 -0000 1.6 *************** *** 29,37 **** */ ! class org.actionstep.ASDraw { //draws a horizontal line with thickness=1. public static function drawHLine(mc:MovieClip, lineColor:Number, x1:Number, x2:Number, y1:Number) { ! drawLine(mc, lineColor, x1, x2, y1, y1); } --- 29,40 ---- */ ! import org.actionstep.NSRect; ! import org.actionstep.NSPoint; ! ! class org.actionstep.ASDraw { //draws a horizontal line with thickness=1. public static function drawHLine(mc:MovieClip, lineColor:Number, x1:Number, x2:Number, y1:Number) { ! drawLineSimple(mc, lineColor, x1, x2, y1, y1); } *************** *** 44,48 **** } y2 += 1; ! drawLine(mc, lineColor, x1, x1, y1, y2); } --- 47,51 ---- } y2 += 1; ! drawLineSimple(mc, lineColor, x1, x1, y1, y2); } *************** *** 55,59 **** } y2 += 1; ! drawLine(mc, lineColor, x1, x1, y1+edge, y2-edge); drawLineFade(mc, lineColor, x1, x1, y1+edge, y1); drawLineFade(mc, lineColor, x1, x1, y2-edge, y2); --- 58,62 ---- } y2 += 1; ! drawLineSimple(mc, lineColor, x1, x1, y1+edge, y2-edge); drawLineFade(mc, lineColor, x1, x1, y1+edge, y1); drawLineFade(mc, lineColor, x1, x1, y2-edge, y2); *************** *** 68,72 **** } x2 += 1; ! drawLine(mc, lineColor, x1+edge, x2-edge, y1, y1); drawLineFade(mc, lineColor, x1+edge, x1, y1, y1); drawLineFade(mc, lineColor, x2-edge, x2, y1, y1); --- 71,75 ---- } x2 += 1; ! drawLineSimple(mc, lineColor, x1+edge, x2-edge, y1, y1); drawLineFade(mc, lineColor, x1+edge, x1, y1, y1); drawLineFade(mc, lineColor, x2-edge, x2, y1, y1); *************** *** 85,89 **** } ! public static function drawLine(mc:MovieClip, lineColor:Number, x1:Number, x2:Number, y1:Number, y2:Number) { drawLineShared(mc, lineColor, false, x1, x2, y1, y2); } --- 88,92 ---- } ! public static function drawLineSimple(mc:MovieClip, lineColor:Number, x1:Number, x2:Number, y1:Number, y2:Number) { drawLineShared(mc, lineColor, false, x1, x2, y1, y2); } *************** *** 359,383 **** } - // ----------- GRADIENT RECTANGLE ----------- // - - public static function gradientRect(mc:MovieClip, thick:Number, color:Number, x1:Number, y1:Number, width:Number, height:Number, - col1:Number,col2:Number,fa1:Number,fa2:Number, - matrixX:Number,matrixY:Number,matrixW:Number, - matrixH:Number){ - mc.lineStyle(thick,color); - var colors:Array = [col1 ,col2]; - var alphas:Array = [ fa1, fa2 ]; - var ratios:Array = [ 7, 0xFF ]; - var matrix:Object = { matrixType:"box", x:matrixX, y:matrixY, - w:matrixW, h:matrixH, r:(45/180)*Math.PI }; - mc.moveTo(x1,y1); - mc.beginGradientFill( "linear", colors, alphas, ratios, matrix ); - mc.lineTo(x1+width, y1); - mc.lineTo(x1+width, y1+height); - mc.lineTo(x1, y1+height); - mc.lineTo(x1, y1); - mc.endFill(); - } - // ----------- DRAW HEXAGON ---------- // --- 362,365 ---- *************** *** 607,609 **** --- 589,997 ---- return Math.atan2( base, height ) } + + /** + * The following methods are the new draw methods. + * The methods above this are being phased out. + */ + + /////////////////////////////////////////// + // BASIC DRAWING METHODS + + //POINTS + + //TODO find out if there is a way to draw a pixel other than doing a 1 pixel solid fill + //TODO also find out if there is a way to get the effing rectangle to draw its own bottom-right corner. + public static function drawPoint(mc:MovieClip, x:Number, y:Number, color:Number){ + solidRect(mc, x, y, 1, 1, color); + } + + public static function drawPointWithAlpha(mc:MovieClip, x:Number, y:Number, color:Number, alpha:Number){ + solidRectWithAlpha(mc, x, y, 1, 1, color, alpha); + } + + static var DEFAULT_LINE_THICKNESS = 1; + static var DEFAULT_ALPHA = 100; + + //LINES + + public static function drawLine(mc:MovieClip, startX:Number, startY:Number, endX:Number, endY:Number, color:Number){ + drawLineWithAlpha(mc, startX, startY, endX, endY, color, DEFAULT_ALPHA); + } + + public static function drawLineWithAlpha(mc:MovieClip, startX:Number, startY:Number, endX:Number, endY:Number, color:Number, alpha:Number){ + mc.lineStyle(DEFAULT_LINE_THICKNESS, color, alpha); + mc.moveTo( startX, startY); + mc.lineTo( endX, endY); + } + + public static function drawLineWithPoint(mc:MovieClip, start:NSPoint, end:NSPoint, color:Number){ + drawLineWithAlpha(mc, start.x, start.y, end.x, end.y, color, DEFAULT_ALPHA); + } + + public static function drawLineWithAlphaPoint(mc:MovieClip, start:NSPoint, end:NSPoint, color:Number, alpha:Number){ + drawLineWithAlpha(mc, start.x, start.y, end.x, end.y, color, alpha); + } + + // END BASIC DRAWING METHODS + /////////////////////////////////////////// + + + /////////////////////////////////////////// + // OUTLINE RECT METHODS + public static function outlineRectWithRect(mc:MovieClip, rect:NSRect, colors:Array, ratios:Array){ + outlineRect(mc, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, colors); + } + + public static function outlineRectWithAlphaRect(mc:MovieClip, rect:NSRect, colors:Array, alphas:Array){ + outlineRectWithAlpha(mc, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, colors, alphas); + } + + public static function outlineRect(mc:MovieClip, x:Number, y:Number, width:Number, height:Number, colors:Array){ + var alphas:Array = buildArray(colors.length, 100); + outlineRectWithAlpha(mc, x, y, width, height, colors, alphas); + } + + public static function outlineRectWithAlpha(mc:MovieClip, x:Number, y:Number, width:Number, height:Number, colors:Array, alphas:Array){ + // mc.beginGradientFill("linear",colors,alphas,actualRatios,matrix); + + colors = getArrayOfFour(colors); + alphas = getArrayOfFour(alphas); + + //change width and height so that the total width/height, including line thickness is the given width/height. + var x2 = x + width -1; + var y2 = y + height -1; + var lineThickness = 1; + + mc.lineStyle(lineThickness, colors[0], alphas[0]); + mc.moveTo( x, y); + mc.lineTo(x2, y); + + //TODO Why the eff won't the bottom right pixel draw? + mc.lineStyle(lineThickness, colors[1], alphas[1]); + mc.lineTo(x2, y2); + + + mc.lineStyle(lineThickness, colors[2], alphas[2]); + mc.lineTo( x, y2); + + mc.lineStyle(lineThickness, colors[3], alphas[3]); + mc.lineTo( x, y); + + // need to draw the bottom right pixel separately for some reason. + // I can't get any line to draw there, so I'm using the 'pixel' method, which is actually a fill. + + //TRACE("DRAW POINT: x2=" + x2 + ", y2=" + y2 + ", color=" + colors[1] + ", alpha=" + alphas[1]); + + drawPointWithAlpha(mc, x2, y2, colors[1], alphas[1]); + + // mc.endFill(); + } + // END OUTLINE RECT METHODS + /////////////////////////////////////////// + + + /////////////////////////////////////////// + // OUTLINE RECT METHODS + public static function solidRectWithRect(mc:MovieClip, rect:NSRect, color:Number){ + solidRect(mc, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, color); + } + + public static function solidRectWithAlphaRect(mc:MovieClip, rect:NSRect, color:Number, alpha:Number){ + solidRectWithAlpha(mc, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, color, alpha); + } + + public static function solidRect(mc:MovieClip, x:Number, y:Number, width:Number, height:Number, color:Number){ + var alpha:Number = 100; + solidRectWithAlpha(mc, x, y, width, height, color, alpha); + } + + public static function solidRectWithAlpha(mc:MovieClip, x:Number, y:Number, width:Number, height:Number, color:Number, alpha:Number){ + + //TRACE("SOLID RECT WITH ALPHA: x=" + x + ", y=" + y + ", width=" + width + ", height=" + height + ", color=" + color + ", alpha=" + alpha); + + mc.lineStyle(undefined, 0, 100); + mc.beginFill(color,alpha); + mc.moveTo(x,y); + mc.lineTo(x+width, y); + mc.lineTo(x+width, y+height); + mc.lineTo(x, y+height); + mc.lineTo(x, y); + mc.endFill(); + } + // END SOLID RECT METHODS + /////////////////////////////////////////// + + + /////////////////////////////////////////// + // GRADIENT RECT METHODS + // + //I think I should make versions of these methods with NSrects instead of x,y,w,h as well. + //for now naming it 'WithRect' to imply that the gradient should also be a rect + //TODO decide on a documentation format for these methods. + //currently using WITH to specify additional params, and then listing params alphabetically. + //Maybe that should be the order as well? + //-gradientRectWithAlpha + //this one uses no outline since they are rarely a single color in our skins. + //this one will also uses no alpha just to keep things simple. for now it's in. + //this one makes the matrix the same size as the rect, again to be simple. + //this one takes an angel, a color array and a ratio array to keep things flexible. + // + public static function gradientRectWithRect(mc:MovieClip, rect:NSRect, angle:Number, colors:Array, ratios:Array){ + gradientRect(mc, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, angle, colors, ratios); + } + + public static function gradientRectWithAlphaRect(mc:MovieClip, rect:NSRect, angle:Number, colors:Array, ratios:Array, alphas:Array){ + gradientRectWithAlpha(mc, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, angle, colors, ratios, alphas); + } + + public static function gradientRect(mc:MovieClip, x:Number, y:Number, width:Number, height:Number, + angle:Number, colors:Array, ratios:Array){ + var alphas:Array = buildArray(colors.length, 100); + gradientRectWithAlpha(mc, x, y, width, height, angle, colors, ratios, alphas); + } + + public static function gradientRectWithAlpha(mc:MovieClip, x:Number, y:Number, width:Number, height:Number, + angle:Number, colors:Array, ratios:Array, alphas:Array){ + var radians:Number = getRadiansFromAngle(angle); + var matrix = getMatrix(new NSRect(x, y, width, height), angle); + var actualRatios:Array = getActualRatios(ratios); + + mc.lineStyle(undefined, 0, 100); + mc.beginGradientFill("linear",colors,alphas,actualRatios,matrix); + mc.moveTo(x,y); + mc.lineTo(x+width, y); + mc.lineTo(x+width, y+height); + mc.lineTo(x, y+height); + mc.lineTo(x, y); + mc.endFill(); + } + // END GRADIENT RECT METHODS + /////////////////////////////////////////// + + + /////////////////////////////////////////// + // DRAW ELLIPSE METHODS + // + public static function gradientEllipseWithRect(mc:MovieClip, rect:NSRect, colors:Array, ratios:Array){ + gradientEllipse(mc, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, colors, ratios); + } + + public static function gradientEllipseWithAlphaRect(mc:MovieClip, rect:NSRect, colors:Array, ratios:Array, alphas:Array){ + gradientEllipseWithAlpha(mc, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, colors, ratios, alphas); + } + + public static function gradientEllipse(mc:MovieClip, x:Number, y:Number, width:Number, height:Number, + colors:Array, ratios:Array){ + var alphas:Array = buildArray(colors.length, 100); + gradientEllipseWithAlpha(mc, x, y, width, height, colors, ratios, alphas); + } + + //top-left with diameters. not center with radii. + public static function gradientEllipseWithAlpha(mc:MovieClip, x:Number, y:Number, width:Number, height:Number, + colors:Array, ratios:Array, alphas:Array){ + var rect = new NSRect(x, y, width, height); + var matrix = getMatrix(rect); + var actualRatios:Array = getActualRatios(ratios); + + mc.lineStyle(undefined, 0, 100); + mc.beginGradientFill("radial",colors,alphas,actualRatios,matrix); + drawEllipse(mc, x, y, width, height); + mc.endFill(); + } + + //taken from AcionScript cookbook's DrawingMethods.as (modified the param order) + //the original used center origin and radius. this one uses top-left origin and width/height. + public static function drawEllipse(mc:MovieClip, xTopLeft, yTopLeft, width, height) { + var radiusRect = getRadiusRect(new NSRect(xTopLeft, yTopLeft, width, height)); + var x = radiusRect.origin.x; + var y = radiusRect.origin.y; + var xRadius = radiusRect.size.width; + var yRadius = radiusRect.size.height; + var angleDelta = Math.PI / 4; + + // TRACE("xTopLeft[" + xTopLeft + "] yTopLeft[" + yTopLeft + "] width[" + width + "] height[" + height + "]"); + // TRACE(" x[" + x + "] y[" + y + "] width[" + xRadius + "] height[" + yRadius + "]"); + + // Whereas the circle has only one distance to the control point + // for each segment, the ellipse has two distances: one that + // corresponds to xRadius and another that corresponds to yRadius. + var xCtrlDist = xRadius/Math.cos(angleDelta/2); + var yCtrlDist = yRadius/Math.cos(angleDelta/2); + var rx, ry, ax, ay; + mc.moveTo(x + xRadius, y); + var angle = 0; + for (var i = 0; i < 8; i++) { + angle += angleDelta; + rx = x + Math.cos(angle-(angleDelta/2))*(xCtrlDist); + ry = y + Math.sin(angle-(angleDelta/2))*(yCtrlDist); + ax = x + Math.cos(angle)*xRadius; + ay = y + Math.sin(angle)*yRadius; + mc.curveTo(rx, ry, ax, ay); + } + } + // END DRAW ELLIPSE METHODS + /////////////////////////////////////////// + + + /////////////////////////////////////////// + // MISC HELPER FUNCTIONS + + // this helper function builds an array of length four from an array of 1 or 2 + // this is helpful for the various rect methods that use different params for the 4 sides. + public static function getArrayOfFour(originalArray:Array){ + + var array:Array = originalArray; + var size = originalArray.length; + if (size == 4){ + //do nothing. this is just to avoid unnecessary checks. + } + else if (size == 1){ + array = [originalArray[0], originalArray[0], originalArray[0], originalArray[0]]; + } + else if (size == 2){ + array = [originalArray[0], originalArray[1], originalArray[1], originalArray[0]]; + } + return array; + } + + // END MISC HELPER FUNCTIONS + /////////////////////////////////////////// + + + /////////////////////////////////////////// + // GRADIENT HELPER FUNCTIONS + + //This method translate an array of numerical values into the ratio format Flash uses for gradients. + //Flash requires a ratio be an array of numbers starting with 0 and ending with 255. + //If those values are already set, then the arry is returned as is, otherwise the numbers are translated to that scale. + //TODO explain this more. + public static function getActualRatios(ratios:Array){ + return getActualNumbers(ratios, 0, 255); + } + + //this actually doesn't make any sense, since alphas don't have to start at 0 and end at 100. + /* + public static function getActualAlphas(alphas:Array){ + return getActualNumbers(alphas, 0, 100); + } + */ + + //This method translate an array of numerical values into the specified format. + //This method is used by other methods that have specific value requirements for Muber Arrays. + public static function getActualNumbers(values:Array, minNumber:Number, maxNumber:Number){ + var size = values.length; + var minValue = values[0]; + var maxValue = values[size-1]; + if (minValue == minNumber && maxValue == maxNumber){ + return values; + } + var actualValues:Array = new Array(); + var value:Number; + var actualValue:Number; + //the idea here is to change the original values to proporionally equivalent values from minValue to maxValue. + //all numbers are adjusted so that the first number starts at minValue and the last in maxValue. + for (var i:Number = 0; i < size; i++){ + value = values[i] - minValue + minNumber; + actualValue = (value/maxValue)*maxNumber; + actualValues.push(actualValue); + //TRACE("actual numbers: original=" + value + " | new=" + actualValue); + } + return actualValues; + } + + public static function buildArray(size:Number, initValue:Number){ + var array:Array = new Array(); + for (var i:Number = 0; i < size; i++){ + array.push(initValue); + } + return array; + } + + public static function getMatrix(rect:NSRect, angle:Number){ + var radians:Number = getRadiansFromAngle(angle); + var matrix = { matrixType:"box", x:rect.origin.x, y:rect.origin.y, w:rect.size.width, h:rect.size.height, r: radians }; + return matrix; + } + + //the rect param is a normal top/left rect, not a radiusRect + public static function getRadialMatrix(rect:NSRect){ + var width = rect.size.width; + var height = rect.size.height; + var matrix = { matrixType:"box", x:-width/2, y:-height/2, w:width, h:height, r: 0 }; + return matrix; + } + + public static function getRadiansFromAngle(angle:Number){ + var radians:Number = angle == 0 ? 0 : (angle/180)*Math.PI; + return radians; + } + + // END GRADIENT HELPER FUNCTIONS + /////////////////////////////////////////// + + /////////////////////////////////////////// + // NSPOINT CONVENIENCE FUNCTIONS + + public static function getOffsetPoint(point:NSPoint, dx:Number, dy:Number):NSPoint { + return new NSPoint(point.x + dx, point.y + dy); + } + + // END NSPOINT CONVENIENCE FUNCTIONS + /////////////////////////////////////////// + + + /////////////////////////////////////////// + // NSRECT CONVENIENCE FUNCTIONS + + public static function getRadiusRect(rect:NSRect){ + var xRadius = rect.size.width/2; + var yRadius = rect.size.height/2; + var x = rect.origin.x + xRadius; + var y = rect.origin.y + yRadius; + return new NSRect(x, y, xRadius, yRadius); + } + + //adds a percent of the width to x + //adds a percent of the height to y + //sets width to a percent of width + //sets height to a percent of height + public static function getScaledPercentRect(rect:NSRect, xPercent:Number, yPercent:Number, widthPercent:Number, heightPercent:Number) + { + var x = rect.origin.x; + var y = rect.origin.y; + var width = rect.size.width; + var height = rect.size.height; + + x = x + width*xPercent*.01; + y = y + height*yPercent*.01; + height = height*widthPercent*.01; + width = width*heightPercent*.01; + + var scaledRect = new NSRect(x, y, width, height); + return scaledRect; + } + + //adds given pixel to x, y, width and height + public static function getScaledPixelRect(rect:NSRect, xPixel:Number, yPixel:Number, widthPixel:Number, heightPixel:Number) + { + var x = rect.origin.x; + var y = rect.origin.y; + var width = rect.size.width; + var height = rect.size.height; + + x = x + xPixel; + y = y + yPixel; + width = width + widthPixel; + height = height + heightPixel; + + var scaledRect = new NSRect(x, y, width, height); + return scaledRect; + } + + // END NSRECT CONVENIENCE FUNCTIONS + /////////////////////////////////////////// + + + + } \ No newline at end of file |