project_files/HedgewarsMobile/Classes/VoicesViewController.m
changeset 3547 02875b1145b7
parent 3546 ccf4854df294
child 3662 a44406f4369b
equal deleted inserted replaced
3546:ccf4854df294 3547:02875b1145b7
       
     1 //
       
     2 //  VoicesViewController.m
       
     3 //  HedgewarsMobile
       
     4 //
       
     5 //  Created by Vittorio on 02/04/10.
       
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
       
     7 //
       
     8 
       
     9 #import "VoicesViewController.h"
       
    10 #import "CommodityFunctions.h"
       
    11 #import "openalbridge.h"
       
    12 
       
    13 
       
    14 @implementation VoicesViewController
       
    15 @synthesize teamDictionary, voiceArray, lastIndexPath;
       
    16 
       
    17 
       
    18 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
       
    19     return rotationManager(interfaceOrientation);
       
    20 }
       
    21 
       
    22 
       
    23 #pragma mark -
       
    24 #pragma mark View lifecycle
       
    25 - (void)viewDidLoad {
       
    26     [super viewDidLoad];
       
    27     srandom(time(NULL));
       
    28 
       
    29     openal_init();
       
    30     voiceBeingPlayed = -1;
       
    31 
       
    32     // load all the voices names and store them into voiceArray
       
    33     // it's here and not in viewWillAppear because user cannot add/remove them
       
    34     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL];
       
    35     self.voiceArray = array;
       
    36 }
       
    37 
       
    38 - (void)viewWillAppear:(BOOL)animated {
       
    39     [super viewWillAppear:animated];
       
    40     
       
    41     // this moves the tableview to the top
       
    42     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
       
    43 }
       
    44 
       
    45 -(void) viewWillDisappear:(BOOL)animated {
       
    46     [super viewWillDisappear:animated];
       
    47     if(voiceBeingPlayed >= 0) {
       
    48         openal_stopsound(voiceBeingPlayed);
       
    49         voiceBeingPlayed = -1;
       
    50     }
       
    51 }
       
    52 
       
    53 
       
    54 #pragma mark -
       
    55 #pragma mark Table view data source
       
    56 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
       
    57     return 1;
       
    58 }
       
    59 
       
    60 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
    61     return [self.voiceArray count];
       
    62 }
       
    63 
       
    64 // Customize the appearance of table view cells.
       
    65 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
    66     
       
    67     static NSString *CellIdentifier = @"Cell";
       
    68     
       
    69     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
       
    70     if (cell == nil) {
       
    71         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
       
    72     }
       
    73     
       
    74     NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
       
    75     cell.textLabel.text = voice;
       
    76     
       
    77     if ([voice isEqualToString:[teamDictionary objectForKey:@"voicepack"]]) {
       
    78         cell.accessoryType = UITableViewCellAccessoryCheckmark;
       
    79         self.lastIndexPath = indexPath;
       
    80     } else {
       
    81         cell.accessoryType = UITableViewCellAccessoryNone;
       
    82     }
       
    83 
       
    84     return cell;
       
    85 }
       
    86 
       
    87 
       
    88 #pragma mark -
       
    89 #pragma mark Table view delegate
       
    90 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
    91     int newRow = [indexPath row];
       
    92     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
       
    93     
       
    94     if (newRow != oldRow) {
       
    95         [teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"];
       
    96         
       
    97         // tell our boss to write this new stuff on disk
       
    98         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
       
    99         [self.tableView reloadData];
       
   100         
       
   101         self.lastIndexPath = indexPath;
       
   102         [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
       
   103     } 
       
   104     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
       
   105     
       
   106     if (voiceBeingPlayed >= 0) {
       
   107         openal_stopsound(voiceBeingPlayed);
       
   108         voiceBeingPlayed = -1;
       
   109     }
       
   110     
       
   111     // the keyword static prevents re-initialization of the variable
       
   112     NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]];
       
   113     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL];
       
   114     
       
   115     int index = random() % [array count];
       
   116     
       
   117     voiceBeingPlayed = openal_loadfile([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]);
       
   118     [voiceDir release];
       
   119     openal_playsound(voiceBeingPlayed);    
       
   120 }
       
   121 
       
   122 
       
   123 #pragma mark -
       
   124 #pragma mark Memory management
       
   125 -(void) didReceiveMemoryWarning {
       
   126     // Releases the view if it doesn't have a superview.
       
   127     [super didReceiveMemoryWarning];
       
   128     // Relinquish ownership any cached data, images, etc that aren't in use.
       
   129 }
       
   130 
       
   131 -(void) viewDidUnload {
       
   132     openal_close();
       
   133     voiceBeingPlayed = -1;
       
   134     self.lastIndexPath = nil;
       
   135     self.teamDictionary = nil;
       
   136     self.voiceArray = nil;
       
   137     [super viewDidUnload];
       
   138     MSG_DIDUNLOAD();
       
   139 }
       
   140 
       
   141 -(void) dealloc {
       
   142     [voiceArray release];
       
   143     [teamDictionary release];
       
   144     [lastIndexPath release];
       
   145     [super dealloc];
       
   146 }
       
   147 
       
   148 
       
   149 @end
       
   150