author | koda |
Sat, 31 Jul 2010 11:24:53 +0200 | |
changeset 3697 | d5b30d6373fc |
parent 3662 | a44406f4369b |
child 3829 | 81db3c85784b |
permissions | -rw-r--r-- |
3547 | 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 |
#import "CommodityFunctions.h" |
|
11 |
#import "UIImageExtra.h" |
|
12 |
||
13 |
@implementation HogHatViewController |
|
14 |
@synthesize teamDictionary, hatArray, normalHogSprite, lastIndexPath, selectedHog; |
|
15 |
||
16 |
||
17 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
18 |
return rotationManager(interfaceOrientation); |
|
19 |
} |
|
20 |
||
21 |
||
22 |
#pragma mark - |
|
23 |
#pragma mark View lifecycle |
|
24 |
- (void)viewDidLoad { |
|
25 |
[super viewDidLoad]; |
|
26 |
||
27 |
// load all the hat file names and store them into hatArray |
|
28 |
NSString *hatsDirectory = HATS_DIRECTORY(); |
|
29 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL]; |
|
30 |
self.hatArray = array; |
|
3697 | 31 |
|
3547 | 32 |
// load the base hog image, drawing will occure in cellForRow... |
33 |
NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()]; |
|
34 |
UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)]; |
|
35 |
[normalHogFile release]; |
|
36 |
self.normalHogSprite = hogSprite; |
|
37 |
[hogSprite release]; |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3547
diff
changeset
|
38 |
|
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3547
diff
changeset
|
39 |
self.title = NSLocalizedString(@"Change hedgehog's hat",@""); |
3547 | 40 |
} |
41 |
||
42 |
- (void)viewWillAppear:(BOOL)animated { |
|
43 |
[super viewWillAppear:animated]; |
|
3697 | 44 |
|
3547 | 45 |
// this updates the hog name and its hat |
46 |
[self.tableView reloadData]; |
|
47 |
// this moves the tableview to the top |
|
48 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
49 |
} |
|
50 |
||
51 |
||
52 |
#pragma mark - |
|
53 |
#pragma mark Table view data source |
|
54 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
55 |
return 1; |
|
56 |
} |
|
57 |
||
58 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
59 |
return [self.hatArray count]; |
|
60 |
} |
|
61 |
||
62 |
// Customize the appearance of table view cells. |
|
63 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
3697 | 64 |
|
3547 | 65 |
static NSString *CellIdentifier = @"Cell"; |
3697 | 66 |
|
3547 | 67 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
3697 | 68 |
if (cell == nil) |
3547 | 69 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
3697 | 70 |
|
3547 | 71 |
NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
72 |
NSString *hat = [hatArray objectAtIndex:[indexPath row]]; |
|
73 |
cell.textLabel.text = [hat stringByDeletingPathExtension]; |
|
3697 | 74 |
|
3547 | 75 |
NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat]; |
76 |
UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
|
77 |
[hatFile release]; |
|
78 |
cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, -5)]; |
|
79 |
[hatSprite release]; |
|
3697 | 80 |
|
3547 | 81 |
if ([hat isEqualToString:[hog objectForKey:@"hat"]]) { |
82 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
83 |
self.lastIndexPath = indexPath; |
|
84 |
} else { |
|
85 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
86 |
} |
|
87 |
||
88 |
return cell; |
|
89 |
} |
|
90 |
||
91 |
||
92 |
#pragma mark - |
|
93 |
#pragma mark Table view delegate |
|
94 |
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
95 |
int newRow = [indexPath row]; |
|
96 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3697 | 97 |
|
3547 | 98 |
if (newRow != oldRow) { |
99 |
// if the two selected rows differ update data on the hog dictionary and reload table content |
|
100 |
// TODO: maybe this section could be cleaned up |
|
101 |
NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
|
3697 | 102 |
|
3547 | 103 |
NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog]; |
104 |
[newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"]; |
|
105 |
[[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog]; |
|
106 |
[newHog release]; |
|
3697 | 107 |
|
3547 | 108 |
// tell our boss to write this new stuff on disk |
109 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
3697 | 110 |
|
3547 | 111 |
UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; |
112 |
newCell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
113 |
UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath]; |
|
114 |
oldCell.accessoryType = UITableViewCellAccessoryNone; |
|
115 |
self.lastIndexPath = indexPath; |
|
116 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
3697 | 117 |
} |
3547 | 118 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
119 |
[self.navigationController popViewControllerAnimated:YES]; |
|
120 |
} |
|
121 |
||
122 |
||
123 |
#pragma mark - |
|
124 |
#pragma mark Memory management |
|
125 |
- (void)didReceiveMemoryWarning { |
|
126 |
// Releases the view if it doesn't have a superview. |
|
127 |
[super didReceiveMemoryWarning]; |
|
128 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
129 |
} |
|
130 |
||
131 |
- (void)viewDidUnload { |
|
132 |
self.lastIndexPath = nil; |
|
133 |
self.normalHogSprite = nil; |
|
134 |
self.teamDictionary = nil; |
|
135 |
self.hatArray = nil; |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3547
diff
changeset
|
136 |
MSG_DIDUNLOAD(); |
3547 | 137 |
[super viewDidUnload]; |
138 |
} |
|
139 |
||
140 |
- (void)dealloc { |
|
141 |
[hatArray release]; |
|
142 |
[teamDictionary release]; |
|
143 |
[normalHogSprite release]; |
|
144 |
[lastIndexPath release]; |
|
145 |
[super dealloc]; |
|
146 |
} |
|
147 |
||
148 |
||
149 |
@end |
|
150 |