cocoaTouch/SettingsViewController.m
changeset 2702 48fc46a922fd
parent 2701 3a8560c00f78
child 2720 a5111ec4d25f
equal deleted inserted replaced
2701:3a8560c00f78 2702:48fc46a922fd
     9 #import "SettingsViewController.h"
     9 #import "SettingsViewController.h"
    10 #import "SDL_uikitappdelegate.h"
    10 #import "SDL_uikitappdelegate.h"
    11 
    11 
    12 @implementation SettingsViewController
    12 @implementation SettingsViewController
    13 
    13 
    14 @synthesize username, password, musicSwitch, effectsSwitch, altDamageSwitch, volumeSlider, volumeLabel, table, volumeCell;
    14 @synthesize username, password, musicSwitch, soundsSwitch, altDamageSwitch, volumeSlider, volumeLabel, table, volumeCell;
    15 
    15 
    16 
    16 
    17 -(void) loadView {
    17 -(void) loadView {
    18 	self.musicSwitch = [[UISwitch alloc] init];
    18 	self.musicSwitch = [[UISwitch alloc] init];
    19 	self.effectsSwitch = [[UISwitch alloc] init];
    19 	self.soundsSwitch = [[UISwitch alloc] init];
    20 	self.altDamageSwitch = [[UISwitch alloc] init];
    20 	self.altDamageSwitch = [[UISwitch alloc] init];
       
    21 	[self.soundsSwitch addTarget:self action:@selector(sameValueSwitch) forControlEvents:UIControlEventValueChanged];
       
    22 	[self.musicSwitch addTarget:self action:@selector(checkValueSwitch) forControlEvents:UIControlEventValueChanged];
       
    23 
    21 	[super loadView];
    24 	[super loadView];
    22 }
    25 }
    23 
    26 
    24 -(void) viewDidLoad {
    27 -(void) viewDidLoad {
    25 	NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"];
    28 	NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"];
    31 		if (1 == [[data objectForKey:@"music"] intValue]) {
    34 		if (1 == [[data objectForKey:@"music"] intValue]) {
    32 			musicSwitch.on = YES;
    35 			musicSwitch.on = YES;
    33 		} else {
    36 		} else {
    34 			musicSwitch.on = NO;
    37 			musicSwitch.on = NO;
    35 		}
    38 		}
    36 		if (1 == [[data objectForKey:@"effects"] intValue]) {
    39 		if (1 == [[data objectForKey:@"sounds"] intValue]) {
    37 			effectsSwitch.on = YES;
    40 			soundsSwitch.on = YES;
    38 		} else {
    41 		} else {
    39 			effectsSwitch.on = NO;
    42 			soundsSwitch.on = NO;
    40 		}
    43 		}
    41 		if (1 == [[data objectForKey:@"alternate"] intValue]) {
    44 		if (1 == [[data objectForKey:@"alternate"] intValue]) {
    42 			altDamageSwitch.on = YES;
    45 			altDamageSwitch.on = YES;
    43 		} else {
    46 		} else {
    44 			altDamageSwitch.on = NO;
    47 			altDamageSwitch.on = NO;
    64 
    67 
    65 -(void) viewDidUnload {
    68 -(void) viewDidUnload {
    66 	self.username = nil;
    69 	self.username = nil;
    67 	self.password = nil;
    70 	self.password = nil;
    68 	self.musicSwitch = nil;
    71 	self.musicSwitch = nil;
    69 	self.effectsSwitch = nil;
    72 	self.soundsSwitch = nil;
    70 	self.altDamageSwitch = nil;
    73 	self.altDamageSwitch = nil;
    71 	self.volumeLabel = nil;
    74 	self.volumeLabel = nil;
    72 	self.volumeSlider = nil;
    75 	self.volumeSlider = nil;
    73 	self.table = nil;
    76 	self.table = nil;
    74 	self.volumeCell = nil;
    77 	self.volumeCell = nil;
    77 
    80 
    78 //- (void)applicationWillTerminate:(NSNotification *)notification {
    81 //- (void)applicationWillTerminate:(NSNotification *)notification {
    79 -(void) viewWillDisappear:(BOOL)animated {
    82 -(void) viewWillDisappear:(BOOL)animated {
    80 	NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init];
    83 	NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init];
    81 	NSString *tmpMus = (musicSwitch.on) ? @"1" : @"0";
    84 	NSString *tmpMus = (musicSwitch.on) ? @"1" : @"0";
    82 	NSString *tmpEff = (effectsSwitch.on) ? @"1" : @"0";
    85 	NSString *tmpEff = (soundsSwitch.on) ? @"1" : @"0";
    83 	NSString *tmpAlt = (altDamageSwitch.on) ? @"1" : @"0";
    86 	NSString *tmpAlt = (altDamageSwitch.on) ? @"1" : @"0";
    84 	
    87 	
    85 	[saveDict setObject:username.text forKey:@"username"];
    88 	[saveDict setObject:username.text forKey:@"username"];
    86 	[saveDict setObject:password.text forKey:@"password"];
    89 	[saveDict setObject:password.text forKey:@"password"];
    87 	[saveDict setObject:tmpMus forKey:@"music"];
    90 	[saveDict setObject:tmpMus forKey:@"music"];
    88 	[saveDict setObject:tmpEff forKey:@"effects"];
    91 	[saveDict setObject:tmpEff forKey:@"sounds"];
    89 	[saveDict setObject:tmpAlt forKey:@"alternate"];
    92 	[saveDict setObject:tmpAlt forKey:@"alternate"];
    90 	[saveDict setObject:volumeLabel.text forKey:@"volume"];
    93 	[saveDict setObject:volumeLabel.text forKey:@"volume"];
    91 	
    94 	
    92 	[saveDict writeToFile:[[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"] atomically:YES];
    95 	[saveDict writeToFile:[[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"] atomically:YES];
    93 	[saveDict release];
    96 	[saveDict release];
    94 	[super viewWillDisappear:animated];
    97 	[super viewWillDisappear:animated];
    95 }
    98 }
    96  
    99 
       
   100 -(void) dealloc {
       
   101 	[username release];
       
   102 	[password release];
       
   103 	[musicSwitch release];
       
   104 	[soundsSwitch release];
       
   105 	[altDamageSwitch release];
       
   106 	[volumeLabel release];
       
   107 	[volumeSlider release];
       
   108 	[table release];
       
   109 	[volumeCell release];
       
   110 	[super dealloc];
       
   111 }
    97 /*
   112 /*
    98 // Override to allow orientations other than the default portrait orientation.
   113 // Override to allow orientations other than the default portrait orientation.
    99 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   114 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   100     // Return YES for supported orientations
   115     // Return YES for supported orientations
   101     return (interfaceOrientation == UIInterfaceOrientationPortrait);
   116     return (interfaceOrientation == UIInterfaceOrientationPortrait);
   111 // makes the keyboard go away when "Done" is tapped
   126 // makes the keyboard go away when "Done" is tapped
   112 -(IBAction) textFieldDoneEditing: (id)sender {
   127 -(IBAction) textFieldDoneEditing: (id)sender {
   113 	[sender resignFirstResponder];
   128 	[sender resignFirstResponder];
   114 }
   129 }
   115 
   130 
       
   131 // update the value of the label when slider is updated
   116 -(IBAction) sliderChanged: (id) sender {
   132 -(IBAction) sliderChanged: (id) sender {
   117 	UISlider *slider = (UISlider *)sender;
   133 	UISlider *slider = (UISlider *)sender;
   118 	int progress = slider.value;
   134 	int progress = slider.value;
   119 	NSString *newLabel = [[NSString alloc] initWithFormat:@"%d",progress];
   135 	NSString *newLabel = [[NSString alloc] initWithFormat:@"%d",progress];
   120 	self.volumeLabel.text = newLabel;
   136 	self.volumeLabel.text = newLabel;
   121 	[newLabel release];
   137 	[newLabel release];
   122 }
   138 }
   123 
   139 
   124 -(void) dealloc {
   140 // set music off when sound is turned off
   125 	[username release];
   141 -(void) sameValueSwitch {
   126 	[password release];
   142 	if (YES == self.musicSwitch.on) {
   127 	[musicSwitch release];
   143 		[musicSwitch setOn:NO animated:YES];
   128 	[effectsSwitch release];
   144 	}
   129 	[altDamageSwitch release];
   145 }
   130 	[volumeLabel release];
   146 
   131 	[volumeSlider release];
   147 // don't enable music when sound is off
   132 	[table release];
   148 -(void) checkValueSwitch {
   133 	[volumeCell release];
   149 	if (NO == self.soundsSwitch.on) {
   134 	[super dealloc];
   150 		[musicSwitch setOn:!musicSwitch.on animated:YES];
       
   151 	}
   135 }
   152 }
   136 
   153 
   137 #pragma mark -
   154 #pragma mark -
   138 #pragma mark TableView Methods
   155 #pragma mark TableView Methods
   139 #define kNetworkFields 0
   156 #define kNetworkFields 0
   191 			}
   208 			}
   192 			break;
   209 			break;
   193 		case kAudioFields:
   210 		case kAudioFields:
   194 			switch ([indexPath row]) {
   211 			switch ([indexPath row]) {
   195 				case 0:
   212 				case 0:
       
   213 					cell.accessoryView = soundsSwitch;
       
   214 					cell.textLabel.text = NSLocalizedString(@"Sound", @"");
       
   215 					break;
       
   216 				case 1:
   196 					cell.accessoryView = musicSwitch;
   217 					cell.accessoryView = musicSwitch;
   197 					cell.textLabel.text = NSLocalizedString(@"Music", @"");
   218 					cell.textLabel.text = NSLocalizedString(@"Music", @"");
   198 					break;
       
   199 				case 1:
       
   200 					cell.accessoryView = effectsSwitch;
       
   201 					cell.textLabel.text = NSLocalizedString(@"Sound Effects", @"");
       
   202 					break;
   219 					break;
   203 				case 2:
   220 				case 2:
   204 					cell = volumeCell;
   221 					cell = volumeCell;
   205 					break;
   222 					break;
   206 				default:
   223 				default: