package com.rubenswieringa.drawing { import flash.display.Graphics; import flash.geom.Point; import flash.geom.Rectangle; /** * Provides basic functionality for drawing with the Drawing API. * * @author Ruben Swieringa * ruben.swieringa@gmail.com * www.rubenswieringa.com * www.rubenswieringa.com/blog * @version 1.0.0 * @see LineStyle * * * edit 1 * * Before modifying and/or redistributing this class, please contact Ruben Swieringa (ruben.swieringa@gmail.com). * * * View code documentation at: * http://www.rubenswieringa.com/code/as3/flex/lib_flex_book_com_rubenswieringa_drawing_DrawingTool/docs/ * */ public class lib_flex_book_com_rubenswieringa_drawing_DrawingTool { // constants: public static const CROSS:String = "cross"; public static const CIRCLE:String = "circle"; /** * Constructor. * @private */ public function lib_flex_book_com_rubenswieringa_drawing_DrawingTool ():void {} /** * Draws a cross shaped marker on the specified coordinate(s). * This method its main functionality is that it executes the lineTo() method, so (for example) when aiming to draw a filled shape, beginFill() must be called first. For drawing lines, either the lineStyle() method of the Graphics class must be executed, or a LineStyle instance must be used as a parameter value for this method. * * @param graphics Graphics instance on which to draw the shape. * @param point Array or Point specifying one or more Point instances. If the point parameter value is neither a Point nor an Array then the function will silently fail. * @param size Size of the marker. * @param shape Shape of the marker, either lib_flex_book_com_rubenswieringa_drawing_DrawingTool.CROSS or lib_flex_book_com_rubenswieringa_drawing_DrawingTool.CIRCLE. Defaults to lib_flex_book_com_rubenswieringa_drawing_DrawingTool.CROSS. * @param lineStyle LineStyle instance carrying the styles for the lines of the marker to be drawn. * * @see LineStyle * @see lib_flex_book_com_rubenswieringa_drawing_DrawingTool#CROSS * @see lib_flex_book_com_rubenswieringa_drawing_DrawingTool#CIRCLE * */ public static function drawMarker (graphics:Graphics, point:*, size:Number=10, shape:String=lib_flex_book_com_rubenswieringa_drawing_DrawingTool.CROSS, lineStyle:LineStyle=null):void { if (point is Array){ for (var i:String in point){ lib_flex_book_com_rubenswieringa_drawing_DrawingTool.drawMarker(graphics, point[i], size, shape); } } if (point is Point){ switch (shape){ case lib_flex_book_com_rubenswieringa_drawing_DrawingTool.CROSS : var point1:Point = new Point(point.x-size/2, point.y-size/2); var point2:Point = new Point(point.x+size/2, point.y+size/2); var point3:Point = new Point(point.x+size/2, point.y-size/2); var point4:Point = new Point(point.x-size/2, point.y+size/2); lib_flex_book_com_rubenswieringa_drawing_DrawingTool.drawLine(graphics, point1, point2, lineStyle); lib_flex_book_com_rubenswieringa_drawing_DrawingTool.drawLine(graphics, point3, point4, lineStyle); break; case lib_flex_book_com_rubenswieringa_drawing_DrawingTool.CIRCLE : if (lineStyle != null){ lineStyle.applyTo(graphics); } graphics.moveTo(point.x+size/2, point.y); graphics.curveTo(point.x+size/2, point.y+size/2, point.x, point.y+size/2); graphics.curveTo(point.x-size/2, point.y+size/2, point.x-size/2, point.y); graphics.curveTo(point.x-size/2, point.y-size/2, point.x, point.y-size/2); graphics.curveTo(point.x+size/2, point.y-size/2, point.x+size/2, point.y); break; } } } /** * Draws lines between the Point instances in the area Array onto graphics with the drawing API. * This method its main functionality is that it executes the lineTo() method, so (for example) when aiming to draw a filled shape, beginFill() must be called first. For drawing lines, either the lineStyle() method of the Graphics class must be executed, or a LineStyle instance must be used as a parameter value for this method (lib_flex_book_com_rubenswieringa_drawing_DrawingTool.draw()). Also see the example below. * * @param graphics Graphics instance on which the lines should be drawn * @param area Array with coordinates (Point instances) * @param connect Boolean indicating whether or not to connect the last Point in area with the last Point in area. Defaults to true. * @param lineStyle LineStyle instance carrying the styles for the lines of the shape to be drawn. * * @example Drawing a filled shape: * * var shape:Array = [new Point(0,0), new Point(100,0), new Point(0,100)]; * myGraphics.beginFill(0xFF0000); * lib_flex_book_com_rubenswieringa_drawing_DrawingTool.draw(myGraphics, shape); * myGraphics.endFill(); * * * @example Drawing a shape with outline: * * var shape:Array = [new Point(0,0), new Point(100,0), new Point(0,100)]; * myGraphics.lineStyle(1, 0x00FF00); * lib_flex_book_com_rubenswieringa_drawing_DrawingTool.draw(myGraphics, shape); * * * @example Drawing a shape with dashed outline using the LineStyle class: * * var shape:Array = [new Point(0,0), new Point(100,0), new Point(0,100)]; * var lineStyle:LineStyle = new LineStyle(1, 0x00FF00); * lineStyle.dash = 4; * lib_flex_book_com_rubenswieringa_drawing_DrawingTool.draw(myGraphics, shape); * * * @see LineStyle * */ public static function draw (graphics:Graphics, area:Array, connect:Boolean=true, lineStyle:LineStyle=null):void { var i:int; var newArea:Array = []; for (i=0; i= distance) ? distance : traveled + lineStyle.dash; next = Point.interpolate(point1, point2, 1-(traveled/distance)); lineStyle.applyTo(graphics); graphics.lineTo(next.x, next.y); // stop if end of dash is equal to end of line: if (next.equals(point2)) break; // draw space: traveled = (traveled + lineStyle.space >= distance) ? distance : traveled + lineStyle.space; next = Point.interpolate(point1, point2, 1-(traveled/distance)); lib_flex_book_com_rubenswieringa_drawing_DrawingTool.clearLineStyle(graphics); graphics.lineTo(next.x, next.y); } }else{ // draw normal line if (lineStyle != null){ lineStyle.applyTo(graphics); } graphics.lineTo(point2.x, point2.y); } } /** * Sets the lineStyle of a Graphics instance to none. * * @param graphics Graphics instance of which to clear the lineStyle. * */ public static function clearLineStyle (graphics:Graphics):void { graphics.lineStyle(undefined, undefined, undefined); } } }