author | koda |
Wed, 07 Apr 2010 03:02:19 +0000 | |
changeset 3318 | 591c42242106 |
parent 3317 | 198ec44b6d92 |
child 3325 | 652a8ebdf667 |
permissions | -rw-r--r-- |
3305 | 1 |
// |
2 |
// HogHatViewController.m |
|
3 |
// HedgewarsMobile |
|
4 |
// |
|
5 |
// Created by Vittorio on 02/04/10. |
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 |
// |
|
8 |
||
9 |
#import "HogHatViewController.h" |
|
10 |
||
11 |
||
12 |
@implementation HogHatViewController |
|
3315 | 13 |
@synthesize teamDictionary, hatArray, hatSprites, lastIndexPath, selectedHog; |
3305 | 14 |
|
15 |
#pragma mark - |
|
16 |
#pragma mark View lifecycle |
|
17 |
- (void)viewDidLoad { |
|
18 |
[super viewDidLoad]; |
|
19 |
||
3315 | 20 |
// load all the hat file names and store them into hatArray |
3305 | 21 |
NSString *hatPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Data/Graphics/Hats/"]; |
3315 | 22 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatPath error:NULL]; |
23 |
self.hatArray = array; |
|
24 |
||
25 |
// load all the hat images from the previous array but save only the first sprite and store it in hatSprites |
|
26 |
NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[hatArray count]]; |
|
27 |
for (int i=0; i< [hatArray count]; i++) { |
|
3316 | 28 |
NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/Data/Graphics/Hats/%@",[[NSBundle mainBundle] resourcePath],[hatArray objectAtIndex:i]]; |
3308 | 29 |
|
30 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile]; |
|
3316 | 31 |
[hatFile release]; |
3308 | 32 |
CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32); |
33 |
CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea); |
|
34 |
[image release]; |
|
35 |
||
36 |
UIImage *hatSprite = [[UIImage alloc] initWithCGImage:cgImgage]; |
|
37 |
[spriteArray addObject:hatSprite]; |
|
38 |
CGImageRelease(cgImgage); |
|
39 |
[hatSprite release]; |
|
40 |
} |
|
41 |
self.hatSprites = spriteArray; |
|
42 |
[spriteArray release]; |
|
3305 | 43 |
} |
44 |
||
45 |
- (void)viewWillAppear:(BOOL)animated { |
|
46 |
[super viewWillAppear:animated]; |
|
3315 | 47 |
self.title = [[[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog] objectForKey:@"hogname"]; |
48 |
||
3308 | 49 |
// this updates the hog name and its hat |
3305 | 50 |
[self.tableView reloadData]; |
3308 | 51 |
// this moves the tableview to the top |
3312 | 52 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
3305 | 53 |
} |
54 |
||
55 |
/* |
|
56 |
- (void)viewDidAppear:(BOOL)animated { |
|
57 |
[super viewDidAppear:animated]; |
|
58 |
} |
|
59 |
*/ |
|
60 |
/* |
|
61 |
- (void)viewWillDisappear:(BOOL)animated { |
|
62 |
[super viewWillDisappear:animated]; |
|
63 |
} |
|
64 |
*/ |
|
65 |
/* |
|
66 |
- (void)viewDidDisappear:(BOOL)animated { |
|
67 |
[super viewDidDisappear:animated]; |
|
68 |
} |
|
69 |
*/ |
|
70 |
||
71 |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
3315 | 72 |
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
3305 | 73 |
} |
74 |
||
75 |
||
76 |
#pragma mark - |
|
77 |
#pragma mark Table view data source |
|
78 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
79 |
return 2; |
|
80 |
} |
|
81 |
||
82 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
83 |
NSInteger rows; |
|
84 |
if (0 == section) |
|
85 |
rows = 1; |
|
86 |
else |
|
3315 | 87 |
rows = [self.hatArray count]; |
3305 | 88 |
return rows; |
89 |
} |
|
90 |
||
91 |
// Customize the appearance of table view cells. |
|
92 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
93 |
||
94 |
static NSString *CellIdentifier = @"Cell"; |
|
95 |
||
96 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
97 |
if (cell == nil) { |
|
98 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
99 |
} |
|
100 |
||
3315 | 101 |
NSDictionary *hog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
3305 | 102 |
if (0 == [indexPath section]) { |
3315 | 103 |
cell.textLabel.text = self.title; |
3305 | 104 |
cell.imageView.image = nil; |
105 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
106 |
} else { |
|
3315 | 107 |
cell.textLabel.text = [[hatArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
3305 | 108 |
if ([cell.textLabel.text isEqualToString:[hog objectForKey:@"hat"]]) { |
109 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
3315 | 110 |
self.lastIndexPath = indexPath; |
3305 | 111 |
} else { |
112 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
113 |
} |
|
114 |
||
3308 | 115 |
cell.imageView.image = [hatSprites objectAtIndex:[indexPath row]]; |
3305 | 116 |
} |
117 |
||
118 |
return cell; |
|
119 |
} |
|
120 |
||
121 |
||
122 |
/* |
|
123 |
// Override to support conditional editing of the table view. |
|
124 |
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
|
125 |
// Return NO if you do not want the specified item to be editable. |
|
126 |
return YES; |
|
127 |
} |
|
128 |
*/ |
|
129 |
||
130 |
||
131 |
/* |
|
132 |
// Override to support editing the table view. |
|
133 |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
134 |
||
135 |
if (editingStyle == UITableViewCellEditingStyleDelete) { |
|
136 |
// Delete the row from the data source |
|
137 |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; |
|
138 |
} |
|
139 |
else if (editingStyle == UITableViewCellEditingStyleInsert) { |
|
140 |
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view |
|
141 |
} |
|
142 |
} |
|
143 |
*/ |
|
144 |
||
145 |
||
146 |
/* |
|
147 |
// Override to support rearranging the table view. |
|
148 |
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { |
|
149 |
} |
|
150 |
*/ |
|
151 |
||
152 |
||
153 |
/* |
|
154 |
// Override to support conditional rearranging of the table view. |
|
155 |
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
|
156 |
// Return NO if you do not want the item to be re-orderable. |
|
157 |
return YES; |
|
158 |
} |
|
159 |
*/ |
|
160 |
||
161 |
||
162 |
#pragma mark - |
|
163 |
#pragma mark Table view delegate |
|
164 |
||
3315 | 165 |
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
166 |
if (1 == [indexPath section]) { |
|
167 |
int newRow = [indexPath row]; |
|
168 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
169 |
||
170 |
if (newRow != oldRow) { |
|
171 |
// if the two selected rows differ update data on the hog dictionary and reload table content |
|
172 |
NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
|
173 |
||
174 |
NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog]; |
|
175 |
[newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"]; |
|
176 |
[[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog]; |
|
177 |
[newHog release]; |
|
178 |
||
179 |
// tell our boss to write this new stuff on disk |
|
180 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
181 |
[self.tableView reloadData]; |
|
182 |
||
183 |
self.lastIndexPath = indexPath; |
|
184 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
185 |
} |
|
186 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
3317
198ec44b6d92
minor tweaks, icon for ipad, merged overlayviewcontroller, pop viewcontroller when selected a hat
koda
parents:
3316
diff
changeset
|
187 |
[self.navigationController popViewControllerAnimated:YES]; |
3315 | 188 |
} |
3305 | 189 |
} |
190 |
||
191 |
||
192 |
#pragma mark - |
|
193 |
#pragma mark Memory management |
|
194 |
- (void)didReceiveMemoryWarning { |
|
195 |
// Releases the view if it doesn't have a superview. |
|
196 |
[super didReceiveMemoryWarning]; |
|
197 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
198 |
} |
|
199 |
||
200 |
- (void)viewDidUnload { |
|
201 |
[super viewDidUnload]; |
|
3315 | 202 |
self.lastIndexPath = nil; |
203 |
self.hatSprites = nil; |
|
204 |
self.teamDictionary = nil; |
|
205 |
self.hatArray = nil; |
|
3305 | 206 |
} |
207 |
||
208 |
- (void)dealloc { |
|
3315 | 209 |
[hatArray release]; |
210 |
[teamDictionary release]; |
|
211 |
[hatSprites release]; |
|
212 |
[lastIndexPath release]; |
|
3305 | 213 |
[super dealloc]; |
214 |
} |
|
215 |
||
216 |
||
217 |
@end |
|
218 |