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