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