|
1 /* |
|
2 * Hedgewars-iOS, a Hedgewars port for iOS devices |
|
3 * Copyright (c) 2009-2011 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 27/03/2010. |
|
19 */ |
|
20 |
|
21 |
|
22 #import "SettingsBaseViewController.h" |
|
23 #import "GeneralSettingsViewController.h" |
|
24 #import "TeamSettingsViewController.h" |
|
25 #import "WeaponSettingsViewController.h" |
|
26 #import "SchemeSettingsViewController.h" |
|
27 #import "SupportViewController.h" |
|
28 |
|
29 @implementation SettingsBaseViewController |
|
30 @synthesize targetController, controllerNames, lastIndexPath; |
|
31 |
|
32 |
|
33 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
34 return rotationManager(interfaceOrientation); |
|
35 } |
|
36 |
|
37 |
|
38 #pragma mark - |
|
39 #pragma mark View lifecycle |
|
40 -(void) viewDidLoad { |
|
41 // the list of available controllers |
|
42 NSArray *array = [[NSArray alloc] initWithObjects:NSLocalizedString(@"General",@""), |
|
43 NSLocalizedString(@"Teams",@""), |
|
44 NSLocalizedString(@"Weapons",@""), |
|
45 NSLocalizedString(@"Schemes",@""), |
|
46 NSLocalizedString(@"Support",@""), |
|
47 nil]; |
|
48 self.controllerNames = array; |
|
49 [array release]; |
|
50 |
|
51 UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone |
|
52 target:self |
|
53 action:@selector(dismissSplitView)]; |
|
54 if (IS_IPAD()) { |
|
55 // this class gets loaded twice, we tell the difference by looking at targetController |
|
56 if (self.targetController != nil) { |
|
57 UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; |
|
58 tableView.delegate = self; |
|
59 tableView.dataSource = self; |
|
60 [tableView reloadData]; |
|
61 [self.view addSubview:tableView]; |
|
62 [self tableView:tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; |
|
63 [tableView release]; |
|
64 self.navigationItem.leftBarButtonItem = doneButton; |
|
65 } |
|
66 } else { |
|
67 // this class just loads all controllers and set up tabbar and navigation controllers |
|
68 NSMutableArray *tabBarNavigationControllers = [[NSMutableArray alloc] initWithCapacity:5]; |
|
69 UINavigationController *navController = nil; |
|
70 |
|
71 if (nil == generalSettingsViewController) { |
|
72 generalSettingsViewController = [[GeneralSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
73 generalSettingsViewController.tabBarItem.title = [self.controllerNames objectAtIndex:0]; |
|
74 generalSettingsViewController.tabBarItem.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/TargetBee.png",GRAPHICS_DIRECTORY()]]; |
|
75 navController = [[UINavigationController alloc] initWithRootViewController:generalSettingsViewController]; |
|
76 generalSettingsViewController.navigationItem.backBarButtonItem = doneButton; |
|
77 generalSettingsViewController.navigationItem.leftBarButtonItem = doneButton; |
|
78 [generalSettingsViewController release]; |
|
79 [tabBarNavigationControllers addObject:navController]; |
|
80 releaseAndNil(navController); |
|
81 } |
|
82 if (nil == teamSettingsViewController) { |
|
83 teamSettingsViewController = [[TeamSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
84 teamSettingsViewController.tabBarItem.title = [self.controllerNames objectAtIndex:1]; |
|
85 teamSettingsViewController.tabBarItem.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/Egg.png",GRAPHICS_DIRECTORY()]]; |
|
86 navController = [[UINavigationController alloc] initWithRootViewController:teamSettingsViewController]; |
|
87 teamSettingsViewController.navigationItem.backBarButtonItem = doneButton; |
|
88 teamSettingsViewController.navigationItem.leftBarButtonItem = doneButton; |
|
89 [tabBarNavigationControllers addObject:navController]; |
|
90 releaseAndNil(navController); |
|
91 } |
|
92 if (nil == weaponSettingsViewController) { |
|
93 weaponSettingsViewController = [[WeaponSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
94 weaponSettingsViewController.tabBarItem.title = [self.controllerNames objectAtIndex:2]; |
|
95 weaponSettingsViewController.tabBarItem.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/cheese.png",GRAPHICS_DIRECTORY()]]; |
|
96 navController = [[UINavigationController alloc] initWithRootViewController:weaponSettingsViewController]; |
|
97 weaponSettingsViewController.navigationItem.backBarButtonItem = doneButton; |
|
98 weaponSettingsViewController.navigationItem.leftBarButtonItem = doneButton; |
|
99 [tabBarNavigationControllers addObject:navController]; |
|
100 releaseAndNil(navController); |
|
101 } |
|
102 if (nil == schemeSettingsViewController) { |
|
103 schemeSettingsViewController = [[SchemeSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
104 schemeSettingsViewController.tabBarItem.title = [self.controllerNames objectAtIndex:3]; |
|
105 schemeSettingsViewController.tabBarItem.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/Targetp.png",GRAPHICS_DIRECTORY()]]; |
|
106 navController = [[UINavigationController alloc] initWithRootViewController:schemeSettingsViewController]; |
|
107 schemeSettingsViewController.navigationItem.backBarButtonItem = doneButton; |
|
108 schemeSettingsViewController.navigationItem.leftBarButtonItem = doneButton; |
|
109 [tabBarNavigationControllers addObject:navController]; |
|
110 releaseAndNil(navController); |
|
111 } |
|
112 if (nil == supportViewController) { |
|
113 supportViewController = [[SupportViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
114 supportViewController.tabBarItem.title = [self.controllerNames objectAtIndex:4]; |
|
115 supportViewController.tabBarItem.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/Seduction.png",GRAPHICS_DIRECTORY()]]; |
|
116 navController = [[UINavigationController alloc] initWithRootViewController:supportViewController]; |
|
117 supportViewController.navigationItem.backBarButtonItem = doneButton; |
|
118 supportViewController.navigationItem.leftBarButtonItem = doneButton; |
|
119 [tabBarNavigationControllers addObject:navController]; |
|
120 releaseAndNil(navController); |
|
121 } |
|
122 |
|
123 UITabBarController *tabController = [[UITabBarController alloc] init]; |
|
124 tabController.viewControllers = tabBarNavigationControllers; |
|
125 tabController.delegate = self; |
|
126 |
|
127 [self.view addSubview:tabController.view]; |
|
128 } |
|
129 [doneButton release]; |
|
130 [super viewDidLoad]; |
|
131 } |
|
132 |
|
133 -(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { |
|
134 [viewController viewWillAppear:NO]; |
|
135 } |
|
136 |
|
137 -(void) dismissSplitView { |
|
138 [AudioManagerController playBackSound]; |
|
139 [[[HedgewarsAppDelegate sharedAppDelegate] mainViewController] dismissModalViewControllerAnimated:YES]; |
|
140 } |
|
141 |
|
142 #pragma mark - |
|
143 #pragma mark Table view data source |
|
144 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
145 return 1; |
|
146 } |
|
147 |
|
148 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
149 return [self.controllerNames count]; |
|
150 } |
|
151 |
|
152 // Customize the appearance of table view cells. |
|
153 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
154 static NSString *CellIdentifier = @"Cell"; |
|
155 |
|
156 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
157 if (cell == nil) |
|
158 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
159 |
|
160 NSString *iconStr = nil; |
|
161 switch ([indexPath row]) { |
|
162 case 0: |
|
163 iconStr = [NSString stringWithFormat:@"%@/TargetBee.png",GRAPHICS_DIRECTORY()]; |
|
164 break; |
|
165 case 1: |
|
166 iconStr = [NSString stringWithFormat:@"%@/Egg.png",GRAPHICS_DIRECTORY()]; |
|
167 break; |
|
168 case 2: |
|
169 iconStr = [NSString stringWithFormat:@"%@/cheese.png",GRAPHICS_DIRECTORY()]; |
|
170 break; |
|
171 case 3: |
|
172 iconStr = [NSString stringWithFormat:@"%@/Target.png",GRAPHICS_DIRECTORY()]; |
|
173 break; |
|
174 case 4: |
|
175 iconStr = [NSString stringWithFormat:@"%@/Seduction.png",GRAPHICS_DIRECTORY()]; |
|
176 break; |
|
177 default: |
|
178 DLog(@"Nope"); |
|
179 break; |
|
180 } |
|
181 |
|
182 cell.accessoryType = UITableViewCellAccessoryNone; |
|
183 cell.textLabel.text = [controllerNames objectAtIndex:[indexPath row]]; |
|
184 UIImage *icon = [[UIImage alloc] initWithContentsOfFile:iconStr]; |
|
185 cell.imageView.image = icon; |
|
186 [icon release]; |
|
187 |
|
188 return cell; |
|
189 } |
|
190 |
|
191 #pragma mark - |
|
192 #pragma mark Table view delegate |
|
193 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
194 int newRow = [indexPath row]; |
|
195 int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
196 UIViewController *nextController = nil; |
|
197 |
|
198 if (newRow != oldRow) { |
|
199 [tableView deselectRowAtIndexPath:lastIndexPath animated:YES]; |
|
200 [targetController.navigationController popToRootViewControllerAnimated:NO]; |
|
201 |
|
202 switch (newRow) { |
|
203 case 0: |
|
204 if (nil == generalSettingsViewController) |
|
205 generalSettingsViewController = [[GeneralSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
206 nextController = generalSettingsViewController; |
|
207 break; |
|
208 case 1: |
|
209 if (nil == teamSettingsViewController) |
|
210 teamSettingsViewController = [[TeamSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
211 nextController = teamSettingsViewController; |
|
212 break; |
|
213 case 2: |
|
214 if (nil == weaponSettingsViewController) |
|
215 weaponSettingsViewController = [[WeaponSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
216 nextController = weaponSettingsViewController; |
|
217 break; |
|
218 case 3: |
|
219 if (nil == schemeSettingsViewController) |
|
220 schemeSettingsViewController = [[SchemeSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
221 nextController = schemeSettingsViewController; |
|
222 break; |
|
223 case 4: |
|
224 if (nil == supportViewController) |
|
225 supportViewController = [[SupportViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
226 nextController = supportViewController; |
|
227 break; |
|
228 } |
|
229 |
|
230 self.lastIndexPath = indexPath; |
|
231 [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
232 |
|
233 nextController.navigationItem.hidesBackButton = YES; |
|
234 [nextController viewWillAppear:NO]; |
|
235 [targetController.navigationController pushViewController:nextController animated:NO]; |
|
236 [AudioManagerController playClickSound]; |
|
237 } |
|
238 } |
|
239 |
|
240 |
|
241 #pragma mark - |
|
242 #pragma mark Memory management |
|
243 -(void) didReceiveMemoryWarning { |
|
244 if (generalSettingsViewController.view.superview == nil) |
|
245 generalSettingsViewController = nil; |
|
246 if (teamSettingsViewController.view.superview == nil) |
|
247 teamSettingsViewController = nil; |
|
248 if (weaponSettingsViewController.view.superview == nil) |
|
249 weaponSettingsViewController = nil; |
|
250 if (schemeSettingsViewController.view.superview == nil) |
|
251 schemeSettingsViewController = nil; |
|
252 if (supportViewController.view.superview == nil) |
|
253 supportViewController = nil; |
|
254 MSG_MEMCLEAN(); |
|
255 [super didReceiveMemoryWarning]; |
|
256 } |
|
257 |
|
258 -(void) viewDidUnload { |
|
259 self.controllerNames = nil; |
|
260 self.lastIndexPath = nil; |
|
261 generalSettingsViewController = nil; |
|
262 teamSettingsViewController = nil; |
|
263 weaponSettingsViewController = nil; |
|
264 schemeSettingsViewController = nil; |
|
265 supportViewController = nil; |
|
266 MSG_DIDUNLOAD(); |
|
267 [super viewDidUnload]; |
|
268 } |
|
269 |
|
270 -(void) dealloc { |
|
271 releaseAndNil(targetController); |
|
272 releaseAndNil(controllerNames); |
|
273 releaseAndNil(lastIndexPath); |
|
274 releaseAndNil(generalSettingsViewController); |
|
275 releaseAndNil(teamSettingsViewController); |
|
276 releaseAndNil(weaponSettingsViewController); |
|
277 releaseAndNil(schemeSettingsViewController); |
|
278 releaseAndNil(supportViewController); |
|
279 [super dealloc]; |
|
280 } |
|
281 |
|
282 @end |
|
283 |