00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 @import <AppKit/CPGraphicsContext.j>
00027 @import <Foundation/CPObject.j>
00028
00029
00030
00031
00032 var DefaultLineWidth = 1.0;
00033
00034
00047 @implementation CPBezierPath : CPObject
00048 {
00049 CGPath _path;
00050 float _lineWidth;
00051 }
00052
00053 + (CPBezierPath)bezierPath
00054 {
00055 return [[[self class] alloc] init];
00056 }
00057
00058 + (CPBezierPath)bezierPathWithOvalInRect:(CGRect)rect
00059 {
00060 var path = [[self class] bezierPath];
00061
00062 [path appendBezierPathWithOvalInRect:rect];
00063
00064 return path;
00065 }
00066
00067 + (CPBezierPath)bezierPathWithRect:(CGRect)rect
00068 {
00069 var path = [[self class] bezierPath];
00070
00071 [path appendBezierPathWithRect:rect];
00072
00073 return path;
00074 }
00075
00076 + (float)defaultLineWidth
00077 {
00078 return DefaultLineWidth;
00079 }
00080
00081 + (void)setDefaultLineWidth:(float)width
00082 {
00083 DefaultLineWidth = width;
00084 }
00085
00086 + (void)fillRect:(CGRect)rect
00087 {
00088 [[[self class] bezierPathWithRect:rect] fill];
00089 }
00090
00091 + (void)strokeRect:(CGRect)rect
00092 {
00093 [[[self class] bezierPathWithRect:rect] stroke];
00094 }
00095
00096 + (void)strokeLineFromPoint:(CGPoint)point1 toPoint:(CGPoint)point2
00097 {
00098 var path = [[self class] bezierPath];
00099
00100 [path moveToPoint:point1];
00101 [path lineToPoint:point2];
00102
00103 [path stroke];
00104 }
00105
00106 - (id)init
00107 {
00108 if (self = [super init])
00109 {
00110 _path = CGPathCreateMutable();
00111 _lineWidth = [[self class] defaultLineWidth];
00112 }
00113
00114 return self;
00115 }
00116
00117 - (void)moveToPoint:(CGPoint)point
00118 {
00119 CGPathMoveToPoint(_path, nil, point.x, point.y);
00120 }
00121
00122 - (void)lineToPoint:(CGPoint)point
00123 {
00124 CGPathAddLineToPoint(_path, nil, point.x, point.y);
00125 }
00126
00127 - (void)curveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2
00128 {
00129 CGPathAddCurveToPoint(_path, nil, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y);
00130 }
00131
00132 - (void)closePath
00133 {
00134 CGPathCloseSubpath(_path);
00135 }
00136
00137 - (void)stroke
00138 {
00139 var ctx = [[CPGraphicsContext currentContext] graphicsPort];
00140
00141 CGContextBeginPath(ctx);
00142 CGContextAddPath(ctx, _path);
00143 CGContextSetLineWidth(ctx, [self lineWidth]);
00144 CGContextClosePath(ctx);
00145 CGContextStrokePath(ctx);
00146 }
00147
00148 - (void)fill
00149 {
00150 var ctx = [[CPGraphicsContext currentContext] graphicsPort];
00151
00152 CGContextBeginPath(ctx);
00153 CGContextAddPath(ctx, _path);
00154 CGContextSetLineWidth(ctx, [self lineWidth]);
00155 CGContextClosePath(ctx);
00156 CGContextFillPath(ctx);
00157 }
00158
00159 - (float)lineWidth
00160 {
00161 return _lineWidth;
00162 }
00163
00164 - (void)setLineWidth:(float)lineWidth
00165 {
00166 _lineWidth = lineWidth;
00167 }
00168
00169 - (unsigned)elementCount
00170 {
00171 return _path.count;
00172 }
00173
00174 - (BOOL)isEmpty
00175 {
00176 return CGPathIsEmpty(_path);
00177 }
00178
00179 - (CGPoint)currentPoint
00180 {
00181 return CGPathGetCurrentPoint(_path);
00182 }
00183
00184 - (void)appendBezierPathWithPoints:(CPArray)points count:(unsigned)count
00185 {
00186 CGPathAddLines(_path, nil, points, count);
00187 }
00188
00189 - (void)appendBezierPathWithRect:(CGRect)rect
00190 {
00191 CGPathAddRect(_path, nil, rect);
00192 }
00193
00194 - (void)appendBezierPathWithOvalInRect:(CGRect)rect
00195 {
00196 CGPathAddPath(_path, nil, CGPathWithEllipseInRect(rect));
00197 }
00198
00199 - (void)appendBezierPathWithRoundedRect:(CGRect)rect xRadius:(float)xRadius yRadius:(float)yRadius
00200 {
00201 CGPathAddPath(_path, nil, CGPathWithRoundedRectangleInRect(rect, xRadius, yRadius, YES, YES, YES, YES));
00202 }
00203
00204 - (void)appendBezierPath:(NSBezierPath *)other
00205 {
00206 CGPathAddPath(_path, nil, other._path);
00207 }
00208
00209 - (void)removeAllPoints
00210 {
00211 _path = CGPathCreateMutable();
00212 }
00213
00214 @end