author | koda |
Sat, 14 Aug 2010 02:10:27 +0200 | |
changeset 3739 | 97cf933e5bd2 |
parent 3697 | d5b30d6373fc |
child 3829 | 81db3c85784b |
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; |
|
3697 | 34 |
|
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
|
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]; |
3697 | 40 |
|
3547 | 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 { |
|
3697 | 67 |
|
3547 | 68 |
static NSString *CellIdentifier = @"Cell"; |
3697 | 69 |
|
3547 | 70 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
71 |
if (cell == nil) { |
|
72 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
73 |
} |
|
3697 | 74 |
|
3547 | 75 |
NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
76 |
cell.textLabel.text = voice; |
|
3697 | 77 |
|
3547 | 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; |
|
3697 | 94 |
|
3547 | 95 |
if (newRow != oldRow) { |
96 |
[teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"]; |
|
3697 | 97 |
|
3547 | 98 |
// tell our boss to write this new stuff on disk |
99 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
100 |
[self.tableView reloadData]; |
|
3697 | 101 |
|
3547 | 102 |
self.lastIndexPath = indexPath; |
103 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
3697 | 104 |
} |
3547 | 105 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
3697 | 106 |
|
3667 | 107 |
if (voiceBeingPlayed != NULL) { |
108 |
Mix_HaltChannel(lastChannel); |
|
109 |
Mix_FreeChunk(voiceBeingPlayed); |
|
110 |
voiceBeingPlayed = NULL; |
|
3547 | 111 |
} |
3697 | 112 |
|
3547 | 113 |
NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]]; |
114 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL]; |
|
3697 | 115 |
|
3547 | 116 |
int index = random() % [array count]; |
3697 | 117 |
|
3667 | 118 |
voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]); |
3547 | 119 |
[voiceDir release]; |
3697 | 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 |