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