Skip to content

Commit

Permalink
Merge pull request #1263 from MSGhero/dev
Browse files Browse the repository at this point in the history
FlxSpriteUtil curveTo + fixes, etc
  • Loading branch information
Gama11 committed Aug 15, 2014
2 parents cc7cd4f + 49f3746 commit c6aedc4
Showing 1 changed file with 81 additions and 59 deletions.
140 changes: 81 additions & 59 deletions flixel/util/FlxSpriteUtil.hx
Expand Up @@ -249,20 +249,41 @@ class FlxSpriteUtil
public static function drawLine(sprite:FlxSprite, StartX:Float, StartY:Float, EndX:Float, EndY:Float,
?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
{
if (lineStyle == null)
lineStyle = { thickness: 1, color: FlxColor.WHITE };
if (lineStyle.thickness == null)
lineStyle.thickness = 1;
if (lineStyle.color == null)
lineStyle.color = FlxColor.WHITE;

beginDraw(0, lineStyle);
lineStyle = getDefaultLineStyle(lineStyle);
beginDraw(0x0, lineStyle);
flashGfx.moveTo(StartX, StartY);
flashGfx.lineTo(EndX, EndY);
endDraw(sprite, drawStyle);
return sprite;
}

/**
* This function draws a curve on a FlxSprite from position X1,Y1
* to anchor position X2,Y2 using control points X3,Y3 with the specified color.
*
* @param sprite The FlxSprite to manipulate
* @param StartX X coordinate of the curve's start point.
* @param StartY Y coordinate of the curve's start point.
* @param EndX X coordinate of the curve's end/anchor point.
* @param EndY Y coordinate of the curve's end/anchor point.
* @param ControlX X coordinate of the curve's control point.
* @param ControlY Y coordinate of the curve's control point.
* @param FillColor The ARGB color to fill this curve with. FlxColor.TRANSPARENT (0x0) means no fill. Filling a curve draws a line from End to Start to complete the figure.
* @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
* @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
* @return The FlxSprite for chaining
*/
public static function drawCurve(sprite:FlxSprite, StartX:Float, StartY:Float, EndX:Float, EndY:Float, ControlX:Float, ControlY:Float,
FillColor:FlxColor = FlxColor.TRANSPARENT, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
{
lineStyle = getDefaultLineStyle(lineStyle);
beginDraw(FillColor, lineStyle);
flashGfx.moveTo(StartX, StartY);
flashGfx.curveTo(EndX, EndY, ControlX, ControlY);
endDraw(sprite, drawStyle);
return sprite;
}

/**
* This function draws a rectangle on a FlxSprite.
*
Expand All @@ -271,16 +292,15 @@ class FlxSpriteUtil
* @param Y Y coordinate of the rectangle's start point.
* @param Width Width of the rectangle
* @param Height Height of the rectangle
* @param Color The rectangle's color.
* @param FillColor The ARGB color to fill this rectangle with. FlxColor.TRANSPARENT (0x0) means no fill.
* @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
* @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle()
* @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
* @return The FlxSprite for chaining
*/
public static function drawRect(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float, Color:FlxColor,
?lineStyle:LineStyle, ?fillStyle:FillStyle, ?drawStyle:DrawStyle):FlxSprite
public static function drawRect(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float,
FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
{
beginDraw(Color, lineStyle, fillStyle);
beginDraw(FillColor, lineStyle);
flashGfx.drawRect(X, Y, Width, Height);
endDraw(sprite, drawStyle);
return sprite;
Expand All @@ -296,16 +316,15 @@ class FlxSpriteUtil
* @param Height Height of the rectangle
* @param EllipseWidth The width of the ellipse used to draw the rounded corners
* @param EllipseHeight The height of the ellipse used to draw the rounded corners
* @param Color The rectangle's color.
* @param FillColor The ARGB color to fill this rectangle with. FlxColor.TRANSPARENT (0x0) means no fill.
* @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
* @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle()
* @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
* @return The FlxSprite for chaining
*/
public static function drawRoundRect(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float, EllipseWidth:Float,
EllipseHeight:Float, Color:FlxColor, ?lineStyle:LineStyle, ?fillStyle:FillStyle, ?drawStyle:DrawStyle):FlxSprite
EllipseHeight:Float, FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
{
beginDraw(Color, lineStyle, fillStyle);
beginDraw(FillColor, lineStyle);
flashGfx.drawRoundRect(X, Y, Width, Height, EllipseWidth, EllipseHeight);
endDraw(sprite, drawStyle);
return sprite;
Expand All @@ -325,17 +344,16 @@ class FlxSpriteUtil
* @param TopRightRadius The radius of the top right corner of the rectangle
* @param BottomLeftRadius The radius of the bottom left corner of the rectangle
* @param BottomRightRadius The radius of the bottom right corner of the rectangle
* @param Color The rectangle's color.
* @param FillColor The ARGB color to fill this rectangle with. FlxColor.TRANSPARENT (0x0) means no fill.
* @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
* @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle()
* @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
* @return The FlxSprite for chaining
*/
public static function drawRoundRectComplex(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float,
TopLeftRadius:Float, TopRightRadius:Float, BottomLeftRadius:Float, BottomRightRadius:Float, Color:FlxColor,
?lineStyle:LineStyle, ?fillStyle:FillStyle, ?drawStyle:DrawStyle):FlxSprite
TopLeftRadius:Float, TopRightRadius:Float, BottomLeftRadius:Float, BottomRightRadius:Float,
FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
{
beginDraw(Color, lineStyle, fillStyle);
beginDraw(FillColor, lineStyle);
flashGfx.drawRoundRectComplex(X, Y, Width, Height, TopLeftRadius, TopRightRadius, BottomLeftRadius, BottomRightRadius);
endDraw(sprite, drawStyle);
return sprite;
Expand All @@ -349,14 +367,13 @@ class FlxSpriteUtil
* @param X X coordinate of the circle's center (automatically centered on the sprite if -1)
* @param Y Y coordinate of the circle's center (automatically centered on the sprite if -1)
* @param Radius Radius of the circle (makes sure the circle fully fits on the sprite's graphic if < 1, assuming and and y are centered)
* @param Color Color of the circle
* @param FillColor The ARGB color to fill this circle with. FlxColor.TRANSPARENT (0x0) means no fill.
* @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
* @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle()
* @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
* @return The FlxSprite for chaining
*/
public static function drawCircle(sprite:FlxSprite, X:Float = - 1, Y:Float = - 1, Radius:Float = -1,
Color:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?fillStyle:FillStyle, ?drawStyle:DrawStyle):FlxSprite
FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
{
if ((X == -1) || (Y == -1))
{
Expand All @@ -376,7 +393,7 @@ class FlxSpriteUtil
Radius = (minVal / 2);
}

beginDraw(Color, lineStyle, fillStyle);
beginDraw(FillColor, lineStyle);
flashGfx.drawCircle(X, Y, Radius);
endDraw(sprite, drawStyle);
return sprite;
Expand All @@ -390,16 +407,15 @@ class FlxSpriteUtil
* @param Y Y coordinate of the ellipse's start point.
* @param Width Width of the ellipse
* @param Height Height of the ellipse
* @param Color The ellipse's color.
* @param FillColor The ARGB color to fill this ellipse with. FlxColor.TRANSPARENT (0x0) means no fill.
* @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
* @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle()
* @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
* @return The FlxSprite for chaining
*/
public static function drawEllipse(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float, Color:FlxColor,
?lineStyle:LineStyle, ?fillStyle:FillStyle, ?drawStyle:DrawStyle):FlxSprite
public static function drawEllipse(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float,
FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
{
beginDraw(Color, lineStyle, fillStyle);
beginDraw(FillColor, lineStyle);
flashGfx.drawEllipse(X, Y, Width, Height);
endDraw(sprite, drawStyle);
return sprite;
Expand All @@ -412,16 +428,15 @@ class FlxSpriteUtil
* @param X X position of the triangle
* @param Y Y position of the triangle
* @param Height Height of the triangle
* @param Color Color of the triangle
* @param FillColor The ARGB color to fill this triangle with. FlxColor.TRANSPARENT (0x0) means no fill.
* @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
* @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle()
* @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
* @return The FlxSprite for chaining
*/
public static function drawTriangle(sprite:FlxSprite, X:Float, Y:Float, Height:Float, Color:FlxColor,
?lineStyle:LineStyle, ?fillStyle:FillStyle, ?drawStyle:DrawStyle):FlxSprite
public static function drawTriangle(sprite:FlxSprite, X:Float, Y:Float, Height:Float,
FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
{
beginDraw(Color, lineStyle, fillStyle);
beginDraw(FillColor, lineStyle);
flashGfx.moveTo(X + Height / 2, Y);
flashGfx.lineTo(X + Height, Height + Y);
flashGfx.lineTo(X, Height + Y);
Expand All @@ -435,16 +450,15 @@ class FlxSpriteUtil
*
* @param sprite The FlxSprite to manipulate
* @param Vertices Array of Vertices to use for drawing the polygon
* @param Color Color of the polygon
* @param FillColor The ARGB color to fill this polygon with. FlxColor.TRANSPARENT (0x0) means no fill.
* @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
* @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle();
* @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
* @return The FlxSprite for chaining
*/
public static function drawPolygon(sprite:FlxSprite, Vertices:Array<FlxPoint>, Color:FlxColor, ?lineStyle:LineStyle,
?fillStyle:FillStyle, ?drawStyle:DrawStyle):FlxSprite
public static function drawPolygon(sprite:FlxSprite, Vertices:Array<FlxPoint>,
FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
{
beginDraw(Color, lineStyle, fillStyle);
beginDraw(FillColor, lineStyle);
var p:FlxPoint = Vertices.shift();
flashGfx.moveTo(p.x, p.y);
for (p in Vertices)
Expand All @@ -458,23 +472,19 @@ class FlxSpriteUtil
/**
* Helper function that the drawing functions use at the start to set the color and lineStyle.
*
* @param Color The color to use for drawing
* @param FillColor The ARGB color to use for drawing
* @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
* @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle()
*/
@:noUsing
public static inline function beginDraw(Color:FlxColor, ?lineStyle:LineStyle, ?fillStyle:FillStyle):Void
public static inline function beginDraw(FillColor:FlxColor, ?lineStyle:LineStyle):Void
{
flashGfx.clear();
setLineStyle(lineStyle);

if ((fillStyle != null) && (fillStyle.hasFill))
if (FillColor != FlxColor.TRANSPARENT)
{
//use the fillStyle for color information
Color = fillStyle.color;
flashGfx.beginFill(FillColor.to24Bit(), FillColor.alphaFloat);
}

flashGfx.beginFill(Color.to24Bit(), Color.alphaFloat);
}

/**
Expand Down Expand Up @@ -529,6 +539,7 @@ class FlxSpriteUtil
if (lineStyle != null)
{
var color = (lineStyle.color == null) ? FlxColor.BLACK : lineStyle.color;
if (lineStyle.thickness == null) { lineStyle.thickness = 1; }
if (lineStyle.pixelHinting == null) { lineStyle.pixelHinting = false; }
if (lineStyle.miterLimit == null) { lineStyle.miterLimit = 3; }

Expand All @@ -544,16 +555,33 @@ class FlxSpriteUtil
}
}

/**
* Helper function for the default line styles of drawLine() and drawCurve()
* @param lineStyle The (optional) lineStyle typedef
* @return
*/
public static inline function getDefaultLineStyle(?lineStyle:LineStyle):LineStyle {

if (lineStyle == null)
lineStyle = { thickness: 1, color: FlxColor.WHITE };
if (lineStyle.thickness == null)
lineStyle.thickness = 1;
if (lineStyle.color == null)
lineStyle.color = FlxColor.WHITE;

return lineStyle;
}

/**
* Fills this sprite's graphic with a specific color.
*
* @param Sprite The FlxSprite to manipulate
* @param Color The color with which to fill the graphic, format 0xAARRGGBB.
* @param FillColor The color with which to fill the graphic, format 0xAARRGGBB.
* @return The FlxSprite for chaining
*/
public static function fill(sprite:FlxSprite, Color:FlxColor):FlxSprite
public static function fill(sprite:FlxSprite, FillColor:FlxColor):FlxSprite
{
sprite.pixels.fillRect(sprite.pixels.rect, Color);
sprite.pixels.fillRect(sprite.pixels.rect, FillColor);

if (sprite.pixels != sprite.framePixels)
{
Expand Down Expand Up @@ -651,16 +679,10 @@ typedef LineStyle = {
?miterLimit:Float
}

typedef FillStyle = {
?hasFill:Bool,
?color:FlxColor,
?alpha:Float
}

typedef DrawStyle = {
?matrix:Matrix,
?colorTransform:ColorTransform,
?blendMode:BlendMode,
?clipRect:Rectangle,
?smoothing:Bool
}
}

0 comments on commit c6aedc4

Please sign in to comment.