author | koda |
Sat, 04 Sep 2010 16:24:00 +0200 | |
changeset 3829 | 81db3c85784b |
parent 3697 | d5b30d6373fc |
child 3883 | 5934ddf9ed5d |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
3 |
* Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 |
* |
|
18 |
* File created on 02/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "VoicesViewController.h" |
|
23 |
#import "CommodityFunctions.h" |
|
24 |
||
25 |
||
26 |
@implementation VoicesViewController |
|
27 |
@synthesize teamDictionary, voiceArray, lastIndexPath; |
|
28 |
||
29 |
||
30 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
31 |
return rotationManager(interfaceOrientation); |
|
32 |
} |
|
33 |
||
34 |
||
35 |
#pragma mark - |
|
36 |
#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
|
37 |
-(void) viewDidLoad { |
3547 | 38 |
[super viewDidLoad]; |
39 |
srandom(time(NULL)); |
|
40 |
||
3667 | 41 |
voiceBeingPlayed = NULL; |
3547 | 42 |
|
43 |
// load all the voices names and store them into voiceArray |
|
44 |
// it's here and not in viewWillAppear because user cannot add/remove them |
|
45 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL]; |
|
46 |
self.voiceArray = array; |
|
3697 | 47 |
|
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
|
48 |
self.title = NSLocalizedString(@"Set hedgehog voices",@""); |
3547 | 49 |
} |
50 |
||
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
|
51 |
-(void) viewWillAppear:(BOOL)animated { |
3547 | 52 |
[super viewWillAppear:animated]; |
3697 | 53 |
|
3547 | 54 |
// this moves the tableview to the top |
55 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
56 |
} |
|
57 |
||
58 |
-(void) viewWillDisappear:(BOOL)animated { |
|
59 |
[super viewWillDisappear:animated]; |
|
3667 | 60 |
if(voiceBeingPlayed != NULL) { |
61 |
Mix_HaltChannel(lastChannel); |
|
62 |
Mix_FreeChunk(voiceBeingPlayed); |
|
63 |
voiceBeingPlayed = NULL; |
|
3547 | 64 |
} |
65 |
} |
|
66 |
||
67 |
||
68 |
#pragma mark - |
|
69 |
#pragma mark Table view data source |
|
70 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
71 |
return 1; |
|
72 |
} |
|
73 |
||
74 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
75 |
return [self.voiceArray count]; |
|
76 |
} |
|
77 |
||
78 |
// Customize the appearance of table view cells. |
|
79 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
3697 | 80 |
|
3547 | 81 |
static NSString *CellIdentifier = @"Cell"; |
3697 | 82 |
|
3547 | 83 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
84 |
if (cell == nil) { |
|
85 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
86 |
} |
|
3697 | 87 |
|
3547 | 88 |
NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
89 |
cell.textLabel.text = voice; |
|
3697 | 90 |
|
3547 | 91 |
if ([voice isEqualToString:[teamDictionary objectForKey:@"voicepack"]]) { |
92 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
93 |
self.lastIndexPath = indexPath; |
|
94 |
} else { |
|
95 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
96 |
} |
|
97 |
||
98 |
return cell; |
|
99 |
} |
|
100 |
||
101 |
||
102 |
#pragma mark - |
|
103 |
#pragma mark Table view delegate |
|
104 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
105 |
int newRow = [indexPath row]; |
|
106 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3697 | 107 |
|
3547 | 108 |
if (newRow != oldRow) { |
109 |
[teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"]; |
|
3697 | 110 |
|
3547 | 111 |
// tell our boss to write this new stuff on disk |
112 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
113 |
[self.tableView reloadData]; |
|
3697 | 114 |
|
3547 | 115 |
self.lastIndexPath = indexPath; |
116 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
3697 | 117 |
} |
3547 | 118 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
3697 | 119 |
|
3667 | 120 |
if (voiceBeingPlayed != NULL) { |
121 |
Mix_HaltChannel(lastChannel); |
|
122 |
Mix_FreeChunk(voiceBeingPlayed); |
|
123 |
voiceBeingPlayed = NULL; |
|
3547 | 124 |
} |
3697 | 125 |
|
3547 | 126 |
NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]]; |
127 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL]; |
|
3697 | 128 |
|
3547 | 129 |
int index = random() % [array count]; |
3697 | 130 |
|
3667 | 131 |
voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]); |
3547 | 132 |
[voiceDir release]; |
3697 | 133 |
lastChannel = Mix_PlayChannel(-1, voiceBeingPlayed, 0); |
3547 | 134 |
} |
135 |
||
136 |
||
137 |
#pragma mark - |
|
138 |
#pragma mark Memory management |
|
139 |
-(void) didReceiveMemoryWarning { |
|
140 |
// Releases the view if it doesn't have a superview. |
|
141 |
[super didReceiveMemoryWarning]; |
|
142 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
143 |
} |
|
144 |
||
145 |
-(void) viewDidUnload { |
|
3667 | 146 |
voiceBeingPlayed = NULL; |
3547 | 147 |
self.lastIndexPath = nil; |
148 |
self.teamDictionary = nil; |
|
149 |
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
|
150 |
MSG_DIDUNLOAD(); |
3547 | 151 |
[super viewDidUnload]; |
152 |
} |
|
153 |
||
154 |
-(void) dealloc { |
|
155 |
[voiceArray release]; |
|
156 |
[teamDictionary release]; |
|
157 |
[lastIndexPath release]; |
|
158 |
[super dealloc]; |
|
159 |
} |
|
160 |
||
161 |
||
162 |
@end |
|
163 |