|
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(20); |
|
30 } |
|
31 |
|
32 - (void)viewWillAppear:(BOOL)animated { |
|
33 [super viewWillAppear:animated]; |
|
34 |
|
35 // this moves the tableview to the top |
|
36 [self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
37 voiceBeingPlayed = -1; |
|
38 |
|
39 // load all the voices names and store them into voiceArray |
|
40 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL]; |
|
41 self.voiceArray = array; |
|
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_stopsound(voiceBeingPlayed); |
|
54 voiceBeingPlayed = -1; |
|
55 } |
|
56 } |
|
57 |
|
58 |
|
59 #pragma mark - |
|
60 #pragma mark Table view data source |
|
61 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
62 return 1; |
|
63 } |
|
64 |
|
65 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
66 return [self.voiceArray count]; |
|
67 } |
|
68 |
|
69 // Customize the appearance of table view cells. |
|
70 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
71 |
|
72 static NSString *CellIdentifier = @"Cell"; |
|
73 |
|
74 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
75 if (cell == nil) { |
|
76 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
77 } |
|
78 |
|
79 NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
|
80 cell.textLabel.text = voice; |
|
81 |
|
82 if ([voice isEqualToString:[teamDictionary objectForKey:@"voicepack"]]) { |
|
83 cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
84 self.lastIndexPath = indexPath; |
|
85 } else { |
|
86 cell.accessoryType = UITableViewCellAccessoryNone; |
|
87 } |
|
88 |
|
89 return cell; |
|
90 } |
|
91 |
|
92 |
|
93 #pragma mark - |
|
94 #pragma mark Table view delegate |
|
95 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
96 int newRow = [indexPath row]; |
|
97 int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
98 |
|
99 if (newRow != oldRow) { |
|
100 [teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"]; |
|
101 |
|
102 // tell our boss to write this new stuff on disk |
|
103 [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
104 [self.tableView reloadData]; |
|
105 |
|
106 self.lastIndexPath = indexPath; |
|
107 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
108 } |
|
109 [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
110 |
|
111 if (voiceBeingPlayed >= 0) { |
|
112 openal_stopsound(voiceBeingPlayed); |
|
113 voiceBeingPlayed = -1; |
|
114 } |
|
115 |
|
116 // the keyword static prevents re-initialization of the variable |
|
117 NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]]; |
|
118 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL]; |
|
119 |
|
120 int index = random() % [array count]; |
|
121 |
|
122 voiceBeingPlayed = openal_loadfile([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]); |
|
123 [voiceDir release]; |
|
124 openal_playsound(voiceBeingPlayed); |
|
125 } |
|
126 |
|
127 |
|
128 #pragma mark - |
|
129 #pragma mark Memory management |
|
130 -(void) didReceiveMemoryWarning { |
|
131 // Releases the view if it doesn't have a superview. |
|
132 [super didReceiveMemoryWarning]; |
|
133 // Relinquish ownership any cached data, images, etc that aren't in use. |
|
134 } |
|
135 |
|
136 -(void) viewDidUnload { |
|
137 openal_close(); |
|
138 voiceBeingPlayed = -1; |
|
139 self.lastIndexPath = nil; |
|
140 self.teamDictionary = nil; |
|
141 self.voiceArray = nil; |
|
142 [super viewDidUnload]; |
|
143 MSG_DIDUNLOAD(); |
|
144 } |
|
145 |
|
146 -(void) dealloc { |
|
147 [voiceArray release]; |
|
148 [teamDictionary release]; |
|
149 [lastIndexPath release]; |
|
150 [super dealloc]; |
|
151 } |
|
152 |
|
153 |
|
154 @end |
|
155 |