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