21 #import <QuartzCore/QuartzCore.h> |
21 #import <QuartzCore/QuartzCore.h> |
22 #import "GameInterfaceBridge.h" |
22 #import "GameInterfaceBridge.h" |
23 |
23 |
24 |
24 |
25 @implementation MissionTrainingViewController |
25 @implementation MissionTrainingViewController |
26 @synthesize listOfMissions, listOfDescriptions, previewImage, tableView, descriptionLabel, missionName; |
|
27 |
26 |
28 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
27 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
29 return rotationManager(interfaceOrientation); |
28 return rotationManager(interfaceOrientation); |
30 } |
29 } |
31 |
30 |
32 #pragma mark - |
31 #pragma mark - |
33 #pragma mark View management |
32 #pragma mark View management |
34 -(void) viewDidLoad { |
33 -(void) viewDidLoad |
|
34 { |
|
35 [super viewDidLoad]; |
|
36 |
35 self.previewImage.layer.borderColor = [[UIColor darkYellowColor] CGColor]; |
37 self.previewImage.layer.borderColor = [[UIColor darkYellowColor] CGColor]; |
36 self.previewImage.layer.borderWidth = 3.8f; |
38 self.previewImage.layer.borderWidth = 3.8f; |
37 self.previewImage.layer.cornerRadius = 14; |
39 self.previewImage.layer.cornerRadius = 14; |
38 |
40 |
39 if (IS_IPAD()) { |
41 if (IS_IPAD()) { |
48 self.tableView.layer.borderWidth = 2.4f; |
50 self.tableView.layer.borderWidth = 2.4f; |
49 self.tableView.layer.cornerRadius = 8; |
51 self.tableView.layer.cornerRadius = 8; |
50 self.tableView.separatorColor = [UIColor whiteColor]; |
52 self.tableView.separatorColor = [UIColor whiteColor]; |
51 |
53 |
52 self.descriptionLabel.textColor = [UIColor lightYellowColor]; |
54 self.descriptionLabel.textColor = [UIColor lightYellowColor]; |
53 [super viewDidLoad]; |
|
54 } |
55 } |
55 |
56 |
56 -(void) viewWillAppear:(BOOL)animated { |
57 -(void) viewWillAppear:(BOOL)animated { |
57 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:arc4random_uniform((int)[self.listOfMissions count]) inSection:0]; |
58 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:arc4random_uniform((int)[self.listOfMissionIDs count]) inSection:0]; |
58 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
59 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; |
59 [self tableView:self.tableView didSelectRowAtIndexPath:indexPath]; |
60 [self tableView:self.tableView didSelectRowAtIndexPath:indexPath]; |
60 [super viewWillAppear:animated]; |
61 [super viewWillAppear:animated]; |
61 } |
62 } |
62 |
63 |
63 -(IBAction) buttonPressed:(id) sender { |
64 -(IBAction) buttonPressed:(id) sender { |
70 [GameInterfaceBridge registerCallingController:self]; |
71 [GameInterfaceBridge registerCallingController:self]; |
71 [GameInterfaceBridge startMissionGame:self.missionName]; |
72 [GameInterfaceBridge startMissionGame:self.missionName]; |
72 } |
73 } |
73 } |
74 } |
74 |
75 |
|
76 #pragma mark - Missions dictionaries methods |
|
77 |
|
78 - (NSDictionary *)newLocalizedMissionsDictionary |
|
79 { |
|
80 NSString *languageID = [HWUtils languageID]; |
|
81 |
|
82 NSString *missionsDescLocation = [[NSString alloc] initWithFormat:@"%@/missions_en.txt",LOCALE_DIRECTORY()]; |
|
83 NSString *localizedMissionsDescLocation = [[NSString alloc] initWithFormat:@"%@/missions_%@.txt", LOCALE_DIRECTORY(), languageID]; |
|
84 |
|
85 if (![languageID isEqualToString:@"en"] && [[NSFileManager defaultManager] fileExistsAtPath:localizedMissionsDescLocation]) |
|
86 { |
|
87 NSDictionary *missionsDict = [self newMissionsDictionaryFromMissionsFile:missionsDescLocation]; |
|
88 NSDictionary *localizedMissionsDict = [self newMissionsDictionaryFromMissionsFile:localizedMissionsDescLocation]; |
|
89 |
|
90 [missionsDescLocation release]; |
|
91 [localizedMissionsDescLocation release]; |
|
92 |
|
93 NSMutableDictionary *tempMissionsDict = [[NSMutableDictionary alloc] init]; |
|
94 |
|
95 for (NSString *key in [missionsDict allKeys]) |
|
96 { |
|
97 if ([localizedMissionsDict objectForKey:key]) |
|
98 { |
|
99 [tempMissionsDict setObject:[localizedMissionsDict objectForKey:key] forKey:key]; |
|
100 } |
|
101 else |
|
102 { |
|
103 [tempMissionsDict setObject:[missionsDict objectForKey:key] forKey:key]; |
|
104 } |
|
105 } |
|
106 |
|
107 [missionsDict release]; |
|
108 [localizedMissionsDict release]; |
|
109 |
|
110 return tempMissionsDict; |
|
111 } |
|
112 else |
|
113 { |
|
114 NSDictionary *missionsDict = [self newMissionsDictionaryFromMissionsFile:missionsDescLocation]; |
|
115 |
|
116 [missionsDescLocation release]; |
|
117 [localizedMissionsDescLocation release]; |
|
118 |
|
119 return missionsDict; |
|
120 } |
|
121 } |
|
122 |
|
123 - (NSDictionary *)newMissionsDictionaryFromMissionsFile:(NSString *)filePath |
|
124 { |
|
125 NSMutableDictionary *missionsDict = [[NSMutableDictionary alloc] init]; |
|
126 |
|
127 NSString *missionsFileContents = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL]; |
|
128 NSArray *missionsLines = [missionsFileContents componentsSeparatedByString:@"\n"]; |
|
129 [missionsFileContents release]; |
|
130 |
|
131 for (NSString *line in missionsLines) |
|
132 { |
|
133 if ([line length] > 0) |
|
134 { |
|
135 NSUInteger firstDotLocation = [line rangeOfString:@"."].location; |
|
136 |
|
137 NSString *missionID = [line substringToIndex:firstDotLocation]; |
|
138 NSString *nameOrDesc = [line substringFromIndex:firstDotLocation+1]; |
|
139 |
|
140 NSString *missionParsedName = ([nameOrDesc hasPrefix:@"name="]) ? [nameOrDesc stringByReplacingOccurrencesOfString:@"name=" withString:@""] : nil; |
|
141 NSString *missionParsedDesc = ([nameOrDesc hasPrefix:@"desc="]) ? [nameOrDesc stringByReplacingOccurrencesOfString:@"desc=" withString:@""] : nil; |
|
142 |
|
143 if (![missionsDict objectForKey:missionID]) |
|
144 { |
|
145 NSMutableDictionary *missionDict = [[NSMutableDictionary alloc] init]; |
|
146 [missionsDict setObject:missionDict forKey:missionID]; |
|
147 [missionDict release]; |
|
148 } |
|
149 |
|
150 NSMutableDictionary *missionDict = [missionsDict objectForKey:missionID]; |
|
151 |
|
152 if (missionParsedName) |
|
153 { |
|
154 [missionDict setObject:missionParsedName forKey:@"name"]; |
|
155 } |
|
156 |
|
157 if (missionParsedDesc) |
|
158 { |
|
159 missionParsedDesc = [missionParsedDesc stringByReplacingOccurrencesOfString:@"\"" withString:@""]; |
|
160 [missionDict setObject:missionParsedDesc forKey:@"desc"]; |
|
161 } |
|
162 |
|
163 [missionsDict setObject:missionDict forKey:missionID]; |
|
164 } |
|
165 } |
|
166 |
|
167 return missionsDict; |
|
168 } |
|
169 |
75 #pragma mark - |
170 #pragma mark - |
76 #pragma mark override setters/getters for better memory handling |
171 #pragma mark override setters/getters for better memory handling |
77 -(NSArray *)listOfMissions { |
172 |
78 if (listOfMissions == nil) |
173 -(NSArray *)listOfMissionIDs |
79 self.listOfMissions = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TRAININGS_DIRECTORY() error:NULL]; |
174 { |
80 return listOfMissions; |
175 if (!_listOfMissionIDs) |
81 } |
176 { |
82 |
177 NSArray *sortedKeys = [[self.dictOfMissions allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; |
83 -(NSArray *)listOfDescriptions { |
178 _listOfMissionIDs = [[NSArray alloc] initWithArray:sortedKeys]; |
84 if (listOfDescriptions == nil) { |
179 } |
85 NSString *descLocation = [[NSString alloc] initWithFormat:@"%@/missions_en.txt",LOCALE_DIRECTORY()]; |
180 |
86 NSString *descComplete = [[NSString alloc] initWithContentsOfFile:descLocation encoding:NSUTF8StringEncoding error:NULL]; |
181 return _listOfMissionIDs; |
87 [descLocation release]; |
182 } |
88 NSArray *descArray = [descComplete componentsSeparatedByString:@"\n"]; |
183 |
89 NSMutableArray *filteredArray = [[NSMutableArray alloc] initWithCapacity:[descArray count]/3]; |
184 - (NSDictionary *)dictOfMissions |
90 [descComplete release]; |
185 { |
91 // sanity check to avoid having missions and descriptions conflicts |
186 if (!_dictOfMissions) |
92 for (NSUInteger i = 0; i < [self.listOfMissions count]; i++) { |
187 { |
93 NSString *desc = [[self.listOfMissions objectAtIndex:i] stringByDeletingPathExtension]; |
188 _dictOfMissions = [self newLocalizedMissionsDictionary]; |
94 for (NSString *str in descArray) |
189 } |
95 { |
190 |
96 if ([str hasPrefix:desc] && [str hasSuffix:@"\""]) { |
191 return _dictOfMissions; |
97 NSArray *descriptionText = [str componentsSeparatedByString:@"\""]; |
|
98 [filteredArray addObject:[descriptionText objectAtIndex:1]]; |
|
99 break; |
|
100 } |
|
101 } |
|
102 |
|
103 if ([filteredArray count] == i) |
|
104 { |
|
105 [filteredArray addObject:@""]; |
|
106 } |
|
107 } |
|
108 self.listOfDescriptions = filteredArray; |
|
109 [filteredArray release]; |
|
110 } |
|
111 return listOfDescriptions; |
|
112 } |
192 } |
113 |
193 |
114 #pragma mark - |
194 #pragma mark - |
115 #pragma mark Table view data source |
195 #pragma mark Table view data source |
116 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
196 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
117 return 1; |
197 return 1; |
118 } |
198 } |
119 |
199 |
120 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
200 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
121 return [self.listOfMissions count]; |
201 return [self.listOfMissionIDs count]; |
122 } |
202 } |
123 |
203 |
124 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
204 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
125 return (IS_IPAD()) ? self.tableView.rowHeight : 80; |
205 return (IS_IPAD()) ? self.tableView.rowHeight : 80; |
126 } |
206 } |
131 |
211 |
132 UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
212 UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
133 if (cell == nil) |
213 if (cell == nil) |
134 cell = [[[UITableViewCell alloc] initWithStyle:(IS_IPAD()) ? UITableViewCellStyleDefault : UITableViewCellStyleSubtitle |
214 cell = [[[UITableViewCell alloc] initWithStyle:(IS_IPAD()) ? UITableViewCellStyleDefault : UITableViewCellStyleSubtitle |
135 reuseIdentifier:CellIdentifier] autorelease]; |
215 reuseIdentifier:CellIdentifier] autorelease]; |
136 |
216 |
137 cell.textLabel.text = [[[self.listOfMissions objectAtIndex:row] stringByDeletingPathExtension] |
217 NSString *missionID = [self.listOfMissionIDs objectAtIndex:row]; |
138 stringByReplacingOccurrencesOfString:@"_" withString:@" "]; |
218 cell.textLabel.text = self.dictOfMissions[missionID][@"name"]; |
|
219 |
139 cell.textLabel.textColor = [UIColor lightYellowColor]; |
220 cell.textLabel.textColor = [UIColor lightYellowColor]; |
140 //cell.textLabel.font = [UIFont fontWithName:@"Bradley Hand Bold" size:[UIFont labelFontSize]]; |
221 //cell.textLabel.font = [UIFont fontWithName:@"Bradley Hand Bold" size:[UIFont labelFontSize]]; |
141 cell.textLabel.textAlignment = (IS_IPAD()) ? UITextAlignmentCenter : UITextAlignmentLeft; |
222 cell.textLabel.textAlignment = (IS_IPAD()) ? UITextAlignmentCenter : UITextAlignmentLeft; |
142 cell.textLabel.backgroundColor = [UIColor clearColor]; |
223 cell.textLabel.backgroundColor = [UIColor clearColor]; |
143 cell.textLabel.adjustsFontSizeToFitWidth = YES; |
224 cell.textLabel.adjustsFontSizeToFitWidth = YES; |
144 cell.detailTextLabel.text = (IS_IPAD()) ? nil : [self.listOfDescriptions objectAtIndex:row]; |
225 cell.detailTextLabel.text = (IS_IPAD()) ? nil : self.dictOfMissions[missionID][@"desc"]; |
145 cell.detailTextLabel.textColor = [UIColor whiteColor]; |
226 cell.detailTextLabel.textColor = [UIColor whiteColor]; |
146 cell.detailTextLabel.backgroundColor = [UIColor clearColor]; |
227 cell.detailTextLabel.backgroundColor = [UIColor clearColor]; |
147 cell.detailTextLabel.adjustsFontSizeToFitWidth = YES; |
228 cell.detailTextLabel.adjustsFontSizeToFitWidth = YES; |
148 cell.detailTextLabel.numberOfLines = ([cell.detailTextLabel.text length] % 40); |
229 cell.detailTextLabel.numberOfLines = ([cell.detailTextLabel.text length] % 40); |
149 cell.detailTextLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters; |
230 cell.detailTextLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters; |
161 #pragma mark - |
242 #pragma mark - |
162 #pragma mark Table view delegate |
243 #pragma mark Table view delegate |
163 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
244 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
164 NSInteger row = [indexPath row]; |
245 NSInteger row = [indexPath row]; |
165 |
246 |
166 self.missionName = [[self.listOfMissions objectAtIndex:row] stringByDeletingPathExtension]; |
247 self.missionName = [self.listOfMissionIDs objectAtIndex:row]; |
167 NSString *size = IS_IPAD() ? @"@2x" : @""; |
248 NSString *size = IS_IPAD() ? @"@2x" : @""; |
168 NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Missions/Training/%@%@.png",GRAPHICS_DIRECTORY(),self.missionName,size]; |
249 NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Missions/Training/%@%@.png",GRAPHICS_DIRECTORY(),self.missionName,size]; |
169 UIImage *img = [[UIImage alloc] initWithContentsOfFile:filePath]; |
250 UIImage *img = [[UIImage alloc] initWithContentsOfFile:filePath]; |
170 [filePath release]; |
251 [filePath release]; |
171 [self.previewImage setImage:img]; |
252 [self.previewImage setImage:img]; |
172 [img release]; |
253 [img release]; |
173 |
254 |
174 self.descriptionLabel.text = [self.listOfDescriptions objectAtIndex:row]; |
255 self.descriptionLabel.text = self.dictOfMissions[self.missionName][@"desc"]; |
175 } |
256 } |
176 |
257 |
177 #pragma mark - |
258 #pragma mark - |
178 #pragma mark Memory management |
259 #pragma mark Memory management |
179 -(void) didReceiveMemoryWarning { |
260 |
|
261 -(void) didReceiveMemoryWarning |
|
262 { |
180 self.previewImage = nil; |
263 self.previewImage = nil; |
181 self.missionName = nil; |
264 self.missionName = nil; |
182 self.listOfMissions = nil; |
265 self.listOfMissionIDs = nil; |
183 self.listOfDescriptions = nil; |
266 self.dictOfMissions = nil; |
184 // if you nil this one it won't get updated anymore |
267 // if you nil this one it won't get updated anymore |
185 //self.previewImage = nil; |
268 //self.previewImage = nil; |
186 [super didReceiveMemoryWarning]; |
269 [super didReceiveMemoryWarning]; |
187 } |
270 } |
188 |
271 |
189 -(void) viewDidUnload { |
272 -(void) viewDidUnload |
190 self.listOfMissions = nil; |
273 { |
191 self.listOfDescriptions = nil; |
274 self.listOfMissionIDs = nil; |
|
275 self.dictOfMissions = nil; |
192 self.previewImage = nil; |
276 self.previewImage = nil; |
193 self.tableView = nil; |
277 self.tableView = nil; |
194 self.descriptionLabel = nil; |
278 self.descriptionLabel = nil; |
195 self.missionName = nil; |
279 self.missionName = nil; |
196 MSG_DIDUNLOAD(); |
280 MSG_DIDUNLOAD(); |
197 [super viewDidUnload]; |
281 [super viewDidUnload]; |
198 } |
282 } |
199 |
283 |
200 |
284 |
201 -(void) dealloc { |
285 -(void) dealloc |
202 releaseAndNil(listOfMissions); |
286 { |
203 releaseAndNil(listOfDescriptions); |
287 releaseAndNil(_listOfMissionIDs); |
204 releaseAndNil(previewImage); |
288 releaseAndNil(_dictOfMissions); |
205 releaseAndNil(tableView); |
289 releaseAndNil(_previewImage); |
206 releaseAndNil(descriptionLabel); |
290 releaseAndNil(_tableView); |
207 releaseAndNil(missionName); |
291 releaseAndNil(_descriptionLabel); |
|
292 releaseAndNil(_missionName); |
208 [super dealloc]; |
293 [super dealloc]; |
209 } |
294 } |
210 |
295 |
211 |
296 |
212 @end |
297 @end |