project_files/HedgewarsMobile/Classes/VoicesViewController.m
author antonc27 <antonc27@mail.ru>
Wed, 14 Oct 2015 21:25:49 +0200
branchios-revival
changeset 11210 2e80c9861818
parent 11148 064a53861759
child 12877 00215a7ec5f5
permissions -rw-r--r--
- Better random number generation for front-end: random() replaced with arc4random_uniform()
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
6700
e04da46ee43c the most important commit of the year
koda
parents: 6078
diff changeset
     3
 * Copyright (c) 2009-2012 Vittorio Giovara <vittorio.giovara@gmail.com>
3829
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
10108
c68cf030eded update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents: 6832
diff changeset
    16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
3829
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
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
#import "VoicesViewController.h"
6078
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5208
diff changeset
    21
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
@implementation VoicesViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
@synthesize teamDictionary, voiceArray, lastIndexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
#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
    33
-(void) viewDidLoad {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
    [super viewDidLoad];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
3667
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    36
    voiceBeingPlayed = NULL;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
    // load all the voices names and store them into voiceArray
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
    // it's here and not in viewWillAppear because user cannot add/remove them
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
    self.voiceArray = array;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    42
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
    43
    self.title = NSLocalizedString(@"Set hedgehog voices",@"");
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
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
    46
-(void) viewWillAppear:(BOOL)animated {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
    [super viewWillAppear:animated];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    48
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
    // this moves the tableview to the top
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
3883
5934ddf9ed5d fixed the voice selection where you couldn't hear a preview sound
koda
parents: 3829
diff changeset
    53
-(void) viewDidAppear:(BOOL)animated {
5934ddf9ed5d fixed the voice selection where you couldn't hear a preview sound
koda
parents: 3829
diff changeset
    54
    [super viewDidAppear:animated];
5934ddf9ed5d fixed the voice selection where you couldn't hear a preview sound
koda
parents: 3829
diff changeset
    55
    Mix_OpenAudio(44100, 0x8010, 1, 1024);
5934ddf9ed5d fixed the voice selection where you couldn't hear a preview sound
koda
parents: 3829
diff changeset
    56
}
5934ddf9ed5d fixed the voice selection where you couldn't hear a preview sound
koda
parents: 3829
diff changeset
    57
5934ddf9ed5d fixed the voice selection where you couldn't hear a preview sound
koda
parents: 3829
diff changeset
    58
-(void) viewDidDisappear:(BOOL)animated {
5934ddf9ed5d fixed the voice selection where you couldn't hear a preview sound
koda
parents: 3829
diff changeset
    59
    [super viewDidDisappear: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
    }
3883
5934ddf9ed5d fixed the voice selection where you couldn't hear a preview sound
koda
parents: 3829
diff changeset
    65
    Mix_CloseAudio();
3547
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
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
    return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
    return [self.voiceArray count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
// Customize the appearance of table view cells.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    81
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
    static NSString *CellIdentifier = @"Cell";
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    83
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
    if (cell == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    88
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
    NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
    cell.textLabel.text = voice;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    91
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
    if ([voice isEqualToString:[teamDictionary objectForKey:@"voicepack"]]) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
        cell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
    return cell;
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
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
11148
064a53861759 - Refactoring in order to remove some warning related to using of int instead of NSInteger
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   106
    NSInteger newRow = [indexPath row];
064a53861759 - Refactoring in order to remove some warning related to using of int instead of NSInteger
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   107
    NSInteger oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   108
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
    if (newRow != oldRow) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
        [teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   111
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
        // tell our boss to write this new stuff on disk
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
        [self.tableView reloadData];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   115
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   118
    }
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   120
3883
5934ddf9ed5d fixed the voice selection where you couldn't hear a preview sound
koda
parents: 3829
diff changeset
   121
    // stop any sound before playing another one
3667
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
   122
    if (voiceBeingPlayed != NULL) {
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
   123
        Mix_HaltChannel(lastChannel);
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
   124
        Mix_FreeChunk(voiceBeingPlayed);
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
   125
        voiceBeingPlayed = NULL;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   127
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
    NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   130
11210
2e80c9861818 - Better random number generation for front-end: random() replaced with arc4random_uniform()
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
   131
    int index = arc4random_uniform((int)[array count]);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   132
3667
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
   133
    voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]);
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
    [voiceDir release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   135
    lastChannel = Mix_PlayChannel(-1, voiceBeingPlayed, 0);
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
-(void) didReceiveMemoryWarning {
3883
5934ddf9ed5d fixed the voice selection where you couldn't hear a preview sound
koda
parents: 3829
diff changeset
   142
    if (voiceBeingPlayed != NULL) {
5934ddf9ed5d fixed the voice selection where you couldn't hear a preview sound
koda
parents: 3829
diff changeset
   143
        Mix_HaltChannel(lastChannel);
5934ddf9ed5d fixed the voice selection where you couldn't hear a preview sound
koda
parents: 3829
diff changeset
   144
        Mix_FreeChunk(voiceBeingPlayed);
5934ddf9ed5d fixed the voice selection where you couldn't hear a preview sound
koda
parents: 3829
diff changeset
   145
        voiceBeingPlayed = NULL;
5934ddf9ed5d fixed the voice selection where you couldn't hear a preview sound
koda
parents: 3829
diff changeset
   146
    }
3971
5c82ee165ed5 minor stuff
koda
parents: 3883
diff changeset
   147
    self.lastIndexPath = nil;
3883
5934ddf9ed5d fixed the voice selection where you couldn't hear a preview sound
koda
parents: 3829
diff changeset
   148
    MSG_MEMCLEAN();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
-(void) viewDidUnload {
3971
5c82ee165ed5 minor stuff
koda
parents: 3883
diff changeset
   153
    if (voiceBeingPlayed != NULL) {
5c82ee165ed5 minor stuff
koda
parents: 3883
diff changeset
   154
        Mix_HaltChannel(lastChannel);
5c82ee165ed5 minor stuff
koda
parents: 3883
diff changeset
   155
        Mix_FreeChunk(voiceBeingPlayed);
5c82ee165ed5 minor stuff
koda
parents: 3883
diff changeset
   156
        voiceBeingPlayed = NULL;
5c82ee165ed5 minor stuff
koda
parents: 3883
diff changeset
   157
    }
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
    self.lastIndexPath = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
    self.teamDictionary = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
    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
   161
    MSG_DIDUNLOAD();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   163
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   164
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   165
-(void) dealloc {
5208
878e551f0b4a all together now! releaseAndNil ftw
koda
parents: 4976
diff changeset
   166
    releaseAndNil(voiceArray);
878e551f0b4a all together now! releaseAndNil ftw
koda
parents: 4976
diff changeset
   167
    releaseAndNil(teamDictionary);
878e551f0b4a all together now! releaseAndNil ftw
koda
parents: 4976
diff changeset
   168
    releaseAndNil(lastIndexPath);
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   170
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   171
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   172
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   173
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   174