18 * File created on 13/06/2010. |
18 * File created on 13/06/2010. |
19 */ |
19 */ |
20 |
20 |
21 |
21 |
22 #import "SchemeWeaponConfigViewController.h" |
22 #import "SchemeWeaponConfigViewController.h" |
23 #import "CommodityFunctions.h" |
23 #import <QuartzCore/QuartzCore.h> |
|
24 |
24 |
25 |
25 #define LABEL_TAG 57423 |
26 #define LABEL_TAG 57423 |
26 |
27 |
|
28 static SchemeWeaponConfigViewController *controllerInstance; |
|
29 |
27 @implementation SchemeWeaponConfigViewController |
30 @implementation SchemeWeaponConfigViewController |
28 @synthesize listOfSchemes, listOfWeapons, listOfScripts, lastIndexPath_sc, lastIndexPath_we, lastIndexPath_lu, |
31 @synthesize tableView, listOfSchemes, listOfWeapons, listOfScripts, lastIndexPath_sc, lastIndexPath_we, lastIndexPath_lu, |
29 selectedScheme, selectedWeapon, selectedScript, scriptCommand, topControl, hideSections; |
32 selectedScheme, selectedWeapon, selectedScript, scriptCommand, topControl, sectionsHidden; |
30 |
33 |
31 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
34 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
32 return rotationManager(interfaceOrientation); |
35 return rotationManager(interfaceOrientation); |
33 } |
36 } |
34 |
37 |
35 #pragma mark - |
38 #pragma mark - |
|
39 #pragma mark custom setters/getters |
|
40 -(NSString *)selectedScheme { |
|
41 if (selectedScheme == nil) |
|
42 self.selectedScheme = @"Default.plist"; |
|
43 return selectedScheme; |
|
44 } |
|
45 |
|
46 -(NSString *)selectedWeapon { |
|
47 if (selectedWeapon == nil) |
|
48 self.selectedWeapon = @"Default.plist"; |
|
49 return selectedWeapon; |
|
50 } |
|
51 |
|
52 -(NSString *)selectedScript { |
|
53 if (selectedScript == nil) |
|
54 self.selectedScript = @"Normal.plist"; |
|
55 return selectedScript; |
|
56 } |
|
57 |
|
58 -(NSString *)scriptCommand { |
|
59 if (scriptCommand == nil) |
|
60 self.scriptCommand = @""; |
|
61 return scriptCommand; |
|
62 } |
|
63 |
|
64 -(NSArray *)listOfSchemes { |
|
65 if (listOfSchemes == nil) |
|
66 self.listOfSchemes = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCHEMES_DIRECTORY() error:NULL]; |
|
67 return listOfSchemes; |
|
68 } |
|
69 |
|
70 -(NSArray *)listOfWeapons { |
|
71 if (listOfWeapons == nil) |
|
72 self.listOfWeapons = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL]; |
|
73 return listOfWeapons; |
|
74 } |
|
75 |
|
76 -(NSArray *)listOfScripts { |
|
77 if (listOfScripts == nil) |
|
78 self.listOfScripts = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCRIPTS_DIRECTORY() error:NULL]; |
|
79 return listOfScripts; |
|
80 } |
|
81 |
|
82 -(UISegmentedControl *)topControl { |
|
83 if (topControl == nil) { |
|
84 NSArray *array = [[NSArray alloc] initWithObjects: |
|
85 NSLocalizedString(@"Scheme",@""), |
|
86 NSLocalizedString(@"Weapon",@""), |
|
87 NSLocalizedString(@"Style",@""),nil]; |
|
88 UISegmentedControl *controller = [[UISegmentedControl alloc] initWithItems:array]; |
|
89 [array release]; |
|
90 controller.segmentedControlStyle = UISegmentedControlStyleBar; |
|
91 controller.tintColor = [UIColor lightGrayColor]; |
|
92 controller.selectedSegmentIndex = 0; |
|
93 self.topControl = controller; |
|
94 [controller addTarget:self.tableView action:@selector(reloadData) forControlEvents:UIControlEventValueChanged]; |
|
95 [controller release]; |
|
96 } |
|
97 return topControl; |
|
98 } |
|
99 |
|
100 #pragma mark - |
36 #pragma mark View lifecycle |
101 #pragma mark View lifecycle |
37 -(void) viewDidLoad { |
102 -(void) viewDidLoad { |
|
103 self.sectionsHidden = NO; |
|
104 |
|
105 UITableView *aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) |
|
106 style:UITableViewStyleGrouped]; |
|
107 aTableView.delegate = self; |
|
108 aTableView.dataSource = self; |
|
109 if (IS_IPAD()) { |
|
110 [aTableView setBackgroundColorForAnyTable:[UIColor darkBlueColorTransparent]]; |
|
111 aTableView.layer.borderColor = [[UIColor darkYellowColor] CGColor]; |
|
112 aTableView.layer.borderWidth = 2.7f; |
|
113 aTableView.layer.cornerRadius = 8; |
|
114 aTableView.contentInset = UIEdgeInsetsMake(5, 0, 5, 0); |
|
115 } else { |
|
116 UIImage *backgroundImage = [[UIImage alloc] initWithContentsOfFile:@"background~iphone.png"]; |
|
117 UIImageView *background = [[UIImageView alloc] initWithImage:backgroundImage]; |
|
118 [backgroundImage release]; |
|
119 [self.view addSubview:background]; |
|
120 [background release]; |
|
121 [aTableView setBackgroundColorForAnyTable:[UIColor clearColor]]; |
|
122 } |
|
123 |
|
124 aTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite; |
|
125 aTableView.separatorColor = [UIColor whiteColor]; |
|
126 aTableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
|
127 self.tableView = aTableView; |
|
128 [aTableView release]; |
|
129 [self.view addSubview:self.tableView]; |
|
130 |
38 [super viewDidLoad]; |
131 [super viewDidLoad]; |
39 |
132 controllerInstance = self; |
40 CGSize screenSize = [[UIScreen mainScreen] bounds].size; |
133 } |
41 self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44); |
|
42 |
|
43 self.selectedScheme = nil; |
|
44 self.selectedWeapon = nil; |
|
45 self.selectedScript = nil; |
|
46 self.scriptCommand = nil; |
|
47 |
|
48 if ([self.tableView respondsToSelector:@selector(setBackgroundView:)]) { |
|
49 if (IS_IPAD()) |
|
50 [self.tableView setBackgroundView:nil]; |
|
51 else { |
|
52 UIImage *backgroundImage = [[UIImage alloc] initWithContentsOfFile:@"background~iphone.png"]; |
|
53 UIImageView *background = [[UIImageView alloc] initWithImage:backgroundImage]; |
|
54 [backgroundImage release]; |
|
55 [self.tableView setBackgroundView:background]; |
|
56 [background release]; |
|
57 } |
|
58 } else { |
|
59 self.view.backgroundColor = [UIColor blackColor]; |
|
60 } |
|
61 |
|
62 self.tableView.separatorColor = UICOLOR_HW_YELLOW_BODER; |
|
63 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
|
64 } |
|
65 |
|
66 -(void) viewWillAppear:(BOOL) animated { |
|
67 [super viewWillAppear:animated]; |
|
68 |
|
69 NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCHEMES_DIRECTORY() error:NULL]; |
|
70 self.listOfSchemes = contentsOfDir; |
|
71 |
|
72 if (self.selectedScheme == nil && [listOfSchemes containsObject:@"Default.plist"]) |
|
73 self.selectedScheme = @"Default.plist"; |
|
74 |
|
75 contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL]; |
|
76 self.listOfWeapons = contentsOfDir; |
|
77 |
|
78 if (self.selectedWeapon == nil && [listOfWeapons containsObject:@"Default.plist"]) |
|
79 self.selectedWeapon = @"Default.plist"; |
|
80 |
|
81 contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCRIPTS_DIRECTORY() error:NULL]; |
|
82 self.listOfScripts = contentsOfDir; |
|
83 self.selectedScript = @"Normal.plist"; |
|
84 self.scriptCommand = @""; |
|
85 |
|
86 [self.tableView reloadData]; |
|
87 } |
|
88 |
|
89 |
134 |
90 #pragma mark - |
135 #pragma mark - |
91 #pragma mark Table view data source |
136 #pragma mark Table view data source |
92 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
137 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
93 if (hideSections) |
138 return (self.sectionsHidden ? 0 : 1); |
94 return 0; |
|
95 else |
|
96 return 1; |
|
97 } |
139 } |
98 |
140 |
99 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
141 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
100 if (self.topControl.selectedSegmentIndex == 0) |
142 if (self.topControl.selectedSegmentIndex == 0) |
101 return [self.listOfSchemes count]; |
143 return [self.listOfSchemes count]; |
260 [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
292 [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
261 } |
293 } |
262 [aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
294 [aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
263 } |
295 } |
264 |
296 |
265 -(void) fillSections { |
297 #pragma mark - |
266 if (hideSections == YES) { |
298 #pragma mark called externally to empty or fill the sections completely |
267 hideSections = NO; |
299 +(void) fillInstanceSections { |
268 NSRange range; |
300 if (controllerInstance.sectionsHidden == YES) { |
269 range.location = 0; |
301 controllerInstance.sectionsHidden = NO; |
270 range.length = 1; |
302 NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)]; |
271 NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range]; |
303 [controllerInstance.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade]; |
272 [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade]; |
304 controllerInstance.tableView.scrollEnabled = YES; |
273 self.selectedScheme = @"Default.plist"; |
305 |
274 self.selectedWeapon = @"Default.plist"; |
306 [[controllerInstance.view viewWithTag:LABEL_TAG] removeFromSuperview]; |
275 self.selectedScript = @"Normal.plist"; |
307 } |
276 |
308 } |
277 self.tableView.scrollEnabled = YES; |
309 |
278 |
310 +(void) emptyInstanceSections { |
279 [[self.view viewWithTag:LABEL_TAG] removeFromSuperview]; |
311 if (controllerInstance.sectionsHidden == NO) { |
280 } |
312 controllerInstance.sectionsHidden = YES; |
281 } |
313 NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)]; |
282 |
314 [controllerInstance.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade]; |
283 -(void) emptySections { |
315 controllerInstance.tableView.scrollEnabled = NO; |
284 hideSections = YES; |
316 |
285 NSRange range; |
317 CGRect frame = CGRectMake(0, 0, controllerInstance.view.frame.size.width * 80/100, 60); |
286 range.location = 0; |
318 UILabel *theLabel = [[UILabel alloc] initWithFrame:frame |
287 range.length = 1; |
319 andTitle:NSLocalizedString(@"Missions don't need further configuration",@"")]; |
288 NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range]; |
320 theLabel.center = CGPointMake(controllerInstance.view.frame.size.width/2, controllerInstance.view.frame.size.height/2); |
289 [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade]; |
321 theLabel.numberOfLines = 2; |
290 self.selectedScheme = @"Default.plist"; |
322 theLabel.tag = LABEL_TAG; |
291 self.selectedWeapon = @"Default.plist"; |
323 |
292 self.selectedScript = @"Normal.plist"; |
324 [controllerInstance.view addSubview:theLabel]; |
293 |
325 [theLabel release]; |
294 self.tableView.scrollEnabled = NO; |
326 } |
295 |
|
296 CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 60); |
|
297 UILabel *theLabel = createBlueLabel(NSLocalizedString(@"Missions don't need further configuration",@""), frame); |
|
298 theLabel.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2); |
|
299 theLabel.numberOfLines = 2; |
|
300 theLabel.tag = LABEL_TAG; |
|
301 |
|
302 [self.view addSubview:theLabel]; |
|
303 [theLabel release]; |
|
304 } |
327 } |
305 |
328 |
306 #pragma mark - |
329 #pragma mark - |
307 #pragma mark Memory management |
330 #pragma mark Memory management |
308 -(void) didReceiveMemoryWarning { |
331 -(void) didReceiveMemoryWarning { |
309 if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) { |
332 if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) { |
|
333 self.tableView = nil; |
310 self.lastIndexPath_sc = nil; |
334 self.lastIndexPath_sc = nil; |
311 self.lastIndexPath_we = nil; |
335 self.lastIndexPath_we = nil; |
312 self.lastIndexPath_lu = nil; |
336 self.lastIndexPath_lu = nil; |
313 self.listOfSchemes = nil; |
337 self.selectedScheme = nil; |
314 self.listOfWeapons = nil; |
338 self.selectedWeapon = nil; |
315 self.listOfScripts = nil; |
339 self.selectedScript = nil; |
|
340 self.scriptCommand = nil; |
316 self.topControl = nil; |
341 self.topControl = nil; |
317 MSG_MEMCLEAN(); |
342 } |
318 } |
343 self.listOfSchemes = nil; |
|
344 self.listOfWeapons = nil; |
|
345 self.listOfScripts = nil; |
|
346 MSG_MEMCLEAN(); |
319 [super didReceiveMemoryWarning]; |
347 [super didReceiveMemoryWarning]; |
320 } |
348 } |
321 |
349 |
322 -(void) viewDidUnload { |
350 -(void) viewDidUnload { |
|
351 self.tableView = nil; |
323 self.listOfSchemes = nil; |
352 self.listOfSchemes = nil; |
324 self.listOfWeapons = nil; |
353 self.listOfWeapons = nil; |
325 self.listOfScripts = nil; |
354 self.listOfScripts = nil; |
326 self.lastIndexPath_sc = nil; |
355 self.lastIndexPath_sc = nil; |
327 self.lastIndexPath_we = nil; |
356 self.lastIndexPath_we = nil; |