author | koda |
Mon, 02 Aug 2010 00:55:24 +0200 | |
changeset 3703 | 12d17c6e8855 |
parent 3697 | d5b30d6373fc |
child 3789 | c3eb56754e92 |
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]; |
|
28 |
NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / 2)]; |
|
29 |
// we need to remove the double entries and the L.png suffix |
|
30 |
for (int i = 0; i < [directoryContents count]; i++) { |
|
31 |
if (i % 2) { |
|
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 |
|
3547 | 91 |
// this creates a scaled down version of the image |
92 |
// TODO: create preview files, scaling is way too slow! |
|
93 |
NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", FORTS_DIRECTORY(), fortName]; |
|
94 |
UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile]; |
|
95 |
[fortFile release]; |
|
96 |
cell.imageView.image = [fortSprite scaleToSize:CGSizeMake(196,196)]; |
|
97 |
[fortSprite release]; |
|
3697 | 98 |
|
3547 | 99 |
cell.detailTextLabel.text = @"Insert funny description here"; |
100 |
if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) { |
|
101 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
102 |
self.lastIndexPath = indexPath; |
|
103 |
} else { |
|
104 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
105 |
} |
|
3697 | 106 |
|
3547 | 107 |
return cell; |
108 |
} |
|
109 |
||
110 |
||
111 |
#pragma mark - |
|
112 |
#pragma mark Table view delegate |
|
113 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
114 |
int newRow = [indexPath row]; |
|
115 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3697 | 116 |
|
3547 | 117 |
if (newRow != oldRow) { |
118 |
// if the two selected rows differ update data on the hog dictionary and reload table content |
|
119 |
[self.teamDictionary setValue:[fortArray objectAtIndex:newRow] forKey:@"fort"]; |
|
120 |
||
121 |
// tell our boss to write this new stuff on disk |
|
122 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
3697 | 123 |
|
3547 | 124 |
UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; |
125 |
newCell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
126 |
UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath]; |
|
127 |
oldCell.accessoryType = UITableViewCellAccessoryNone; |
|
128 |
self.lastIndexPath = indexPath; |
|
129 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
130 |
} |
|
131 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
132 |
[self.navigationController popViewControllerAnimated:YES]; |
|
133 |
} |
|
134 |
||
135 |
||
136 |
#pragma mark - |
|
137 |
#pragma mark Memory management |
|
138 |
-(void) didReceiveMemoryWarning { |
|
139 |
// Releases the view if it doesn't have a superview. |
|
140 |
[super didReceiveMemoryWarning]; |
|
141 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
142 |
} |
|
143 |
||
144 |
-(void) viewDidUnload { |
|
145 |
self.teamDictionary = nil; |
|
146 |
self.lastIndexPath = nil; |
|
147 |
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
|
148 |
MSG_DIDUNLOAD(); |
3547 | 149 |
[super viewDidUnload]; |
150 |
} |
|
151 |
||
152 |
||
3663
8c28abf427f5
reduce the number of keywords used and switch to BMP format for screenshots
koda
parents:
3662
diff
changeset
|
153 |
-(void) dealloc { |
3547 | 154 |
[teamDictionary release]; |
155 |
[lastIndexPath release]; |
|
156 |
[fortArray release]; |
|
157 |
// [fortSprites release]; |
|
158 |
[super dealloc]; |
|
159 |
} |
|
160 |
||
161 |
||
162 |
@end |
|
163 |