project_files/HedgewarsMobile/Classes/VoicesViewController.m
branchios-develop
changeset 12872 00215a7ec5f5
parent 11206 2e80c9861818
equal deleted inserted replaced
12871:2c06b1120749 12872:00215a7ec5f5
    22 
    22 
    23 @implementation VoicesViewController
    23 @implementation VoicesViewController
    24 @synthesize teamDictionary, voiceArray, lastIndexPath;
    24 @synthesize teamDictionary, voiceArray, lastIndexPath;
    25 
    25 
    26 
    26 
    27 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    27 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    28     return rotationManager(interfaceOrientation);
    28     return rotationManager(interfaceOrientation);
    29 }
    29 }
    30 
    30 
    31 #pragma mark -
    31 #pragma mark -
    32 #pragma mark View lifecycle
    32 #pragma mark View lifecycle
    33 -(void) viewDidLoad {
    33 - (void)viewDidLoad {
    34     [super viewDidLoad];
    34     [super viewDidLoad];
    35 
    35 
    36     voiceBeingPlayed = NULL;
    36     voiceBeingPlayed = NULL;
    37 
    37 
    38     // load all the voices names and store them into voiceArray
    38     // load all the voices names and store them into voiceArray
    41     self.voiceArray = array;
    41     self.voiceArray = array;
    42 
    42 
    43     self.title = NSLocalizedString(@"Set hedgehog voices",@"");
    43     self.title = NSLocalizedString(@"Set hedgehog voices",@"");
    44 }
    44 }
    45 
    45 
    46 -(void) viewWillAppear:(BOOL)animated {
    46 - (void)viewWillAppear:(BOOL)animated {
    47     [super viewWillAppear:animated];
    47     [super viewWillAppear:animated];
    48 
    48 
    49     // this moves the tableview to the top
    49     // this moves the tableview to the top
    50     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    50     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    51 }
    51 }
    52 
    52 
    53 -(void) viewDidAppear:(BOOL)animated {
    53 - (void)viewDidAppear:(BOOL)animated {
    54     [super viewDidAppear:animated];
    54     [super viewDidAppear:animated];
    55     Mix_OpenAudio(44100, 0x8010, 1, 1024);
    55     Mix_OpenAudio(44100, 0x8010, 1, 1024);
    56 }
    56 }
    57 
    57 
    58 -(void) viewDidDisappear:(BOOL)animated {
    58 - (void)viewDidDisappear:(BOOL)animated {
    59     [super viewDidDisappear:animated];
    59     [super viewDidDisappear:animated];
    60     if(voiceBeingPlayed != NULL) {
    60     if(voiceBeingPlayed != NULL) {
    61         Mix_HaltChannel(lastChannel);
    61         Mix_HaltChannel(lastChannel);
    62         Mix_FreeChunk(voiceBeingPlayed);
    62         Mix_FreeChunk(voiceBeingPlayed);
    63         voiceBeingPlayed = NULL;
    63         voiceBeingPlayed = NULL;
    66 }
    66 }
    67 
    67 
    68 
    68 
    69 #pragma mark -
    69 #pragma mark -
    70 #pragma mark Table view data source
    70 #pragma mark Table view data source
    71 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    71 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    72     return 1;
    72     return 1;
    73 }
    73 }
    74 
    74 
    75 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    75 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    76     return [self.voiceArray count];
    76     return [self.voiceArray count];
    77 }
    77 }
    78 
    78 
    79 // Customize the appearance of table view cells.
    79 // Customize the appearance of table view cells.
    80 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    80 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    81 
    81 
    82     static NSString *CellIdentifier = @"Cell";
    82     static NSString *CellIdentifier = @"Cell";
    83 
    83 
    84     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    84     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    85     if (cell == nil) {
    85     if (cell == nil) {
    86         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    86         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    87     }
    87     }
    88 
    88 
    89     NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
    89     NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
    90     cell.textLabel.text = voice;
    90     cell.textLabel.text = voice;
    91 
    91 
   100 }
   100 }
   101 
   101 
   102 
   102 
   103 #pragma mark -
   103 #pragma mark -
   104 #pragma mark Table view delegate
   104 #pragma mark Table view delegate
   105 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   105 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   106     NSInteger newRow = [indexPath row];
   106     NSInteger newRow = [indexPath row];
   107     NSInteger oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   107     NSInteger oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   108 
   108 
   109     if (newRow != oldRow) {
   109     if (newRow != oldRow) {
   110         [teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"];
   110         [teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"];
   129     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL];
   129     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL];
   130 
   130 
   131     int index = arc4random_uniform((int)[array count]);
   131     int index = arc4random_uniform((int)[array count]);
   132 
   132 
   133     voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]);
   133     voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]);
   134     [voiceDir release];
       
   135     lastChannel = Mix_PlayChannel(-1, voiceBeingPlayed, 0);
   134     lastChannel = Mix_PlayChannel(-1, voiceBeingPlayed, 0);
   136 }
   135 }
   137 
   136 
   138 
   137 
   139 #pragma mark -
   138 #pragma mark -
   140 #pragma mark Memory management
   139 #pragma mark Memory management
   141 -(void) didReceiveMemoryWarning {
   140 
       
   141 - (void)didReceiveMemoryWarning {
   142     if (voiceBeingPlayed != NULL) {
   142     if (voiceBeingPlayed != NULL) {
   143         Mix_HaltChannel(lastChannel);
   143         Mix_HaltChannel(lastChannel);
   144         Mix_FreeChunk(voiceBeingPlayed);
   144         Mix_FreeChunk(voiceBeingPlayed);
   145         voiceBeingPlayed = NULL;
   145         voiceBeingPlayed = NULL;
   146     }
   146     }
   147     self.lastIndexPath = nil;
   147     self.lastIndexPath = nil;
   148     MSG_MEMCLEAN();
   148     MSG_MEMCLEAN();
   149     [super didReceiveMemoryWarning];
   149     [super didReceiveMemoryWarning];
   150 }
   150 }
   151 
   151 
   152 -(void) viewDidUnload {
   152 - (void)dealloc {
   153     if (voiceBeingPlayed != NULL) {
   153     if (voiceBeingPlayed != NULL) {
   154         Mix_HaltChannel(lastChannel);
   154         Mix_HaltChannel(lastChannel);
   155         Mix_FreeChunk(voiceBeingPlayed);
   155         Mix_FreeChunk(voiceBeingPlayed);
   156         voiceBeingPlayed = NULL;
   156         voiceBeingPlayed = NULL;
   157     }
   157     }
   158     self.lastIndexPath = nil;
       
   159     self.teamDictionary = nil;
       
   160     self.voiceArray = nil;
       
   161     MSG_DIDUNLOAD();
       
   162     [super viewDidUnload];
       
   163 }
   158 }
   164 
       
   165 -(void) dealloc {
       
   166     releaseAndNil(voiceArray);
       
   167     releaseAndNil(teamDictionary);
       
   168     releaseAndNil(lastIndexPath);
       
   169     [super dealloc];
       
   170 }
       
   171 
       
   172 
   159 
   173 @end
   160 @end
   174 
   161