3325
|
1 |
//
|
3352
|
2 |
// FortsViewController.m
|
3325
|
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"
|
3352
|
11 |
#import "UIImageExtra.h"
|
3325
|
12 |
|
|
13 |
@implementation FortsViewController
|
|
14 |
@synthesize teamDictionary, fortArray, fortSprites, lastIndexPath;
|
|
15 |
|
|
16 |
|
3335
|
17 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
|
|
18 |
return rotationManager(interfaceOrientation);
|
3325
|
19 |
}
|
|
20 |
|
3335
|
21 |
|
3325
|
22 |
#pragma mark -
|
|
23 |
#pragma mark View lifecycle
|
|
24 |
- (void)viewDidLoad {
|
|
25 |
[super viewDidLoad];
|
|
26 |
|
|
27 |
NSString *fortsDirectory = FORTS_DIRECTORY();
|
|
28 |
NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: fortsDirectory error:NULL];
|
|
29 |
NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / 2)];
|
|
30 |
// we need to remove the double entries and the L.png suffix
|
|
31 |
for (int i = 0; i < [directoryContents count]; i++) {
|
|
32 |
if (i % 2) {
|
|
33 |
NSString *currentName = [directoryContents objectAtIndex:i];
|
|
34 |
NSString *correctName = [currentName substringToIndex:([currentName length] - 5)];
|
|
35 |
[filteredContents addObject:correctName];
|
|
36 |
}
|
|
37 |
}
|
|
38 |
|
|
39 |
self.fortArray = filteredContents;
|
|
40 |
[filteredContents release];
|
|
41 |
|
|
42 |
//NSLog(@"%@",fortArray);
|
|
43 |
|
|
44 |
// this creates a scaled down version of the image
|
|
45 |
NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[fortArray count]];
|
|
46 |
for (NSString *fortName in fortArray) {
|
|
47 |
NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", fortsDirectory, fortName];
|
|
48 |
UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile];
|
|
49 |
[fortFile release];
|
|
50 |
[spriteArray addObject:[fortSprite scaleToSize:CGSizeMake(196,196)]];
|
|
51 |
[fortSprite release];
|
|
52 |
}
|
|
53 |
self.fortSprites = spriteArray;
|
|
54 |
[spriteArray release];
|
|
55 |
}
|
|
56 |
|
|
57 |
|
|
58 |
- (void)viewWillAppear:(BOOL)animated {
|
|
59 |
[super viewWillAppear:animated];
|
|
60 |
[self.tableView reloadData];
|
|
61 |
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
|
|
62 |
}
|
|
63 |
|
|
64 |
/*
|
|
65 |
- (void)viewDidAppear:(BOOL)animated {
|
|
66 |
[super viewDidAppear:animated];
|
|
67 |
}
|
|
68 |
*/
|
|
69 |
/*
|
|
70 |
- (void)viewWillDisappear:(BOOL)animated {
|
|
71 |
[super viewWillDisappear:animated];
|
|
72 |
}
|
|
73 |
*/
|
|
74 |
/*
|
|
75 |
- (void)viewDidDisappear:(BOOL)animated {
|
|
76 |
[super viewDidDisappear:animated];
|
|
77 |
}
|
|
78 |
*/
|
|
79 |
|
|
80 |
|
|
81 |
#pragma mark -
|
|
82 |
#pragma mark Table view data source
|
|
83 |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
|
84 |
return 1;
|
|
85 |
}
|
|
86 |
|
|
87 |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
88 |
return [fortArray count];
|
|
89 |
}
|
|
90 |
|
|
91 |
// Customize the appearance of table view cells.
|
|
92 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
93 |
|
|
94 |
static NSString *CellIdentifier = @"Cell";
|
|
95 |
|
|
96 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
|
|
97 |
if (cell == nil) {
|
|
98 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
|
|
99 |
reuseIdentifier:CellIdentifier] autorelease];
|
|
100 |
}
|
|
101 |
|
|
102 |
cell.imageView.image = [fortSprites objectAtIndex:[indexPath row]];
|
|
103 |
cell.textLabel.text = [fortArray objectAtIndex:[indexPath row]];
|
|
104 |
cell.detailTextLabel.text = @"Insert funny description here";
|
|
105 |
if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) {
|
|
106 |
cell.accessoryType = UITableViewCellAccessoryCheckmark;
|
|
107 |
self.lastIndexPath = indexPath;
|
|
108 |
} else {
|
|
109 |
cell.accessoryType = UITableViewCellAccessoryNone;
|
|
110 |
}
|
|
111 |
|
|
112 |
return cell;
|
|
113 |
}
|
|
114 |
|
|
115 |
|
|
116 |
/*
|
|
117 |
// Override to support conditional editing of the table view.
|
|
118 |
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
119 |
// Return NO if you do not want the specified item to be editable.
|
|
120 |
return YES;
|
|
121 |
}
|
|
122 |
*/
|
|
123 |
|
|
124 |
|
|
125 |
/*
|
|
126 |
// Override to support editing the table view.
|
|
127 |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
128 |
|
|
129 |
if (editingStyle == UITableViewCellEditingStyleDelete) {
|
|
130 |
// Delete the row from the data source
|
|
131 |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
|
|
132 |
}
|
|
133 |
else if (editingStyle == UITableViewCellEditingStyleInsert) {
|
|
134 |
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
|
|
135 |
}
|
|
136 |
}
|
|
137 |
*/
|
|
138 |
|
|
139 |
|
|
140 |
/*
|
|
141 |
// Override to support rearranging the table view.
|
|
142 |
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
|
|
143 |
}
|
|
144 |
*/
|
|
145 |
|
|
146 |
|
|
147 |
/*
|
|
148 |
// Override to support conditional rearranging of the table view.
|
|
149 |
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
150 |
// Return NO if you do not want the item to be re-orderable.
|
|
151 |
return YES;
|
|
152 |
}
|
|
153 |
*/
|
|
154 |
|
|
155 |
|
|
156 |
#pragma mark -
|
|
157 |
#pragma mark Table view delegate
|
|
158 |
-(void) tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
159 |
int newRow = [indexPath row];
|
|
160 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
|
|
161 |
|
|
162 |
if (newRow != oldRow) {
|
|
163 |
// if the two selected rows differ update data on the hog dictionary and reload table content
|
|
164 |
[self.teamDictionary setValue:[fortArray objectAtIndex:newRow] forKey:@"fort"];
|
|
165 |
|
|
166 |
// tell our boss to write this new stuff on disk
|
|
167 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
|
|
168 |
[self.tableView reloadData];
|
|
169 |
|
|
170 |
self.lastIndexPath = indexPath;
|
|
171 |
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
|
|
172 |
}
|
|
173 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
174 |
[self.navigationController popViewControllerAnimated:YES];
|
|
175 |
}
|
|
176 |
|
|
177 |
-(CGFloat) tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
178 |
return 200;
|
|
179 |
}
|
|
180 |
|
|
181 |
#pragma mark -
|
|
182 |
#pragma mark Memory management
|
|
183 |
-(void) didReceiveMemoryWarning {
|
|
184 |
// Releases the view if it doesn't have a superview.
|
|
185 |
[super didReceiveMemoryWarning];
|
|
186 |
// Relinquish ownership any cached data, images, etc that aren't in use.
|
|
187 |
}
|
|
188 |
|
|
189 |
-(void) viewDidUnload {
|
|
190 |
self.teamDictionary = nil;
|
|
191 |
self.lastIndexPath = nil;
|
|
192 |
self.fortArray = nil;
|
|
193 |
self.fortSprites = nil;
|
|
194 |
[super viewDidUnload];
|
|
195 |
}
|
|
196 |
|
|
197 |
|
|
198 |
- (void)dealloc {
|
|
199 |
[teamDictionary release];
|
|
200 |
[lastIndexPath release];
|
|
201 |
[fortArray release];
|
|
202 |
[fortSprites release];
|
|
203 |
[super dealloc];
|
|
204 |
}
|
|
205 |
|
|
206 |
|
|
207 |
@end
|
|
208 |
|