author | Xeli |
Tue, 07 Feb 2012 18:56:49 +0100 | |
changeset 6648 | 025473a2c420 |
parent 6078 | 8c0cc07731e5 |
child 6700 | e04da46ee43c |
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" |
|
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
6074
diff
changeset
|
23 |
|
3547 | 24 |
|
25 |
@implementation HogHatViewController |
|
26 |
@synthesize teamDictionary, hatArray, normalHogSprite, lastIndexPath, selectedHog; |
|
27 |
||
28 |
||
29 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
30 |
return rotationManager(interfaceOrientation); |
|
31 |
} |
|
32 |
||
33 |
#pragma mark - |
|
34 |
#pragma mark View lifecycle |
|
3971 | 35 |
-(void) viewDidLoad { |
3547 | 36 |
[super viewDidLoad]; |
37 |
||
38 |
// load all the hat file names and store them into hatArray |
|
39 |
NSString *hatsDirectory = HATS_DIRECTORY(); |
|
40 |
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL]; |
|
41 |
self.hatArray = array; |
|
3697 | 42 |
|
3547 | 43 |
// load the base hog image, drawing will occure in cellForRow... |
5983 | 44 |
NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/basehat-hedgehog.png",[[NSBundle mainBundle] resourcePath]]; |
45 |
UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile]; |
|
3547 | 46 |
[normalHogFile release]; |
47 |
self.normalHogSprite = hogSprite; |
|
48 |
[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
|
49 |
|
6074
047eaed35cbb
ios major refactoring for ios settings, now they are presented differently on iphone/ipad, code is simplified and optimized, and ui is a little refreshed (eg. no more stuck selected fields)
koda
parents:
5983
diff
changeset
|
50 |
self.title = NSLocalizedString(@"Change hedgehogs' hat",@""); |
3547 | 51 |
} |
52 |
||
3971 | 53 |
-(void) viewWillAppear:(BOOL)animated { |
3547 | 54 |
[super viewWillAppear:animated]; |
3697 | 55 |
|
3547 | 56 |
// this updates the hog name and its hat |
57 |
[self.tableView reloadData]; |
|
58 |
// this moves the tableview to the top |
|
59 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
60 |
} |
|
61 |
||
62 |
||
63 |
#pragma mark - |
|
64 |
#pragma mark Table view data source |
|
65 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
66 |
return 1; |
|
67 |
} |
|
68 |
||
69 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
70 |
return [self.hatArray count]; |
|
71 |
} |
|
72 |
||
73 |
// Customize the appearance of table view cells. |
|
3971 | 74 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
3697 | 75 |
|
3547 | 76 |
static NSString *CellIdentifier = @"Cell"; |
3697 | 77 |
|
3547 | 78 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
3697 | 79 |
if (cell == nil) |
3547 | 80 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
3697 | 81 |
|
3547 | 82 |
NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
83 |
NSString *hat = [hatArray objectAtIndex:[indexPath row]]; |
|
84 |
cell.textLabel.text = [hat stringByDeletingPathExtension]; |
|
3697 | 85 |
|
3547 | 86 |
NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat]; |
87 |
UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)]; |
|
88 |
[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
|
89 |
cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, 5)]; |
3547 | 90 |
[hatSprite release]; |
3697 | 91 |
|
3547 | 92 |
if ([hat isEqualToString:[hog objectForKey:@"hat"]]) { |
93 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
94 |
self.lastIndexPath = indexPath; |
|
95 |
} else { |
|
96 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
97 |
} |
|
98 |
||
99 |
return cell; |
|
100 |
} |
|
101 |
||
102 |
||
103 |
#pragma mark - |
|
104 |
#pragma mark Table view delegate |
|
3971 | 105 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
3547 | 106 |
int newRow = [indexPath row]; |
107 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3697 | 108 |
|
3547 | 109 |
if (newRow != oldRow) { |
110 |
// if the two selected rows differ update data on the hog dictionary and reload table content |
|
111 |
// TODO: maybe this section could be cleaned up |
|
112 |
NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog]; |
|
3697 | 113 |
|
3547 | 114 |
NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog]; |
115 |
[newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"]; |
|
116 |
[[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog]; |
|
117 |
[newHog release]; |
|
3697 | 118 |
|
3547 | 119 |
// tell our boss to write this new stuff on disk |
120 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
3697 | 121 |
|
3547 | 122 |
UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; |
123 |
newCell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
124 |
UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath]; |
|
125 |
oldCell.accessoryType = UITableViewCellAccessoryNone; |
|
126 |
self.lastIndexPath = indexPath; |
|
127 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
3697 | 128 |
} |
3547 | 129 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
130 |
[self.navigationController popViewControllerAnimated:YES]; |
|
131 |
} |
|
132 |
||
133 |
||
134 |
#pragma mark - |
|
135 |
#pragma mark Memory management |
|
3971 | 136 |
-(void) didReceiveMemoryWarning { |
137 |
self.lastIndexPath = nil; |
|
138 |
MSG_MEMCLEAN(); |
|
3547 | 139 |
[super didReceiveMemoryWarning]; |
140 |
} |
|
141 |
||
3971 | 142 |
-(void) viewDidUnload { |
3547 | 143 |
self.lastIndexPath = nil; |
144 |
self.normalHogSprite = nil; |
|
145 |
self.teamDictionary = nil; |
|
146 |
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
|
147 |
MSG_DIDUNLOAD(); |
3547 | 148 |
[super viewDidUnload]; |
149 |
} |
|
150 |
||
3971 | 151 |
-(void) dealloc { |
5208 | 152 |
releaseAndNil(hatArray); |
153 |
releaseAndNil(teamDictionary); |
|
154 |
releaseAndNil(normalHogSprite); |
|
155 |
releaseAndNil(lastIndexPath); |
|
3547 | 156 |
[super dealloc]; |
157 |
} |
|
158 |
||
159 |
||
160 |
@end |
|
161 |