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