author | koda |
Fri, 29 Oct 2010 01:28:15 +0200 | |
changeset 4008 | d87619de120d |
parent 3996 | eb549fd864a5 |
child 4030 | adbb37b8c487 |
permissions | -rw-r--r-- |
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
1 |
/* |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
3 |
* Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com> |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
4 |
* |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
7 |
* the Free Software Foundation; version 2 of the License |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
8 |
* |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
13 |
* |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
17 |
* |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
18 |
* File created on 22/09/2010. |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
19 |
*/ |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
20 |
|
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
21 |
|
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
22 |
#import "SavedGamesViewController.h" |
3898 | 23 |
#import "SDL_uikitappdelegate.h" |
24 |
#import "CommodityFunctions.h" |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
25 |
|
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
26 |
@implementation SavedGamesViewController |
3898 | 27 |
@synthesize tableView, listOfSavegames; |
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
28 |
|
3898 | 29 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
30 |
return rotationManager(interfaceOrientation); |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
31 |
} |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
32 |
|
3923
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
33 |
-(void) updateTable { |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
34 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SAVES_DIRECTORY() error:NULL]; |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
35 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES]; |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
36 |
self.listOfSavegames = array; |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
37 |
[array release]; |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
38 |
|
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
39 |
[self.tableView reloadData]; |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
40 |
} |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
41 |
|
3898 | 42 |
-(void) viewDidLoad { |
43 |
self.tableView.backgroundView = nil; |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
44 |
[super viewDidLoad]; |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
45 |
} |
3898 | 46 |
|
47 |
-(void) viewWillAppear:(BOOL)animated { |
|
3978 | 48 |
[self updateTable]; |
3898 | 49 |
[super viewWillAppear:animated]; |
50 |
} |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
51 |
|
3923
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
52 |
#pragma mark - |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
53 |
#pragma mark button functions |
3898 | 54 |
-(IBAction) buttonPressed:(id) sender { |
55 |
playSound(@"backSound"); |
|
3903 | 56 |
[self.tableView setEditing:NO animated:YES]; |
3898 | 57 |
[[self parentViewController] dismissModalViewControllerAnimated:YES]; |
58 |
} |
|
59 |
||
3903 | 60 |
-(IBAction) toggleEdit:(id) sender { |
61 |
BOOL isEditing = self.tableView.editing; |
|
62 |
[self.tableView setEditing:!isEditing animated:YES]; |
|
3971 | 63 |
} |
64 |
||
65 |
-(void) duplicateEntry:(id) sender { |
|
66 |
UIButton *button = (UIButton *)sender; |
|
67 |
NSUInteger row = button.tag; |
|
68 |
||
69 |
if (self.listOfSavegames == nil) |
|
70 |
[self updateTable]; |
|
71 |
||
72 |
[(EditableCellView *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]] save:nil]; |
|
73 |
NSString *currentSaveName = [self.listOfSavegames objectAtIndex:row]; |
|
74 |
NSString *newSaveName = [[currentSaveName stringByDeletingPathExtension] stringByAppendingFormat:@" %d.hws",[self.listOfSavegames count]]; |
|
75 |
||
76 |
NSString *currentFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),currentSaveName]; |
|
77 |
NSString *newFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),newSaveName]; |
|
78 |
[[NSFileManager defaultManager] copyItemAtPath:currentFilePath toPath:newFilePath error:nil]; |
|
79 |
[self.listOfSavegames addObject:newSaveName]; |
|
80 |
[self.listOfSavegames sortUsingSelector:@selector(compare:)]; |
|
81 |
||
82 |
//[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[self.listOfSavegames indexOfObject:newSaveName] inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; |
|
83 |
[self.tableView reloadData]; |
|
3923
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
84 |
} |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
85 |
|
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
86 |
-(IBAction) clearAll:(id) sender { |
3971 | 87 |
NSString *titleStr, *cancelStr, *confirmStr; |
3996 | 88 |
if (IS_IPAD()) { |
3971 | 89 |
titleStr = nil; |
90 |
cancelStr = nil; |
|
91 |
confirmStr = NSLocalizedString(@"Tap to confirm",@""); |
|
92 |
} else { |
|
93 |
titleStr = NSLocalizedString(@"Are you reeeeeally sure?", @""); |
|
94 |
cancelStr = NSLocalizedString(@"Well, maybe not...", @""); |
|
95 |
confirmStr = NSLocalizedString(@"Of course!", @""); |
|
96 |
} |
|
97 |
||
98 |
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:titleStr |
|
99 |
delegate:self |
|
100 |
cancelButtonTitle:cancelStr |
|
101 |
destructiveButtonTitle:confirmStr |
|
102 |
otherButtonTitles:nil]; |
|
103 |
[actionSheet showInView:self.view]; |
|
104 |
[actionSheet release]; |
|
105 |
} |
|
106 |
||
107 |
-(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex { |
|
108 |
if ([actionSheet cancelButtonIndex] != buttonIndex) { |
|
109 |
[[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL]; |
|
110 |
[[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL]; |
|
111 |
[self updateTable]; |
|
112 |
} |
|
3903 | 113 |
} |
114 |
||
3898 | 115 |
#pragma mark - |
116 |
#pragma mark Table view data source |
|
117 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
118 |
return 1; |
|
119 |
} |
|
120 |
||
121 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
3971 | 122 |
if (self.listOfSavegames == nil) |
123 |
[self updateTable]; |
|
3898 | 124 |
return [self.listOfSavegames count]; |
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
125 |
} |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
126 |
|
3898 | 127 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
128 |
static NSString *CellIdentifier = @"Cell"; |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
129 |
|
3971 | 130 |
if (self.listOfSavegames == nil) |
131 |
[self updateTable]; |
|
3903 | 132 |
EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
133 |
if (editableCell == nil) { |
|
134 |
editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
135 |
editableCell.delegate = self; |
|
136 |
} |
|
137 |
editableCell.tag = [indexPath row]; |
|
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
138 |
editableCell.respectEditing = YES; |
3903 | 139 |
editableCell.textField.text = [[self.listOfSavegames objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
140 |
editableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
|
141 |
||
142 |
UIImage *addImg = [UIImage imageWithContentsOfFile:@"plus.png"]; |
|
143 |
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeContactAdd]; |
|
144 |
customButton.tag = [indexPath row]; |
|
145 |
[customButton setImage:addImg forState:UIControlStateNormal]; |
|
146 |
[customButton addTarget:self action:@selector(duplicateEntry:) forControlEvents:UIControlEventTouchUpInside]; |
|
147 |
editableCell.editingAccessoryView = customButton; |
|
148 |
||
149 |
return (UITableViewCell *)editableCell; |
|
150 |
} |
|
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
151 |
|
3903 | 152 |
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger) section { |
3904
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
153 |
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)]; |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
154 |
footer.backgroundColor = [UIColor clearColor]; |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
155 |
|
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
156 |
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width*80/100, 100)]; |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
157 |
label.center = CGPointMake(self.tableView.frame.size.width/2,70); |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
158 |
label.textAlignment = UITextAlignmentCenter; |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
159 |
label.font = [UIFont systemFontOfSize:16]; |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
160 |
label.textColor = [UIColor lightGrayColor]; |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
161 |
label.numberOfLines = 5; |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
162 |
label.text = NSLocalizedString(@"Games are automatically saved and can be resumed by selecting an entry above.\nYou can modify this list by pressing the 'Edit' button.\nNotice that completed games are deleted, so make backups.",@""); |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
163 |
|
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
164 |
label.backgroundColor = [UIColor clearColor]; |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
165 |
[footer addSubview:label]; |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
166 |
[label release]; |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
167 |
return [footer autorelease]; |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
168 |
} |
3898 | 169 |
|
3903 | 170 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
171 |
NSUInteger row = [indexPath row]; |
|
172 |
[(EditableCellView *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]] save:nil]; |
|
173 |
||
3971 | 174 |
if (self.listOfSavegames == nil) |
175 |
[self updateTable]; |
|
176 |
||
3903 | 177 |
NSString *saveName = [self.listOfSavegames objectAtIndex:row]; |
178 |
NSString *currentFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),saveName]; |
|
179 |
[[NSFileManager defaultManager] removeItemAtPath:currentFilePath error:nil]; |
|
180 |
[self.listOfSavegames removeObject:saveName]; |
|
181 |
||
182 |
[self.tableView reloadData]; |
|
183 |
} |
|
3898 | 184 |
|
185 |
#pragma mark - |
|
186 |
#pragma mark Table view delegate |
|
187 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
188 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
3971 | 189 |
if (self.listOfSavegames == nil) |
190 |
[self updateTable]; |
|
191 |
||
3903 | 192 |
[(EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath] save:nil]; |
193 |
||
3898 | 194 |
NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:[indexPath row]]]; |
195 |
||
196 |
NSDictionary *allDataNecessary = [NSDictionary dictionaryWithObjectsAndKeys: |
|
197 |
filePath,@"savefile", |
|
198 |
[NSNumber numberWithBool:NO],@"netgame", |
|
3978 | 199 |
[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:self.interfaceOrientation] forKey:@"orientation"],@"game_dictionary", |
3898 | 200 |
nil]; |
201 |
[[SDLUIKitDelegate sharedAppDelegate] startSDLgame:allDataNecessary]; |
|
3978 | 202 |
[self.parentViewController dismissModalViewControllerAnimated:NO]; |
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
203 |
} |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
204 |
|
3898 | 205 |
#pragma mark - |
3903 | 206 |
#pragma mark editableCellView delegate |
207 |
// rename old file if names differ |
|
208 |
-(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue { |
|
3971 | 209 |
if (self.listOfSavegames == nil) |
210 |
[self updateTable]; |
|
3903 | 211 |
NSString *oldFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:tagValue]]; |
212 |
NSString *newFilePath = [NSString stringWithFormat:@"%@/%@.hws",SAVES_DIRECTORY(),textString]; |
|
213 |
||
214 |
if ([oldFilePath isEqualToString:newFilePath] == NO) { |
|
215 |
[[NSFileManager defaultManager] moveItemAtPath:oldFilePath toPath:newFilePath error:nil]; |
|
216 |
[self.listOfSavegames replaceObjectAtIndex:tagValue withObject:[textString stringByAppendingString:@".hws"]]; |
|
217 |
} |
|
218 |
||
219 |
} |
|
220 |
||
221 |
#pragma mark - |
|
3898 | 222 |
#pragma mark Memory Management |
223 |
-(void) didReceiveMemoryWarning { |
|
3971 | 224 |
self.listOfSavegames = nil; |
225 |
MSG_MEMCLEAN(); |
|
3898 | 226 |
[super didReceiveMemoryWarning]; |
227 |
} |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
228 |
|
3898 | 229 |
-(void) viewDidUnload { |
230 |
self.tableView = nil; |
|
231 |
self.listOfSavegames = nil; |
|
3971 | 232 |
MSG_DIDUNLOAD(); |
3898 | 233 |
[super viewDidUnload]; |
234 |
} |
|
235 |
||
236 |
-(void) dealloc { |
|
237 |
[tableView release]; |
|
238 |
[listOfSavegames release]; |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
239 |
[super dealloc]; |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
240 |
} |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
241 |
|
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
242 |
@end |