author | unc0rr |
Sun, 26 Jan 2014 10:20:42 +0400 | |
changeset 10077 | ca67740f19b2 |
parent 6832 | fae8fd118da9 |
child 10108 | c68cf030eded |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
6700 | 3 |
* Copyright (c) 2009-2012 Vittorio Giovara <vittorio.giovara@gmail.com> |
3829 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 |
*/ |
|
18 |
||
3547 | 19 |
|
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 | 22 |
|
23 |
@implementation VoicesViewController |
|
24 |
@synthesize teamDictionary, voiceArray, lastIndexPath; |
|
25 |
||
26 |
||
27 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
28 |
return rotationManager(interfaceOrientation); |
|
29 |
} |
|
30 |
||
31 |
#pragma mark - |
|
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 | 34 |
[super viewDidLoad]; |
35 |
srandom(time(NULL)); |
|
36 |
||
3667 | 37 |
voiceBeingPlayed = NULL; |
3547 | 38 |
|
39 |
// load all the voices names and store them into voiceArray |
|
40 |
// it's here and not in viewWillAppear because user cannot add/remove them |
|
41 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL]; |
|
42 |
self.voiceArray = array; |
|
3697 | 43 |
|
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
|
44 |
self.title = NSLocalizedString(@"Set hedgehog voices",@""); |
3547 | 45 |
} |
46 |
||
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
|
47 |
-(void) viewWillAppear:(BOOL)animated { |
3547 | 48 |
[super viewWillAppear:animated]; |
3697 | 49 |
|
3547 | 50 |
// this moves the tableview to the top |
51 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
52 |
} |
|
53 |
||
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
54 |
-(void) viewDidAppear:(BOOL)animated { |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
55 |
[super viewDidAppear:animated]; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
56 |
Mix_OpenAudio(44100, 0x8010, 1, 1024); |
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 |
|
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
59 |
-(void) viewDidDisappear:(BOOL)animated { |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
60 |
[super viewDidDisappear:animated]; |
3667 | 61 |
if(voiceBeingPlayed != NULL) { |
62 |
Mix_HaltChannel(lastChannel); |
|
63 |
Mix_FreeChunk(voiceBeingPlayed); |
|
64 |
voiceBeingPlayed = NULL; |
|
3547 | 65 |
} |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
66 |
Mix_CloseAudio(); |
3547 | 67 |
} |
68 |
||
69 |
||
70 |
#pragma mark - |
|
71 |
#pragma mark Table view data source |
|
72 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
73 |
return 1; |
|
74 |
} |
|
75 |
||
76 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
77 |
return [self.voiceArray count]; |
|
78 |
} |
|
79 |
||
80 |
// Customize the appearance of table view cells. |
|
81 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
3697 | 82 |
|
3547 | 83 |
static NSString *CellIdentifier = @"Cell"; |
3697 | 84 |
|
3547 | 85 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
86 |
if (cell == nil) { |
|
87 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
88 |
} |
|
3697 | 89 |
|
3547 | 90 |
NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
91 |
cell.textLabel.text = voice; |
|
3697 | 92 |
|
3547 | 93 |
if ([voice isEqualToString:[teamDictionary objectForKey:@"voicepack"]]) { |
94 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
95 |
self.lastIndexPath = indexPath; |
|
96 |
} else { |
|
97 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
98 |
} |
|
99 |
||
100 |
return cell; |
|
101 |
} |
|
102 |
||
103 |
||
104 |
#pragma mark - |
|
105 |
#pragma mark Table view delegate |
|
106 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
107 |
int newRow = [indexPath row]; |
|
108 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3697 | 109 |
|
3547 | 110 |
if (newRow != oldRow) { |
111 |
[teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"]; |
|
3697 | 112 |
|
3547 | 113 |
// tell our boss to write this new stuff on disk |
114 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
115 |
[self.tableView reloadData]; |
|
3697 | 116 |
|
3547 | 117 |
self.lastIndexPath = indexPath; |
118 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
3697 | 119 |
} |
3547 | 120 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
3697 | 121 |
|
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
122 |
// stop any sound before playing another one |
3667 | 123 |
if (voiceBeingPlayed != NULL) { |
124 |
Mix_HaltChannel(lastChannel); |
|
125 |
Mix_FreeChunk(voiceBeingPlayed); |
|
126 |
voiceBeingPlayed = NULL; |
|
3547 | 127 |
} |
3697 | 128 |
|
3547 | 129 |
NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]]; |
130 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL]; |
|
3697 | 131 |
|
3547 | 132 |
int index = random() % [array count]; |
3697 | 133 |
|
3667 | 134 |
voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]); |
3547 | 135 |
[voiceDir release]; |
3697 | 136 |
lastChannel = Mix_PlayChannel(-1, voiceBeingPlayed, 0); |
3547 | 137 |
} |
138 |
||
139 |
||
140 |
#pragma mark - |
|
141 |
#pragma mark Memory management |
|
142 |
-(void) didReceiveMemoryWarning { |
|
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
143 |
if (voiceBeingPlayed != NULL) { |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
144 |
Mix_HaltChannel(lastChannel); |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
145 |
Mix_FreeChunk(voiceBeingPlayed); |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
146 |
voiceBeingPlayed = NULL; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
147 |
} |
3971 | 148 |
self.lastIndexPath = nil; |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
149 |
MSG_MEMCLEAN(); |
3547 | 150 |
[super didReceiveMemoryWarning]; |
151 |
} |
|
152 |
||
153 |
-(void) viewDidUnload { |
|
3971 | 154 |
if (voiceBeingPlayed != NULL) { |
155 |
Mix_HaltChannel(lastChannel); |
|
156 |
Mix_FreeChunk(voiceBeingPlayed); |
|
157 |
voiceBeingPlayed = NULL; |
|
158 |
} |
|
3547 | 159 |
self.lastIndexPath = nil; |
160 |
self.teamDictionary = nil; |
|
161 |
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
|
162 |
MSG_DIDUNLOAD(); |
3547 | 163 |
[super viewDidUnload]; |
164 |
} |
|
165 |
||
166 |
-(void) dealloc { |
|
5208 | 167 |
releaseAndNil(voiceArray); |
168 |
releaseAndNil(teamDictionary); |
|
169 |
releaseAndNil(lastIndexPath); |
|
3547 | 170 |
[super dealloc]; |
171 |
} |
|
172 |
||
173 |
||
174 |
@end |
|
175 |