|
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 03/10/2011. |
|
19 */ |
|
20 |
|
21 |
|
22 #import "MissionTrainingViewController.h" |
|
23 #import <QuartzCore/QuartzCore.h> |
|
24 #import "GameInterfaceBridge.h" |
|
25 |
|
26 |
|
27 @implementation MissionTrainingViewController |
|
28 @synthesize listOfMissions, listOfDescriptions, previewImage, tableView, descriptionLabel, missionName; |
|
29 |
|
30 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
31 return rotationManager(interfaceOrientation); |
|
32 } |
|
33 |
|
34 #pragma mark - |
|
35 #pragma mark View management |
|
36 -(void) viewDidLoad { |
|
37 NSString *imgName = (IS_IPAD()) ? @"mediumBackground~ipad.png" : @"smallerBackground~iphone.png"; |
|
38 UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgName]; |
|
39 self.view.backgroundColor = [UIColor colorWithPatternImage:img]; |
|
40 [img release]; |
|
41 |
|
42 self.previewImage.layer.borderColor = [[UIColor darkYellowColor] CGColor]; |
|
43 self.previewImage.layer.borderWidth = 3.8f; |
|
44 self.previewImage.layer.cornerRadius = 14; |
|
45 |
|
46 if (IS_IPAD()) { |
|
47 [self.tableView setBackgroundColorForAnyTable:[UIColor darkBlueColorTransparent]]; |
|
48 self.tableView.layer.borderColor = [[UIColor darkYellowColor] CGColor]; |
|
49 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
|
50 } else { |
|
51 [self.tableView setBackgroundColorForAnyTable:[UIColor blackColorTransparent]]; |
|
52 self.tableView.layer.borderColor = [[UIColor whiteColor] CGColor]; |
|
53 self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; |
|
54 } |
|
55 self.tableView.layer.borderWidth = 2.4f; |
|
56 self.tableView.layer.cornerRadius = 8; |
|
57 self.tableView.separatorColor = [UIColor whiteColor]; |
|
58 |
|
59 self.descriptionLabel.textColor = [UIColor lightYellowColor]; |
|
60 [super viewDidLoad]; |
|
61 } |
|
62 |
|
63 -(void) viewWillAppear:(BOOL)animated { |
|
64 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:random()%[self.listOfMissions count] inSection:0]; |
|
65 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
66 [self tableView:self.tableView didSelectRowAtIndexPath:indexPath]; |
|
67 [super viewWillAppear:animated]; |
|
68 } |
|
69 |
|
70 -(IBAction) buttonPressed:(id) sender { |
|
71 UIButton *button = (UIButton *)sender; |
|
72 |
|
73 if (button.tag == 0) { |
|
74 [AudioManagerController playBackSound]; |
|
75 [[self parentViewController] dismissModalViewControllerAnimated:YES]; |
|
76 } else { |
|
77 GameInterfaceBridge *bridge = [[GameInterfaceBridge alloc] initWithController:self]; |
|
78 [bridge startMissionGame:self.missionName]; |
|
79 [bridge release]; |
|
80 } |
|
81 } |
|
82 |
|
83 #pragma mark - |
|
84 #pragma mark override setters/getters for better memory handling |
|
85 -(NSArray *)listOfMissions { |
|
86 if (listOfMissions == nil) |
|
87 self.listOfMissions = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TRAININGS_DIRECTORY() error:NULL]; |
|
88 return listOfMissions; |
|
89 } |
|
90 |
|
91 -(NSArray *)listOfDescriptions { |
|
92 if (listOfDescriptions == nil) { |
|
93 NSString *descLocation = [[NSString alloc] initWithFormat:@"%@/missions_en.txt",LOCALE_DIRECTORY()]; |
|
94 NSString *descComplete = [[NSString alloc] initWithContentsOfFile:descLocation encoding:NSUTF8StringEncoding error:NULL]; |
|
95 [descLocation release]; |
|
96 NSArray *descArray = [descComplete componentsSeparatedByString:@"\n"]; |
|
97 NSMutableArray *filteredArray = [[NSMutableArray alloc] initWithCapacity:[descArray count]]; |
|
98 [descComplete release]; |
|
99 // sanity check to avoid having missions and descriptions conflicts |
|
100 for (int i = 0; i < [self.listOfMissions count]; i++) { |
|
101 NSString *desc = [[self.listOfMissions objectAtIndex:i] stringByDeletingPathExtension]; |
|
102 for (NSString *str in descArray) |
|
103 if ([str hasPrefix:desc]) { |
|
104 NSArray *descriptionText = [str componentsSeparatedByString:@"\""]; |
|
105 [filteredArray insertObject:[descriptionText objectAtIndex:1] atIndex:i]; |
|
106 break; |
|
107 } |
|
108 } |
|
109 self.listOfDescriptions = filteredArray; |
|
110 [filteredArray release]; |
|
111 } |
|
112 return listOfDescriptions; |
|
113 } |
|
114 |
|
115 #pragma mark - |
|
116 #pragma mark Table view data source |
|
117 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
118 return 1; |
|
119 } |
|
120 |
|
121 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
122 return [self.listOfMissions count]; |
|
123 } |
|
124 |
|
125 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
126 return (IS_IPAD()) ? self.tableView.rowHeight : 80; |
|
127 } |
|
128 |
|
129 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
130 static NSString *CellIdentifier = @"CellTr"; |
|
131 NSInteger row = [indexPath row]; |
|
132 |
|
133 UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
134 if (cell == nil) |
|
135 cell = [[[UITableViewCell alloc] initWithStyle:(IS_IPAD()) ? UITableViewCellStyleDefault : UITableViewCellStyleSubtitle |
|
136 reuseIdentifier:CellIdentifier] autorelease]; |
|
137 |
|
138 cell.textLabel.text = [[[self.listOfMissions objectAtIndex:row] stringByDeletingPathExtension] |
|
139 stringByReplacingOccurrencesOfString:@"_" withString:@" "]; |
|
140 cell.textLabel.textColor = [UIColor lightYellowColor]; |
|
141 //cell.textLabel.font = [UIFont fontWithName:@"Bradley Hand Bold" size:[UIFont labelFontSize]]; |
|
142 cell.textLabel.textAlignment = (IS_IPAD()) ? UITextAlignmentCenter : UITextAlignmentLeft; |
|
143 cell.textLabel.backgroundColor = [UIColor clearColor]; |
|
144 cell.textLabel.adjustsFontSizeToFitWidth = YES; |
|
145 cell.detailTextLabel.text = (IS_IPAD()) ? nil : [self.listOfDescriptions objectAtIndex:row]; |
|
146 cell.detailTextLabel.textColor = [UIColor whiteColor]; |
|
147 cell.detailTextLabel.backgroundColor = [UIColor clearColor]; |
|
148 cell.detailTextLabel.adjustsFontSizeToFitWidth = YES; |
|
149 cell.detailTextLabel.numberOfLines = ([cell.detailTextLabel.text length] % 40); |
|
150 cell.detailTextLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters; |
|
151 |
|
152 cell.backgroundColor = [UIColor blackColorTransparent]; |
|
153 return cell; |
|
154 } |
|
155 |
|
156 #pragma mark - |
|
157 #pragma mark Table view delegate |
|
158 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
159 NSInteger row = [indexPath row]; |
|
160 |
|
161 self.missionName = [[self.listOfMissions objectAtIndex:row] stringByDeletingPathExtension]; |
|
162 NSString *size = IS_IPAD() ? @"@2x" : @""; |
|
163 NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Missions/Training/%@%@.png",GRAPHICS_DIRECTORY(),self.missionName,size]; |
|
164 UIImage *img = [[UIImage alloc] initWithContentsOfFile:filePath]; |
|
165 [filePath release]; |
|
166 [self.previewImage setImage:img]; |
|
167 [img release]; |
|
168 |
|
169 self.descriptionLabel.text = [self.listOfDescriptions objectAtIndex:row]; |
|
170 } |
|
171 |
|
172 #pragma mark - |
|
173 #pragma mark Memory management |
|
174 -(void) didReceiveMemoryWarning { |
|
175 self.previewImage = nil; |
|
176 self.missionName = nil; |
|
177 self.listOfMissions = nil; |
|
178 self.listOfDescriptions = nil; |
|
179 // if you nil this one it won't get updated anymore |
|
180 //self.previewImage = nil; |
|
181 [super didReceiveMemoryWarning]; |
|
182 } |
|
183 |
|
184 -(void) viewDidUnload { |
|
185 self.listOfMissions = nil; |
|
186 self.listOfDescriptions = nil; |
|
187 self.previewImage = nil; |
|
188 self.tableView = nil; |
|
189 self.descriptionLabel = nil; |
|
190 self.missionName = nil; |
|
191 MSG_DIDUNLOAD(); |
|
192 [super viewDidUnload]; |
|
193 } |
|
194 |
|
195 |
|
196 -(void) dealloc { |
|
197 releaseAndNil(listOfMissions); |
|
198 releaseAndNil(listOfDescriptions); |
|
199 releaseAndNil(previewImage); |
|
200 releaseAndNil(tableView); |
|
201 releaseAndNil(descriptionLabel); |
|
202 releaseAndNil(missionName); |
|
203 [super dealloc]; |
|
204 } |
|
205 |
|
206 |
|
207 @end |