author | koda |
Sun, 06 Nov 2011 16:41:14 +0100 | |
changeset 6300 | db8fd3ff693c |
parent 6078 | 8c0cc07731e5 |
child 6700 | e04da46ee43c |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
4976 | 3 |
* Copyright (c) 2009-2011 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 |
* File created on 02/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#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
|
23 |
|
3547 | 24 |
|
25 |
@implementation VoicesViewController |
|
26 |
@synthesize teamDictionary, voiceArray, lastIndexPath; |
|
27 |
||
28 |
||
29 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
30 |
return rotationManager(interfaceOrientation); |
|
31 |
} |
|
32 |
||
33 |
#pragma mark - |
|
34 |
#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
|
35 |
-(void) viewDidLoad { |
3547 | 36 |
[super viewDidLoad]; |
37 |
srandom(time(NULL)); |
|
38 |
||
3667 | 39 |
voiceBeingPlayed = NULL; |
3547 | 40 |
|
41 |
// load all the voices names and store them into voiceArray |
|
42 |
// it's here and not in viewWillAppear because user cannot add/remove them |
|
43 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL]; |
|
44 |
self.voiceArray = array; |
|
3697 | 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 |
self.title = NSLocalizedString(@"Set hedgehog voices",@""); |
3547 | 47 |
} |
48 |
||
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
|
49 |
-(void) viewWillAppear:(BOOL)animated { |
3547 | 50 |
[super viewWillAppear:animated]; |
3697 | 51 |
|
3547 | 52 |
// this moves the tableview to the top |
53 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
54 |
} |
|
55 |
||
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
56 |
-(void) viewDidAppear:(BOOL)animated { |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
57 |
[super viewDidAppear:animated]; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
58 |
Mix_OpenAudio(44100, 0x8010, 1, 1024); |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
59 |
} |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
60 |
|
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
61 |
-(void) viewDidDisappear:(BOOL)animated { |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
62 |
[super viewDidDisappear:animated]; |
3667 | 63 |
if(voiceBeingPlayed != NULL) { |
64 |
Mix_HaltChannel(lastChannel); |
|
65 |
Mix_FreeChunk(voiceBeingPlayed); |
|
66 |
voiceBeingPlayed = NULL; |
|
3547 | 67 |
} |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
68 |
Mix_CloseAudio(); |
3547 | 69 |
} |
70 |
||
71 |
||
72 |
#pragma mark - |
|
73 |
#pragma mark Table view data source |
|
74 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
75 |
return 1; |
|
76 |
} |
|
77 |
||
78 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
79 |
return [self.voiceArray count]; |
|
80 |
} |
|
81 |
||
82 |
// Customize the appearance of table view cells. |
|
83 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
3697 | 84 |
|
3547 | 85 |
static NSString *CellIdentifier = @"Cell"; |
3697 | 86 |
|
3547 | 87 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
88 |
if (cell == nil) { |
|
89 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
90 |
} |
|
3697 | 91 |
|
3547 | 92 |
NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
93 |
cell.textLabel.text = voice; |
|
3697 | 94 |
|
3547 | 95 |
if ([voice isEqualToString:[teamDictionary objectForKey:@"voicepack"]]) { |
96 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
97 |
self.lastIndexPath = indexPath; |
|
98 |
} else { |
|
99 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
100 |
} |
|
101 |
||
102 |
return cell; |
|
103 |
} |
|
104 |
||
105 |
||
106 |
#pragma mark - |
|
107 |
#pragma mark Table view delegate |
|
108 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
109 |
int newRow = [indexPath row]; |
|
110 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3697 | 111 |
|
3547 | 112 |
if (newRow != oldRow) { |
113 |
[teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"]; |
|
3697 | 114 |
|
3547 | 115 |
// tell our boss to write this new stuff on disk |
116 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
117 |
[self.tableView reloadData]; |
|
3697 | 118 |
|
3547 | 119 |
self.lastIndexPath = indexPath; |
120 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
3697 | 121 |
} |
3547 | 122 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
3697 | 123 |
|
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
124 |
// stop any sound before playing another one |
3667 | 125 |
if (voiceBeingPlayed != NULL) { |
126 |
Mix_HaltChannel(lastChannel); |
|
127 |
Mix_FreeChunk(voiceBeingPlayed); |
|
128 |
voiceBeingPlayed = NULL; |
|
3547 | 129 |
} |
3697 | 130 |
|
3547 | 131 |
NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]]; |
132 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL]; |
|
3697 | 133 |
|
3547 | 134 |
int index = random() % [array count]; |
3697 | 135 |
|
3667 | 136 |
voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]); |
3547 | 137 |
[voiceDir release]; |
3697 | 138 |
lastChannel = Mix_PlayChannel(-1, voiceBeingPlayed, 0); |
3547 | 139 |
} |
140 |
||
141 |
||
142 |
#pragma mark - |
|
143 |
#pragma mark Memory management |
|
144 |
-(void) didReceiveMemoryWarning { |
|
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
145 |
if (voiceBeingPlayed != NULL) { |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
146 |
Mix_HaltChannel(lastChannel); |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
147 |
Mix_FreeChunk(voiceBeingPlayed); |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
148 |
voiceBeingPlayed = NULL; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
149 |
} |
3971 | 150 |
self.lastIndexPath = nil; |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
151 |
MSG_MEMCLEAN(); |
3547 | 152 |
[super didReceiveMemoryWarning]; |
153 |
} |
|
154 |
||
155 |
-(void) viewDidUnload { |
|
3971 | 156 |
if (voiceBeingPlayed != NULL) { |
157 |
Mix_HaltChannel(lastChannel); |
|
158 |
Mix_FreeChunk(voiceBeingPlayed); |
|
159 |
voiceBeingPlayed = NULL; |
|
160 |
} |
|
3547 | 161 |
self.lastIndexPath = nil; |
162 |
self.teamDictionary = nil; |
|
163 |
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
|
164 |
MSG_DIDUNLOAD(); |
3547 | 165 |
[super viewDidUnload]; |
166 |
} |
|
167 |
||
168 |
-(void) dealloc { |
|
5208 | 169 |
releaseAndNil(voiceArray); |
170 |
releaseAndNil(teamDictionary); |
|
171 |
releaseAndNil(lastIndexPath); |
|
3547 | 172 |
[super dealloc]; |
173 |
} |
|
174 |
||
175 |
||
176 |
@end |
|
177 |