1 // |
|
2 // VoicesViewController.m |
|
3 // HedgewarsMobile |
|
4 // |
|
5 // Created by Vittorio on 02/04/10. |
|
6 // Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 // |
|
8 |
|
9 #import "VoicesViewController.h" |
|
10 #import "CommodityFunctions.h" |
|
11 #import "openalbridge.h" |
|
12 |
|
13 |
|
14 @implementation VoicesViewController |
|
15 @synthesize teamDictionary, voiceArray, lastIndexPath; |
|
16 |
|
17 |
|
18 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
19 return rotationManager(interfaceOrientation); |
|
20 } |
|
21 |
|
22 |
|
23 #pragma mark - |
|
24 #pragma mark View lifecycle |
|
25 - (void)viewDidLoad { |
|
26 [super viewDidLoad]; |
|
27 srandom(time(NULL)); |
|
28 |
|
29 openal_init(1); |
|
30 voiceBeingPlayed = -1; |
|
31 |
|
32 // load all the voices names and store them into voiceArray |
|
33 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL]; |
|
34 self.voiceArray = array; |
|
35 } |
|
36 |
|
37 - (void)viewWillAppear:(BOOL)animated { |
|
38 [super viewWillAppear:animated]; |
|
39 |
|
40 // this moves the tableview to the top |
|
41 [self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
42 } |
|
43 |
|
44 /* |
|
45 - (void)viewDidAppear:(BOOL)animated { |
|
46 [super viewDidAppear:animated]; |
|
47 } |
|
48 */ |
|
49 |
|
50 - (void)viewWillDisappear:(BOOL)animated { |
|
51 [super viewWillDisappear:animated]; |
|
52 if(voiceBeingPlayed >= 0) { |
|
53 openal_freesound(voiceBeingPlayed); |
|
54 voiceBeingPlayed = -1; |
|
55 } |
|
56 } |
|
57 |
|
58 /* |
|
59 - (void)viewDidDisappear:(BOOL)animated { |
|
60 [super viewDidDisappear:animated]; |
|
61 } |
|
62 */ |
|
63 |
|
64 |
|
65 #pragma mark - |
|
66 #pragma mark Table view data source |
|
67 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
68 return 1; |
|
69 } |
|
70 |
|
71 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
72 return [self.voiceArray count]; |
|
73 } |
|
74 |
|
75 // Customize the appearance of table view cells. |
|
76 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
77 |
|
78 static NSString *CellIdentifier = @"Cell"; |
|
79 |
|
80 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
81 if (cell == nil) { |
|
82 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
83 } |
|
84 |
|
85 NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
|
86 cell.textLabel.text = voice; |
|
87 |
|
88 if ([voice isEqualToString:[teamDictionary objectForKey:@"voicepack"]]) { |
|
89 cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
90 self.lastIndexPath = indexPath; |
|
91 } else { |
|
92 cell.accessoryType = UITableViewCellAccessoryNone; |
|
93 } |
|
94 |
|
95 return cell; |
|
96 } |
|
97 |
|
98 |
|
99 /* |
|
100 // Override to support conditional editing of the table view. |
|
101 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
|
102 // Return NO if you do not want the specified item to be editable. |
|
103 return YES; |
|
104 } |
|
105 */ |
|
106 |
|
107 |
|
108 /* |
|
109 // Override to support editing the table view. |
|
110 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
111 |
|
112 if (editingStyle == UITableViewCellEditingStyleDelete) { |
|
113 // Delete the row from the data source |
|
114 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; |
|
115 } |
|
116 else if (editingStyle == UITableViewCellEditingStyleInsert) { |
|
117 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view |
|
118 } |
|
119 } |
|
120 */ |
|
121 |
|
122 |
|
123 /* |
|
124 // Override to support rearranging the table view. |
|
125 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { |
|
126 } |
|
127 */ |
|
128 |
|
129 |
|
130 /* |
|
131 // Override to support conditional rearranging of the table view. |
|
132 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
|
133 // Return NO if you do not want the item to be re-orderable. |
|
134 return YES; |
|
135 } |
|
136 */ |
|
137 |
|
138 #pragma mark - |
|
139 #pragma mark Table view delegate |
|
140 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
141 int newRow = [indexPath row]; |
|
142 int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
143 |
|
144 if (newRow != oldRow) { |
|
145 [teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"]; |
|
146 |
|
147 // tell our boss to write this new stuff on disk |
|
148 [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
149 [self.tableView reloadData]; |
|
150 |
|
151 self.lastIndexPath = indexPath; |
|
152 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
153 } |
|
154 [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
155 |
|
156 if (voiceBeingPlayed >= 0) { |
|
157 openal_stopsound(voiceBeingPlayed); |
|
158 openal_freesound(voiceBeingPlayed); |
|
159 voiceBeingPlayed = -1; |
|
160 } |
|
161 |
|
162 // the keyword static prevents re-initialization of the variable |
|
163 NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]]; |
|
164 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL]; |
|
165 |
|
166 int index = random() % [array count]; |
|
167 |
|
168 voiceBeingPlayed = openal_loadfile([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]); |
|
169 [voiceDir release]; |
|
170 openal_playsound(voiceBeingPlayed); |
|
171 } |
|
172 |
|
173 |
|
174 #pragma mark - |
|
175 #pragma mark Memory management |
|
176 -(void) didReceiveMemoryWarning { |
|
177 openal_stopsound(voiceBeingPlayed); |
|
178 openal_freesound(voiceBeingPlayed); |
|
179 voiceBeingPlayed = -1; |
|
180 // Releases the view if it doesn't have a superview. |
|
181 [super didReceiveMemoryWarning]; |
|
182 // Relinquish ownership any cached data, images, etc that aren't in use. |
|
183 } |
|
184 |
|
185 -(void) viewDidUnload { |
|
186 openal_close(); |
|
187 voiceBeingPlayed = -1; |
|
188 self.lastIndexPath = nil; |
|
189 self.teamDictionary = nil; |
|
190 self.voiceArray = nil; |
|
191 [super viewDidUnload]; |
|
192 MSG_DIDUNLOAD(); |
|
193 } |
|
194 |
|
195 -(void) dealloc { |
|
196 [voiceArray release]; |
|
197 [teamDictionary release]; |
|
198 [lastIndexPath release]; |
|
199 [super dealloc]; |
|
200 } |
|
201 |
|
202 |
|
203 @end |
|
204 |
|
205 |
|