author | koda |
Mon, 30 Aug 2010 05:42:03 +0200 | |
changeset 3791 | 98072b3871c1 |
parent 3789 | c3eb56754e92 |
child 3829 | 81db3c85784b |
permissions | -rw-r--r-- |
3547 | 1 |
// |
2 |
// FortsViewController.m |
|
3 |
// HedgewarsMobile |
|
4 |
// |
|
5 |
// Created by Vittorio on 08/04/10. |
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 |
// |
|
8 |
||
9 |
#import "FortsViewController.h" |
|
10 |
#import "CommodityFunctions.h" |
|
11 |
#import "UIImageExtra.h" |
|
12 |
||
13 |
@implementation FortsViewController |
|
14 |
@synthesize teamDictionary, fortArray, lastIndexPath; |
|
15 |
||
16 |
||
17 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
18 |
return rotationManager(interfaceOrientation); |
|
19 |
} |
|
20 |
||
21 |
||
22 |
#pragma mark - |
|
23 |
#pragma mark View lifecycle |
|
24 |
-(void) viewDidLoad { |
|
25 |
[super viewDidLoad]; |
|
26 |
||
27 |
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
|
28 |
NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / 3)]; |
3547 | 29 |
// we need to remove the double entries and the L.png suffix |
30 |
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
|
31 |
if (i % 3 == 1) { |
3547 | 32 |
NSString *currentName = [directoryContents objectAtIndex:i]; |
33 |
NSString *correctName = [currentName substringToIndex:([currentName length] - 5)]; |
|
34 |
[filteredContents addObject:correctName]; |
|
3697 | 35 |
} |
3547 | 36 |
} |
37 |
self.fortArray = filteredContents; |
|
38 |
[filteredContents release]; |
|
3697 | 39 |
|
3547 | 40 |
/* |
41 |
// this creates a scaled down version of the image |
|
42 |
NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[fortArray count]]; |
|
43 |
for (NSString *fortName in fortArray) { |
|
44 |
NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", fortsDirectory, fortName]; |
|
45 |
UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile]; |
|
46 |
[fortFile release]; |
|
47 |
[spriteArray addObject:[fortSprite scaleToSize:CGSizeMake(196,196)]]; |
|
48 |
[fortSprite release]; |
|
49 |
} |
|
50 |
self.fortSprites = spriteArray; |
|
51 |
[spriteArray release]; |
|
52 |
*/ |
|
3697 | 53 |
|
3547 | 54 |
// statically set row height instead of using delegate method for performance reasons |
55 |
self.tableView.rowHeight = 200; |
|
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 |
||
61 |
- (void)viewWillAppear:(BOOL)animated { |
|
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]; |
83 |
if (cell == nil) { |
|
84 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle |
|
85 |
reuseIdentifier:CellIdentifier] autorelease]; |
|
86 |
} |
|
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 { |
|
137 |
// Releases the view if it doesn't have a superview. |
|
138 |
[super didReceiveMemoryWarning]; |
|
139 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
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 |
// [fortSprites release]; |
|
156 |
[super dealloc]; |
|
157 |
} |
|
158 |
||
159 |
||
160 |
@end |
|
161 |