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
00056 + (CPBezierPath)bezierPath
00057 {
00058 return [[self alloc] init];
00059 }
00060
00064 + (CPBezierPath)bezierPathWithOvalInRect:(CGRect)rect
00065 {
00066 var path = [self bezierPath];
00067
00068 [path appendBezierPathWithOvalInRect:rect];
00069
00070 return path;
00071 }
00072
00076 + (CPBezierPath)bezierPathWithRect:(CGRect)rect
00077 {
00078 var path = [self bezierPath];
00079
00080 [path appendBezierPathWithRect:rect];
00081
00082 return path;
00083 }
00084
00088 + (float)defaultLineWidth
00089 {
00090 return DefaultLineWidth;
00091 }
00092
00096 + (void)setDefaultLineWidth:(float)width
00097 {
00098 DefaultLineWidth = width;
00099 }
00100
00104 + (void)fillRect:(CGRect)aRect
00105 {
00106 [[self bezierPathWithRect:aRect] fill];
00107 }
00108
00112 + (void)strokeRect:(CGRect)aRect
00113 {
00114 [[self bezierPathWithRect:aRect] stroke];
00115 }
00116
00120 + (void)strokeLineFromPoint:(CGPoint)point1 toPoint:(CGPoint)point2
00121 {
00122 var path = [self bezierPath];
00123
00124 [path moveToPoint:point1];
00125 [path lineToPoint:point2];
00126
00127 [path stroke];
00128 }
00129
00133 - (id)init
00134 {
00135 if (self = [super init])
00136 {
00137 _path = CGPathCreateMutable();
00138 _lineWidth = [[self class] defaultLineWidth];
00139 }
00140
00141 return self;
00142 }
00143
00147 - (void)moveToPoint:(CGPoint)point
00148 {
00149 CGPathMoveToPoint(_path, nil, point.x, point.y);
00150 }
00151
00155 - (void)lineToPoint:(CGPoint)point
00156 {
00157 CGPathAddLineToPoint(_path, nil, point.x, point.y);
00158 }
00159
00163 - (void)curveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2
00164 {
00165 CGPathAddCurveToPoint(_path, nil, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y);
00166 }
00167
00171 - (void)closePath
00172 {
00173 CGPathCloseSubpath(_path);
00174 }
00175
00179 - (void)stroke
00180 {
00181 var ctx = [[CPGraphicsContext currentContext] graphicsPort];
00182
00183 CGContextBeginPath(ctx);
00184 CGContextAddPath(ctx, _path);
00185 CGContextSetLineWidth(ctx, [self lineWidth]);
00186 CGContextClosePath(ctx);
00187 CGContextStrokePath(ctx);
00188 }
00189
00193 - (void)fill
00194 {
00195 var ctx = [[CPGraphicsContext currentContext] graphicsPort];
00196
00197 CGContextBeginPath(ctx);
00198 CGContextAddPath(ctx, _path);
00199 CGContextSetLineWidth(ctx, [self lineWidth]);
00200 CGContextClosePath(ctx);
00201 CGContextFillPath(ctx);
00202 }
00203
00207 - (float)lineWidth
00208 {
00209 return _lineWidth;
00210 }
00211
00215 - (void)setLineWidth:(float)lineWidth
00216 {
00217 _lineWidth = lineWidth;
00218 }
00219
00223 - (unsigned)elementCount
00224 {
00225 return _path.count;
00226 }
00227
00231 - (BOOL)isEmpty
00232 {
00233 return CGPathIsEmpty(_path);
00234 }
00235
00239 - (CGPoint)currentPoint
00240 {
00241 return CGPathGetCurrentPoint(_path);
00242 }
00243
00247 - (void)appendBezierPathWithPoints:(CPArray)points count:(unsigned)count
00248 {
00249 CGPathAddLines(_path, nil, points, count);
00250 }
00251
00255 - (void)appendBezierPathWithRect:(CGRect)rect
00256 {
00257 CGPathAddRect(_path, nil, rect);
00258 }
00259
00263 - (void)appendBezierPathWithOvalInRect:(CGRect)rect
00264 {
00265 CGPathAddPath(_path, nil, CGPathWithEllipseInRect(rect));
00266 }
00267
00271 - (void)appendBezierPathWithRoundedRect:(CGRect)rect xRadius:(float)xRadius yRadius:(float)yRadius
00272 {
00273 CGPathAddPath(_path, nil, CGPathWithRoundedRectangleInRect(rect, xRadius, yRadius, YES, YES, YES, YES));
00274 }
00275
00279 - (void)appendBezierPath:(NSBezierPath *)other
00280 {
00281 CGPathAddPath(_path, nil, other._path);
00282 }
00286 - (void)removeAllPoints
00287 {
00288 _path = CGPathCreateMutable();
00289 }
00290
00291 @end