Commit 8e10a443 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

9780596526955

0 → 120000
+1 −0
Changes for 9780596526955: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
9780596526955
 No newline at end of file
+112 −0
Changes for AS3CBLibrary/classes/ascb/display/DraggableSprite.as: 112 added lines, 0 removed lines.
Original line number Diff line number Diff line

package ascb.display {
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * A DraggableSprite is a Sprite that has the ability to interact
	 * with the Mouse in drag and drop scenarios.  All Sprites have
	 * the startDrop() and stopDrag() methods, but those methods only
	 * update the display list during enterFrame events, instead of
	 * during mouseMove events, which leads to choppy dragging.  The
	 * DraggableSprite provides a drag() method, similar to startDrag()
	 * and a drop() method, similar to stopDrag(), that enable smooth
	 * drag and drop operations.
	 */
	public class DraggableSprite extends Sprite {
	
		// Store the location of the cursor within the sprite
		// so we can position correctly when the cursor moves
		private var x_offset:Number = 0;
		private var y_offset:Number = 0;
		
		// Keep track of the area where dragging is allowed
		// so the sprite can be kept in bounds.
		private var bounds:Rectangle;
		
		/**
		 * Constructor - nothing to do
		 */
		public function DraggableSprite() {
			// do nothing
		}
		
		/**
		 * Starts a smooth dragging operation, forcing the player to redraw
		 * the Sprite after every mouse move.  Cancel the drag() operation
		 * by calling the drop() method.
		 */
		public function drag( lockCenter:Boolean = false, rectangle:Rectangle = null ):void {
			// Save the cursor position in the sprite so we can adjust
			// the x and y locations correctly when the cursor position
			// chnages based on the lockCenter parameter.
			var pt:Point;
			if ( !lockCenter ) {
				// lockCenter is false, use the mouse coordinates at the point
				pt = localToGlobal( new Point( mouseX, mouseY ) );
			} else {
				// lockCenter is true, ignore the mouse coordinates
				// and use (0,0) instead as the point
				pt = localToGlobal( new Point( 0, 0 ) );
			}
			
			// Save the offset values so we can compute x and y correctly
			x_offset = pt.x - x;
			y_offset = pt.y - y;
			
			// Save the bounds rectangle
			bounds = rectangle;
			
			// Wire the Sprite to the mouse - whenever the mouse moves
			// invoke handleDrag to update the Sprite position
			stage.addEventListener( MouseEvent.MOUSE_MOVE, handleDrag );
			
			// Detect a drop by listening for mouse up on the stage
			stage.addEventListener( MouseEvent.MOUSE_UP, drop );
		}
		
		/**
		 * Called everytime the mouse moves after the drag() method has
		 * been called.  Updates the position of the Sprite based on
		 * the location of the mouse cursor.
		 */
		private function handleDrag( event:MouseEvent ):void {
			// Set the x and y location based on the mouse position
			x = event.stageX - x_offset;
			y = event.stageY - y_offset;
			
			// Keep sprite in bounds if bounds was specified in drag
			if ( bounds != null ) {
				if ( x < bounds.left ) {
					x = bounds.left;
				} else if ( x > bounds.right ) {
					x = bounds.right;
				}
				
				if ( y < bounds.top ) {
					y = bounds.top;	
				} else if ( y > bounds.bottom ) {
					y = bounds.bottom;	
				}
			}
			
			// Force the player to re-draw the sprite after the event.
			// This makes the movement look smooth, unlike startDrag()
			event.updateAfterEvent();
		}
	
		/**
		 * Cancels a drag() operation
		 */
		public function drop():void {
			// The mouse up indicated the drop, so remove the mouse up listener ...
			stage.removeEventListener( MouseEvent.MOUSE_UP, drop );
			
			// ... and remove the mouse move listener
			stage.removeEventListener( MouseEvent.MOUSE_MOVE, handleDrag );
		}
	}
}
 No newline at end of file
+329 −0
Changes for AS3CBLibrary/classes/ascb/drawing/Pen.as: 329 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ascb.drawing {

  import flash.display.Graphics;
  import flash.geom.Matrix;
  import flash.display.BitmapData;

  public class Pen {

    private var _gTarget:Graphics;
    private var _bLineStyleSet:Boolean;

    /**
     *  Get or set the target movie clip. You can also set the target
     *  movie clip when calling the constructor. When calling the
     *  constructor you can also have the Pen automatically create a
     *  new empty movie clip in a parent.
     *  @example
     *  <pre>
     *  import ascb.drawing.Pen;
     *  
     *  // Have the Pen create a new empty movie clip in this.
     *  var pCircle:Pen = new Pen(this, true);
     *  pCircle.drawCircle(10);
     *  trace(pCircle.target);
     */
    public function set target(gTarget:Graphics):void {
      _gTarget = gTarget;
    }

    public function get target():Graphics {
      return _gTarget;
    }

    public function Pen(gTarget:Graphics) {
      _gTarget = gTarget;
    }

    /**
     *  The lineStyle() method is optional. The Pen class uses a default
     *  line style of you don't call the method.
     *  @param  thickness   (optional) The point thickness of the line (default 0)
     *  @param  rgb         (optional) The RGB value of the line (default 0)
     *  @param  alpha       (optional) The alpha of the line (default 100)
     */
    public function lineStyle(nThickness:Number = 1, nRGB:Number = 0, nAlpha:Number = 1, bPixelHinting:Boolean = false, sScaleMode:String = "normal", sCaps:String = null, sJoints:String = null, nMiterLimit:Number = 3):void {
      _gTarget.lineStyle(nThickness, nRGB, nAlpha, bPixelHinting, sScaleMode, sCaps, sJoints, nMiterLimit);
      _bLineStyleSet = true;
    }
    
    public function lineGradientStyle(sType:String, aColors:Array, aAlphas:Array, aRatios:Array, mtxTransform:Matrix = null, sMethod:String = "pad", sInterpolation:String = "rgb", nFocalPoint:Number = 0):void {
      if(!_bLineStyleSet) {
      	lineStyle();
      }
      _gTarget.lineGradientStyle(sType, aColors, aAlphas, aRatios, mtxTransform, sMethod, sInterpolation, nFocalPoint);
    }

    public function beginFill(nRGB:Number, nAlpha:Number = 1):void {
      _gTarget.beginFill(nRGB, nAlpha);
    }

    public function beginGradientFill(sFillType:String, aColors:Array, aAlphas:Array, aRatios:Array, mtxTransform:Matrix = null, sMethod:String = "pad", sInterpolation:String = "rgb", nFocalPoint:Number = 0):void {
      _gTarget.beginGradientFill(sFillType, aColors, aAlphas, aRatios, mtxTransform, sMethod, sInterpolation, nFocalPoint);
    }
    
    public function beginBitmapFill(bmpData:BitmapData, mtxTransform:Matrix = null, bRepeat:Boolean = true, bSmooth:Boolean = false):void {
      _gTarget.beginBitmapFill(bmpData, mtxTransform, bRepeat, bSmooth);
    }

    public function endFill():void {
      _gTarget.endFill();
    }

    public function clear():void {
      _gTarget.clear();
      _bLineStyleSet = false;
    }

    public function moveTo(nX:Number, nY:Number):void {
      _gTarget.moveTo(nX, nY);
    }

    public function lineTo(nX:Number, nY:Number):void {
      // Make sure the line style is set. Otherwise, use the default values.
      if(!_bLineStyleSet) {
        lineStyle();
      }
      _gTarget.lineTo(nX, nY);
    }

    public function curveTo(nCtrlX:Number, nCtrlY:Number, nAnchorX:Number, nAnchorY:Number):void {
      // Make sure the line style is set. Otherwise, use the default values.
      if(!_bLineStyleSet) {
        lineStyle();
      }
      _gTarget.curveTo(nCtrlX, nCtrlY, nAnchorX, nAnchorY);
    }

    /**
     *  Draw a line from one point to another.
     */
    public function drawLine(nX0:Number, nY0:Number, nX1:Number, nY1:Number):void {
      // Make sure the line style is set. Otherwise, use the default values.
      if(!_bLineStyleSet) {
        lineStyle();
      }
      _gTarget.moveTo(nX0, nY0);
      _gTarget.lineTo(nX1, nY1);
    }

    /**
     *  Draw a curve from one point to another.
     */
    public function drawCurve(nX:Number, nY:Number, nCtrlX:Number, nCtrlY:Number, nAnchorX:Number, nAnchorY:Number):void {
      // Make sure the line style is set. Otherwise, use the default values.
      if(!_bLineStyleSet) {
        lineStyle();
      }
      _gTarget.moveTo(nX, nY);
      _gTarget.curveTo(nCtrlX, nCtrlY, nAnchorX, nAnchorY);
    }

    public function drawRect(nX:Number, nY:Number, nWidth:Number, nHeight:Number):void {
      if(!_bLineStyleSet) {
        lineStyle();
      }
      _gTarget.drawRect(nX, nY, nWidth, nHeight);
    }
    
    public function drawRoundRect(nX:Number, nY:Number, nWidth:Number, nHeight:Number, nRadius:Number):void {
      if(!_bLineStyleSet) {
        lineStyle();
      }
      _gTarget.drawRoundRect(nX, nY, nWidth, nHeight, nRadius);
    }

    public function drawRoundRectComplex(nX:Number, nY:Number, nWidth:Number, nHeight:Number, nA:Number, nB:Number, nC:Number, nD:Number):void {
      if(!_bLineStyleSet) {
        lineStyle();
      }
      _gTarget.drawRoundRectComplex(nX, nY, nWidth, nHeight, nA, nB, nC, nD);
    }

    public function drawCircle(nX:Number, nY:Number, nRadius:Number):void {
      if(!_bLineStyleSet) {
        lineStyle();
      }
      _gTarget.drawCircle(nX, nY, nRadius);
    }

    public function drawSlice (nArc:Number, nRadius:Number, nStartingAngle:Number, nX:Number, nY:Number):void {
      drawArc(nX, nY, nRadius, nArc, nStartingAngle, true);
    }

    public function drawArc (nX:Number, nY:Number, nRadius:Number, nArc:Number, nStartingAngle:Number = 0, bRadialLines:Boolean = false):void  {

      // The angle of each of the eight segments is 45 degrees (360 divided by eight),
      // which equals p/4 radians.
      if(nArc > 360) {
        nArc = 360;
      }
      nArc = Math.PI/180 * nArc;
      var nAngleDelta:Number = nArc/8;

      // Find the distance from the circle's center to the control points
      // for the curves.
      var nCtrlDist:Number = nRadius/Math.cos(nAngleDelta/2);

      nStartingAngle *= Math.PI / 180;

      var nAngle:Number = nStartingAngle;
      var nCtrlX:Number;
      var nCtrlY:Number;
      var nAnchorX:Number;
      var nAnchorY:Number;

      var nStartingX:Number = nX + Math.cos(nStartingAngle) * nRadius;
      var nStartingY:Number = nY + Math.sin(nStartingAngle) * nRadius;

      if(bRadialLines) {
        moveTo(nX, nY);
        lineTo(nStartingX, nStartingY);
      }
      else {
        // Move to the starting point, one radius to the right of the circle's center.
        moveTo(nStartingX, nStartingY);
      }

      // Repeat eight times to create eight segments.
      for (var i:Number = 0; i < 8; i++) {

        // Increment the angle by angleDelta (p/4) to create the whole circle (2p).
        nAngle += nAngleDelta;

        // The control points are derived using sine and cosine.
        nCtrlX = nX + Math.cos(nAngle-(nAngleDelta/2))*(nCtrlDist);
        nCtrlY = nY + Math.sin(nAngle-(nAngleDelta/2))*(nCtrlDist);

        // The anchor points (end points of the curve) can be found similarly to the
        // control points.
        nAnchorX = nX + Math.cos(nAngle) * nRadius;
        nAnchorY = nY + Math.sin(nAngle) * nRadius;

        // Draw the segment.
        curveTo(nCtrlX, nCtrlY, nAnchorX, nAnchorY);
      }
      if(bRadialLines) {
        lineTo(nX, nY);
      }
    }


    public function drawEllipse (nX:Number, nY:Number, nRadiusX:Number, nRadiusY:Number):void {

      var nAngleDelta:Number = Math.PI / 4;

      var nAngle:Number = 0;

      // 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 nCtrlDistX:Number = nRadiusX / Math.cos(nAngleDelta/2);
      var nCtrlDistY:Number = nRadiusY / Math.cos(nAngleDelta/2);
      var nCtrlX:Number;
      var nCtrlY:Number;
      var nAnchorX:Number;
      var nAnchorY:Number;

      moveTo(nX + nRadiusX, nY);

      for (var i:Number = 0; i < 8; i++) {
        nAngle += nAngleDelta;
        nCtrlX = nX + Math.cos(nAngle-(nAngleDelta/2))*(nCtrlDistX);
        nCtrlY = nY + Math.sin(nAngle-(nAngleDelta/2))*(nCtrlDistY);
        nAnchorX = nX + Math.cos(nAngle) * nRadiusX;
        nAnchorY = nY + Math.sin(nAngle) * nRadiusY;
        this.curveTo(nCtrlX, nCtrlY, nAnchorX, nAnchorY);
      }
    }

    /**
     *  Draw a triangle given the lengths of two sides and the angle between them.
     */
    public function drawTriangle (nX:Number, nY:Number, nAB:Number, nAC:Number, nAngle:Number, nRotation:Number = 0):void {

      nRotation = nRotation * Math.PI / 180;

      // Convert the angle between the sides from degrees to radians.
      nAngle = nAngle * Math.PI / 180;


      // Calculate the coordinates of points b and c.
      var nBx:Number = Math.cos(nAngle - nRotation) * nAB;
      var nBy:Number = Math.sin(nAngle - nRotation) * nAB;
      var nCx:Number = Math.cos(-nRotation) * nAC;
      var nCy:Number = Math.sin(-nRotation) * nAC;

      // Calculate the centroid's coordinates.
      var nCentroidX:Number = 0;
      var nCentroidY:Number = 0;

      // Move to point a, then draw line ac, then line cb, and finally ba (ab).
      drawLine(-nCentroidX + nX, -nCentroidY + nY, nCx - nCentroidX + nX, nCy - nCentroidY + nY);
      lineTo(nBx - nCentroidX + nX, nBy - nCentroidY + nY);
      lineTo(-nCentroidX + nX, -nCentroidY + nY);
    }

    public function drawRegularPolygon (nX:Number, nY:Number, nSides:Number, nLength:Number, nRotation:Number = 0):void {

      nRotation = nRotation * Math.PI / 180;

      // The angle formed between the segments from the polygon's center as shown in 
      // Figure 4-5. Since the total angle in the center is 360 degrees (2p radians),
      // each segment's angle is 2p divided by the number of sides.
      var nAngle:Number = (2 * Math.PI) / nSides;

      // Calculate the length of the radius that circumscribes the polygon (which is
      // also the distance from the center to any of the vertices).
      var nRadius:Number = (nLength/2)/Math.sin(nAngle/2);

      // The starting point of the polygon is calculated using trigonometry where 
      // radius is the hypotenuse and nRotation is the angle.
      var nPx:Number = (Math.cos(nRotation) * nRadius) + nX;
      var nPy:Number = (Math.sin(nRotation) * nRadius) + nY;

      // Move to the starting point without yet drawing a line.
      moveTo(nPx, nPy);

      // Draw each side. Calculate the vertex coordinates using the same trigonometric
      // ratios used to calculate px and py earlier.
      for (var i:Number = 1; i <= nSides; i++) {
        nPx = (Math.cos((nAngle * i) + nRotation) * nRadius) + nX;
        nPy = (Math.sin((nAngle * i) + nRotation) * nRadius) + nY;
        lineTo(nPx, nPy);
      }
    }

    public function drawStar(nX:Number, nY:Number, nPoints:Number, nInnerRadius:Number, nOuterRadius:Number, nRotation:Number = 0):void {

      if(nPoints < 3) {
        return;
      }

      var nAngleDelta:Number = (Math.PI * 2) / nPoints;
      nRotation = Math.PI * (nRotation - 90) / 180;

      var nAngle:Number = nRotation;

      var nPenX:Number = nX + Math.cos(nAngle + nAngleDelta / 2) * nInnerRadius;
      var nPenY:Number = nY + Math.sin(nAngle + nAngleDelta / 2) * nInnerRadius;

      moveTo(nPenX, nPenY);

      nAngle += nAngleDelta;

      for(var i:Number = 0; i < nPoints; i++) {
        nPenX = nX + Math.cos(nAngle) * nOuterRadius;
        nPenY = nY + Math.sin(nAngle) * nOuterRadius;
        lineTo(nPenX, nPenY);
        nPenX = nX + Math.cos(nAngle + nAngleDelta / 2) * nInnerRadius;
        nPenY = nY + Math.sin(nAngle + nAngleDelta / 2) * nInnerRadius;
        lineTo(nPenX, nPenY);
        nAngle += nAngleDelta;
      }

    }

  }
  
}
 No newline at end of file
+31 −0
Changes for AS3CBLibrary/classes/ascb/filters/ColorMatrixArrays.as: 31 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ascb.filters {

  public class ColorMatrixArrays {
  	
  	public static const DIGITAL_NEGATIVE:Array = [-1, 0, 0, 0, 255, 0, -1, 0, 0, 255, 0, 0, -1, 0, 255, 0, 0, 0, 1, 0];
  	public static const GRAYSCALE:Array = [0.3086, 0.6094, 0.0820, 0, 0, 0.3086, 0.6094, 0.0820, 0, 0, 0.3086, 0.6094, 0.0820, 0, 0, 0, 0, 0, 1, 0];
  	
  	public static function getSaturationArray(nValue:Number):Array {
  	  var nRed:Number = 0.3086;
  	  var nGreen:Number = 0.6094;
  	  var nBlue:Number = 0.0820;
  	  var nA:Number = (1 - nValue) * nRed + nValue;
  	  var nB:Number = (1 - nValue) * nGreen; 
  	  var nC:Number = (1 - nValue) * nBlue;
  	  var nD:Number = (1 - nValue) * nRed;
  	  var nE:Number = (1 - nValue) * nGreen + nValue;
  	  var nF:Number = (1 - nValue) * nBlue;
  	  var nG:Number = (1 - nValue) * nRed;
  	  var nH:Number = (1 - nValue) * nGreen;
  	  var nI:Number = (1 - nValue) * nBlue + nValue;
      return [nA, nB, nC, 0, 0, nD, nE, nF, 0, 0, nG, nH, nI, 0, 0, 0, 0, 0, 1, 0];
 	}
  	
  	public static function getContrastArray(nValue:Number):Array {
  	  var nScale:Number = nValue * 11;
  	  var nOffset:Number = 63.6 - (nValue * 698.5);
  	  return [nScale, 0, 0, 0, nOffset, 0, nScale, 0, 0, nOffset, 0, 0, nScale, 0, nOffset, 0, 0, 0, 1, 0];
  	}
  	
  }
}
 No newline at end of file
+11 −0
Changes for AS3CBLibrary/classes/ascb/filters/ConvolutionMatrixArrays.as: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ascb.filters {


  public class ConvolutionMatrixArrays {	

  	public static const EMBOSS:Array = [-2, -1, 0, -1, 1, 1, 0, 1, 2];
  	public static const EDGE_DETECT:Array = [0, 1, 0, 1, -4, 1, 0, 1, 0];
  	public static const SHARPEN:Array = [0, -1, 0, -1, 5, -1, 0, -1, 0];

  }
}
 No newline at end of file
Loading