All pastes #832912 Raw Edit

Anonym

public text v1 · immutable
#832912 ·published 2007-12-26 19:39 UTC
rendered paste body
#import "ButtonBarView.h"

@implementation IWebCams

static IWebCams *sharedInstance;

+ (IWebCams *)sharedInstance
{
	if (!sharedInstance) {
		sharedInstance = [[IWebCams alloc] init];
		//NSLog(@"sharedInstance: creating new inst");
	}
	
	//NSLog(@"returning sharedinst");
	return sharedInstance;
}
/*
- (void)deviceOrientationChanged:(struct __GSEvent *)fp8
{
	NSLog(@"Orientation change");
	int orientation = [UIHardware deviceOrientation: YES];
	NSLog(@"Orientation: %d", orientation);
	[buttonBarView setTransform:CGAffineTransformIdentity];
	float angle = 90.0 * orientation;
	[buttonBarView setRotationBy:angle];
	[super deviceOrientationChanged:fp8];
}
*/
- (void) applicationDidFinishLaunching:(NSNotification *)aNotification
{	
	sharedInstance = self;
	
	struct CGRect frame = [UIHardware fullScreenApplicationContentRect];
	frame.origin.x = frame.origin.y = 0.0f;
	mainWindow = [[UIWindow alloc] initWithContentRect: frame];
	
	// The "master" view that contains other views
	buttonBarView = [[ButtonBarView alloc] initWithFrame:frame];
	
	[super setStatusBarMode:0 duration:0]; 
	[mainWindow orderFront: self];
	[mainWindow makeKey: self];
	[mainWindow _setHidden: NO];	
	[mainWindow setContentView: buttonBarView]; 
	[buttonBarView release];
}

@end

@implementation ButtonBarView

-(id)initWithFrame:(CGRect)frame
{
	if ((self == [super initWithFrame:frame])!=nil) {
		
		barStatus = 1;
		
		_offScreenRect = frame;
		_onScreenRect = frame;
		_onScreenRect.origin.x = 0.0f;
		rect = [UIHardware fullScreenApplicationContentRect];
		rect.origin.x = rect.origin.y = 0.0f;
		rect.size.height = rect.size.height-40.0f;
		
		// Create some colors
		float black[4] = {0, 0, 0, 0};
		
		// Create your views
		locationsView = [ [ LocationsView alloc ] initWithFrame: rect app:self];
		locationsItemView = [[LocationsItemView alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.x, rect.size.width, rect.size.height+40.0f) app:self];
		aboutView = [ [ AboutView alloc ] initWithFrame:rect ];
		showView = [ [ ShowView alloc ] initWithFrame: rect app:self row:0];
		
		[locationsView setBackgroundColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), black)];
		[locationsItemView setBackgroundColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), black)];
		[aboutView setBackgroundColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), black)];
		[showView setBackgroundColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), black)];

		// Locations Page
		[locationsView setDelegate:self];
		
		// About Page
		[aboutView setDelegate:self];

		// Show Page
		[showView setDelegate:self];

		// _transitionView is used for handling transitions of the views only
		// _barTransitionView is used to handle the button bars transitioning
		_transitionView = [ self createTransitionView ];
		_barTransitionView = [ self createBarTransitionView ];
		_buttonBar = [ self createButtonBar ];
		
		[ self addSubview: _transitionView ];
		[ self addSubview: _barTransitionView ];
		
		[ _transitionView transition:6 toView:showView ];
		[ _barTransitionView transition:6 toView:_buttonBar ];                  
		
	}
	return self;
}

-(void)fillTable
{
	NSLog(@"buttonBarView fillTable");
	[locationList release];
	locationList =  [[NSMutableArray alloc] init];
	[urlList release];
	urlList =  [[NSMutableArray alloc] init];
	[imageList release];
	imageList = [[NSMutableArray alloc] init];
	
	NSDictionary *locationDict = [NSDictionary dictionaryWithContentsOfFile: @"/Applications/iWebCams.app/locations.plist"];
	int locationCount = [[locationDict allKeys] count];
	int i;
	NSString *currentName = nil;
	NSString *currentURL = nil;
	id currentItem = nil;
	NSString *currentKey = nil;
	
	for (i = 0; i < locationCount; i++)
	{
		currentKey = [[locationDict allKeys] objectAtIndex:i];
		currentItem = [locationDict valueForKey:currentKey];
		currentName = [currentItem valueForKey:@"name"];
		currentURL = [currentItem valueForKey:@"url"];
		[locationList addObject:currentName];
		[urlList addObject:currentURL];

		NSData *imageData = [[NSData dataWithContentsOfURL:[NSURL URLWithString:currentURL]] retain];
		[imageList addObject:imageData];
	}
	
	NSLog(@"Dict: %@", locationDict);
	NSLog(@"Locations: %@", locationList);
	NSLog(@"URLs: %@", urlList);
}

-(void)storeData
{
	NSLog(@"Store changed data");
	NSString *error;
	
	int locationCount = [locationList count];
	NSMutableDictionary *locationDict = [[[NSMutableDictionary alloc] initWithCapacity: locationCount] autorelease];

	int i;
	for (i = 0; i < locationCount; i++)
	{
		NSLog(@"loop %d",i);
		NSDictionary *elementDict = [NSDictionary dictionaryWithObjectsAndKeys:
																 [locationList objectAtIndex:i],
																	@"name",
																 [urlList objectAtIndex:i],
																	@"url",
																	nil
																];
		[locationDict setObject: elementDict forKey:[NSString stringWithFormat:@"%d", i]];
	}
	NSLog(@"New Dictionary: %@", locationDict);
	//Seralize settings dictionary
	NSData *rawPList = [NSPropertyListSerialization dataFromPropertyList: locationDict format: NSPropertyListXMLFormat_v1_0 errorDescription: &error];
	
	//Write settings plist file
	[rawPList writeToFile: [@"/Applications/iWebCams.app/locations.plist" retain] atomically: YES];
	
	[locationsView reloadData];
	[showView reloadData:0];
}

- (NSMutableArray *)getLocationList
{
	return locationList;
}

- (NSMutableArray *)getURLList
{
	return urlList;
}

- (NSMutableArray *)getImageList
{
	return imageList;
}

- (void)changeLocation:(int)row name:(NSString *)name url:(NSString *)url
{
	NSData *imageData = [[NSData dataWithContentsOfURL:[NSURL URLWithString:url]] retain];

	if (row == -1)
	{
		[locationList addObject:name];
		[urlList addObject:url];	
		[imageList addObject:imageData];
	} else {
		[locationList replaceObjectAtIndex:row withObject:name];
		[urlList replaceObjectAtIndex:row withObject:url];	
		[imageList replaceObjectAtIndex:row withObject:imageData];
	}
	[self storeData];
}

- (void)removeLocation:(int)row
{
	[locationList removeObjectAtIndex:row];
	[urlList removeObjectAtIndex:row];
	[imageList removeObjectAtIndex:row];
	[self storeData];
}

- (void)swapLocations:(int)row toRow:(int)toRow
{
	int i;
	if (row<toRow)
	{
		for (i = row; i < toRow; i++)
		{
			[locationList exchangeObjectAtIndex: i withObjectAtIndex: i+1];
			[urlList exchangeObjectAtIndex: i withObjectAtIndex: i+1];
			[imageList exchangeObjectAtIndex: i withObjectAtIndex: i+1];
		}
	} else if (row>toRow)
	{
		for (i = row; i > toRow; i--)
		{
			[locationList exchangeObjectAtIndex: i withObjectAtIndex: i-1];
			[urlList exchangeObjectAtIndex: i withObjectAtIndex: i-1];
			[imageList exchangeObjectAtIndex: i withObjectAtIndex: i-1];
		}		
	}
	[self storeData];
}

////////////// Transitions ////////////////
- (void)transToLocationsItemView:(int)row
{
	[locationsItemView setItem:(row)];
	currentView = CB_9;
	[ _barTransitionView transition:5 fromView:_buttonBar toView:blankView ];

	[_transitionView transition:1 fromView:locationsView toView:locationsItemView];
	barStatus = 0;
}

- (void)transToLocationsView
{
	currentView = CB_2;
	[_transitionView transition:2 fromView:locationsItemView toView:locationsView];
	
	[ _barTransitionView transition:3 fromView:blankView toView:_buttonBar];
	barStatus = 1;
}

- (UITransitionView *)createTransitionView
{
	UITransitionView *transitionView = [ [ UITransitionView alloc ] initWithFrame:CGRectMake(0,0,320,460)];
	return transitionView;
}

- (UITransitionView *)createBarTransitionView
{
	struct CGRect buttonBarRect = [UIHardware fullScreenApplicationContentRect];
	buttonBarRect.origin.x = 0.0f;
	buttonBarRect.origin.y = buttonBarRect.size.height-49.0f;
	buttonBarRect.size.height = 49.0f;

	UITransitionView *transitionBarView = [ [ UITransitionView alloc ] initWithFrame:buttonBarRect];
	// Create empty(blank) view for to transition the button bar to
	blankView = [ [ UIView alloc ] initWithFrame:buttonBarRect];
	
	return transitionBarView;
}

- (UIButtonBar *)createButtonBar
{
	struct CGRect buttonBarRect = [UIHardware fullScreenApplicationContentRect];
	buttonBarRect.origin.x = buttonBarRect.origin.y = 0.0f;
	buttonBarRect.size.height = 49.0f;
	
	UIButtonBar *buttonBar;
	buttonBar = [ [ UIButtonBar alloc ] 
							 initInView: self
							 withFrame: buttonBarRect
							 withItemList: [ self buttonBarItems ] ];
	[buttonBar setDelegate:self];
	[buttonBar setBarStyle:1];
	[buttonBar setButtonBarTrackingMode: 2];
	
	int buttons[3] = { 1, 2, 3};
	[buttonBar registerButtonGroup:0 withButtons:buttons withCount: 3];
	[buttonBar showButtonGroup: 0 withDuration: 0.0f];
	int tag;
	
	for(tag = 1; tag < 3; tag++) {
		[ [ buttonBar viewWithTag:tag ] 
		 setFrame:CGRectMake(2.0f + ((tag - 1) * 106.0f), 1.0f, 106.0f, 48.0f)
		 ];
	}
	[ buttonBar showSelectionForButton: 1];
	
	return buttonBar;
}

- (NSArray *)buttonBarItems
{
	return [ NSArray arrayWithObjects:
					[ NSDictionary dictionaryWithObjectsAndKeys:
					 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
					 @"buttonShowOn.png", kUIButtonBarButtonInfo,
					 @"buttonShowOff.png", kUIButtonBarButtonSelectedInfo,
					 [ NSNumber numberWithInt: 1], kUIButtonBarButtonTag,
					 self, kUIButtonBarButtonTarget,
					 @"Show", kUIButtonBarButtonTitle,
					 @"0", kUIButtonBarButtonType,
					 nil 
					 ],
					
					[ NSDictionary dictionaryWithObjectsAndKeys:
					 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
					 @"buttonSettingsOn.png", kUIButtonBarButtonInfo,
					 @"buttonSettingsOff.png", kUIButtonBarButtonSelectedInfo,
					 [ NSNumber numberWithInt: 2], kUIButtonBarButtonTag,
					 self, kUIButtonBarButtonTarget,
					 @"Locations", kUIButtonBarButtonTitle,
					 @"0", kUIButtonBarButtonType,
					 nil 
					 ], 
					
					[ NSDictionary dictionaryWithObjectsAndKeys:
					 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
					 @"buttonInfoOn.png", kUIButtonBarButtonInfo,
					 @"buttonInfoOff.png", kUIButtonBarButtonSelectedInfo,
					 [ NSNumber numberWithInt: 3], kUIButtonBarButtonTag,
					 self, kUIButtonBarButtonTarget,
					 @"About", kUIButtonBarButtonTitle,
					 @"0", kUIButtonBarButtonType,
					 nil 
					 ], 
					
					nil
					];
}

- (void)buttonBarItemTapped:(id) sender
{
	int button = [ sender tag ];
	switch (button) {
		case 1:
				if (currentView == CB_1) { }
				else
				{
					[self testAnimation:@"fromRight"];
					[ _transitionView transition:0 toView:showView ];
					currentView = CB_1;
				}
				break;
			case 2:
				if (currentView == CB_2) { }
				else
				{
					if (currentView == CB_1) {
						[self testAnimation:@"fromLeft"];
					} else {
						[self testAnimation:@"fromRight"];
					}
					[ _transitionView transition:0 toView:locationsView ];
					currentView = CB_2;
				}
				break;
			case 3:
			if (currentView == CB_3) { }
			else
			{
				[self testAnimation:@"fromLeft"];
				[ _transitionView transition:0 toView:aboutView ];
				currentView = CB_3;
			}
			break;
	}
	
}


- (void)reloadButtonBar
{
	[ _buttonBar removeFromSuperview ];
	[ _buttonBar release ];
	_buttonBar = [ self createButtonBar ];
}

/*
// Gets a mouse down event in the rect, but you can use it to catch mouse event in any rect you want
- (void)mouseDown:(GSEvent *)event
{
	NSLog(@"%s", _cmd);
	
	CGPoint point = GSEventGetLocationInWindow(event);
	if (CGRectContainsPoint(rect, point)) {
		if (!barStatus) {
			[ _barTransitionView transition:3 fromView:blankView toView:_buttonBar];
			barStatus = 1;
		}
		else {
			[ _barTransitionView transition:5 fromView:_buttonBar toView:blankView ];
			barStatus = 0;
		}
	}
}
*/

// Sample layerkit animation
-(void)testAnimation:(NSString *)direction
{
	LKAnimation *animation = [LKTransition animation];
  [animation setType: @"oglFlip"];
  [animation setTimingFunction: [LKTimingFunction functionWithName: @"easeInEaseOut"]];
  [animation setFillMode: @"extended"];
  [animation setTransitionFlags: 3];
  [animation setDuration: 10];
  [animation setSpeed:0.35];
  [animation setSubtype: direction];
	[[self _layer] addAnimation: animation forKey: 0];
}


@end