author | koda |
Thu, 22 Jul 2010 03:08:17 +0200 | |
changeset 3662 | a44406f4369b |
parent 3547 | 02875b1145b7 |
child 3667 | 9359a70df013 |
permissions | -rw-r--r-- |
3547 | 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 |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3547
diff
changeset
|
25 |
-(void) viewDidLoad { |
3547 | 26 |
[super viewDidLoad]; |
27 |
srandom(time(NULL)); |
|
28 |
||
29 |
openal_init(); |
|
30 |
voiceBeingPlayed = -1; |
|
31 |
||
32 |
// load all the voices names and store them into voiceArray |
|
33 |
// it's here and not in viewWillAppear because user cannot add/remove them |
|
34 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL]; |
|
35 |
self.voiceArray = array; |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3547
diff
changeset
|
36 |
|
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3547
diff
changeset
|
37 |
self.title = NSLocalizedString(@"Set hedgehog voices",@""); |
3547 | 38 |
} |
39 |
||
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3547
diff
changeset
|
40 |
-(void) viewWillAppear:(BOOL)animated { |
3547 | 41 |
[super viewWillAppear:animated]; |
42 |
||
43 |
// this moves the tableview to the top |
|
44 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
45 |
} |
|
46 |
||
47 |
-(void) viewWillDisappear:(BOOL)animated { |
|
48 |
[super viewWillDisappear:animated]; |
|
49 |
if(voiceBeingPlayed >= 0) { |
|
50 |
openal_stopsound(voiceBeingPlayed); |
|
51 |
voiceBeingPlayed = -1; |
|
52 |
} |
|
53 |
} |
|
54 |
||
55 |
||
56 |
#pragma mark - |
|
57 |
#pragma mark Table view data source |
|
58 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
59 |
return 1; |
|
60 |
} |
|
61 |
||
62 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
63 |
return [self.voiceArray count]; |
|
64 |
} |
|
65 |
||
66 |
// Customize the appearance of table view cells. |
|
67 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
68 |
||
69 |
static NSString *CellIdentifier = @"Cell"; |
|
70 |
||
71 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
72 |
if (cell == nil) { |
|
73 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
74 |
} |
|
75 |
||
76 |
NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
|
77 |
cell.textLabel.text = voice; |
|
78 |
||
79 |
if ([voice isEqualToString:[teamDictionary objectForKey:@"voicepack"]]) { |
|
80 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
81 |
self.lastIndexPath = indexPath; |
|
82 |
} else { |
|
83 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
84 |
} |
|
85 |
||
86 |
return cell; |
|
87 |
} |
|
88 |
||
89 |
||
90 |
#pragma mark - |
|
91 |
#pragma mark Table view delegate |
|
92 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
93 |
int newRow = [indexPath row]; |
|
94 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
95 |
||
96 |
if (newRow != oldRow) { |
|
97 |
[teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"]; |
|
98 |
||
99 |
// tell our boss to write this new stuff on disk |
|
100 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
101 |
[self.tableView reloadData]; |
|
102 |
||
103 |
self.lastIndexPath = indexPath; |
|
104 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
105 |
} |
|
106 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
107 |
||
108 |
if (voiceBeingPlayed >= 0) { |
|
109 |
openal_stopsound(voiceBeingPlayed); |
|
110 |
voiceBeingPlayed = -1; |
|
111 |
} |
|
112 |
||
113 |
// the keyword static prevents re-initialization of the variable |
|
114 |
NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]]; |
|
115 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL]; |
|
116 |
||
117 |
int index = random() % [array count]; |
|
118 |
||
119 |
voiceBeingPlayed = openal_loadfile([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]); |
|
120 |
[voiceDir release]; |
|
121 |
openal_playsound(voiceBeingPlayed); |
|
122 |
} |
|
123 |
||
124 |
||
125 |
#pragma mark - |
|
126 |
#pragma mark Memory management |
|
127 |
-(void) didReceiveMemoryWarning { |
|
128 |
// Releases the view if it doesn't have a superview. |
|
129 |
[super didReceiveMemoryWarning]; |
|
130 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
131 |
} |
|
132 |
||
133 |
-(void) viewDidUnload { |
|
134 |
openal_close(); |
|
135 |
voiceBeingPlayed = -1; |
|
136 |
self.lastIndexPath = nil; |
|
137 |
self.teamDictionary = nil; |
|
138 |
self.voiceArray = nil; |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3547
diff
changeset
|
139 |
MSG_DIDUNLOAD(); |
3547 | 140 |
[super viewDidUnload]; |
141 |
} |
|
142 |
||
143 |
-(void) dealloc { |
|
144 |
[voiceArray release]; |
|
145 |
[teamDictionary release]; |
|
146 |
[lastIndexPath release]; |
|
147 |
[super dealloc]; |
|
148 |
} |
|
149 |
||
150 |
||
151 |
@end |
|
152 |