author | koda |
Wed, 03 Nov 2010 00:48:42 +0100 | |
changeset 4098 | 40df542b5f62 |
parent 3971 | 5c82ee165ed5 |
child 4476 | 4bf74e158f44 |
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 |
||
26 |
@implementation FortsViewController |
|
27 |
@synthesize teamDictionary, fortArray, lastIndexPath; |
|
28 |
||
29 |
||
30 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
31 |
return rotationManager(interfaceOrientation); |
|
32 |
} |
|
33 |
||
34 |
||
35 |
#pragma mark - |
|
36 |
#pragma mark View lifecycle |
|
37 |
-(void) viewDidLoad { |
|
38 |
[super viewDidLoad]; |
|
39 |
||
40 |
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
|
41 |
NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / 3)]; |
3547 | 42 |
// we need to remove the double entries and the L.png suffix |
43 |
for (int i = 0; i < [directoryContents count]; i++) { |
|
3789
c3eb56754e92
added a smaller version of forts, fixed a couple of regressions
koda
parents:
3697
diff
changeset
|
44 |
if (i % 3 == 1) { |
3547 | 45 |
NSString *currentName = [directoryContents objectAtIndex:i]; |
46 |
NSString *correctName = [currentName substringToIndex:([currentName length] - 5)]; |
|
47 |
[filteredContents addObject:correctName]; |
|
3697 | 48 |
} |
3547 | 49 |
} |
50 |
self.fortArray = filteredContents; |
|
51 |
[filteredContents release]; |
|
3697 | 52 |
|
3547 | 53 |
// statically set row height instead of using delegate method for performance reasons |
54 |
self.tableView.rowHeight = 200; |
|
3697 | 55 |
|
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
|
56 |
self.title = NSLocalizedString(@"Choose team fort",@""); |
3547 | 57 |
} |
58 |
||
59 |
||
60 |
- (void)viewWillAppear:(BOOL)animated { |
|
61 |
[super viewWillAppear:animated]; |
|
62 |
[self.tableView reloadData]; |
|
63 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
|
64 |
} |
|
65 |
||
66 |
||
67 |
#pragma mark - |
|
68 |
#pragma mark Table view data source |
|
69 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
70 |
return 1; |
|
71 |
} |
|
72 |
||
73 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
74 |
return [self.fortArray count]; |
|
75 |
} |
|
76 |
||
77 |
// Customize the appearance of table view cells. |
|
78 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
79 |
static NSString *CellIdentifier = @"Cell"; |
|
3697 | 80 |
|
3547 | 81 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
82 |
if (cell == nil) { |
|
83 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle |
|
84 |
reuseIdentifier:CellIdentifier] autorelease]; |
|
85 |
} |
|
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 { |
3547 | 151 |
[teamDictionary release]; |
152 |
[lastIndexPath release]; |
|
153 |
[fortArray release]; |
|
154 |
// [fortSprites release]; |
|
155 |
[super dealloc]; |
|
156 |
} |
|
157 |
||
158 |
||
159 |
@end |
|
160 |