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