author | koda |
Sat, 13 Nov 2010 01:24:39 +0100 | |
changeset 4287 | 7dbdc862097c |
parent 4284 | 57a501a69e5f |
child 4356 | d1d26f8963a3 |
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 20/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "TeamConfigViewController.h" |
|
23 |
#import "CommodityFunctions.h" |
|
24 |
#import "HogButtonView.h" |
|
25 |
#import "SquareButtonView.h" |
|
26 |
||
27 |
@implementation TeamConfigViewController |
|
3739 | 28 |
@synthesize listOfTeams, listOfSelectedTeams, cachedContentsOfDir; |
3547 | 29 |
|
30 |
#define NUMBERBUTTON_TAG 123456 |
|
31 |
#define SQUAREBUTTON_TAG 654321 |
|
32 |
#define LABEL_TAG 456123 |
|
33 |
||
34 |
#pragma mark - |
|
35 |
#pragma mark View lifecycle |
|
36 |
-(void) viewDidLoad { |
|
37 |
[super viewDidLoad]; |
|
3697 | 38 |
|
3547 | 39 |
CGSize screenSize = [[UIScreen mainScreen] bounds].size; |
40 |
self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44); |
|
4082 | 41 |
|
4244 | 42 |
if ([self.tableView respondsToSelector:@selector(setBackgroundView:)]) { |
43 |
if (IS_IPAD()) |
|
44 |
[self.tableView setBackgroundView:nil]; |
|
45 |
else { |
|
46 |
UIImage *backgroundImage = [[UIImage alloc] initWithContentsOfFile:@"backgroundCenter.png"]; |
|
47 |
UIImageView *background = [[UIImageView alloc] initWithImage:backgroundImage]; |
|
48 |
[backgroundImage release]; |
|
49 |
[self.tableView setBackgroundView:background]; |
|
50 |
[background release]; |
|
51 |
} |
|
52 |
} else { |
|
53 |
self.view.backgroundColor = [UIColor blackColor]; |
|
54 |
} |
|
55 |
||
3917 | 56 |
self.tableView.separatorColor = UICOLOR_HW_YELLOW_BODER; |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
57 |
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
3547 | 58 |
} |
59 |
||
60 |
-(void) viewWillAppear:(BOOL)animated { |
|
61 |
[super viewWillAppear:animated]; |
|
62 |
||
3739 | 63 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL]; |
3551 | 64 |
// avoid overwriting selected teams when returning on this view |
3971 | 65 |
if ([self.cachedContentsOfDir isEqualToArray:contentsOfDir] == NO) { |
3917 | 66 |
NSArray *colors = getAvailableColors(); |
3551 | 67 |
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[contentsOfDir count]]; |
68 |
for (int i = 0; i < [contentsOfDir count]; i++) { |
|
69 |
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: |
|
70 |
[contentsOfDir objectAtIndex:i],@"team", |
|
71 |
[NSNumber numberWithInt:4],@"number", |
|
3917 | 72 |
[colors objectAtIndex:i%[colors count]],@"color",nil]; |
3551 | 73 |
[array addObject:dict]; |
74 |
[dict release]; |
|
75 |
} |
|
76 |
self.listOfTeams = array; |
|
77 |
[array release]; |
|
3697 | 78 |
|
3551 | 79 |
NSMutableArray *emptyArray = [[NSMutableArray alloc] initWithObjects:nil]; |
80 |
self.listOfSelectedTeams = emptyArray; |
|
81 |
[emptyArray release]; |
|
3739 | 82 |
|
83 |
cachedContentsOfDir = [[NSArray alloc] initWithArray:contentsOfDir copyItems:YES]; |
|
3547 | 84 |
} |
85 |
[self.tableView reloadData]; |
|
86 |
} |
|
87 |
||
88 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
89 |
return rotationManager(interfaceOrientation); |
|
90 |
} |
|
91 |
||
92 |
||
93 |
#pragma mark - |
|
94 |
#pragma mark Table view data source |
|
95 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
96 |
return 2; |
|
97 |
} |
|
98 |
||
99 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
100 |
if (section == 0) |
|
101 |
return [listOfSelectedTeams count] ; |
|
102 |
else |
|
103 |
return [listOfTeams count]; |
|
104 |
} |
|
105 |
||
106 |
// Customize the appearance of table view cells. |
|
107 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
108 |
static NSString *CellIdentifier0 = @"Cell0"; |
|
109 |
static NSString *CellIdentifier1 = @"Cell1"; |
|
110 |
NSInteger section = [indexPath section]; |
|
111 |
UITableViewCell *cell; |
|
3697 | 112 |
|
3547 | 113 |
if (section == 0) { |
114 |
cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier0]; |
|
115 |
if (cell == nil) { |
|
116 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0] autorelease]; |
|
117 |
||
118 |
UIButton *numberButton = [[HogButtonView alloc] initWithFrame:CGRectMake(12, 5, 88, 32)]; |
|
119 |
numberButton.tag = NUMBERBUTTON_TAG; |
|
120 |
[cell addSubview:numberButton]; |
|
121 |
[numberButton release]; |
|
3697 | 122 |
|
3814 | 123 |
SquareButtonView *squareButton = [[SquareButtonView alloc] initWithFrame:CGRectMake(12+88+6, 5, 36, 36)]; |
3547 | 124 |
squareButton.tag = SQUAREBUTTON_TAG; |
125 |
[cell addSubview:squareButton]; |
|
126 |
[squareButton release]; |
|
3697 | 127 |
|
4284
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4244
diff
changeset
|
128 |
NSInteger length; |
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4244
diff
changeset
|
129 |
if (IS_IPAD()) |
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4244
diff
changeset
|
130 |
length = 103; |
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4244
diff
changeset
|
131 |
else |
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4244
diff
changeset
|
132 |
length = 285; |
57a501a69e5f
update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents:
4244
diff
changeset
|
133 |
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12+88+6+36, 10, length, 25)]; |
3547 | 134 |
label.textAlignment = UITextAlignmentLeft; |
3814 | 135 |
label.minimumFontSize = 11; |
136 |
label.adjustsFontSizeToFitWidth = YES; |
|
137 |
label.baselineAdjustment = UIBaselineAdjustmentAlignCenters; |
|
3547 | 138 |
label.backgroundColor = [UIColor clearColor]; |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
139 |
label.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
140 |
label.textColor = UICOLOR_HW_YELLOW_TEXT; |
3547 | 141 |
label.tag = LABEL_TAG; |
142 |
[cell.contentView addSubview:label]; |
|
143 |
[label release]; |
|
144 |
} |
|
3697 | 145 |
|
3547 | 146 |
NSMutableDictionary *selectedRow = [listOfSelectedTeams objectAtIndex:[indexPath row]]; |
3697 | 147 |
|
3547 | 148 |
UILabel *cellLabel = (UILabel *)[cell viewWithTag:LABEL_TAG]; |
149 |
cellLabel.text = [[selectedRow objectForKey:@"team"] stringByDeletingPathExtension]; |
|
3697 | 150 |
|
3547 | 151 |
HogButtonView *numberButton = (HogButtonView *)[cell viewWithTag:NUMBERBUTTON_TAG]; |
152 |
[numberButton drawManyHogs:[[selectedRow objectForKey:@"number"] intValue]]; |
|
153 |
numberButton.ownerDictionary = selectedRow; |
|
3697 | 154 |
|
3547 | 155 |
SquareButtonView *squareButton = (SquareButtonView *)[cell viewWithTag:SQUAREBUTTON_TAG]; |
156 |
[squareButton selectColor:[[selectedRow objectForKey:@"color"] intValue]]; |
|
157 |
squareButton.ownerDictionary = selectedRow; |
|
3814 | 158 |
|
159 |
NSString *teamPath = [NSString stringWithFormat:@"%@/%@",TEAMS_DIRECTORY(),[selectedRow objectForKey:@"team"]]; |
|
160 |
NSDictionary *firstHog = [[[NSDictionary dictionaryWithContentsOfFile:teamPath] objectForKey:@"hedgehogs"] objectAtIndex:0]; |
|
161 |
if ([[firstHog objectForKey:@"level"] intValue]> 0) { |
|
162 |
NSString *filePath = [NSString stringWithFormat:@"%@/cyborg.png",HATS_DIRECTORY()]; |
|
163 |
UIImage *sprite = [[UIImage alloc] initWithContentsOfFile:filePath andCutAt:CGRectMake(0, 2, 32, 32)]; |
|
164 |
UIImageView *spriteView = [[UIImageView alloc] initWithImage:sprite]; |
|
165 |
[sprite release]; |
|
166 |
||
167 |
cell.accessoryView = spriteView; |
|
168 |
[spriteView release]; |
|
169 |
} else |
|
170 |
cell.accessoryView = nil; |
|
3547 | 171 |
} else { |
172 |
cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1]; |
|
3697 | 173 |
if (cell == nil) |
3547 | 174 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; |
3697 | 175 |
|
3547 | 176 |
cell.textLabel.text = [[[listOfTeams objectAtIndex:[indexPath row]] objectForKey:@"team"] stringByDeletingPathExtension]; |
3814 | 177 |
cell.accessoryView = nil; |
3547 | 178 |
} |
3917 | 179 |
|
180 |
cell.textLabel.textColor = UICOLOR_HW_YELLOW_TEXT; |
|
181 |
cell.backgroundColor = [UIColor blackColor]; |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
182 |
|
3547 | 183 |
return cell; |
184 |
} |
|
185 |
||
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
186 |
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
187 |
return 40.0; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
188 |
} |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
189 |
|
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
190 |
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { |
3983
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3971
diff
changeset
|
191 |
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 30); |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3971
diff
changeset
|
192 |
NSString *text; |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
193 |
if (section == 0) |
3983
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3971
diff
changeset
|
194 |
text = NSLocalizedString(@"Playing Teams",@""); |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
195 |
else |
3983
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3971
diff
changeset
|
196 |
text = NSLocalizedString(@"Available Teams",@""); |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3971
diff
changeset
|
197 |
UILabel *theLabel = createBlueLabel(text, frame); |
3780
7c704e69242e
fade in when returning from game, labels rewritten so that they are drawn at runtime (more flexibility with i18n and iphone support)
koda
parents:
3739
diff
changeset
|
198 |
theLabel.center = CGPointMake(self.view.frame.size.width/2, 20); |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
199 |
|
3780
7c704e69242e
fade in when returning from game, labels rewritten so that they are drawn at runtime (more flexibility with i18n and iphone support)
koda
parents:
3739
diff
changeset
|
200 |
UIView *theView = [[[UIView alloc] init] autorelease]; |
7c704e69242e
fade in when returning from game, labels rewritten so that they are drawn at runtime (more flexibility with i18n and iphone support)
koda
parents:
3739
diff
changeset
|
201 |
[theView addSubview:theLabel]; |
7c704e69242e
fade in when returning from game, labels rewritten so that they are drawn at runtime (more flexibility with i18n and iphone support)
koda
parents:
3739
diff
changeset
|
202 |
[theLabel release]; |
7c704e69242e
fade in when returning from game, labels rewritten so that they are drawn at runtime (more flexibility with i18n and iphone support)
koda
parents:
3739
diff
changeset
|
203 |
return theView; |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
204 |
} |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
205 |
|
3547 | 206 |
#pragma mark - |
207 |
#pragma mark Table view delegate |
|
208 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
209 |
NSInteger row = [indexPath row]; |
|
210 |
NSInteger section = [indexPath section]; |
|
211 |
||
212 |
if (section == 0) { |
|
213 |
[self.listOfTeams addObject:[self.listOfSelectedTeams objectAtIndex:row]]; |
|
214 |
[self.listOfSelectedTeams removeObjectAtIndex:row]; |
|
215 |
} else { |
|
216 |
[self.listOfSelectedTeams addObject:[self.listOfTeams objectAtIndex:row]]; |
|
3697 | 217 |
[self.listOfTeams removeObjectAtIndex:row]; |
3547 | 218 |
} |
219 |
[aTableView reloadData]; |
|
220 |
} |
|
221 |
||
222 |
||
223 |
#pragma mark - |
|
224 |
#pragma mark Memory management |
|
225 |
-(void) didReceiveMemoryWarning { |
|
226 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
3739 | 227 |
self.cachedContentsOfDir = nil; |
3971 | 228 |
MSG_MEMCLEAN(); |
229 |
[super didReceiveMemoryWarning]; |
|
3547 | 230 |
} |
231 |
||
232 |
-(void) viewDidUnload { |
|
233 |
self.listOfTeams = nil; |
|
3739 | 234 |
self.listOfSelectedTeams = nil; |
235 |
self.cachedContentsOfDir = nil; |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3659
diff
changeset
|
236 |
MSG_DIDUNLOAD(); |
3547 | 237 |
[super viewDidUnload]; |
238 |
} |
|
239 |
||
240 |
||
241 |
-(void) dealloc { |
|
3739 | 242 |
[listOfTeams release]; |
243 |
[listOfSelectedTeams release]; |
|
244 |
[cachedContentsOfDir release]; |
|
3547 | 245 |
[super dealloc]; |
246 |
} |
|
247 |
||
248 |
||
249 |
@end |
|
250 |