![]() |
API 0.9.5
|
00001 /* 00002 * CPBezierPath.j 00003 * 00004 * Created by Ross Boucher. 00005 * Copyright 2009, 280 North, Inc. 00006 * 00007 * Adapted from Kevin Wojniak, portions Copyright 2009 Kevin Wojniak. 00008 * 00009 * This library is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Lesser General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2.1 of the License, or (at your option) any later version. 00013 * 00014 * This library is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public 00020 * License along with this library; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00022 * Copyright 2009 280 North, Inc. 00023 00024 */ 00025 00026 00027 00028 00029 // Class defaults 00030 00031 var DefaultLineWidth = 1.0; 00032 00033 00046 @implementation CPBezierPath : CPObject 00047 { 00048 CGPath _path; 00049 float _lineWidth; 00050 } 00051 00055 + (CPBezierPath)bezierPath 00056 { 00057 return [[self alloc] init]; 00058 } 00059 00063 + (CPBezierPath)bezierPathWithOvalInRect:(CGRect)rect 00064 { 00065 var path = [self bezierPath]; 00066 00067 [path appendBezierPathWithOvalInRect:rect]; 00068 00069 return path; 00070 } 00071 00075 + (CPBezierPath)bezierPathWithRect:(CGRect)rect 00076 { 00077 var path = [self bezierPath]; 00078 00079 [path appendBezierPathWithRect:rect]; 00080 00081 return path; 00082 } 00083 00087 + (float)defaultLineWidth 00088 { 00089 return DefaultLineWidth; 00090 } 00091 00095 + (void)setDefaultLineWidth:(float)width 00096 { 00097 DefaultLineWidth = width; 00098 } 00099 00103 + (void)fillRect:(CGRect)aRect 00104 { 00105 [[self bezierPathWithRect:aRect] fill]; 00106 } 00107 00111 + (void)strokeRect:(CGRect)aRect 00112 { 00113 [[self bezierPathWithRect:aRect] stroke]; 00114 } 00115 00119 + (void)strokeLineFromPoint:(CGPoint)point1 toPoint:(CGPoint)point2 00120 { 00121 var path = [self bezierPath]; 00122 00123 [path moveToPoint:point1]; 00124 [path lineToPoint:point2]; 00125 00126 [path stroke]; 00127 } 00128 00132 - (id)init 00133 { 00134 if (self = [super init]) 00135 { 00136 _path = CGPathCreateMutable(); 00137 _lineWidth = [[self class] defaultLineWidth]; 00138 } 00139 00140 return self; 00141 } 00142 00146 - (void)moveToPoint:(CGPoint)point 00147 { 00148 CGPathMoveToPoint(_path, nil, point.x, point.y); 00149 } 00150 00154 - (void)lineToPoint:(CGPoint)point 00155 { 00156 CGPathAddLineToPoint(_path, nil, point.x, point.y); 00157 } 00158 00162 - (void)curveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2 00163 { 00164 CGPathAddCurveToPoint(_path, nil, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y); 00165 } 00166 00170 - (void)closePath 00171 { 00172 CGPathCloseSubpath(_path); 00173 } 00174 00178 - (void)stroke 00179 { 00180 var ctx = [[CPGraphicsContext currentContext] graphicsPort]; 00181 00182 CGContextBeginPath(ctx); 00183 CGContextAddPath(ctx, _path); 00184 CGContextSetLineWidth(ctx, [self lineWidth]); 00185 CGContextStrokePath(ctx); 00186 } 00187 00191 - (void)fill 00192 { 00193 var ctx = [[CPGraphicsContext currentContext] graphicsPort]; 00194 00195 CGContextBeginPath(ctx); 00196 CGContextAddPath(ctx, _path); 00197 CGContextSetLineWidth(ctx, [self lineWidth]); 00198 CGContextClosePath(ctx); 00199 CGContextFillPath(ctx); 00200 } 00201 00205 - (float)lineWidth 00206 { 00207 return _lineWidth; 00208 } 00209 00213 - (void)setLineWidth:(float)lineWidth 00214 { 00215 _lineWidth = lineWidth; 00216 } 00217 00221 - (unsigned)elementCount 00222 { 00223 return _path.count; 00224 } 00225 00229 - (BOOL)isEmpty 00230 { 00231 return CGPathIsEmpty(_path); 00232 } 00233 00237 - (CGPoint)currentPoint 00238 { 00239 return CGPathGetCurrentPoint(_path); 00240 } 00241 00245 - (void)appendBezierPathWithPoints:(CPArray)points count:(unsigned)count 00246 { 00247 CGPathAddLines(_path, nil, points, count); 00248 } 00249 00253 - (void)appendBezierPathWithRect:(CGRect)rect 00254 { 00255 CGPathAddRect(_path, nil, rect); 00256 } 00257 00261 - (void)appendBezierPathWithOvalInRect:(CGRect)rect 00262 { 00263 CGPathAddPath(_path, nil, CGPathWithEllipseInRect(rect)); 00264 } 00265 00269 - (void)appendBezierPathWithRoundedRect:(CGRect)rect xRadius:(float)xRadius yRadius:(float)yRadius 00270 { 00271 CGPathAddPath(_path, nil, CGPathWithRoundedRectangleInRect(rect, xRadius, yRadius, YES, YES, YES, YES)); 00272 } 00273 00277 - (void)appendBezierPath:(NSBezierPath *)other 00278 { 00279 CGPathAddPath(_path, nil, other._path); 00280 } 00281 00285 - (void)removeAllPoints 00286 { 00287 _path = CGPathCreateMutable(); 00288 } 00289 00290 @end