author | koda |
Sat, 25 Sep 2010 03:04:22 +0200 | |
changeset 3902 | 3aac7ca07b0e |
parent 3829 | 81db3c85784b |
child 3948 | 24daa33a3114 |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
3 |
* Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 |
* |
|
18 |
* File created on 02/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "HogHatViewController.h" |
|
23 |
#import "CommodityFunctions.h" |
|
24 |
#import "UIImageExtra.h" |
|
25 |
||
26 |
@implementation HogHatViewController |
|
27 |
@synthesize teamDictionary, hatArray, normalHogSprite, lastIndexPath, selectedHog; |
|
28 |
||
29 |
||
30 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
31 |
return rotationManager(interfaceOrientation); |
|
32 |
} |
|
33 |
||
34 |
||
35 |
#pragma mark - |
|
36 |
#pragma mark View lifecycle |
|
37 |
- (void)viewDidLoad { |
|
38 |
[super viewDidLoad]; |
|
39 |
||
40 |
// load all the hat file names and store them into hatArray |
|
41 |
NSString *hatsDirectory = HATS_DIRECTORY(); |
|
42 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL]; |
|
43 |
self.hatArray = array; |
|
3697 | 44 |
|
3547 | 45 |
// load the base hog image, drawing will occure in cellForRow... |
46 |
NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()]; |
|
47 |
UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)]; |
|
48 |
[normalHogFile release]; |
|
49 |
self.normalHogSprite = hogSprite; |
|
50 |
[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
|
51 |
|
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
|
52 |
self.title = NSLocalizedString(@"Change hedgehog's hat",@""); |
3547 | 53 |
} |
54 |
||
55 |
- (void)viewWillAppear:(BOOL)animated { |
|
56 |
[super viewWillAppear:animated]; |
|
3697 | 57 |
|
3547 | 58 |
// this updates the hog name and its hat |
59 |
[self.tableView reloadData]; |
|
60 |
// this moves the tableview to the top |
|
61 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
62 |
} |
|
63 |
||
64 |
||
65 |
#pragma mark - |
|
66 |
#pragma mark Table view data source |
|
67 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
68 |
return 1; |
|
69 |
} |
|
70 |
||
71 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
72 |
return [self.hatArray count]; |
|
73 |
} |
|
74 |
||
75 |
// Customize the appearance of table view cells. |
|
76 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
3697 | 77 |
|
3547 | 78 |
static NSString *CellIdentifier = @"Cell"; |
3697 | 79 |
|
3547 | 80 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
3697 | 81 |
if (cell == nil) |
3547 | 82 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
3697 | 83 |
|
3547 | 84 |
NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
85 |
NSString *hat = [hatArray objectAtIndex:[indexPath row]]; |
|
86 |
cell.textLabel.text = [hat stringByDeletingPathExtension]; |
|
3697 | 87 |
|
3547 | 88 |
NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat]; |
89 |
UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
|
90 |
[hatFile release]; |
|
91 |
cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, -5)]; |
|
92 |
[hatSprite release]; |
|
3697 | 93 |
|
3547 | 94 |
if ([hat isEqualToString:[hog objectForKey:@"hat"]]) { |
95 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
96 |
self.lastIndexPath = indexPath; |
|
97 |
} else { |
|
98 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
99 |
} |
|
100 |
||
101 |
return cell; |
|
102 |
} |
|
103 |
||
104 |
||
105 |
#pragma mark - |
|
106 |
#pragma mark Table view delegate |
|
107 |
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
108 |
int newRow = [indexPath row]; |
|
109 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3697 | 110 |
|
3547 | 111 |
if (newRow != oldRow) { |
112 |
// if the two selected rows differ update data on the hog dictionary and reload table content |
|
113 |
// TODO: maybe this section could be cleaned up |
|
114 |
NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
|
3697 | 115 |
|
3547 | 116 |
NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog]; |
117 |
[newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"]; |
|
118 |
[[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog]; |
|
119 |
[newHog release]; |
|
3697 | 120 |
|
3547 | 121 |
// tell our boss to write this new stuff on disk |
122 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
3697 | 123 |
|
3547 | 124 |
UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; |
125 |
newCell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
126 |
UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath]; |
|
127 |
oldCell.accessoryType = UITableViewCellAccessoryNone; |
|
128 |
self.lastIndexPath = indexPath; |
|
129 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
3697 | 130 |
} |
3547 | 131 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
132 |
[self.navigationController popViewControllerAnimated:YES]; |
|
133 |
} |
|
134 |
||
135 |
||
136 |
#pragma mark - |
|
137 |
#pragma mark Memory management |
|
138 |
- (void)didReceiveMemoryWarning { |
|
139 |
// Releases the view if it doesn't have a superview. |
|
140 |
[super didReceiveMemoryWarning]; |
|
141 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
142 |
} |
|
143 |
||
144 |
- (void)viewDidUnload { |
|
145 |
self.lastIndexPath = nil; |
|
146 |
self.normalHogSprite = nil; |
|
147 |
self.teamDictionary = nil; |
|
148 |
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
|
149 |
MSG_DIDUNLOAD(); |
3547 | 150 |
[super viewDidUnload]; |
151 |
} |
|
152 |
||
153 |
- (void)dealloc { |
|
154 |
[hatArray release]; |
|
155 |
[teamDictionary release]; |
|
156 |
[normalHogSprite release]; |
|
157 |
[lastIndexPath release]; |
|
158 |
[super dealloc]; |
|
159 |
} |
|
160 |
||
161 |
||
162 |
@end |
|
163 |