author | Wuzzy <Wuzzy2@mail.ru> |
Thu, 25 Jul 2019 19:18:48 +0200 | |
changeset 15276 | a0e425458694 |
parent 12877 | 00215a7ec5f5 |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
6700 | 3 |
* Copyright (c) 2009-2012 Vittorio Giovara <vittorio.giovara@gmail.com> |
3829 | 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 |
|
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
6832
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
3829 | 17 |
*/ |
18 |
||
3547 | 19 |
|
20 |
#import "VoicesViewController.h" |
|
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5208
diff
changeset
|
21 |
|
3547 | 22 |
|
23 |
@implementation VoicesViewController |
|
24 |
@synthesize teamDictionary, voiceArray, lastIndexPath; |
|
25 |
||
26 |
||
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11210
diff
changeset
|
27 |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
3547 | 28 |
return rotationManager(interfaceOrientation); |
29 |
} |
|
30 |
||
31 |
#pragma mark - |
|
32 |
#pragma mark View lifecycle |
|
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11210
diff
changeset
|
33 |
- (void)viewDidLoad { |
3547 | 34 |
[super viewDidLoad]; |
35 |
||
3667 | 36 |
voiceBeingPlayed = NULL; |
3547 | 37 |
|
38 |
// load all the voices names and store them into voiceArray |
|
39 |
// it's here and not in viewWillAppear because user cannot add/remove them |
|
40 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL]; |
|
41 |
self.voiceArray = array; |
|
3697 | 42 |
|
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
|
43 |
self.title = NSLocalizedString(@"Set hedgehog voices",@""); |
3547 | 44 |
} |
45 |
||
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11210
diff
changeset
|
46 |
- (void)viewWillAppear:(BOOL)animated { |
3547 | 47 |
[super viewWillAppear:animated]; |
3697 | 48 |
|
3547 | 49 |
// this moves the tableview to the top |
50 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
51 |
} |
|
52 |
||
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11210
diff
changeset
|
53 |
- (void)viewDidAppear:(BOOL)animated { |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
54 |
[super viewDidAppear:animated]; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
55 |
Mix_OpenAudio(44100, 0x8010, 1, 1024); |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
56 |
} |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
57 |
|
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11210
diff
changeset
|
58 |
- (void)viewDidDisappear:(BOOL)animated { |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
59 |
[super viewDidDisappear:animated]; |
3667 | 60 |
if(voiceBeingPlayed != NULL) { |
61 |
Mix_HaltChannel(lastChannel); |
|
62 |
Mix_FreeChunk(voiceBeingPlayed); |
|
63 |
voiceBeingPlayed = NULL; |
|
3547 | 64 |
} |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
65 |
Mix_CloseAudio(); |
3547 | 66 |
} |
67 |
||
68 |
||
69 |
#pragma mark - |
|
70 |
#pragma mark Table view data source |
|
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11210
diff
changeset
|
71 |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
3547 | 72 |
return 1; |
73 |
} |
|
74 |
||
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11210
diff
changeset
|
75 |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
3547 | 76 |
return [self.voiceArray count]; |
77 |
} |
|
78 |
||
79 |
// Customize the appearance of table view cells. |
|
80 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
3697 | 81 |
|
3547 | 82 |
static NSString *CellIdentifier = @"Cell"; |
3697 | 83 |
|
3547 | 84 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
85 |
if (cell == nil) { |
|
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11210
diff
changeset
|
86 |
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; |
3547 | 87 |
} |
3697 | 88 |
|
3547 | 89 |
NSString *voice = [[voiceArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
90 |
cell.textLabel.text = voice; |
|
3697 | 91 |
|
3547 | 92 |
if ([voice isEqualToString:[teamDictionary objectForKey:@"voicepack"]]) { |
93 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
94 |
self.lastIndexPath = indexPath; |
|
95 |
} else { |
|
96 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
97 |
} |
|
98 |
||
99 |
return cell; |
|
100 |
} |
|
101 |
||
102 |
||
103 |
#pragma mark - |
|
104 |
#pragma mark Table view delegate |
|
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11210
diff
changeset
|
105 |
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
11148
064a53861759
- Refactoring in order to remove some warning related to using of int instead of NSInteger
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
106 |
NSInteger newRow = [indexPath row]; |
064a53861759
- Refactoring in order to remove some warning related to using of int instead of NSInteger
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
107 |
NSInteger oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
3697 | 108 |
|
3547 | 109 |
if (newRow != oldRow) { |
110 |
[teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"]; |
|
3697 | 111 |
|
3547 | 112 |
// tell our boss to write this new stuff on disk |
113 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
114 |
[self.tableView reloadData]; |
|
3697 | 115 |
|
3547 | 116 |
self.lastIndexPath = indexPath; |
117 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
3697 | 118 |
} |
3547 | 119 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
3697 | 120 |
|
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
121 |
// stop any sound before playing another one |
3667 | 122 |
if (voiceBeingPlayed != NULL) { |
123 |
Mix_HaltChannel(lastChannel); |
|
124 |
Mix_FreeChunk(voiceBeingPlayed); |
|
125 |
voiceBeingPlayed = NULL; |
|
3547 | 126 |
} |
3697 | 127 |
|
3547 | 128 |
NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]]; |
129 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL]; |
|
3697 | 130 |
|
11210
2e80c9861818
- Better random number generation for front-end: random() replaced with arc4random_uniform()
antonc27 <antonc27@mail.ru>
parents:
11148
diff
changeset
|
131 |
int index = arc4random_uniform((int)[array count]); |
3697 | 132 |
|
3667 | 133 |
voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]); |
3697 | 134 |
lastChannel = Mix_PlayChannel(-1, voiceBeingPlayed, 0); |
3547 | 135 |
} |
136 |
||
137 |
||
138 |
#pragma mark - |
|
139 |
#pragma mark Memory management |
|
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11210
diff
changeset
|
140 |
|
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11210
diff
changeset
|
141 |
- (void)didReceiveMemoryWarning { |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
142 |
if (voiceBeingPlayed != NULL) { |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
143 |
Mix_HaltChannel(lastChannel); |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
144 |
Mix_FreeChunk(voiceBeingPlayed); |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
145 |
voiceBeingPlayed = NULL; |
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
146 |
} |
3971 | 147 |
self.lastIndexPath = nil; |
3883
5934ddf9ed5d
fixed the voice selection where you couldn't hear a preview sound
koda
parents:
3829
diff
changeset
|
148 |
MSG_MEMCLEAN(); |
3547 | 149 |
[super didReceiveMemoryWarning]; |
150 |
} |
|
151 |
||
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
11210
diff
changeset
|
152 |
- (void)dealloc { |
3971 | 153 |
if (voiceBeingPlayed != NULL) { |
154 |
Mix_HaltChannel(lastChannel); |
|
155 |
Mix_FreeChunk(voiceBeingPlayed); |
|
156 |
voiceBeingPlayed = NULL; |
|
157 |
} |
|
3547 | 158 |
} |
159 |
||
160 |
@end |
|
161 |