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