author | koda |
Sun, 12 Feb 2012 18:08:34 +0100 | |
changeset 6678 | beab48f963d5 |
parent 6104 | 117bdf4e7af9 |
child 6700 | e04da46ee43c |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
4976 | 3 |
* Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com> |
3829 | 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 "FlagsViewController.h" |
|
4478 | 23 |
#import <QuartzCore/QuartzCore.h> |
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5208
diff
changeset
|
24 |
|
3547 | 25 |
|
26 |
@implementation FlagsViewController |
|
3950 | 27 |
@synthesize teamDictionary, flagArray, communityArray, lastIndexPath; |
3547 | 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 |
||
3950 | 40 |
NSMutableArray *array_na = [[NSMutableArray alloc] init]; |
41 |
NSMutableArray *array_cm = [[NSMutableArray alloc] init]; |
|
42 |
||
43 |
for (NSString *name in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FLAGS_DIRECTORY() error:NULL]) { |
|
44 |
if ([name hasPrefix:@"cm_"]) { |
|
45 |
NSString *processed = [name substringFromIndex:3]; |
|
46 |
[array_cm addObject:processed]; |
|
47 |
} else |
|
48 |
[array_na addObject:name]; |
|
49 |
} |
|
50 |
||
51 |
self.flagArray = array_na; |
|
52 |
[array_na release]; |
|
53 |
self.communityArray = array_cm; |
|
54 |
[array_cm release]; |
|
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(@"Set team flag",@""); |
3547 | 57 |
} |
58 |
||
59 |
-(void) viewWillAppear:(BOOL)animated { |
|
60 |
[super viewWillAppear:animated]; |
|
3950 | 61 |
// reloadData needed because team might change |
3547 | 62 |
[self.tableView reloadData]; |
3950 | 63 |
//[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
3547 | 64 |
} |
65 |
||
66 |
||
67 |
#pragma mark - |
|
68 |
#pragma mark Table view data source |
|
69 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
3950 | 70 |
return 2; |
3547 | 71 |
} |
72 |
||
73 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
3950 | 74 |
if (section == 0) |
75 |
return [self.flagArray count]; |
|
76 |
else |
|
77 |
return [self.communityArray count]; |
|
3547 | 78 |
} |
79 |
||
80 |
// Customize the appearance of table view cells. |
|
81 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
82 |
static NSString *CellIdentifier = @"Cell"; |
|
3950 | 83 |
NSInteger row = [indexPath row]; |
3697 | 84 |
|
3547 | 85 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
86 |
if (cell == nil) { |
|
87 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
88 |
} |
|
3697 | 89 |
|
3950 | 90 |
NSString *flagName = nil; |
91 |
NSArray *source = nil; |
|
92 |
if ([indexPath section] == 0) { |
|
93 |
source = self.flagArray; |
|
94 |
flagName = [source objectAtIndex:row]; |
|
95 |
} else { |
|
96 |
source = self.communityArray; |
|
97 |
flagName = [NSString stringWithFormat:@"cm_%@",[source objectAtIndex:row]]; |
|
98 |
} |
|
99 |
NSString *flagFile = [[NSString alloc] initWithFormat:@"%@/%@", FLAGS_DIRECTORY(), flagName]; |
|
3547 | 100 |
UIImage *flagSprite = [[UIImage alloc] initWithContentsOfFile:flagFile]; |
101 |
[flagFile release]; |
|
102 |
cell.imageView.image = flagSprite; |
|
103 |
[flagSprite release]; |
|
6104 | 104 |
cell.imageView.layer.borderWidth = 1; |
105 |
cell.imageView.layer.borderColor = [[UIColor blackColor] CGColor]; |
|
3697 | 106 |
|
3950 | 107 |
cell.textLabel.text = [[source objectAtIndex:row] stringByDeletingPathExtension]; |
108 |
if ([[flagName stringByDeletingPathExtension] isEqualToString:[self.teamDictionary objectForKey:@"flag"]]) { |
|
3547 | 109 |
cell.accessoryType = UITableViewCellAccessoryCheckmark; |
110 |
self.lastIndexPath = indexPath; |
|
111 |
} else { |
|
112 |
cell.accessoryType = UITableViewCellAccessoryNone; |
|
113 |
} |
|
3697 | 114 |
|
3547 | 115 |
return cell; |
116 |
} |
|
117 |
||
3950 | 118 |
-(NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section { |
119 |
NSString *sectionTitle = nil; |
|
120 |
switch (section) { |
|
121 |
case 0: |
|
122 |
sectionTitle = NSLocalizedString(@"Worldwide", @""); |
|
123 |
break; |
|
124 |
case 1: |
|
125 |
sectionTitle = NSLocalizedString(@"Community", @""); |
|
126 |
break; |
|
127 |
default: |
|
128 |
DLog(@"nope"); |
|
129 |
break; |
|
130 |
} |
|
131 |
return sectionTitle; |
|
132 |
} |
|
133 |
||
3547 | 134 |
|
135 |
#pragma mark - |
|
136 |
#pragma mark Table view delegate |
|
137 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
138 |
int newRow = [indexPath row]; |
|
139 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3950 | 140 |
int newSection = [indexPath section]; |
141 |
int oldSection = (lastIndexPath != nil) ? [lastIndexPath section] : -1; |
|
142 |
||
143 |
if (newRow != oldRow || newSection != oldSection) { |
|
144 |
NSString *flag = nil; |
|
145 |
if ([indexPath section] == 0) |
|
146 |
flag = [self.flagArray objectAtIndex:newRow]; |
|
147 |
else |
|
148 |
flag = [NSString stringWithFormat:@"cm_%@",[self.communityArray objectAtIndex:newRow]]; |
|
149 |
||
3547 | 150 |
// if the two selected rows differ update data on the hog dictionary and reload table content |
3950 | 151 |
[self.teamDictionary setValue:[flag stringByDeletingPathExtension] forKey:@"flag"]; |
3547 | 152 |
|
153 |
// tell our boss to write this new stuff on disk |
|
154 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil]; |
|
155 |
||
156 |
UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; |
|
157 |
newCell.accessoryType = UITableViewCellAccessoryCheckmark; |
|
158 |
UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath]; |
|
159 |
oldCell.accessoryType = UITableViewCellAccessoryNone; |
|
160 |
self.lastIndexPath = indexPath; |
|
161 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
162 |
} |
|
163 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
164 |
[self.navigationController popViewControllerAnimated:YES]; |
|
165 |
} |
|
166 |
||
167 |
||
168 |
#pragma mark - |
|
169 |
#pragma mark Memory management |
|
170 |
-(void) didReceiveMemoryWarning { |
|
3950 | 171 |
self.lastIndexPath = nil; |
172 |
MSG_MEMCLEAN(); |
|
3547 | 173 |
[super didReceiveMemoryWarning]; |
174 |
} |
|
175 |
||
176 |
-(void) viewDidUnload { |
|
177 |
self.teamDictionary = nil; |
|
178 |
self.lastIndexPath = nil; |
|
179 |
self.flagArray = nil; |
|
3950 | 180 |
self.communityArray = 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
|
181 |
MSG_DIDUNLOAD(); |
3547 | 182 |
[super viewDidUnload]; |
183 |
} |
|
184 |
||
185 |
-(void) dealloc { |
|
5208 | 186 |
releaseAndNil(teamDictionary); |
187 |
releaseAndNil(lastIndexPath); |
|
188 |
releaseAndNil(flagArray); |
|
189 |
releaseAndNil(communityArray); |
|
3547 | 190 |
[super dealloc]; |
191 |
} |
|
192 |
||
193 |
||
194 |
@end |
|
195 |