author | nemo |
Thu, 06 May 2010 15:26:14 +0000 | |
changeset 3434 | 6af73e7f2438 |
parent 3364 | e5403e2bf02c |
child 3490 | 016b3172b645 |
permissions | -rw-r--r-- |
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" |
3353
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
11 |
#import "openalbridge.h" |
3339 | 12 |
|
13 |
||
14 |
@implementation VoicesViewController |
|
3340 | 15 |
@synthesize teamDictionary, voiceArray, lastIndexPath; |
3339 | 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 |
||
3364 | 29 |
openal_init(1); |
3353
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
30 |
voiceBeingPlayed = -1; |
3339 | 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]; |
|
3353
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
52 |
if(voiceBeingPlayed >= 0) { |
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
53 |
openal_freesound(voiceBeingPlayed); |
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
54 |
voiceBeingPlayed = -1; |
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 |
*/ |
|
137 |
||
138 |
#pragma mark - |
|
139 |
#pragma mark Table view delegate |
|
3340 | 140 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
3339 | 141 |
int newRow = [indexPath row]; |
142 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
143 |
||
144 |
if (newRow != oldRow) { |
|
3340 | 145 |
[teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"]; |
3339 | 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]; |
|
3340 | 155 |
|
3353
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
156 |
if (voiceBeingPlayed >= 0) { |
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
157 |
openal_stopsound(voiceBeingPlayed); |
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
158 |
openal_freesound(voiceBeingPlayed); |
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
159 |
voiceBeingPlayed = -1; |
3339 | 160 |
} |
3340 | 161 |
|
3339 | 162 |
// the keyword static prevents re-initialization of the variable |
3340 | 163 |
NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]]; |
3339 | 164 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL]; |
3340 | 165 |
|
3339 | 166 |
int index = random() % [array count]; |
167 |
||
3353
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
168 |
voiceBeingPlayed = openal_loadfile([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]); |
3339 | 169 |
[voiceDir release]; |
3353
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
170 |
openal_playsound(voiceBeingPlayed); |
3339 | 171 |
} |
172 |
||
173 |
||
174 |
#pragma mark - |
|
175 |
#pragma mark Memory management |
|
176 |
- (void)didReceiveMemoryWarning { |
|
3353
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
177 |
openal_stopsound(voiceBeingPlayed); |
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
178 |
openal_freesound(voiceBeingPlayed); |
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
179 |
voiceBeingPlayed = -1; |
3339 | 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 |
[super viewDidUnload]; |
|
187 |
||
3353
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
188 |
openal_close(); |
a767dd3786b5
OpenAL makes a comeback! (might require some cleanup)
koda
parents:
3352
diff
changeset
|
189 |
voiceBeingPlayed = -1; |
3339 | 190 |
self.lastIndexPath = nil; |
191 |
self.teamDictionary = nil; |
|
192 |
self.voiceArray = nil; |
|
193 |
} |
|
194 |
||
195 |
- (void)dealloc { |
|
196 |
[voiceArray release]; |
|
197 |
[teamDictionary release]; |
|
198 |
[lastIndexPath release]; |
|
199 |
[super dealloc]; |
|
200 |
} |
|
201 |
||
202 |
||
203 |
@end |
|
204 |
||
205 |