project_files/HedgewarsMobile/Classes/VoicesViewController.m
author nemo
Sun, 20 Jun 2010 22:35:10 -0400
changeset 3526 a1d2180fef42
parent 3514 59dbd31e9953
child 3522 156c04c6a3d8
permissions -rw-r--r--
Replace SHA1 with adler32. For simple purposes of checking to see if players are playing the same map, this should be quite adequate and runs 15 times faster.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
     1
//
3352
ac5d14a35482 complete previews on the team settings
koda
parents: 3340
diff changeset
     2
//  VoicesViewController.m
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
     3
//  HedgewarsMobile
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
     4
//
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
     5
//  Created by Vittorio on 02/04/10.
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
     7
//
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
     8
3340
96dd168b080b fix all of what was committed before
koda
parents: 3339
diff changeset
     9
#import "VoicesViewController.h"
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    10
#import "CommodityFunctions.h"
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
    11
#import "openalbridge.h"
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    12
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    13
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    14
@implementation VoicesViewController
3340
96dd168b080b fix all of what was committed before
koda
parents: 3339
diff changeset
    15
@synthesize teamDictionary, voiceArray, lastIndexPath;
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    16
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    17
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    18
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    19
	return rotationManager(interfaceOrientation);
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    20
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    21
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    22
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    23
#pragma mark -
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    24
#pragma mark View lifecycle
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    25
- (void)viewDidLoad {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    26
    [super viewDidLoad];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    27
    srandom(time(NULL));
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    28
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3490
diff changeset
    29
    openal_init(20);
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    30
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    31
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    32
- (void)viewWillAppear:(BOOL)animated {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    33
    [super viewWillAppear:animated];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    34
    
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    35
    // this moves the tableview to the top
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    36
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3490
diff changeset
    37
    voiceBeingPlayed = -1;
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3490
diff changeset
    38
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3490
diff changeset
    39
    // load all the voices names and store them into voiceArray
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3490
diff changeset
    40
    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL];
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3490
diff changeset
    41
    self.voiceArray = array;
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    42
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    43
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    44
/*
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    45
- (void)viewDidAppear:(BOOL)animated {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    46
    [super viewDidAppear:animated];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    47
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    48
*/
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    49
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    50
- (void)viewWillDisappear:(BOOL)animated {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    51
    [super viewWillDisappear:animated];
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
    52
    if(voiceBeingPlayed >= 0) {
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3490
diff changeset
    53
        openal_stopsound(voiceBeingPlayed);
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
    54
        voiceBeingPlayed = -1;
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    55
    }
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    56
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    57
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    58
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    59
#pragma mark -
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    60
#pragma mark Table view data source
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    61
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    62
    return 1;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    63
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    64
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    65
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    66
    return [self.voiceArray count];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    67
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    68
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    69
// Customize the appearance of table view cells.
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3490
diff changeset
    70
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    71
    
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    72
    static NSString *CellIdentifier = @"Cell";
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    73
    
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    74
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    75
    if (cell == nil) {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    76
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    77
    }
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    78
    
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    79
    NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    80
    cell.textLabel.text = voice;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    81
    
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    82
    if ([voice isEqualToString:[teamDictionary objectForKey:@"voicepack"]]) {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    83
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    84
        self.lastIndexPath = indexPath;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    85
    } else {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    86
        cell.accessoryType = UITableViewCellAccessoryNone;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    87
    }
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    88
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    89
    return cell;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    90
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    91
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    92
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    93
#pragma mark -
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    94
#pragma mark Table view delegate
3340
96dd168b080b fix all of what was committed before
koda
parents: 3339
diff changeset
    95
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    96
    int newRow = [indexPath row];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    97
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    98
    
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    99
    if (newRow != oldRow) {
3340
96dd168b080b fix all of what was committed before
koda
parents: 3339
diff changeset
   100
        [teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"];
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   101
        
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   102
        // tell our boss to write this new stuff on disk
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   103
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   104
        [self.tableView reloadData];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   105
        
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   106
        self.lastIndexPath = indexPath;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   107
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   108
    } 
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   109
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
3340
96dd168b080b fix all of what was committed before
koda
parents: 3339
diff changeset
   110
    
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
   111
    if (voiceBeingPlayed >= 0) {
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
   112
        openal_stopsound(voiceBeingPlayed);
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
   113
        voiceBeingPlayed = -1;
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   114
    }
3340
96dd168b080b fix all of what was committed before
koda
parents: 3339
diff changeset
   115
    
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   116
    // the keyword static prevents re-initialization of the variable
3340
96dd168b080b fix all of what was committed before
koda
parents: 3339
diff changeset
   117
    NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]];
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   118
    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL];
3340
96dd168b080b fix all of what was committed before
koda
parents: 3339
diff changeset
   119
    
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   120
    int index = random() % [array count];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   121
    
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
   122
    voiceBeingPlayed = openal_loadfile([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]);
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   123
    [voiceDir release];
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
   124
    openal_playsound(voiceBeingPlayed);    
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   125
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   126
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   127
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   128
#pragma mark -
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   129
#pragma mark Memory management
3490
016b3172b645 a bunch of minor stuff
koda
parents: 3364
diff changeset
   130
-(void) didReceiveMemoryWarning {
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   131
    // Releases the view if it doesn't have a superview.
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   132
    [super didReceiveMemoryWarning];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   133
    // Relinquish ownership any cached data, images, etc that aren't in use.
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   134
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   135
3490
016b3172b645 a bunch of minor stuff
koda
parents: 3364
diff changeset
   136
-(void) viewDidUnload {
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
   137
    openal_close();
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
   138
    voiceBeingPlayed = -1;
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   139
    self.lastIndexPath = nil;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   140
    self.teamDictionary = nil;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   141
    self.voiceArray = nil;
3490
016b3172b645 a bunch of minor stuff
koda
parents: 3364
diff changeset
   142
    [super viewDidUnload];
016b3172b645 a bunch of minor stuff
koda
parents: 3364
diff changeset
   143
    MSG_DIDUNLOAD();
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   144
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   145
3490
016b3172b645 a bunch of minor stuff
koda
parents: 3364
diff changeset
   146
-(void) dealloc {
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   147
    [voiceArray release];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   148
    [teamDictionary release];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   149
    [lastIndexPath release];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   150
    [super dealloc];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   151
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   152
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   153
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   154
@end
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   155