9 #import "HogHatViewController.h" |
9 #import "HogHatViewController.h" |
10 #import "CommodityFunctions.h" |
10 #import "CommodityFunctions.h" |
11 #import "UIImageExtra.h" |
11 #import "UIImageExtra.h" |
12 |
12 |
13 @implementation HogHatViewController |
13 @implementation HogHatViewController |
14 @synthesize teamDictionary, hatArray, hatSprites, lastIndexPath, selectedHog; |
14 @synthesize teamDictionary, hatArray, normalHogSprite, lastIndexPath, selectedHog; |
15 |
15 |
16 |
16 |
17 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
17 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
18 return rotationManager(interfaceOrientation); |
18 return rotationManager(interfaceOrientation); |
19 } |
19 } |
27 // load all the hat file names and store them into hatArray |
27 // load all the hat file names and store them into hatArray |
28 NSString *hatsDirectory = HATS_DIRECTORY(); |
28 NSString *hatsDirectory = HATS_DIRECTORY(); |
29 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL]; |
29 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL]; |
30 self.hatArray = array; |
30 self.hatArray = array; |
31 |
31 |
|
32 // load the base hog image, drawing will occure in cellForRow... |
32 NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()]; |
33 NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()]; |
33 UIImage *normalHogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)]; |
34 UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)]; |
34 [normalHogFile release]; |
35 [normalHogFile release]; |
35 |
36 self.normalHogSprite = hogSprite; |
36 // load all the hat images from the previous array but save only the first sprite and store it in hatSprites |
37 [hogSprite release]; |
37 NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[hatArray count]]; |
|
38 for (NSString *hat in hatArray) { |
|
39 NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", hatsDirectory,hat]; |
|
40 UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
|
41 [hatFile release]; |
|
42 [spriteArray addObject:[normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, -5)]]; |
|
43 [hatSprite release]; |
|
44 } |
|
45 [normalHogSprite release]; |
|
46 self.hatSprites = spriteArray; |
|
47 [spriteArray release]; |
|
48 } |
38 } |
49 |
39 |
50 - (void)viewWillAppear:(BOOL)animated { |
40 - (void)viewWillAppear:(BOOL)animated { |
51 [super viewWillAppear:animated]; |
41 [super viewWillAppear:animated]; |
52 self.title = [[[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog] objectForKey:@"hogname"]; |
42 self.title = [[[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog] objectForKey:@"hogname"]; |
54 // this updates the hog name and its hat |
44 // this updates the hog name and its hat |
55 [self.tableView reloadData]; |
45 [self.tableView reloadData]; |
56 // this moves the tableview to the top |
46 // this moves the tableview to the top |
57 [self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
47 [self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
58 } |
48 } |
59 |
|
60 /* |
|
61 - (void)viewDidAppear:(BOOL)animated { |
|
62 [super viewDidAppear:animated]; |
|
63 } |
|
64 */ |
|
65 /* |
|
66 - (void)viewWillDisappear:(BOOL)animated { |
|
67 [super viewWillDisappear:animated]; |
|
68 } |
|
69 */ |
|
70 /* |
|
71 - (void)viewDidDisappear:(BOOL)animated { |
|
72 [super viewDidDisappear:animated]; |
|
73 } |
|
74 */ |
|
75 |
49 |
76 |
50 |
77 #pragma mark - |
51 #pragma mark - |
78 #pragma mark Table view data source |
52 #pragma mark Table view data source |
79 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
53 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
88 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
62 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
89 |
63 |
90 static NSString *CellIdentifier = @"Cell"; |
64 static NSString *CellIdentifier = @"Cell"; |
91 |
65 |
92 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
66 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
93 if (cell == nil) { |
67 if (cell == nil) |
94 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
68 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
95 } |
|
96 |
69 |
97 NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
70 NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
|
71 NSString *hat = [hatArray objectAtIndex:[indexPath row]]; |
|
72 cell.textLabel.text = [hat stringByDeletingPathExtension]; |
98 |
73 |
99 NSString *hat = [[hatArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
74 NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat]; |
100 cell.textLabel.text = hat; |
75 UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
101 cell.imageView.image = [hatSprites objectAtIndex:[indexPath row]]; |
76 [hatFile release]; |
102 |
77 cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, -5)]; |
|
78 [hatSprite release]; |
|
79 |
103 if ([hat isEqualToString:[hog objectForKey:@"hat"]]) { |
80 if ([hat isEqualToString:[hog objectForKey:@"hat"]]) { |
104 cell.accessoryType = UITableViewCellAccessoryCheckmark; |
81 cell.accessoryType = UITableViewCellAccessoryCheckmark; |
105 self.lastIndexPath = indexPath; |
82 self.lastIndexPath = indexPath; |
106 } else { |
83 } else { |
107 cell.accessoryType = UITableViewCellAccessoryNone; |
84 cell.accessoryType = UITableViewCellAccessoryNone; |
109 |
86 |
110 return cell; |
87 return cell; |
111 } |
88 } |
112 |
89 |
113 |
90 |
114 /* |
|
115 // Override to support conditional editing of the table view. |
|
116 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
|
117 // Return NO if you do not want the specified item to be editable. |
|
118 return YES; |
|
119 } |
|
120 */ |
|
121 |
|
122 |
|
123 /* |
|
124 // Override to support editing the table view. |
|
125 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
126 |
|
127 if (editingStyle == UITableViewCellEditingStyleDelete) { |
|
128 // Delete the row from the data source |
|
129 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; |
|
130 } |
|
131 else if (editingStyle == UITableViewCellEditingStyleInsert) { |
|
132 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view |
|
133 } |
|
134 } |
|
135 */ |
|
136 |
|
137 |
|
138 /* |
|
139 // Override to support rearranging the table view. |
|
140 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { |
|
141 } |
|
142 */ |
|
143 |
|
144 |
|
145 /* |
|
146 // Override to support conditional rearranging of the table view. |
|
147 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
|
148 // Return NO if you do not want the item to be re-orderable. |
|
149 return YES; |
|
150 } |
|
151 */ |
|
152 |
|
153 |
|
154 #pragma mark - |
91 #pragma mark - |
155 #pragma mark Table view delegate |
92 #pragma mark Table view delegate |
156 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
93 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
157 int newRow = [indexPath row]; |
94 int newRow = [indexPath row]; |
158 int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
95 int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
159 |
96 |
160 if (newRow != oldRow) { |
97 if (newRow != oldRow) { |
161 // if the two selected rows differ update data on the hog dictionary and reload table content |
98 // if the two selected rows differ update data on the hog dictionary and reload table content |
162 // TODO: maybe this section could be cleaned up |
99 // TODO: maybe this section could be cleaned up |
163 NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
100 NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
164 |
101 |
165 NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog]; |
102 NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog]; |
166 [newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"]; |
103 [newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"]; |
167 [[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog]; |
104 [[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog]; |
168 [newHog release]; |
105 [newHog release]; |
169 |
106 |
170 // tell our boss to write this new stuff on disk |
107 // tell our boss to write this new stuff on disk |
171 [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
108 [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
172 [self.tableView reloadData]; |
|
173 |
109 |
|
110 UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; |
|
111 newCell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
112 UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath]; |
|
113 oldCell.accessoryType = UITableViewCellAccessoryNone; |
174 self.lastIndexPath = indexPath; |
114 self.lastIndexPath = indexPath; |
175 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
115 [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
176 } |
116 } |
177 [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
117 [aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
178 [self.navigationController popViewControllerAnimated:YES]; |
118 [self.navigationController popViewControllerAnimated:YES]; |
179 } |
119 } |
180 |
120 |
181 |
121 |
182 #pragma mark - |
122 #pragma mark - |
188 } |
128 } |
189 |
129 |
190 - (void)viewDidUnload { |
130 - (void)viewDidUnload { |
191 [super viewDidUnload]; |
131 [super viewDidUnload]; |
192 self.lastIndexPath = nil; |
132 self.lastIndexPath = nil; |
193 self.hatSprites = nil; |
133 self.normalHogSprite = nil; |
194 self.teamDictionary = nil; |
134 self.teamDictionary = nil; |
195 self.hatArray = nil; |
135 self.hatArray = nil; |
196 } |
136 } |
197 |
137 |
198 - (void)dealloc { |
138 - (void)dealloc { |
199 [hatArray release]; |
139 [hatArray release]; |
200 [teamDictionary release]; |
140 [teamDictionary release]; |
201 [hatSprites release]; |
141 [normalHogSprite release]; |
202 [lastIndexPath release]; |
142 [lastIndexPath release]; |
203 [super dealloc]; |
143 [super dealloc]; |
204 } |
144 } |
205 |
145 |
206 |
146 |