Luky/Gui/LLShapes.m

00001 //
00002 //  LLShapes.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 27/04/2006.
00006 //  Copyright 2006 Luky Soft. All rights reserved.
00007 //
00008 
00009 #import "LLShapes.h"
00010 
00011 
00012 @implementation LLShapes
00013 
00014 static NSBezierPath *line = nil; // this makes us fast, but not rentrant
00015 
00016 - (id) init {
00017     self = [super init];
00018     if (self != nil) {
00019                 line = [[NSBezierPath alloc] init];   
00020     }
00021     return self;
00022 }
00023 
00024 - (NSRect) rectWithHeight:(int)height width:(int)width centerPoint:(NSPoint)centerGravity {
00025     
00026     NSRect result;
00027     
00028     result.size.width = width;
00029     result.size.height = height;
00030     result.origin.x = centerGravity.x - width / 2;
00031     result.origin.y = centerGravity.y - height / 2;
00032     
00033     return result;
00034 }
00035 
00036 - (void)drawTriangleNotchUpInRect:(NSRect)rect {
00037     
00038     // left is bottom left
00039     NSPoint left = rect.origin;
00040     left.y += rect.size.height;
00041     // right is left + width
00042     NSPoint right = left;
00043     right.x += rect.size.width;
00044     // mid is mid top
00045     NSPoint mid = rect.origin;
00046     mid.x += rect.size.width/2; 
00047     
00048     // color must be set outside!
00049     [line removeAllPoints];
00050     [line moveToPoint:left];
00051     [line lineToPoint:right];
00052     [line lineToPoint:mid];
00053     [line lineToPoint:left];
00054     [line closePath];
00055     [line stroke];
00056 }
00057 
00058 - (void)drawTriangleNotchDownInRect:(NSRect)rect {
00059     
00060     // left is top left
00061     NSPoint left = rect.origin;
00062     // right is left + width
00063     NSPoint right = rect.origin;
00064     right.x += rect.size.width;
00065     // mid is mid bottom
00066     NSPoint mid = rect.origin;
00067     mid.x += rect.size.width/2; 
00068     mid.y += rect.size.height;
00069               
00070     // color must be set outside!
00071     [line removeAllPoints];
00072     [line moveToPoint:left];
00073     [line lineToPoint:right];
00074     [line lineToPoint:mid];
00075     [line lineToPoint:left];
00076     [line closePath];
00077     [line stroke];
00078 }
00079 
00080 - (void) drawCircleInRect:(NSRect) rect {
00081     // color must be set outside!
00082     [line removeAllPoints];
00083     [line appendBezierPathWithOvalInRect:rect];
00084     [line stroke];  
00085 }
00086 
00087 - (void)   drawSpaceShipInRect:(NSRect) rect {
00088     
00089     [line removeAllPoints];
00090     
00091     // soucer section first
00092     NSPoint p;
00093     NSRect r;
00094     
00095     // center x
00096     p.x = rect.origin.x + rect.size.width / 2;
00097     // diameter 1/2 with 1/4 space on each side
00098     p.y = rect.origin.y + rect.size.height / 4;
00099     // draw
00100     [line appendBezierPathWithArcWithCenter:p radius:(rect.size.height / 4)
00101                                  startAngle:0 endAngle:360];
00102     
00103     // create a nascell half hight (lower)  1/5 width
00104     r.origin.x = rect.origin.x;
00105     r.origin.y = rect.origin.y + rect.size.height / 2;
00106     r.size.width = rect.size.width / 5;
00107     r.size.height = rect.size.height / 2;
00108     // add left
00109     [line appendBezierPathWithRect:r];
00110     // calc right (shift 4/5)
00111     r.origin.x += 4 * r.size.width;
00112     [line appendBezierPathWithRect:r];
00113 
00114     // t shape enginering  soucer to nascells
00115     // place nacell 2 steps back and a little up
00116     r.origin.x -= 2 * r.size.width; 
00117     r.origin.y -= (rect.size.width / 5);
00118     [line appendBezierPathWithRect:r];
00119     // cross beam
00120     r = rect; // start with whole and reduce
00121     r.size.height = (rect.size.width / 5);
00122     r.origin.y += (rect.size.height - 2*r.size.height);
00123     [line appendBezierPathWithRect:r];
00124 
00125     // draw it
00126     [line fill];  
00127 }
00128 
00129 - (void) drawDoubleCircleInRect:(NSRect) rect {
00130     // color must be set outside!
00131     [line removeAllPoints];
00132     [line setWindingRule:NSEvenOddWindingRule];
00133     [line appendBezierPathWithOvalInRect:rect];
00134     [line appendBezierPathWithOvalInRect:NSInsetRect(rect, 5, 5)];
00135     [line stroke];  
00136     // reset
00137     [line setWindingRule:[NSBezierPath defaultWindingRule]];
00138 }
00139 
00140 - (NSRect) createRectAroundOrigin:(NSRect)Rect {
00141     NSRect result = Rect;
00142     result.origin.x = -(result.size.width/2);
00143     result.origin.y = -(result.size.height/2);
00144     return result;
00145 }
00146 
00147 - (NSPoint) centreOfRect:(NSRect)Rect { 
00148     NSPoint result = Rect.origin;
00149     result.x += Rect.size.width/2;
00150     result.y += Rect.size.height/2;
00151     return result;
00152 }
00153 
00154 - (void)roundedLabel:(NSRect)aRect withColor:(NSColor *)col {
00155     
00156     float radius = 0.25 * aRect.size.height;
00157     [[col colorWithAlphaComponent:0.5] set];
00158     [self roundedRectangle:aRect withRadius:radius fill:YES];
00159     [col set];
00160     //    [self roundedRectangle:aRect withRadius:radius fill:NO];
00161     // dirty enhancement, we know line still holds the correct figure
00162     [line stroke];
00163 }
00164 
00165 - (void)roundedRectangle:(NSRect)aRect withRadius:(float)radius fill:(bool)fill {
00166     
00167     // make sure it fits
00168     if (aRect.size.height < 2*radius) {
00169         radius = aRect.size.height / 2;
00170     }
00171     if (aRect.size.width < 2*radius) {
00172         radius = aRect.size.width / 2;
00173     }
00174     
00175     NSPoint topMid = NSMakePoint(NSMidX(aRect), NSMaxY(aRect));
00176     NSPoint topLeft = NSMakePoint(NSMinX(aRect), NSMaxY(aRect));
00177     NSPoint topRight = NSMakePoint(NSMaxX(aRect), NSMaxY(aRect));
00178     NSPoint bottomRight = NSMakePoint(NSMaxX(aRect), NSMinY(aRect));
00179     
00180     // color must be set outside!
00181     [line removeAllPoints];
00182     [line moveToPoint:topMid];
00183     [line appendBezierPathWithArcFromPoint:topLeft toPoint:aRect.origin radius:radius];
00184     [line appendBezierPathWithArcFromPoint:aRect.origin toPoint:bottomRight radius:radius];
00185     [line appendBezierPathWithArcFromPoint: bottomRight toPoint:topRight radius:radius];
00186     [line appendBezierPathWithArcFromPoint:topRight toPoint:topLeft radius:radius];
00187     [line closePath];
00188     
00189     if (fill) {
00190         [line fill];
00191     } else {
00192         [line stroke];
00193     }
00194 }
00195 
00196 @end

Generated on Sat Aug 26 21:14:16 2006 for MacTrek by  doxygen 1.4.7