Source

HFUIKit / Classes / HFMultiIndexedTableView / HFSecondaryIndexControl.m

Full commit
//
//  HFSecondaryIndexControl.m
//  GroceryList
//
//  Created by Keith Alperin on 9/25/09.
//  Copyright 2009 Helium Foot Software. All rights reserved.
//

#import "HFSecondaryIndexControl.h"
@interface HFOvalMaskLayer : CALayer {
}
@end

@implementation HFOvalMaskLayer

- (void)drawInContext:(CGContextRef)ctx
{
	
	CGFloat semiMinor = SEMI_MINOR;
	CGFloat semiMajor = SEMI_MAJOR;
	
	CGRect ovalRect = CGRectMake(0, (self.bounds.size.height - (semiMajor * 2))/2.0, (semiMinor * 2), (semiMajor * 2));
	CGContextSetRGBFillColor(ctx, 0, 0, 0, 1.0);
    CGContextFillEllipseInRect(ctx, ovalRect);
}


@end


@interface HFSecondaryIndexControl ()
-(void)drawOval;

@end


@implementation HFSecondaryIndexControl

@synthesize backgroundView = __backgroundView;
@synthesize primaryIndexTitle = __primaryIndexTitle;
@synthesize hideAtDate = __hideAtDate;


- (void)dealloc {
    [__backgroundView release];
    [__primaryIndexTitle release];
    [__hideAtDate release];
    [super dealloc];
}

-(void)drawOval
{
	
	HFOvalMaskLayer *maskLayer = [[HFOvalMaskLayer alloc] init];
	[maskLayer setFrame:self.frame];
	// move the layer
	CGFloat y = self.frame.size.height/2.0;
	CGFloat x = self.frame.size.width/2.0;
	[maskLayer setPosition:CGPointMake(x,y)];
	[maskLayer setNeedsDisplay];	
	[self.layer setNeedsDisplay];
	[self.layer setMask:maskLayer];
	[maskLayer release];
	
}

-(void)showWithPrimaryIndexTitle:(NSString *)title
{
	
	self.primaryIndexTitle = title;
	CGRect secondaryFrame = self.frame;
	secondaryFrame.size.width = SECONDARY_INDEX_WIDTH;
	secondaryFrame.origin.x = 0.0;
	self.frame = secondaryFrame;
	[self setNeedsDisplay];
	
}
-(void)hide
{
	CGRect secondaryFrame = self.frame;
	secondaryFrame.size.width = 0.0;
	self.frame = secondaryFrame;
}

- (void)drawRect:(CGRect)rect {
	[self drawOval];	
	//these need to be derived
	CGFloat semiMinor = SEMI_MINOR;
	CGFloat semiMajor = SEMI_MAJOR;
	

	//so should these
	CGRect ovalRect = CGRectMake(0, (self.frame.size.height/2) - (semiMajor), (semiMinor * 2), (semiMajor * 2));
	CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextClearRect(ctx, rect);
	CGContextSetRGBFillColor(ctx, 0, 0, 0, 0.50);
	CGContextFillRect(ctx, rect);
	CGContextSetStrokeColorWithColor(ctx, [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.65] CGColor]);
	CGContextSetLineWidth(ctx, 3.0);
	CGContextStrokeEllipseInRect(ctx, ovalRect);

	CGFloat colors[] = 
	{
		0.0,0.0,0.0,0.50,
		0.0,0.0,0.0,0.0
	};
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colors, NULL, sizeof(colors)/(sizeof(colors[0]) * 4));
	CFRelease(colorSpace);
	
	CGPoint start, end;

	start = CGPointMake(0,self.bounds.size.height);
	end = CGPointMake(0,self.bounds.size.height - 75);
	CGContextDrawLinearGradient(ctx,gradient, start, end, 0);
	
	CFRelease(gradient);
	
	
	
//    CGContextFillEllipseInRect(ctx, ovalRect);
	

	semiMinor = SEMI_MINOR;
	semiMajor = SEMI_MAJOR;

	
	NSArray *entries = [self.datasource secondaryIndexTitlesForPrimaryIndexTitle:self.primaryIndexTitle];
	NSInteger i = 0;
	[[UIColor colorWithRed:240.0/255.0 green:228.0/255.0 blue:91.0/255.0 alpha:1.0] set];
	
	CGFloat entryHeight = (self.frame.size.height / [entries count]);
	UIFont *font = [UIFont boldSystemFontOfSize:SECONDARY_INDEX_FONT_SIZE];
	for (NSString *entry in entries) {
		
		CGSize entrySize = [entry sizeWithFont:font];
		
		CGFloat entryTopY = entryHeight * i;
		CGFloat entryMiddleYInViewSpace = (entryTopY + (entryHeight / 2.0));
		//we want the middle of the elipse, so we subtract from half the height of the view
		CGFloat entryMiddleYInOvalSpace = (self.frame.size.height/2.0) - entryMiddleYInViewSpace;
		CGFloat y = entryMiddleYInViewSpace - (entrySize.height / 2.0);
		CGFloat x = 6.0 +  (semiMinor - sqrt(pow(semiMinor,2.0) - ((pow(entryMiddleYInOvalSpace,2.0) * pow(semiMinor,2.0))/pow(semiMajor, 2.0))));
		
		
		//keith says:  we should draw these in CALayers so that we can animate them more easily
		CGSize myShadowOffset = CGSizeMake(0, 0);
		float myColorValues[] = {0, 0, 0, .99};

		CGContextRef myContext = UIGraphicsGetCurrentContext();
		CGContextSaveGState(myContext);
		
		CGContextSetShadow (myContext, myShadowOffset, 4);
		CGColorSpaceRef myColorSpace = CGColorSpaceCreateDeviceRGB();
		CGColorRef myColor = CGColorCreate(myColorSpace, myColorValues);
		CGContextSetShadowWithColor (myContext, myShadowOffset, 4, myColor);

		[entry drawAtPoint:CGPointMake(x, y) withFont:font];
		
		
		CGColorRelease(myColor);
		CGColorSpaceRelease(myColorSpace); 
		
		CGContextRestoreGState(myContext);
		
		i++;
		
		
		
	}
	
}
- (void) drawTextInRect:(CGRect)rect {
    CGSize myShadowOffset = CGSizeMake(4, -4);
    float myColorValues[] = {0, 0, 0, .8};
	
    CGContextRef myContext = UIGraphicsGetCurrentContext();
    CGContextSaveGState(myContext);
	
    CGContextSetShadow (myContext, myShadowOffset, 1);
    CGColorSpaceRef myColorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef myColor = CGColorCreate(myColorSpace, myColorValues);
    CGContextSetShadowWithColor (myContext, myShadowOffset, 5, myColor);
	
	
    CGColorRelease(myColor);
    CGColorSpaceRelease(myColorSpace); 
	
    CGContextRestoreGState(myContext);
}




@end