22 |
22 |
23 @implementation HogHatViewController |
23 @implementation HogHatViewController |
24 @synthesize teamDictionary, hatArray, normalHogSprite, selectedHog; |
24 @synthesize teamDictionary, hatArray, normalHogSprite, selectedHog; |
25 |
25 |
26 |
26 |
27 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
27 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
28 return rotationManager(interfaceOrientation); |
28 return rotationManager(interfaceOrientation); |
29 } |
29 } |
30 |
30 |
31 #pragma mark - |
31 #pragma mark - |
32 #pragma mark View lifecycle |
32 #pragma mark View lifecycle |
33 -(void) viewDidLoad { |
33 - (void)viewDidLoad { |
34 [super viewDidLoad]; |
34 [super viewDidLoad]; |
35 |
35 |
36 // load all the hat file names and store them into hatArray |
36 // load all the hat file names and store them into hatArray |
37 NSString *hatsDirectory = HATS_DIRECTORY(); |
37 NSString *hatsDirectory = HATS_DIRECTORY(); |
38 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL]; |
38 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL]; |
39 self.hatArray = array; |
39 self.hatArray = array; |
40 |
40 |
41 // load the base hog image, drawing will occure in cellForRow... |
41 // load the base hog image, drawing will occure in cellForRow... |
42 NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/basehat-hedgehog.png",[[NSBundle mainBundle] resourcePath]]; |
42 NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/basehat-hedgehog.png",[[NSBundle mainBundle] resourcePath]]; |
43 UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile]; |
43 UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile]; |
44 [normalHogFile release]; |
|
45 self.normalHogSprite = hogSprite; |
44 self.normalHogSprite = hogSprite; |
46 [hogSprite release]; |
|
47 |
45 |
48 self.title = NSLocalizedString(@"Change hedgehogs' hat",@""); |
46 self.title = NSLocalizedString(@"Change hedgehogs' hat",@""); |
49 } |
47 } |
50 |
48 |
51 -(void) viewWillAppear:(BOOL)animated { |
49 - (void)viewWillAppear:(BOOL)animated { |
52 [super viewWillAppear:animated]; |
50 [super viewWillAppear:animated]; |
53 |
51 |
54 // this updates the hog name and its hat |
52 // this updates the hog name and its hat |
55 [self.tableView reloadData]; |
53 [self.tableView reloadData]; |
56 // this moves the tableview to the top |
54 // this moves the tableview to the top |
58 } |
56 } |
59 |
57 |
60 |
58 |
61 #pragma mark - |
59 #pragma mark - |
62 #pragma mark Table view data source |
60 #pragma mark Table view data source |
63 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
61 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
64 return 1; |
62 return 1; |
65 } |
63 } |
66 |
64 |
67 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
65 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
68 return [self.hatArray count]; |
66 return [self.hatArray count]; |
69 } |
67 } |
70 |
68 |
71 // Customize the appearance of table view cells. |
69 // Customize the appearance of table view cells. |
72 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
70 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
73 |
71 |
74 static NSString *CellIdentifier = @"Cell"; |
72 static NSString *CellIdentifier = @"Cell"; |
75 |
73 |
76 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
74 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
77 if (cell == nil) |
75 if (cell == nil) |
78 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
76 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; |
79 |
77 |
80 NSString *hat = [self.hatArray objectAtIndex:[indexPath row]]; |
78 NSString *hat = [self.hatArray objectAtIndex:[indexPath row]]; |
81 cell.textLabel.text = [hat stringByDeletingPathExtension]; |
79 cell.textLabel.text = [hat stringByDeletingPathExtension]; |
82 |
80 |
83 NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat]; |
81 NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat]; |
84 UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
82 UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
85 [hatFile release]; |
|
86 cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, 5)]; |
83 cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, 5)]; |
87 [hatSprite release]; |
|
88 |
84 |
89 NSDictionary *hog = (self.selectedHog != -1) ? [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:self.selectedHog] : nil; |
85 NSDictionary *hog = (self.selectedHog != -1) ? [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:self.selectedHog] : nil; |
90 if ([[hat stringByDeletingPathExtension] isEqualToString:[hog objectForKey:@"hat"]]) { |
86 if ([[hat stringByDeletingPathExtension] isEqualToString:[hog objectForKey:@"hat"]]) { |
91 cell.accessoryType = UITableViewCellAccessoryCheckmark; |
87 cell.accessoryType = UITableViewCellAccessoryCheckmark; |
92 } else { |
88 } else { |
97 } |
93 } |
98 |
94 |
99 |
95 |
100 #pragma mark - |
96 #pragma mark - |
101 #pragma mark Table view delegate |
97 #pragma mark Table view delegate |
102 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath |
98 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath |
103 { |
99 { |
104 NSInteger selectedRow = [indexPath row]; |
100 NSInteger selectedRow = [indexPath row]; |
105 NSString *newHat = [[self.hatArray objectAtIndex:selectedRow] stringByDeletingPathExtension]; |
101 NSString *newHat = [[self.hatArray objectAtIndex:selectedRow] stringByDeletingPathExtension]; |
106 |
102 |
107 // update data on the hogs dictionary |
103 // update data on the hogs dictionary |
132 { |
128 { |
133 NSDictionary *oldHog = [hogsArray objectAtIndex:i]; |
129 NSDictionary *oldHog = [hogsArray objectAtIndex:i]; |
134 NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary:oldHog]; |
130 NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary:oldHog]; |
135 [newHog setObject:newHat forKey:@"hat"]; |
131 [newHog setObject:newHat forKey:@"hat"]; |
136 [hogsArray replaceObjectAtIndex:i withObject:newHog]; |
132 [hogsArray replaceObjectAtIndex:i withObject:newHog]; |
137 [newHog release]; |
|
138 } |
133 } |
139 } |
134 } |
140 |
135 |
141 #pragma mark - |
136 #pragma mark - |
142 #pragma mark Memory management |
137 #pragma mark Memory management |
143 -(void) didReceiveMemoryWarning { |
138 |
|
139 - (void)didReceiveMemoryWarning { |
144 MSG_MEMCLEAN(); |
140 MSG_MEMCLEAN(); |
145 [super didReceiveMemoryWarning]; |
141 [super didReceiveMemoryWarning]; |
146 } |
142 } |
147 |
143 |
148 -(void) viewDidUnload { |
|
149 self.normalHogSprite = nil; |
|
150 self.teamDictionary = nil; |
|
151 self.hatArray = nil; |
|
152 MSG_DIDUNLOAD(); |
|
153 [super viewDidUnload]; |
|
154 } |
|
155 |
|
156 -(void) dealloc { |
|
157 releaseAndNil(hatArray); |
|
158 releaseAndNil(teamDictionary); |
|
159 releaseAndNil(normalHogSprite); |
|
160 [super dealloc]; |
|
161 } |
|
162 |
|
163 |
|
164 @end |
144 @end |
165 |
145 |