author | koda |
Thu, 29 Apr 2010 02:43:28 +0000 | |
changeset 3373 | c1ff724a5c34 |
parent 3357 | 3836a31879e7 |
child 3374 | 0d522416d97f |
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" |
3352 | 11 |
#import "UIImageExtra.h" |
3305 | 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; |
3340 | 31 |
|
3357 | 32 |
NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()]; |
33 |
UIImage *normalHogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)]; |
|
34 |
[normalHogFile release]; |
|
35 |
||
3315 | 36 |
// load all the hat images from the previous array but save only the first sprite and store it in hatSprites |
37 |
NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[hatArray count]]; |
|
3340 | 38 |
for (NSString *hat in hatArray) { |
39 |
NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", hatsDirectory,hat]; |
|
3352 | 40 |
UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
3316 | 41 |
[hatFile release]; |
3357 | 42 |
[spriteArray addObject:[normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, -5)]]; |
3308 | 43 |
[hatSprite release]; |
44 |
} |
|
3357 | 45 |
[normalHogSprite release]; |
3308 | 46 |
self.hatSprites = spriteArray; |
47 |
[spriteArray release]; |
|
3305 | 48 |
} |
49 |
||
50 |
- (void)viewWillAppear:(BOOL)animated { |
|
51 |
[super viewWillAppear:animated]; |
|
3315 | 52 |
self.title = [[[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog] objectForKey:@"hogname"]; |
53 |
||
3308 | 54 |
// this updates the hog name and its hat |
3305 | 55 |
[self.tableView reloadData]; |
3308 | 56 |
// this moves the tableview to the top |
3312 | 57 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
3305 | 58 |
} |
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 |
||
76 |
||
77 |
#pragma mark - |
|
78 |
#pragma mark Table view data source |
|
79 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
80 |
return 1; |
3305 | 81 |
} |
82 |
||
83 |
-(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
|
84 |
return [self.hatArray count]; |
3305 | 85 |
} |
86 |
||
87 |
// Customize the appearance of table view cells. |
|
88 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
89 |
||
90 |
static NSString *CellIdentifier = @"Cell"; |
|
91 |
||
92 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
93 |
if (cell == nil) { |
|
94 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
95 |
} |
|
96 |
||
3325 | 97 |
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
|
98 |
|
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
99 |
NSString *hat = [[hatArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
100 |
cell.textLabel.text = hat; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
101 |
cell.imageView.image = [hatSprites objectAtIndex:[indexPath row]]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
102 |
|
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
103 |
if ([hat isEqualToString:[hog objectForKey:@"hat"]]) { |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
104 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
105 |
self.lastIndexPath = indexPath; |
3305 | 106 |
} else { |
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
107 |
cell.accessoryType = UITableViewCellAccessoryNone; |
3305 | 108 |
} |
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
109 |
|
3305 | 110 |
return cell; |
111 |
} |
|
112 |
||
113 |
||
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 - |
|
155 |
#pragma mark Table view delegate |
|
3315 | 156 |
- (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
|
157 |
int newRow = [indexPath row]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
158 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
159 |
|
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
160 |
if (newRow != oldRow) { |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
161 |
// if the two selected rows differ update data on the hog dictionary and reload table content |
3339 | 162 |
// TODO: maybe this section could be cleaned up |
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
163 |
NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
3315 | 164 |
|
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
165 |
NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
166 |
[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
|
167 |
[[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
168 |
[newHog release]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
169 |
|
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
170 |
// 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
|
171 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
172 |
[self.tableView reloadData]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
173 |
|
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
174 |
self.lastIndexPath = indexPath; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
175 |
[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
|
176 |
} |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
177 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
178 |
[self.navigationController popViewControllerAnimated:YES]; |
3305 | 179 |
} |
180 |
||
181 |
||
182 |
#pragma mark - |
|
183 |
#pragma mark Memory management |
|
184 |
- (void)didReceiveMemoryWarning { |
|
185 |
// Releases the view if it doesn't have a superview. |
|
186 |
[super didReceiveMemoryWarning]; |
|
187 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
188 |
} |
|
189 |
||
190 |
- (void)viewDidUnload { |
|
191 |
[super viewDidUnload]; |
|
3315 | 192 |
self.lastIndexPath = nil; |
193 |
self.hatSprites = nil; |
|
194 |
self.teamDictionary = nil; |
|
195 |
self.hatArray = nil; |
|
3305 | 196 |
} |
197 |
||
198 |
- (void)dealloc { |
|
3315 | 199 |
[hatArray release]; |
200 |
[teamDictionary release]; |
|
201 |
[hatSprites release]; |
|
202 |
[lastIndexPath release]; |
|
3305 | 203 |
[super dealloc]; |
204 |
} |
|
205 |
||
206 |
||
207 |
@end |
|
208 |