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