cocoaTouch/VoicesViewController.m
changeset 3340 96dd168b080b
parent 3339 d558bc5a73c5
child 3352 ac5d14a35482
equal deleted inserted replaced
3339:d558bc5a73c5 3340:96dd168b080b
     4 //
     4 //
     5 //  Created by Vittorio on 02/04/10.
     5 //  Created by Vittorio on 02/04/10.
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
     7 //
     7 //
     8 
     8 
     9 #import "HogHatViewController.h"
     9 #import "VoicesViewController.h"
    10 #import "CommodityFunctions.h"
    10 #import "CommodityFunctions.h"
    11 
    11 
    12 
    12 
    13 @implementation VoicesViewController
    13 @implementation VoicesViewController
    14 @synthesize teamDictionary, voiceArray, lastIndexPath, musicBeingPlayed;
    14 @synthesize teamDictionary, voiceArray, lastIndexPath;
    15 
    15 
    16 
    16 
    17 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    17 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    18 	return rotationManager(interfaceOrientation);
    18 	return rotationManager(interfaceOrientation);
    19 }
    19 }
    24 - (void)viewDidLoad {
    24 - (void)viewDidLoad {
    25     [super viewDidLoad];
    25     [super viewDidLoad];
    26     srandom(time(NULL));
    26     srandom(time(NULL));
    27 
    27 
    28     Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 1024);
    28     Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 1024);
    29     musicBeingPlayed = NULL;
    29     voiceBeingPlayed = NULL;
    30 
    30 
    31     // load all the voices names and store them into voiceArray
    31     // load all the voices names and store them into voiceArray
    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     [array release];
       
    35 }
    34 }
    36 
    35 
    37 - (void)viewWillAppear:(BOOL)animated {
    36 - (void)viewWillAppear:(BOOL)animated {
    38     [super viewWillAppear:animated];
    37     [super viewWillAppear:animated];
    39     
    38     
    47 }
    46 }
    48 */
    47 */
    49 
    48 
    50 - (void)viewWillDisappear:(BOOL)animated {
    49 - (void)viewWillDisappear:(BOOL)animated {
    51     [super viewWillDisappear:animated];
    50     [super viewWillDisappear:animated];
    52     if(musicBeingPlayed != NULL) {
    51     if(voiceBeingPlayed != NULL) {
    53 	Mix_HaltMusic();
    52         Mix_HaltChannel(-1);
    54 	Mix_FreeMusic(musicBeingPlayed);
    53         Mix_FreeChunk(voiceBeingPlayed);
    55 	musicBeingPlayed = NULL;
    54         voiceBeingPlayed = NULL;
    56     }
    55     }
    57 }
    56 }
    58 
    57 
    59 /*
    58 /*
    60 - (void)viewDidDisappear:(BOOL)animated {
    59 - (void)viewDidDisappear:(BOOL)animated {
   133 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
   132 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
   134     // Return NO if you do not want the item to be re-orderable.
   133     // Return NO if you do not want the item to be re-orderable.
   135     return YES;
   134     return YES;
   136 }
   135 }
   137 */
   136 */
   138 
   137 -(void) allowSelection {
       
   138     self.tableView.allowsSelection = YES;
       
   139 }
   139 
   140 
   140 #pragma mark -
   141 #pragma mark -
   141 #pragma mark Table view delegate
   142 #pragma mark Table view delegate
   142 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   143 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   143     int newRow = [indexPath row];
   144     int newRow = [indexPath row];
   144     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   145     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   145     
   146     
       
   147     // avoid a crash in sdl_mixer
       
   148     self.tableView.allowsSelection = NO;
       
   149     [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(allowSelection) userInfo:nil repeats:NO];
       
   150     
   146     if (newRow != oldRow) {
   151     if (newRow != oldRow) {
   147 	[teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"];
   152         [teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"];
   148         
   153         
   149         // tell our boss to write this new stuff on disk
   154         // tell our boss to write this new stuff on disk
   150         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   155         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   151         [self.tableView reloadData];
   156         [self.tableView reloadData];
   152         
   157         
   153         self.lastIndexPath = indexPath;
   158         self.lastIndexPath = indexPath;
   154         [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   159         [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   155     } 
   160     } 
   156     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
   161     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
   157 
   162     
   158     if(musicBeingPlayed != NULL) {
   163     if (voiceBeingPlayed != NULL) {
   159 	Mix_HaltMusic();
   164         Mix_HaltChannel(-1);
   160 	Mix_FreeMusic(musicBeingPlayed);
   165         Mix_FreeChunk(voiceBeingPlayed);
   161 	musicBeingPlayed = NULL;
   166         voiceBeingPlayed = NULL;
   162     }
   167     }
       
   168     
   163     // the keyword static prevents re-initialization of the variable
   169     // the keyword static prevents re-initialization of the variable
   164     NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow];
   170     NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]];
   165     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL];
   171     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL];
   166 
   172     
   167     int index = random() % [array count];
   173     int index = random() % [array count];
   168     
   174     
   169     music = Mix_LoadMUS([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]);
   175     voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]);
   170     [array release];
       
   171     [voiceDir release];
   176     [voiceDir release];
       
   177     Mix_PlayChannel(-1, voiceBeingPlayed, 0);    
   172 }
   178 }
   173 
   179 
   174 
   180 
   175 #pragma mark -
   181 #pragma mark -
   176 #pragma mark Memory management
   182 #pragma mark Memory management
   177 - (void)didReceiveMemoryWarning {
   183 - (void)didReceiveMemoryWarning {
   178     Mix_HaltMusic();
   184     Mix_HaltChannel(-1);
   179     Mix_FreeMusic(musicBeingPlayed);
   185     Mix_FreeChunk(voiceBeingPlayed);
   180     musicBeingPlayed = NULL;
   186     voiceBeingPlayed = NULL;
   181     // Releases the view if it doesn't have a superview.
   187     // Releases the view if it doesn't have a superview.
   182     [super didReceiveMemoryWarning];
   188     [super didReceiveMemoryWarning];
   183     // Relinquish ownership any cached data, images, etc that aren't in use.
   189     // Relinquish ownership any cached data, images, etc that aren't in use.
   184 }
   190 }
   185 
   191 
   186 - (void)viewDidUnload {
   192 - (void)viewDidUnload {
   187     [super viewDidUnload];
   193     [super viewDidUnload];
   188 
   194 
   189     Mix_CloseAudio();
   195     Mix_CloseAudio();
   190     self.musicBeingPlayed = NULL;
   196     voiceBeingPlayed = NULL;
   191     self.lastIndexPath = nil;
   197     self.lastIndexPath = nil;
   192     self.teamDictionary = nil;
   198     self.teamDictionary = nil;
   193     self.voiceArray = nil;
   199     self.voiceArray = nil;
   194 }
   200 }
   195 
   201 
   196 - (void)dealloc {
   202 - (void)dealloc {
   197     [musicBeingPlayed release];
       
   198     [voiceArray release];
   203     [voiceArray release];
   199     [teamDictionary release];
   204     [teamDictionary release];
   200     [lastIndexPath release];
   205     [lastIndexPath release];
   201     [super dealloc];
   206     [super dealloc];
   202 }
   207 }