author | koda |
Wed, 05 Oct 2011 01:06:58 +0200 | |
changeset 6084 | e692c0348e74 |
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 08/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "FortsViewController.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:
5982
diff
changeset
|
23 |
|
3547 | 24 |
|
5982
283be2ca54a7
mad several updates to the resource copying phase in the ios project file, changed paths of some images and added some smaller forts version
koda
parents:
5208
diff
changeset
|
25 |
#define IMGNUM_PER_FORT 6 |
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
3971
diff
changeset
|
26 |
|
3547 | 27 |
@implementation FortsViewController |
28 |
@synthesize teamDictionary, fortArray, lastIndexPath; |
|
29 |
||
30 |
||
31 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
32 |
return rotationManager(interfaceOrientation); |
|
33 |
} |
|
34 |
||
35 |
||
36 |
#pragma mark - |
|
37 |
#pragma mark View lifecycle |
|
38 |
-(void) viewDidLoad { |
|
39 |
[super viewDidLoad]; |
|
40 |
||
41 |
NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FORTS_DIRECTORY() error:NULL]; |
|
5982
283be2ca54a7
mad several updates to the resource copying phase in the ios project file, changed paths of some images and added some smaller forts version
koda
parents:
5208
diff
changeset
|
42 |
NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / IMGNUM_PER_FORT)]; |
3547 | 43 |
// we need to remove the double entries and the L.png suffix |
44 |
for (int i = 0; i < [directoryContents count]; i++) { |
|
5982
283be2ca54a7
mad several updates to the resource copying phase in the ios project file, changed paths of some images and added some smaller forts version
koda
parents:
5208
diff
changeset
|
45 |
if (i % IMGNUM_PER_FORT == IMGNUM_PER_FORT-1) { |
3547 | 46 |
NSString *currentName = [directoryContents objectAtIndex:i]; |
47 |
NSString *correctName = [currentName substringToIndex:([currentName length] - 5)]; |
|
48 |
[filteredContents addObject:correctName]; |
|
3697 | 49 |
} |
3547 | 50 |
} |
51 |
self.fortArray = filteredContents; |
|
52 |
[filteredContents release]; |
|
3697 | 53 |
|
3547 | 54 |
// statically set row height instead of using delegate method for performance reasons |
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
3971
diff
changeset
|
55 |
self.tableView.rowHeight = 128; |
3697 | 56 |
|
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
|
57 |
self.title = NSLocalizedString(@"Choose team fort",@""); |
3547 | 58 |
} |
59 |
||
60 |
||
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
3971
diff
changeset
|
61 |
-(void) viewWillAppear:(BOOL)animated { |
3547 | 62 |
[super viewWillAppear:animated]; |
63 |
[self.tableView reloadData]; |
|
64 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
65 |
} |
|
66 |
||
67 |
||
68 |
#pragma mark - |
|
69 |
#pragma mark Table view data source |
|
70 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
71 |
return 1; |
|
72 |
} |
|
73 |
||
74 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
75 |
return [self.fortArray count]; |
|
76 |
} |
|
77 |
||
78 |
// Customize the appearance of table view cells. |
|
79 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
80 |
static NSString *CellIdentifier = @"Cell"; |
|
3697 | 81 |
|
3547 | 82 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
3971
diff
changeset
|
83 |
if (cell == nil) |
3547 | 84 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle |
85 |
reuseIdentifier:CellIdentifier] autorelease]; |
|
3697 | 86 |
|
3547 | 87 |
NSString *fortName = [fortArray objectAtIndex:[indexPath row]]; |
88 |
cell.textLabel.text = fortName; |
|
3697 | 89 |
|
3789
c3eb56754e92
added a smaller version of forts, fixed a couple of regressions
koda
parents:
3697
diff
changeset
|
90 |
NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@-preview.png", FORTS_DIRECTORY(), fortName]; |
3547 | 91 |
UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile]; |
92 |
[fortFile release]; |
|
3789
c3eb56754e92
added a smaller version of forts, fixed a couple of regressions
koda
parents:
3697
diff
changeset
|
93 |
cell.imageView.image = fortSprite; |
3547 | 94 |
[fortSprite release]; |
3697 | 95 |
|
3789
c3eb56754e92
added a smaller version of forts, fixed a couple of regressions
koda
parents:
3697
diff
changeset
|
96 |
//cell.detailTextLabel.text = @"Insert funny description here"; |
3547 | 97 |
if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) { |
98 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
99 |
self.lastIndexPath = indexPath; |
|
100 |
} else { |
|
101 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
102 |
} |
|
3697 | 103 |
|
3547 | 104 |
return cell; |
105 |
} |
|
106 |
||
107 |
||
108 |
#pragma mark - |
|
109 |
#pragma mark Table view delegate |
|
110 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
111 |
int newRow = [indexPath row]; |
|
112 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3697 | 113 |
|
3547 | 114 |
if (newRow != oldRow) { |
115 |
// if the two selected rows differ update data on the hog dictionary and reload table content |
|
116 |
[self.teamDictionary setValue:[fortArray objectAtIndex:newRow] forKey:@"fort"]; |
|
117 |
||
118 |
// tell our boss to write this new stuff on disk |
|
119 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
3697 | 120 |
|
3547 | 121 |
UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; |
122 |
newCell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
123 |
UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath]; |
|
124 |
oldCell.accessoryType = UITableViewCellAccessoryNone; |
|
125 |
self.lastIndexPath = indexPath; |
|
126 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
127 |
} |
|
128 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
129 |
[self.navigationController popViewControllerAnimated:YES]; |
|
130 |
} |
|
131 |
||
132 |
||
133 |
#pragma mark - |
|
134 |
#pragma mark Memory management |
|
135 |
-(void) didReceiveMemoryWarning { |
|
3971 | 136 |
self.lastIndexPath = nil; |
137 |
MSG_MEMCLEAN(); |
|
3547 | 138 |
[super didReceiveMemoryWarning]; |
139 |
} |
|
140 |
||
141 |
-(void) viewDidUnload { |
|
142 |
self.teamDictionary = nil; |
|
143 |
self.lastIndexPath = nil; |
|
144 |
self.fortArray = 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 |
||
149 |
||
3663
8c28abf427f5
reduce the number of keywords used and switch to BMP format for screenshots
koda
parents:
3662
diff
changeset
|
150 |
-(void) dealloc { |
5208 | 151 |
releaseAndNil(teamDictionary); |
152 |
releaseAndNil(lastIndexPath); |
|
153 |
releaseAndNil(fortArray); |
|
3547 | 154 |
[super dealloc]; |
155 |
} |
|
156 |
||
157 |
||
158 |
@end |
|
159 |