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 rootController, 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 [super viewDidLoad]; |
|
43 |
|
44 // the list of selectable controllers |
|
45 NSArray *array = [[NSArray alloc] initWithObjects:NSLocalizedString(@"General",@""), |
|
46 NSLocalizedString(@"Teams",@""), |
|
47 NSLocalizedString(@"Weapons",@""), |
|
48 NSLocalizedString(@"Schemes",@""), |
|
49 NSLocalizedString(@"Support",@""), |
|
50 nil]; |
|
51 self.controllerNames = array; |
|
52 [array release]; |
|
53 |
|
54 // targetControllers tells whether we're on the right or left side of the splitview -- on iphone we only use the right side |
|
55 if (targetController == nil && IS_IPAD()) { |
|
56 if (nil == generalSettingsViewController) |
|
57 generalSettingsViewController = [[GeneralSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
58 generalSettingsViewController.navigationItem.hidesBackButton = YES; |
|
59 [generalSettingsViewController viewWillAppear:YES]; |
|
60 [self.navigationController pushViewController:generalSettingsViewController animated:NO]; |
|
61 } else { |
|
62 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone |
|
63 target:self |
|
64 action:@selector(dismissSplitView)]; |
|
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 [controllerNames count]; |
|
76 } |
|
77 |
|
78 // Customize the appearance of table view cells. |
|
79 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
80 static NSString *CellIdentifier = @"Cell"; |
|
81 |
|
82 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
83 if (cell == nil) |
|
84 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
85 |
|
86 NSString *iconStr = nil; |
|
87 switch ([indexPath row]) { |
|
88 case 0: |
|
89 iconStr = [NSString stringWithFormat:@"%@/TargetBee.png",GRAPHICS_DIRECTORY()]; |
|
90 break; |
|
91 case 1: |
|
92 iconStr = [NSString stringWithFormat:@"%@/Egg.png",GRAPHICS_DIRECTORY()]; |
|
93 break; |
|
94 case 2: |
|
95 iconStr = [NSString stringWithFormat:@"%@/Molotov.png",GRAPHICS_DIRECTORY()]; |
|
96 break; |
|
97 case 3: |
|
98 iconStr = [NSString stringWithFormat:@"%@/Target.png",GRAPHICS_DIRECTORY()]; |
|
99 break; |
|
100 case 4: |
|
101 iconStr = [NSString stringWithFormat:@"%@/Seduction.png",GRAPHICS_DIRECTORY()]; |
|
102 break; |
|
103 default: |
|
104 //seduction.png for support page |
|
105 DLog(@"Nope"); |
|
106 break; |
|
107 } |
|
108 |
|
109 if (nil == targetController) |
|
110 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
|
111 else |
|
112 cell.accessoryType = UITableViewCellAccessoryNone; |
|
113 |
|
114 cell.textLabel.text = [controllerNames objectAtIndex:[indexPath row]]; |
|
115 UIImage *icon = [[UIImage alloc] initWithContentsOfFile:iconStr]; |
|
116 cell.imageView.image = icon; |
|
117 [icon release]; |
|
118 |
|
119 return cell; |
|
120 } |
|
121 |
|
122 #pragma mark - |
|
123 #pragma mark Table view delegate |
|
124 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
125 int newRow = [indexPath row]; |
|
126 int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
127 UIViewController *nextController = nil; |
|
128 |
|
129 if (newRow != oldRow) { |
|
130 [self.tableView deselectRowAtIndexPath:lastIndexPath animated:YES]; |
|
131 [targetController.navigationController popToRootViewControllerAnimated:NO]; |
|
132 |
|
133 switch (newRow) { |
|
134 case 0: |
|
135 if (nil == generalSettingsViewController) |
|
136 generalSettingsViewController = [[GeneralSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
137 nextController = generalSettingsViewController; |
|
138 break; |
|
139 case 1: |
|
140 if (nil == teamSettingsViewController) |
|
141 teamSettingsViewController = [[TeamSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
142 nextController = teamSettingsViewController; |
|
143 break; |
|
144 case 2: |
|
145 if (nil == weaponSettingsViewController) |
|
146 weaponSettingsViewController = [[WeaponSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
147 nextController = weaponSettingsViewController; |
|
148 break; |
|
149 case 3: |
|
150 if (nil == schemeSettingsViewController) |
|
151 schemeSettingsViewController = [[SchemeSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
152 nextController = schemeSettingsViewController; |
|
153 break; |
|
154 case 4: |
|
155 if (nil == supportViewController) |
|
156 supportViewController = [[SupportViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
157 nextController = supportViewController; |
|
158 break; |
|
159 } |
|
160 |
|
161 nextController.title = [controllerNames objectAtIndex:newRow]; |
|
162 self.lastIndexPath = indexPath; |
|
163 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
164 |
|
165 if (nil == targetController) { |
|
166 nextController.navigationItem.hidesBackButton = NO; |
|
167 [self.navigationController pushViewController:nextController animated:YES]; |
|
168 } else { |
|
169 playSound(@"clickSound"); |
|
170 nextController.navigationItem.hidesBackButton = YES; |
|
171 [targetController.navigationController pushViewController:nextController animated:NO]; |
|
172 } |
|
173 } |
|
174 } |
|
175 |
|
176 |
|
177 #pragma mark - |
|
178 #pragma mark Memory management |
|
179 -(void) didReceiveMemoryWarning { |
|
180 if (generalSettingsViewController.view.superview == nil) |
|
181 generalSettingsViewController = nil; |
|
182 if (teamSettingsViewController.view.superview == nil) |
|
183 teamSettingsViewController = nil; |
|
184 if (weaponSettingsViewController.view.superview == nil) |
|
185 weaponSettingsViewController = nil; |
|
186 if (schemeSettingsViewController.view.superview == nil) |
|
187 schemeSettingsViewController = nil; |
|
188 if (supportViewController.view.superview == nil) |
|
189 supportViewController = nil; |
|
190 MSG_MEMCLEAN(); |
|
191 [super didReceiveMemoryWarning]; |
|
192 } |
|
193 |
|
194 -(void) viewDidUnload { |
|
195 //self.rootController = nil; |
|
196 //self.targetController = nil; |
|
197 self.controllerNames = nil; |
|
198 self.lastIndexPath = nil; |
|
199 generalSettingsViewController = nil; |
|
200 teamSettingsViewController = nil; |
|
201 weaponSettingsViewController = nil; |
|
202 schemeSettingsViewController = nil; |
|
203 supportViewController = nil; |
|
204 MSG_DIDUNLOAD(); |
|
205 [super viewDidUnload]; |
|
206 } |
|
207 |
|
208 -(void) dealloc { |
|
209 releaseAndNil(rootController); |
|
210 releaseAndNil(targetController); |
|
211 releaseAndNil(controllerNames); |
|
212 releaseAndNil(lastIndexPath); |
|
213 releaseAndNil(generalSettingsViewController); |
|
214 releaseAndNil(teamSettingsViewController); |
|
215 releaseAndNil(weaponSettingsViewController); |
|
216 releaseAndNil(schemeSettingsViewController); |
|
217 releaseAndNil(supportViewController); |
|
218 [super dealloc]; |
|
219 } |
|
220 |
|
221 -(IBAction) dismissSplitView { |
|
222 [self.rootController dismissModalViewControllerAnimated:YES]; |
|
223 } |
|
224 |
|
225 @end |
|
226 |
|