3339
|
1 |
//
|
3352
|
2 |
// VoicesViewController.m
|
3339
|
3 |
// HedgewarsMobile
|
|
4 |
//
|
|
5 |
// Created by Vittorio on 02/04/10.
|
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved.
|
|
7 |
//
|
|
8 |
|
3340
|
9 |
#import "VoicesViewController.h"
|
3339
|
10 |
#import "CommodityFunctions.h"
|
|
11 |
|
|
12 |
|
|
13 |
@implementation VoicesViewController
|
3340
|
14 |
@synthesize teamDictionary, voiceArray, lastIndexPath;
|
3339
|
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);
|
3340
|
29 |
voiceBeingPlayed = NULL;
|
3339
|
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 |
}
|
|
35 |
|
|
36 |
- (void)viewWillAppear:(BOOL)animated {
|
|
37 |
[super viewWillAppear:animated];
|
|
38 |
|
|
39 |
// this moves the tableview to the top
|
|
40 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
|
|
41 |
}
|
|
42 |
|
|
43 |
/*
|
|
44 |
- (void)viewDidAppear:(BOOL)animated {
|
|
45 |
[super viewDidAppear:animated];
|
|
46 |
}
|
|
47 |
*/
|
|
48 |
|
|
49 |
- (void)viewWillDisappear:(BOOL)animated {
|
|
50 |
[super viewWillDisappear:animated];
|
3340
|
51 |
if(voiceBeingPlayed != NULL) {
|
|
52 |
Mix_HaltChannel(-1);
|
|
53 |
Mix_FreeChunk(voiceBeingPlayed);
|
|
54 |
voiceBeingPlayed = NULL;
|
3339
|
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 |
*/
|
3340
|
137 |
-(void) allowSelection {
|
|
138 |
self.tableView.allowsSelection = YES;
|
|
139 |
}
|
3339
|
140 |
|
|
141 |
#pragma mark -
|
|
142 |
#pragma mark Table view delegate
|
3340
|
143 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
3339
|
144 |
int newRow = [indexPath row];
|
|
145 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
|
|
146 |
|
3340
|
147 |
// avoid a crash in sdl_mixer
|
|
148 |
self.tableView.allowsSelection = NO;
|
|
149 |
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(allowSelection) userInfo:nil repeats:NO];
|
|
150 |
|
3339
|
151 |
if (newRow != oldRow) {
|
3340
|
152 |
[teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"];
|
3339
|
153 |
|
|
154 |
// tell our boss to write this new stuff on disk
|
|
155 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
|
|
156 |
[self.tableView reloadData];
|
|
157 |
|
|
158 |
self.lastIndexPath = indexPath;
|
|
159 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
|
|
160 |
}
|
|
161 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
|
3340
|
162 |
|
|
163 |
if (voiceBeingPlayed != NULL) {
|
|
164 |
Mix_HaltChannel(-1);
|
|
165 |
Mix_FreeChunk(voiceBeingPlayed);
|
|
166 |
voiceBeingPlayed = NULL;
|
3339
|
167 |
}
|
3340
|
168 |
|
3339
|
169 |
// the keyword static prevents re-initialization of the variable
|
3340
|
170 |
NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]];
|
3339
|
171 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL];
|
3340
|
172 |
|
3339
|
173 |
int index = random() % [array count];
|
|
174 |
|
3340
|
175 |
voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]);
|
3339
|
176 |
[voiceDir release];
|
3340
|
177 |
Mix_PlayChannel(-1, voiceBeingPlayed, 0);
|
3339
|
178 |
}
|
|
179 |
|
|
180 |
|
|
181 |
#pragma mark -
|
|
182 |
#pragma mark Memory management
|
|
183 |
- (void)didReceiveMemoryWarning {
|
3340
|
184 |
Mix_HaltChannel(-1);
|
|
185 |
Mix_FreeChunk(voiceBeingPlayed);
|
|
186 |
voiceBeingPlayed = NULL;
|
3339
|
187 |
// Releases the view if it doesn't have a superview.
|
|
188 |
[super didReceiveMemoryWarning];
|
|
189 |
// Relinquish ownership any cached data, images, etc that aren't in use.
|
|
190 |
}
|
|
191 |
|
|
192 |
- (void)viewDidUnload {
|
|
193 |
[super viewDidUnload];
|
|
194 |
|
|
195 |
Mix_CloseAudio();
|
3340
|
196 |
voiceBeingPlayed = NULL;
|
3339
|
197 |
self.lastIndexPath = nil;
|
|
198 |
self.teamDictionary = nil;
|
|
199 |
self.voiceArray = nil;
|
|
200 |
}
|
|
201 |
|
|
202 |
- (void)dealloc {
|
|
203 |
[voiceArray release];
|
|
204 |
[teamDictionary release];
|
|
205 |
[lastIndexPath release];
|
|
206 |
[super dealloc];
|
|
207 |
}
|
|
208 |
|
|
209 |
|
|
210 |
@end
|
|
211 |
|
|
212 |
|