author | koda |
Wed, 02 Mar 2011 00:27:20 +0100 | |
changeset 4976 | 088d40d8aba2 |
parent 3971 | 5c82ee165ed5 |
child 5208 | 878e551f0b4a |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
4976 | 3 |
* Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com> |
3829 | 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 |
#pragma mark - |
|
35 |
#pragma mark View lifecycle |
|
3971 | 36 |
-(void) viewDidLoad { |
3547 | 37 |
[super viewDidLoad]; |
38 |
||
39 |
// load all the hat file names and store them into hatArray |
|
40 |
NSString *hatsDirectory = HATS_DIRECTORY(); |
|
41 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL]; |
|
42 |
self.hatArray = array; |
|
3697 | 43 |
|
3547 | 44 |
// load the base hog image, drawing will occure in cellForRow... |
45 |
NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()]; |
|
46 |
UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)]; |
|
47 |
[normalHogFile release]; |
|
48 |
self.normalHogSprite = hogSprite; |
|
49 |
[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
|
50 |
|
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 |
self.title = NSLocalizedString(@"Change hedgehog's hat",@""); |
3547 | 52 |
} |
53 |
||
3971 | 54 |
-(void) viewWillAppear:(BOOL)animated { |
3547 | 55 |
[super viewWillAppear:animated]; |
3697 | 56 |
|
3547 | 57 |
// this updates the hog name and its hat |
58 |
[self.tableView reloadData]; |
|
59 |
// this moves the tableview to the top |
|
60 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
61 |
} |
|
62 |
||
63 |
||
64 |
#pragma mark - |
|
65 |
#pragma mark Table view data source |
|
66 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
67 |
return 1; |
|
68 |
} |
|
69 |
||
70 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
71 |
return [self.hatArray count]; |
|
72 |
} |
|
73 |
||
74 |
// Customize the appearance of table view cells. |
|
3971 | 75 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
3697 | 76 |
|
3547 | 77 |
static NSString *CellIdentifier = @"Cell"; |
3697 | 78 |
|
3547 | 79 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
3697 | 80 |
if (cell == nil) |
3547 | 81 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
3697 | 82 |
|
3547 | 83 |
NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
84 |
NSString *hat = [hatArray objectAtIndex:[indexPath row]]; |
|
85 |
cell.textLabel.text = [hat stringByDeletingPathExtension]; |
|
3697 | 86 |
|
3547 | 87 |
NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat]; |
88 |
UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
|
89 |
[hatFile release]; |
|
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3829
diff
changeset
|
90 |
cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, 5)]; |
3547 | 91 |
[hatSprite release]; |
3697 | 92 |
|
3547 | 93 |
if ([hat isEqualToString:[hog objectForKey:@"hat"]]) { |
94 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
95 |
self.lastIndexPath = indexPath; |
|
96 |
} else { |
|
97 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
98 |
} |
|
99 |
||
100 |
return cell; |
|
101 |
} |
|
102 |
||
103 |
||
104 |
#pragma mark - |
|
105 |
#pragma mark Table view delegate |
|
3971 | 106 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
3547 | 107 |
int newRow = [indexPath row]; |
108 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3697 | 109 |
|
3547 | 110 |
if (newRow != oldRow) { |
111 |
// if the two selected rows differ update data on the hog dictionary and reload table content |
|
112 |
// TODO: maybe this section could be cleaned up |
|
113 |
NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
|
3697 | 114 |
|
3547 | 115 |
NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog]; |
116 |
[newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"]; |
|
117 |
[[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog]; |
|
118 |
[newHog release]; |
|
3697 | 119 |
|
3547 | 120 |
// tell our boss to write this new stuff on disk |
121 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
3697 | 122 |
|
3547 | 123 |
UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; |
124 |
newCell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
125 |
UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath]; |
|
126 |
oldCell.accessoryType = UITableViewCellAccessoryNone; |
|
127 |
self.lastIndexPath = indexPath; |
|
128 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
3697 | 129 |
} |
3547 | 130 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
131 |
[self.navigationController popViewControllerAnimated:YES]; |
|
132 |
} |
|
133 |
||
134 |
||
135 |
#pragma mark - |
|
136 |
#pragma mark Memory management |
|
3971 | 137 |
-(void) didReceiveMemoryWarning { |
138 |
self.lastIndexPath = nil; |
|
139 |
MSG_MEMCLEAN(); |
|
3547 | 140 |
[super didReceiveMemoryWarning]; |
141 |
} |
|
142 |
||
3971 | 143 |
-(void) viewDidUnload { |
3547 | 144 |
self.lastIndexPath = nil; |
145 |
self.normalHogSprite = nil; |
|
146 |
self.teamDictionary = nil; |
|
147 |
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
|
148 |
MSG_DIDUNLOAD(); |
3547 | 149 |
[super viewDidUnload]; |
150 |
} |
|
151 |
||
3971 | 152 |
-(void) dealloc { |
3547 | 153 |
[hatArray release]; |
154 |
[teamDictionary release]; |
|
155 |
[normalHogSprite release]; |
|
156 |
[lastIndexPath release]; |
|
157 |
[super dealloc]; |
|
158 |
} |
|
159 |
||
160 |
||
161 |
@end |
|
162 |