project_files/HedgewarsMobile/Classes/VoicesViewController.m
changeset 3697 d5b30d6373fc
parent 3667 9359a70df013
child 3829 81db3c85784b
equal deleted inserted replaced
3695:c11abf387a7d 3697:d5b30d6373fc
    29 
    29 
    30     // load all the voices names and store them into voiceArray
    30     // load all the voices names and store them into voiceArray
    31     // it's here and not in viewWillAppear because user cannot add/remove them
    31     // it's here and not in viewWillAppear because user cannot add/remove them
    32     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL];
    32     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL];
    33     self.voiceArray = array;
    33     self.voiceArray = array;
    34     
    34 
    35     self.title = NSLocalizedString(@"Set hedgehog voices",@"");
    35     self.title = NSLocalizedString(@"Set hedgehog voices",@"");
    36 }
    36 }
    37 
    37 
    38 -(void) viewWillAppear:(BOOL)animated {
    38 -(void) viewWillAppear:(BOOL)animated {
    39     [super viewWillAppear:animated];
    39     [super viewWillAppear:animated];
    40     
    40 
    41     // this moves the tableview to the top
    41     // this moves the tableview to the top
    42     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    42     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    43 }
    43 }
    44 
    44 
    45 -(void) viewWillDisappear:(BOOL)animated {
    45 -(void) viewWillDisappear:(BOOL)animated {
    62     return [self.voiceArray count];
    62     return [self.voiceArray count];
    63 }
    63 }
    64 
    64 
    65 // Customize the appearance of table view cells.
    65 // Customize the appearance of table view cells.
    66 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    66 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    67     
    67 
    68     static NSString *CellIdentifier = @"Cell";
    68     static NSString *CellIdentifier = @"Cell";
    69     
    69 
    70     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    70     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    71     if (cell == nil) {
    71     if (cell == nil) {
    72         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    72         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    73     }
    73     }
    74     
    74 
    75     NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
    75     NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
    76     cell.textLabel.text = voice;
    76     cell.textLabel.text = voice;
    77     
    77 
    78     if ([voice isEqualToString:[teamDictionary objectForKey:@"voicepack"]]) {
    78     if ([voice isEqualToString:[teamDictionary objectForKey:@"voicepack"]]) {
    79         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    79         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    80         self.lastIndexPath = indexPath;
    80         self.lastIndexPath = indexPath;
    81     } else {
    81     } else {
    82         cell.accessoryType = UITableViewCellAccessoryNone;
    82         cell.accessoryType = UITableViewCellAccessoryNone;
    89 #pragma mark -
    89 #pragma mark -
    90 #pragma mark Table view delegate
    90 #pragma mark Table view delegate
    91 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    91 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    92     int newRow = [indexPath row];
    92     int newRow = [indexPath row];
    93     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
    93     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
    94     
    94 
    95     if (newRow != oldRow) {
    95     if (newRow != oldRow) {
    96         [teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"];
    96         [teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"];
    97         
    97 
    98         // tell our boss to write this new stuff on disk
    98         // tell our boss to write this new stuff on disk
    99         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
    99         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   100         [self.tableView reloadData];
   100         [self.tableView reloadData];
   101         
   101 
   102         self.lastIndexPath = indexPath;
   102         self.lastIndexPath = indexPath;
   103         [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   103         [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   104     } 
   104     }
   105     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
   105     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
   106     
   106 
   107     if (voiceBeingPlayed != NULL) {
   107     if (voiceBeingPlayed != NULL) {
   108         Mix_HaltChannel(lastChannel);
   108         Mix_HaltChannel(lastChannel);
   109         Mix_FreeChunk(voiceBeingPlayed);
   109         Mix_FreeChunk(voiceBeingPlayed);
   110         voiceBeingPlayed = NULL;
   110         voiceBeingPlayed = NULL;
   111     }
   111     }
   112     
   112 
   113     NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]];
   113     NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]];
   114     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL];
   114     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL];
   115     
   115 
   116     int index = random() % [array count];
   116     int index = random() % [array count];
   117     
   117 
   118     voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]);
   118     voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]);
   119     [voiceDir release];
   119     [voiceDir release];
   120     lastChannel = Mix_PlayChannel(-1, voiceBeingPlayed, 0);    
   120     lastChannel = Mix_PlayChannel(-1, voiceBeingPlayed, 0);
   121 }
   121 }
   122 
   122 
   123 
   123 
   124 #pragma mark -
   124 #pragma mark -
   125 #pragma mark Memory management
   125 #pragma mark Memory management