project_files/HedgewarsMobile/Classes/VoicesViewController.m
author koda
Sat, 04 Sep 2010 16:24:00 +0200
changeset 3829 81db3c85784b
parent 3697 d5b30d6373fc
child 3883 5934ddf9ed5d
permissions -rw-r--r--
headers ftw, also right project file
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     1
/*
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     3
 * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com>
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     4
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     8
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    12
 * GNU General Public License for more details.
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    13
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    14
 * You should have received a copy of the GNU General Public License
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    15
 * along with this program; if not, write to the Free Software
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    17
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    18
 * File created on 02/04/2010.
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    19
 */
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    20
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
#import "VoicesViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
@implementation VoicesViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
@synthesize teamDictionary, voiceArray, lastIndexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
#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
    37
-(void) viewDidLoad {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
    [super viewDidLoad];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
    srandom(time(NULL));
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
3667
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    41
    voiceBeingPlayed = NULL;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
    // load all the voices names and store them into voiceArray
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
    // it's here and not in viewWillAppear because user cannot add/remove them
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
    self.voiceArray = array;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    47
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
    48
    self.title = NSLocalizedString(@"Set hedgehog voices",@"");
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
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
    51
-(void) viewWillAppear:(BOOL)animated {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
    [super viewWillAppear:animated];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    53
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
    // this moves the tableview to the top
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
-(void) viewWillDisappear:(BOOL)animated {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
    [super viewWillDisappear:animated];
3667
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    60
    if(voiceBeingPlayed != NULL) {
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    61
        Mix_HaltChannel(lastChannel);
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    62
        Mix_FreeChunk(voiceBeingPlayed);
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    63
        voiceBeingPlayed = NULL;
3547
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
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
    return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
    return [self.voiceArray count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
// Customize the appearance of table view cells.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    80
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
    static NSString *CellIdentifier = @"Cell";
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    82
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
    if (cell == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    87
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
    NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
    cell.textLabel.text = voice;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    90
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
    if ([voice isEqualToString:[teamDictionary objectForKey:@"voicepack"]]) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
        cell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
    int newRow = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   107
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
    if (newRow != oldRow) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
        [teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   110
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
        // tell our boss to write this new stuff on disk
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
        [self.tableView reloadData];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   114
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   117
    }
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   119
3667
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
   120
    if (voiceBeingPlayed != NULL) {
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
   121
        Mix_HaltChannel(lastChannel);
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
   122
        Mix_FreeChunk(voiceBeingPlayed);
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
   123
        voiceBeingPlayed = NULL;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   125
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
    NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   128
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
    int index = random() % [array count];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   130
3667
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
   131
    voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]);
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
    [voiceDir release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   133
    lastChannel = Mix_PlayChannel(-1, voiceBeingPlayed, 0);
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
-(void) didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
    // Releases the view if it doesn't have a superview.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
    // Relinquish ownership any cached data, images, etc that aren't in use.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
-(void) viewDidUnload {
3667
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
   146
    voiceBeingPlayed = NULL;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
    self.lastIndexPath = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
    self.teamDictionary = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
    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
   150
    MSG_DIDUNLOAD();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
    [voiceArray release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
    [teamDictionary release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
    [lastIndexPath release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   163