|
1 // |
|
2 // FortsViewController.m |
|
3 // HedgewarsMobile |
|
4 // |
|
5 // Created by Vittorio on 08/04/10. |
|
6 // Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 // |
|
8 |
|
9 #import "FortsViewController.h" |
|
10 #import "CommodityFunctions.h" |
|
11 #import "UIImageExtra.h" |
|
12 |
|
13 @implementation FortsViewController |
|
14 @synthesize teamDictionary, fortArray, lastIndexPath; |
|
15 |
|
16 |
|
17 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
18 return rotationManager(interfaceOrientation); |
|
19 } |
|
20 |
|
21 |
|
22 #pragma mark - |
|
23 #pragma mark View lifecycle |
|
24 -(void) viewDidLoad { |
|
25 [super viewDidLoad]; |
|
26 |
|
27 NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FORTS_DIRECTORY() error:NULL]; |
|
28 NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / 2)]; |
|
29 // we need to remove the double entries and the L.png suffix |
|
30 for (int i = 0; i < [directoryContents count]; i++) { |
|
31 if (i % 2) { |
|
32 NSString *currentName = [directoryContents objectAtIndex:i]; |
|
33 NSString *correctName = [currentName substringToIndex:([currentName length] - 5)]; |
|
34 [filteredContents addObject:correctName]; |
|
35 } |
|
36 } |
|
37 self.fortArray = filteredContents; |
|
38 [filteredContents release]; |
|
39 |
|
40 /* |
|
41 // this creates a scaled down version of the image |
|
42 NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[fortArray count]]; |
|
43 for (NSString *fortName in fortArray) { |
|
44 NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", fortsDirectory, fortName]; |
|
45 UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile]; |
|
46 [fortFile release]; |
|
47 [spriteArray addObject:[fortSprite scaleToSize:CGSizeMake(196,196)]]; |
|
48 [fortSprite release]; |
|
49 } |
|
50 self.fortSprites = spriteArray; |
|
51 [spriteArray release]; |
|
52 */ |
|
53 |
|
54 // statically set row height instead of using delegate method for performance reasons |
|
55 self.tableView.rowHeight = 200; |
|
56 } |
|
57 |
|
58 |
|
59 - (void)viewWillAppear:(BOOL)animated { |
|
60 [super viewWillAppear:animated]; |
|
61 [self.tableView reloadData]; |
|
62 [self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
63 } |
|
64 |
|
65 |
|
66 #pragma mark - |
|
67 #pragma mark Table view data source |
|
68 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
69 return 1; |
|
70 } |
|
71 |
|
72 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
73 return [self.fortArray count]; |
|
74 } |
|
75 |
|
76 // Customize the appearance of table view cells. |
|
77 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
78 static NSString *CellIdentifier = @"Cell"; |
|
79 |
|
80 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
81 if (cell == nil) { |
|
82 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle |
|
83 reuseIdentifier:CellIdentifier] autorelease]; |
|
84 } |
|
85 |
|
86 NSString *fortName = [fortArray objectAtIndex:[indexPath row]]; |
|
87 cell.textLabel.text = fortName; |
|
88 |
|
89 // this creates a scaled down version of the image |
|
90 // TODO: create preview files, scaling is way too slow! |
|
91 NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", FORTS_DIRECTORY(), fortName]; |
|
92 UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile]; |
|
93 [fortFile release]; |
|
94 cell.imageView.image = [fortSprite scaleToSize:CGSizeMake(196,196)]; |
|
95 [fortSprite release]; |
|
96 |
|
97 cell.detailTextLabel.text = @"Insert funny description here"; |
|
98 if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) { |
|
99 cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
100 self.lastIndexPath = indexPath; |
|
101 } else { |
|
102 cell.accessoryType = UITableViewCellAccessoryNone; |
|
103 } |
|
104 |
|
105 return cell; |
|
106 } |
|
107 |
|
108 |
|
109 #pragma mark - |
|
110 #pragma mark Table view delegate |
|
111 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
112 int newRow = [indexPath row]; |
|
113 int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
114 |
|
115 if (newRow != oldRow) { |
|
116 // if the two selected rows differ update data on the hog dictionary and reload table content |
|
117 [self.teamDictionary setValue:[fortArray objectAtIndex:newRow] forKey:@"fort"]; |
|
118 |
|
119 // tell our boss to write this new stuff on disk |
|
120 [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
121 |
|
122 UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; |
|
123 newCell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
124 UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath]; |
|
125 oldCell.accessoryType = UITableViewCellAccessoryNone; |
|
126 self.lastIndexPath = indexPath; |
|
127 [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
128 } |
|
129 [aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
130 [self.navigationController popViewControllerAnimated:YES]; |
|
131 } |
|
132 |
|
133 |
|
134 #pragma mark - |
|
135 #pragma mark Memory management |
|
136 -(void) didReceiveMemoryWarning { |
|
137 // Releases the view if it doesn't have a superview. |
|
138 [super didReceiveMemoryWarning]; |
|
139 // Relinquish ownership any cached data, images, etc that aren't in use. |
|
140 } |
|
141 |
|
142 -(void) viewDidUnload { |
|
143 self.teamDictionary = nil; |
|
144 self.lastIndexPath = nil; |
|
145 self.fortArray = nil; |
|
146 [super viewDidUnload]; |
|
147 MSG_DIDUNLOAD(); |
|
148 } |
|
149 |
|
150 |
|
151 - (void)dealloc { |
|
152 [teamDictionary release]; |
|
153 [lastIndexPath release]; |
|
154 [fortArray release]; |
|
155 // [fortSprites release]; |
|
156 [super dealloc]; |
|
157 } |
|
158 |
|
159 |
|
160 @end |
|
161 |