cocoaTouch/SettingsViewController.m
author koda
Thu, 14 Jan 2010 16:38:43 +0000
changeset 2694 dcd248e04f3d
parent 2687 28b8330b8af1
child 2696 41aa7b56c17b
permissions -rw-r--r--
can use latest sdlimage, work on setting panel and option when launching the game, minor fixes

//
//  SettingsViewController.m
//  hwengine
//
//  Created by Vittorio on 08/01/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SettingsViewController.h"


@implementation SettingsViewController

@synthesize username, password, musicOn, effectsOn, volumeSlider, volumeLabel;

-(NSString *)dataFilePath: (NSString *)fileName {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	return [documentsDirectory stringByAppendingPathComponent:fileName];
}


-(void) viewDidLoad {
	NSString *filePath = [self dataFilePath:@"settings.plist"];
	
	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {	
		NSUserDefaults *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
		username.text = [data objectForKey:@"username"];
		password.text = [data objectForKey:@"password"];
		if (1 == [[data objectForKey:@"music"] intValue]) {
			musicOn.on = YES;
		} else {
			musicOn.on = NO;
		}
		if (1 == [[data objectForKey:@"effects"] intValue]) {
			effectsOn.on = YES;
		} else {
			effectsOn.on = NO;
		}
		[volumeSlider setValue:[[data objectForKey:@"volume"] intValue] animated:NO];
		NSString *tmpVol = [[NSString alloc] initWithFormat:@"%d", (int) volumeSlider.value];
		volumeLabel.text = tmpVol;
		[tmpVol release];
	}
	
	
/*	
	UIApplication *app = [UIApplication sharedApplication];
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(applicationWillTerminate:)
												 name:UIApplicationWillTerminateNotification
											   object:app];
*/	[super viewDidLoad];
}

-(void) viewDidUnload {
	self.username = nil;
	self.password = nil;
	self.musicOn = nil;
	self.effectsOn = nil;
	self.volumeLabel = nil;
	self.volumeSlider = nil;
	[super viewDidUnload];
}

//- (void)applicationWillTerminate:(NSNotification *)notification {
-(void) viewWillDisappear:(BOOL)animated {
	NSMutableDictionary *saveArray = [[NSMutableDictionary alloc] init];
	NSString *tmpMus = (musicOn.on) ? @"1" : @"0";
	NSString *tmpEff = (effectsOn.on) ? @"1" : @"0";
	
	[saveArray setObject:username.text forKey:@"username"];
	[saveArray setObject:password.text forKey:@"password"];
	[saveArray setObject:tmpMus forKey:@"music"];
	[saveArray setObject:tmpEff forKey:@"effects"];
	[saveArray setObject:volumeLabel.text forKey:@"volume"];
	
	[saveArray writeToFile:[self dataFilePath:@"settings.plist"] atomically:YES];
	[saveArray release];
	[super viewWillDisappear:animated];
}
 
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

-(IBAction) backgroundTap: (id)sender {
	[username resignFirstResponder];
	[password resignFirstResponder];
}

-(IBAction) textFieldDoneEditing: (id)sender {
	[sender resignFirstResponder];
}

-(IBAction) sliderChanged: (id) sender {
	UISlider *slider = (UISlider *)sender;
	int progress = slider.value;
	NSString *newLabel = [[NSString alloc] initWithFormat:@"%d",progress];
	self.volumeLabel.text = newLabel;
	[newLabel release];
}

-(void) dealloc {
	[username release];
	[password release];
	[musicOn release];
	[effectsOn release];
	[volumeLabel release];
	[volumeSlider release];
    [super dealloc];
}


@end