project_files/HedgewarsMobile/MGSplitCornersView.m
changeset 6658 2cccf6b2b89d
equal deleted inserted replaced
6657:e1125559359f 6658:2cccf6b2b89d
       
     1 //
       
     2 //  MGSplitCornersView.m
       
     3 //  MGSplitView
       
     4 //
       
     5 //  Created by Matt Gemmell on 28/07/2010.
       
     6 //  Copyright 2010 Instinctive Code.
       
     7 //
       
     8 
       
     9 #import "MGSplitCornersView.h"
       
    10 
       
    11 
       
    12 @implementation MGSplitCornersView
       
    13 
       
    14 
       
    15 #pragma mark -
       
    16 #pragma mark Setup and teardown
       
    17 
       
    18 
       
    19 - (id)initWithFrame:(CGRect)frame
       
    20 {
       
    21     if ((self = [super initWithFrame:frame])) {
       
    22 		self.contentMode = UIViewContentModeRedraw;
       
    23 		self.userInteractionEnabled = NO;
       
    24 		self.opaque = NO;
       
    25 		self.backgroundColor = [UIColor clearColor];
       
    26 		cornerRadius = 0.0; // actual value is set by the splitViewController.
       
    27 		cornersPosition = MGCornersPositionLeadingVertical;
       
    28     }
       
    29 	
       
    30     return self;
       
    31 }
       
    32 
       
    33 
       
    34 - (void)dealloc
       
    35 {
       
    36 	self.cornerBackgroundColor = nil;
       
    37 	
       
    38 	[super dealloc];
       
    39 }
       
    40 
       
    41 
       
    42 #pragma mark -
       
    43 #pragma mark Geometry helpers
       
    44 
       
    45 
       
    46 double deg2Rad(double degrees)
       
    47 {
       
    48 	// Converts degrees to radians.
       
    49 	return degrees * (M_PI / 180.0);
       
    50 }
       
    51 
       
    52 
       
    53 double rad2Deg(double radians)
       
    54 {
       
    55 	// Converts radians to degrees.
       
    56 	return radians * (180 / M_PI);
       
    57 }
       
    58 
       
    59 
       
    60 #pragma mark -
       
    61 #pragma mark Drawing
       
    62 
       
    63 
       
    64 - (void)drawRect:(CGRect)rect
       
    65 {
       
    66 	// Draw two appropriate corners, with cornerBackgroundColor behind them.
       
    67 	if (cornerRadius > 0) {
       
    68 		if (NO) { // just for debugging.
       
    69 			[[UIColor redColor] set];
       
    70 			UIRectFill(self.bounds);
       
    71 		}
       
    72 		
       
    73 		float maxX = CGRectGetMaxX(self.bounds);
       
    74 		float maxY = CGRectGetMaxY(self.bounds);
       
    75 		UIBezierPath *path = [UIBezierPath bezierPath];
       
    76 		CGPoint pt = CGPointZero;
       
    77 		switch (cornersPosition) {
       
    78 			case MGCornersPositionLeadingVertical: // top of screen for a left/right split
       
    79 				[path moveToPoint:pt];
       
    80 				pt.y += cornerRadius;
       
    81 				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(90) endAngle:0 clockwise:YES]];
       
    82 				pt.x += cornerRadius;
       
    83 				pt.y -= cornerRadius;
       
    84 				[path addLineToPoint:pt];
       
    85 				[path addLineToPoint:CGPointZero];
       
    86 				[path closePath];
       
    87 				
       
    88 				pt.x = maxX - cornerRadius;
       
    89 				pt.y = 0;
       
    90 				[path moveToPoint:pt];
       
    91 				pt.y = maxY;
       
    92 				[path addLineToPoint:pt];
       
    93 				pt.x += cornerRadius;
       
    94 				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(180) endAngle:deg2Rad(90) clockwise:YES]];
       
    95 				pt.y -= cornerRadius;
       
    96 				[path addLineToPoint:pt];
       
    97 				pt.x -= cornerRadius;
       
    98 				[path addLineToPoint:pt];
       
    99 				[path closePath];
       
   100 				
       
   101 				break;
       
   102 				
       
   103 			case MGCornersPositionTrailingVertical: // bottom of screen for a left/right split
       
   104 				pt.y = maxY;
       
   105 				[path moveToPoint:pt];
       
   106 				pt.y -= cornerRadius;
       
   107 				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(270) endAngle:deg2Rad(360) clockwise:NO]];
       
   108 				pt.x += cornerRadius;
       
   109 				pt.y += cornerRadius;
       
   110 				[path addLineToPoint:pt];
       
   111 				pt.x -= cornerRadius;
       
   112 				[path addLineToPoint:pt];
       
   113 				[path closePath];
       
   114 				
       
   115 				pt.x = maxX - cornerRadius;
       
   116 				pt.y = maxY;
       
   117 				[path moveToPoint:pt];
       
   118 				pt.y -= cornerRadius;
       
   119 				[path addLineToPoint:pt];
       
   120 				pt.x += cornerRadius;
       
   121 				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(180) endAngle:deg2Rad(270) clockwise:NO]];
       
   122 				pt.y += cornerRadius;
       
   123 				[path addLineToPoint:pt];
       
   124 				pt.x -= cornerRadius;
       
   125 				[path addLineToPoint:pt];
       
   126 				[path closePath];
       
   127 				
       
   128 				break;
       
   129 				
       
   130 			case MGCornersPositionLeadingHorizontal: // left of screen for a top/bottom split
       
   131 				pt.x = 0;
       
   132 				pt.y = cornerRadius;
       
   133 				[path moveToPoint:pt];
       
   134 				pt.y -= cornerRadius;
       
   135 				[path addLineToPoint:pt];
       
   136 				pt.x += cornerRadius;
       
   137 				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(180) endAngle:deg2Rad(270) clockwise:NO]];
       
   138 				pt.y += cornerRadius;
       
   139 				[path addLineToPoint:pt];
       
   140 				pt.x -= cornerRadius;
       
   141 				[path addLineToPoint:pt];
       
   142 				[path closePath];
       
   143 				
       
   144 				pt.x = 0;
       
   145 				pt.y = maxY - cornerRadius;
       
   146 				[path moveToPoint:pt];
       
   147 				pt.y = maxY;
       
   148 				[path addLineToPoint:pt];
       
   149 				pt.x += cornerRadius;
       
   150 				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(180) endAngle:deg2Rad(90) clockwise:YES]];
       
   151 				pt.y -= cornerRadius;
       
   152 				[path addLineToPoint:pt];
       
   153 				pt.x -= cornerRadius;
       
   154 				[path addLineToPoint:pt];
       
   155 				[path closePath];
       
   156 				
       
   157 				break;
       
   158 				
       
   159 			case MGCornersPositionTrailingHorizontal: // right of screen for a top/bottom split
       
   160 				pt.y = cornerRadius;
       
   161 				[path moveToPoint:pt];
       
   162 				pt.y -= cornerRadius;
       
   163 				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(270) endAngle:deg2Rad(360) clockwise:NO]];
       
   164 				pt.x += cornerRadius;
       
   165 				pt.y += cornerRadius;
       
   166 				[path addLineToPoint:pt];
       
   167 				pt.x -= cornerRadius;
       
   168 				[path addLineToPoint:pt];
       
   169 				[path closePath];
       
   170 				
       
   171 				pt.y = maxY - cornerRadius;
       
   172 				[path moveToPoint:pt];
       
   173 				pt.y += cornerRadius;
       
   174 				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:deg2Rad(90) endAngle:0 clockwise:YES]];
       
   175 				pt.x += cornerRadius;
       
   176 				pt.y -= cornerRadius;
       
   177 				[path addLineToPoint:pt];
       
   178 				pt.x -= cornerRadius;
       
   179 				[path addLineToPoint:pt];
       
   180 				[path closePath];
       
   181 				
       
   182 				break;
       
   183 				
       
   184 			default:
       
   185 				break;
       
   186 		}
       
   187 		
       
   188 		[self.cornerBackgroundColor set];
       
   189 		[path fill];
       
   190 	}
       
   191 }
       
   192 
       
   193 
       
   194 #pragma mark -
       
   195 #pragma mark Accessors and properties
       
   196 
       
   197 
       
   198 - (void)setCornerRadius:(float)newRadius
       
   199 {
       
   200 	if (newRadius != cornerRadius) {
       
   201 		cornerRadius = newRadius;
       
   202 		[self setNeedsDisplay];
       
   203 	}
       
   204 }
       
   205 
       
   206 
       
   207 - (void)setSplitViewController:(MGSplitViewController *)theController
       
   208 {
       
   209 	if (theController != splitViewController) {
       
   210 		splitViewController = theController;
       
   211 		[self setNeedsDisplay];
       
   212 	}
       
   213 }
       
   214 
       
   215 
       
   216 - (void)setCornersPosition:(MGCornersPosition)posn
       
   217 {
       
   218 	if (cornersPosition != posn) {
       
   219 		cornersPosition = posn;
       
   220 		[self setNeedsDisplay];
       
   221 	}
       
   222 }
       
   223 
       
   224 
       
   225 - (void)setCornerBackgroundColor:(UIColor *)color
       
   226 {
       
   227 	if (color != cornerBackgroundColor) {
       
   228 		[cornerBackgroundColor release];
       
   229 		cornerBackgroundColor = [color retain];
       
   230 		[self setNeedsDisplay];
       
   231 	}
       
   232 }
       
   233 
       
   234 
       
   235 @synthesize cornerRadius;
       
   236 @synthesize splitViewController;
       
   237 @synthesize cornersPosition;
       
   238 @synthesize cornerBackgroundColor;
       
   239 
       
   240 
       
   241 @end