author | koda |
Sat, 09 Oct 2010 05:57:46 +0200 | |
changeset 3933 | 1a873262f5dd |
parent 3923 | 694e6f6e0e30 |
child 3971 | 5c82ee165ed5 |
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; |
|
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
|
44 |
[[NSNotificationCenter defaultCenter] addObserver:self |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
45 |
selector:@selector(viewWillAppear:) |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
46 |
name:@"removedSave" |
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
47 |
object:nil]; |
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
48 |
[super viewDidLoad]; |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
49 |
} |
3898 | 50 |
|
51 |
-(void) viewWillAppear:(BOOL)animated { |
|
52 |
[super viewWillAppear:animated]; |
|
3923
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
53 |
[self updateTable]; |
3898 | 54 |
} |
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
55 |
|
3923
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
56 |
#pragma mark - |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
57 |
#pragma mark button functions |
3898 | 58 |
-(IBAction) buttonPressed:(id) sender { |
59 |
playSound(@"backSound"); |
|
3903 | 60 |
[self.tableView setEditing:NO animated:YES]; |
3898 | 61 |
[[self parentViewController] dismissModalViewControllerAnimated:YES]; |
62 |
} |
|
63 |
||
3903 | 64 |
-(IBAction) toggleEdit:(id) sender { |
65 |
BOOL isEditing = self.tableView.editing; |
|
66 |
[self.tableView setEditing:!isEditing animated:YES]; |
|
3923
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
67 |
UIToolbar *toolbar = (UIToolbar *)[self.view viewWithTag:458912]; |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
68 |
for (UIBarButtonItem *item in toolbar.items) |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
69 |
if (item.tag == 452198) |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
70 |
item.enabled = !isEditing; |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
71 |
} |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
72 |
|
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
73 |
-(IBAction) clearAll:(id) sender { |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
74 |
[[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL]; |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
75 |
[[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL]; |
694e6f6e0e30
various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents:
3904
diff
changeset
|
76 |
[self updateTable]; |
3903 | 77 |
} |
78 |
||
3898 | 79 |
#pragma mark - |
80 |
#pragma mark Table view data source |
|
81 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
82 |
return 1; |
|
83 |
} |
|
84 |
||
85 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
86 |
return [self.listOfSavegames count]; |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
87 |
} |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
88 |
|
3898 | 89 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
90 |
static NSString *CellIdentifier = @"Cell"; |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
91 |
|
3903 | 92 |
EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
93 |
if (editableCell == nil) { |
|
94 |
editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
95 |
editableCell.delegate = self; |
|
96 |
} |
|
97 |
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
|
98 |
editableCell.respectEditing = YES; |
3903 | 99 |
editableCell.textField.text = [[self.listOfSavegames objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
100 |
editableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
|
101 |
||
102 |
UIImage *addImg = [UIImage imageWithContentsOfFile:@"plus.png"]; |
|
103 |
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeContactAdd]; |
|
104 |
customButton.tag = [indexPath row]; |
|
105 |
[customButton setImage:addImg forState:UIControlStateNormal]; |
|
106 |
[customButton addTarget:self action:@selector(duplicateEntry:) forControlEvents:UIControlEventTouchUpInside]; |
|
107 |
editableCell.editingAccessoryView = customButton; |
|
108 |
||
109 |
return (UITableViewCell *)editableCell; |
|
110 |
} |
|
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
|
111 |
|
3903 | 112 |
-(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
|
113 |
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
|
114 |
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
|
115 |
|
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
116 |
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
|
117 |
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
|
118 |
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
|
119 |
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
|
120 |
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
|
121 |
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
|
122 |
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
|
123 |
|
22e4d74240e5
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents:
3903
diff
changeset
|
124 |
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
|
125 |
[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
|
126 |
[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
|
127 |
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
|
128 |
} |
3898 | 129 |
|
3903 | 130 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
131 |
NSUInteger row = [indexPath row]; |
|
132 |
[(EditableCellView *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]] save:nil]; |
|
133 |
||
134 |
NSString *saveName = [self.listOfSavegames objectAtIndex:row]; |
|
135 |
NSString *currentFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),saveName]; |
|
136 |
[[NSFileManager defaultManager] removeItemAtPath:currentFilePath error:nil]; |
|
137 |
[self.listOfSavegames removeObject:saveName]; |
|
138 |
||
139 |
[self.tableView reloadData]; |
|
140 |
} |
|
3898 | 141 |
|
3903 | 142 |
-(void) duplicateEntry:(id) sender { |
143 |
UIButton *button = (UIButton *)sender; |
|
144 |
NSUInteger row = button.tag; |
|
145 |
||
146 |
[(EditableCellView *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]] save:nil]; |
|
147 |
NSString *currentSaveName = [self.listOfSavegames objectAtIndex:row]; |
|
148 |
NSString *newSaveName = [[currentSaveName stringByDeletingPathExtension] stringByAppendingFormat:@" %d.hws",[self.listOfSavegames count]]; |
|
149 |
||
150 |
NSString *currentFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),currentSaveName]; |
|
151 |
NSString *newFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),newSaveName]; |
|
152 |
[[NSFileManager defaultManager] copyItemAtPath:currentFilePath toPath:newFilePath error:nil]; |
|
153 |
[self.listOfSavegames addObject:newSaveName]; |
|
154 |
[self.listOfSavegames sortUsingSelector:@selector(compare:)]; |
|
155 |
||
156 |
//[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[self.listOfSavegames indexOfObject:newSaveName] inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; |
|
157 |
[self.tableView reloadData]; |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
158 |
} |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
159 |
|
3898 | 160 |
#pragma mark - |
161 |
#pragma mark Table view delegate |
|
162 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
163 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
164 |
||
3903 | 165 |
[(EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath] save:nil]; |
166 |
||
3898 | 167 |
NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:[indexPath row]]]; |
168 |
||
169 |
NSDictionary *allDataNecessary = [NSDictionary dictionaryWithObjectsAndKeys: |
|
170 |
[NSDictionary dictionary],@"game_dictionary", |
|
171 |
filePath,@"savefile", |
|
172 |
[NSNumber numberWithBool:NO],@"netgame", |
|
173 |
nil]; |
|
174 |
[[SDLUIKitDelegate sharedAppDelegate] startSDLgame:allDataNecessary]; |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
175 |
} |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
176 |
|
3898 | 177 |
#pragma mark - |
3903 | 178 |
#pragma mark editableCellView delegate |
179 |
// rename old file if names differ |
|
180 |
-(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue { |
|
181 |
NSString *oldFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:tagValue]]; |
|
182 |
NSString *newFilePath = [NSString stringWithFormat:@"%@/%@.hws",SAVES_DIRECTORY(),textString]; |
|
183 |
||
184 |
if ([oldFilePath isEqualToString:newFilePath] == NO) { |
|
185 |
[[NSFileManager defaultManager] moveItemAtPath:oldFilePath toPath:newFilePath error:nil]; |
|
186 |
[self.listOfSavegames replaceObjectAtIndex:tagValue withObject:[textString stringByAppendingString:@".hws"]]; |
|
187 |
} |
|
188 |
||
189 |
} |
|
190 |
||
191 |
#pragma mark - |
|
3898 | 192 |
#pragma mark Memory Management |
193 |
-(void) didReceiveMemoryWarning { |
|
194 |
[super didReceiveMemoryWarning]; |
|
195 |
} |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
196 |
|
3898 | 197 |
-(void) viewDidUnload { |
198 |
self.tableView = nil; |
|
199 |
self.listOfSavegames = nil; |
|
200 |
[super viewDidUnload]; |
|
201 |
} |
|
202 |
||
203 |
-(void) dealloc { |
|
204 |
[tableView release]; |
|
205 |
[listOfSavegames release]; |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
206 |
[super dealloc]; |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
207 |
} |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
208 |
|
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
209 |
@end |