project_files/HedgewarsMobile/Classes/MGSplitViewController/MGSplitCornersView.m
author Wuzzy <almikes@aol.com>
Sat, 30 Sep 2017 23:52:08 +0200
changeset 12627 07fdda8c13a2
parent 11221 e68b3e392091
permissions -rwxr-xr-x
TrophyRace: Fix game never eliminating any hogs after a hog skipped or ran out of time Warning: This commit _might_ invalidate past records, but I'm not sure if this is actually the case. Note that only the eliminiation part of the script is touched, not the actual race logic. Even if records are actually broken by this, I and sheepluva have decided that it's more imporant to fix this very, VERY stupid and old bug than to preserve records.

//
//  MGSplitCornersView.m
//  MGSplitView
//
//  Created by Matt Gemmell on 28/07/2010.
//  Copyright 2010 Instinctive Code.
//

#import "MGSplitCornersView.h"


@implementation MGSplitCornersView


#pragma mark -
#pragma mark Setup and teardown


- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame])) {
		self.contentMode = UIViewContentModeRedraw;
		self.userInteractionEnabled = NO;
		self.opaque = NO;
		self.backgroundColor = [UIColor clearColor];
		cornerRadius = 0.0; // actual value is set by the splitViewController.
		cornersPosition = MGCornersPositionLeadingVertical;
    }
	
    return self;
}


- (void)dealloc
{
	self.cornerBackgroundColor = nil;
}


#pragma mark -
#pragma mark Geometry helpers


static double deg2Rad(double degrees)
{
    // Converts degrees to radians.
    return degrees * (M_PI / 180.0);
}


#pragma mark -
#pragma mark Drawing


- (void)drawRect:(CGRect)rect
{
	// Draw two appropriate corners, with cornerBackgroundColor behind them.
	if (cornerRadius > 0) {
		
		float maxX = CGRectGetMaxX(self.bounds);
		float maxY = CGRectGetMaxY(self.bounds);
		UIBezierPath *path = [UIBezierPath bezierPath];
		CGPoint pt = CGPointZero;
		switch (cornersPosition) {
			case MGCornersPositionLeadingVertical: // top of screen for a left/right split
				[path moveToPoint:pt];
				pt.y += cornerRadius;
				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:(float)deg2Rad(90) endAngle:0 clockwise:YES]];
				pt.x += cornerRadius;
				pt.y -= cornerRadius;
				[path addLineToPoint:pt];
				[path addLineToPoint:CGPointZero];
				[path closePath];
				
				pt.x = maxX - cornerRadius;
				pt.y = 0;
				[path moveToPoint:pt];
				pt.y = maxY;
				[path addLineToPoint:pt];
				pt.x += cornerRadius;
				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:(float)deg2Rad(180) endAngle:(float)deg2Rad(90) clockwise:YES]];
				pt.y -= cornerRadius;
				[path addLineToPoint:pt];
				pt.x -= cornerRadius;
				[path addLineToPoint:pt];
				[path closePath];
				
				break;
				
			case MGCornersPositionTrailingVertical: // bottom of screen for a left/right split
				pt.y = maxY;
				[path moveToPoint:pt];
				pt.y -= cornerRadius;
				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:(float)deg2Rad(270) endAngle:(float)deg2Rad(360) clockwise:NO]];
				pt.x += cornerRadius;
				pt.y += cornerRadius;
				[path addLineToPoint:pt];
				pt.x -= cornerRadius;
				[path addLineToPoint:pt];
				[path closePath];
				
				pt.x = maxX - cornerRadius;
				pt.y = maxY;
				[path moveToPoint:pt];
				pt.y -= cornerRadius;
				[path addLineToPoint:pt];
				pt.x += cornerRadius;
				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:(float)deg2Rad(180) endAngle:(float)deg2Rad(270) clockwise:NO]];
				pt.y += cornerRadius;
				[path addLineToPoint:pt];
				pt.x -= cornerRadius;
				[path addLineToPoint:pt];
				[path closePath];
				
				break;
				
			case MGCornersPositionLeadingHorizontal: // left of screen for a top/bottom split
				pt.x = 0;
				pt.y = cornerRadius;
				[path moveToPoint:pt];
				pt.y -= cornerRadius;
				[path addLineToPoint:pt];
				pt.x += cornerRadius;
				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:(float)deg2Rad(180) endAngle:(float)deg2Rad(270) clockwise:NO]];
				pt.y += cornerRadius;
				[path addLineToPoint:pt];
				pt.x -= cornerRadius;
				[path addLineToPoint:pt];
				[path closePath];
				
				pt.x = 0;
				pt.y = maxY - cornerRadius;
				[path moveToPoint:pt];
				pt.y = maxY;
				[path addLineToPoint:pt];
				pt.x += cornerRadius;
				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:(float)deg2Rad(180) endAngle:(float)deg2Rad(90) clockwise:YES]];
				pt.y -= cornerRadius;
				[path addLineToPoint:pt];
				pt.x -= cornerRadius;
				[path addLineToPoint:pt];
				[path closePath];
				
				break;
				
			case MGCornersPositionTrailingHorizontal: // right of screen for a top/bottom split
				pt.y = cornerRadius;
				[path moveToPoint:pt];
				pt.y -= cornerRadius;
				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:(float)deg2Rad(270) endAngle:(float)deg2Rad(360) clockwise:NO]];
				pt.x += cornerRadius;
				pt.y += cornerRadius;
				[path addLineToPoint:pt];
				pt.x -= cornerRadius;
				[path addLineToPoint:pt];
				[path closePath];
				
				pt.y = maxY - cornerRadius;
				[path moveToPoint:pt];
				pt.y += cornerRadius;
				[path appendPath:[UIBezierPath bezierPathWithArcCenter:pt radius:cornerRadius startAngle:(float)deg2Rad(90) endAngle:0 clockwise:YES]];
				pt.x += cornerRadius;
				pt.y -= cornerRadius;
				[path addLineToPoint:pt];
				pt.x -= cornerRadius;
				[path addLineToPoint:pt];
				[path closePath];
				
				break;
				
			default:
				break;
		}
		
		[self.cornerBackgroundColor set];
		[path fill];
	}
}


#pragma mark -
#pragma mark Accessors and properties


- (void)setCornerRadius:(float)newRadius
{
	if (newRadius != cornerRadius) {
		cornerRadius = newRadius;
		[self setNeedsDisplay];
	}
}


- (void)setSplitViewController:(MGSplitViewController *)theController
{
	if (theController != splitViewController) {
		splitViewController = theController;
		[self setNeedsDisplay];
	}
}


- (void)setCornersPosition:(MGCornersPosition)posn
{
	if (cornersPosition != posn) {
		cornersPosition = posn;
		[self setNeedsDisplay];
	}
}


- (void)setCornerBackgroundColor:(UIColor *)color
{
	if (color != cornerBackgroundColor) {
		cornerBackgroundColor = color;
		[self setNeedsDisplay];
	}
}


@synthesize cornerRadius;
@synthesize splitViewController;
@synthesize cornersPosition;
@synthesize cornerBackgroundColor;


@end