Skip to content

Instantly share code, notes, and snippets.

@squeedee
Created August 25, 2010 19:56
Show Gist options
  • Save squeedee/550165 to your computer and use it in GitHub Desktop.
Save squeedee/550165 to your computer and use it in GitHub Desktop.
Decorator for the Graphics class and a sample implementation
package com.visfleet.util.datatypes {
import flash.display.Graphics;
import flash.geom.Matrix;
import flash.geom.Point;
public class Grafix extends GraphicsDecorator implements IGraphics {
private var _transforms:Array;
public function Grafix(graphics:Graphics) {
super(graphics);
setupTransforms();
}
private function setupTransforms():void {
_transforms = [
new Matrix()
];
}
public function moveToPoint(point:Point):void {
var newPoint:Point = getCurrentTransformConst().transformPoint(point);
moveTo(newPoint.x, newPoint.y);
}
public function lineToPoint(point:Point):void {
var newPoint:Point = getCurrentTransformConst().transformPoint(point);
lineTo(newPoint.x, newPoint.y);
}
public function noLineStyle():void {
lineStyle(0,0,0);
}
public function drawPolygon(points:Array):void {
drawPoly(points);
lineToPoint(points[0]);
}
public function drawPolyLine(points:Array):void {
drawPoly(points);
}
public function beginTransform(matrix:Matrix):void {
var newTransform:Matrix = getCurrentTransform();
newTransform.concat(matrix);
_transforms.push(newTransform)
}
public function endTransform():void {
if (_transforms.length < 2)
throw new Error("Attempted to end too many transforms", 8723585);
_transforms.pop();
}
public function getCurrentTransform():Matrix {
return getCurrentTransformConst().clone();
}
protected function getCurrentTransformConst():Matrix {
return _transforms[_transforms.length - 1];
}
protected function drawPoly(points:Array):void {
var pointCount:int = points.length;
if (pointCount < 1)
return;
var pointIndex:int = 0;
var point:Point = points[pointIndex];
moveToPoint(point);
while (++pointIndex < pointCount) {
point = points[pointIndex];
lineToPoint(point);
}
}
}
}
package com.visfleet.util.datatypes {
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.geom.Matrix;
public class GraphicsDecorator implements IGraphics {
protected var g:Graphics;
public function GraphicsDecorator(graphics:Graphics) {
g = graphics;
}
//noinspection OverlyComplexFunctionJS
public function drawRoundRectComplex(x:Number, y:Number, width:Number, height:Number, topLeftRadius:Number, topRightRadius:Number, bottomLeftRadius:Number, bottomRightRadius:Number):void {
g.drawRoundRectComplex(x, y, width, height, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
}
public function drawCircle(x:Number, y:Number, radius:Number):void {
g.drawCircle(x, y, radius);
}
public function drawRect(x:Number, y:Number, width:Number, height:Number):void {
g.drawRect(x, y, width, height);
}
public function curveTo(controlX:Number, controlY:Number, anchorX:Number, anchorY:Number):void {
g.curveTo(controlX, controlY, anchorX, anchorY);
}
public function beginFill(color:uint, alpha:Number = 1):void {
g.beginFill(color, alpha);
}
//noinspection OverlyComplexFunctionJS
public function lineGradientStyle(type:String, colors:Array, alphas:Array, ratios:Array, matrix:Matrix = null, spreadMethod:String = "pad", interpolationMethod:String = "rgb", focalPointRatio:Number = 0):void {
g.lineGradientStyle(type, colors, alphas, ratios, matrix, spreadMethod, interpolationMethod, focalPointRatio);
}
//noinspection OverlyComplexFunctionJS
public function drawRoundRect(x:Number, y:Number, width:Number, height:Number, ellipseWidth:Number, ellipseHeight:Number = 0):void {
g.drawRoundRect(x, y, width, height, ellipseWidth, ellipseHeight);
}
public function moveTo(x:Number, y:Number):void {
g.moveTo(x, y);
}
public function clear():void {
g.clear();
}
public function lineTo(x:Number, y:Number):void {
g.lineTo(x, y);
}
public function drawEllipse(x:Number, y:Number, width:Number, height:Number):void {
g.drawEllipse(x, y, width, height);
}
public function beginBitmapFill(bitmap:BitmapData, matrix:Matrix = null, repeat:Boolean = true, smooth:Boolean = false):void {
g.beginBitmapFill(bitmap, matrix, repeat, smooth);
}
//noinspection OverlyComplexFunctionJS
public function beginGradientFill(type:String, colors:Array, alphas:Array, ratios:Array, matrix:Matrix = null, spreadMethod:String = "pad", interpolationMethod:String = "rgb", focalPointRatio:Number = 0):void {
g.beginGradientFill(type, colors, alphas, ratios, matrix, spreadMethod, interpolationMethod, focalPointRatio);
}
//noinspection OverlyComplexFunctionJS
public function lineStyle(thickness:Number = 0, color:uint = 0, alpha:Number = 1, pixelHinting:Boolean = false, scaleMode:String = "normal", caps:String = null, joints:String = null, miterLimit:Number = 3):void {
g.lineStyle(thickness, color, alpha, pixelHinting, scaleMode, caps, joints, miterLimit);
}
public function endFill():void {
g.endFill();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment