project_files/HedgewarsMobile/Classes/VoicesViewController.m
author koda
Mon, 21 Jun 2010 22:45:52 +0200
changeset 3532 04e2fea3e83a
parent 3522 156c04c6a3d8
child 3546 ccf4854df294
permissions -rw-r--r--
merge+smallupdate
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
3532
04e2fea3e83a merge+smallupdate
koda
parents: 3522
diff changeset
    29
    openal_init();
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    30
    voiceBeingPlayed = -1;
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    31
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    32
    // load all the voices names and store them into voiceArray
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    33
    // it's here and not in viewWillAppear because user cannot add/remove them
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    34
    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    35
    self.voiceArray = array;
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    36
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    37
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    38
- (void)viewWillAppear:(BOOL)animated {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    39
    [super viewWillAppear:animated];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    40
    
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    41
    // this moves the tableview to the top
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    42
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
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
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    45
-(void) viewWillDisappear:(BOOL)animated {
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    46
    [super viewWillDisappear:animated];
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
    47
    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
    48
        openal_stopsound(voiceBeingPlayed);
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
    49
        voiceBeingPlayed = -1;
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    50
    }
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    51
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    52
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    53
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    54
#pragma mark -
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    55
#pragma mark Table view data source
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    56
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    57
    return 1;
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
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    60
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    61
    return [self.voiceArray count];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    62
}
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
// 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
    65
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    66
    
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    67
    static NSString *CellIdentifier = @"Cell";
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
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    70
    if (cell == nil) {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    71
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    72
    }
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
    NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    75
    cell.textLabel.text = voice;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    76
    
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    77
    if ([voice isEqualToString:[teamDictionary objectForKey:@"voicepack"]]) {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    78
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    79
        self.lastIndexPath = indexPath;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    80
    } else {
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    81
        cell.accessoryType = UITableViewCellAccessoryNone;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    82
    }
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    83
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    84
    return cell;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    85
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    86
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
#pragma mark -
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    89
#pragma mark Table view delegate
3340
96dd168b080b fix all of what was committed before
koda
parents: 3339
diff changeset
    90
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    91
    int newRow = [indexPath row];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    92
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    93
    
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    94
    if (newRow != oldRow) {
3340
96dd168b080b fix all of what was committed before
koda
parents: 3339
diff changeset
    95
        [teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"];
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    96
        
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    97
        // tell our boss to write this new stuff on disk
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    98
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
    99
        [self.tableView reloadData];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   100
        
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   101
        self.lastIndexPath = indexPath;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   102
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   103
    } 
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   104
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
3340
96dd168b080b fix all of what was committed before
koda
parents: 3339
diff changeset
   105
    
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
   106
    if (voiceBeingPlayed >= 0) {
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
   107
        openal_stopsound(voiceBeingPlayed);
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
   108
        voiceBeingPlayed = -1;
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   109
    }
3340
96dd168b080b fix all of what was committed before
koda
parents: 3339
diff changeset
   110
    
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   111
    // the keyword static prevents re-initialization of the variable
3340
96dd168b080b fix all of what was committed before
koda
parents: 3339
diff changeset
   112
    NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]];
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   113
    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL];
3340
96dd168b080b fix all of what was committed before
koda
parents: 3339
diff changeset
   114
    
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   115
    int index = random() % [array count];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   116
    
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
   117
    voiceBeingPlayed = openal_loadfile([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]);
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   118
    [voiceDir release];
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
   119
    openal_playsound(voiceBeingPlayed);    
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   120
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   121
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   122
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   123
#pragma mark -
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   124
#pragma mark Memory management
3490
016b3172b645 a bunch of minor stuff
koda
parents: 3364
diff changeset
   125
-(void) didReceiveMemoryWarning {
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   126
    // Releases the view if it doesn't have a superview.
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   127
    [super didReceiveMemoryWarning];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   128
    // Relinquish ownership any cached data, images, etc that aren't in use.
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   129
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   130
3490
016b3172b645 a bunch of minor stuff
koda
parents: 3364
diff changeset
   131
-(void) viewDidUnload {
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
   132
    openal_close();
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents: 3352
diff changeset
   133
    voiceBeingPlayed = -1;
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   134
    self.lastIndexPath = nil;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   135
    self.teamDictionary = nil;
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   136
    self.voiceArray = nil;
3490
016b3172b645 a bunch of minor stuff
koda
parents: 3364
diff changeset
   137
    [super viewDidUnload];
016b3172b645 a bunch of minor stuff
koda
parents: 3364
diff changeset
   138
    MSG_DIDUNLOAD();
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   139
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   140
3490
016b3172b645 a bunch of minor stuff
koda
parents: 3364
diff changeset
   141
-(void) dealloc {
3339
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   142
    [voiceArray release];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   143
    [teamDictionary release];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   144
    [lastIndexPath release];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   145
    [super dealloc];
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   146
}
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   147
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   148
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   149
@end
d558bc5a73c5 lots of untested stuff for the ifrontend
koda
parents:
diff changeset
   150