22 #import "SavedGamesViewController.h" |
22 #import "SavedGamesViewController.h" |
23 #import "GameInterfaceBridge.h" |
23 #import "GameInterfaceBridge.h" |
24 #import "CommodityFunctions.h" |
24 #import "CommodityFunctions.h" |
25 |
25 |
26 @implementation SavedGamesViewController |
26 @implementation SavedGamesViewController |
27 @synthesize tableView, listOfSavegames, interfaceBridge; |
27 @synthesize tableView, listOfSavegames, interfaceBridge, numberOfItems; |
28 |
28 |
29 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
29 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
30 return rotationManager(interfaceOrientation); |
30 return rotationManager(interfaceOrientation); |
31 } |
31 } |
32 |
32 |
33 -(void) updateTable { |
33 -(void) updateTable { |
34 NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SAVES_DIRECTORY() error:NULL]; |
34 NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SAVES_DIRECTORY() error:NULL]; |
35 NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES]; |
35 NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES]; |
36 self.listOfSavegames = array; |
36 self.listOfSavegames = array; |
37 [array release]; |
37 [array release]; |
|
38 self.numberOfItems = [self.listOfSavegames count]; |
38 |
39 |
39 [self.tableView reloadData]; |
40 [self.tableView reloadData]; |
40 } |
41 } |
41 |
42 |
42 -(void) viewDidLoad { |
43 -(void) viewDidLoad { |
61 } |
64 } |
62 |
65 |
63 #pragma mark - |
66 #pragma mark - |
64 #pragma mark button functions |
67 #pragma mark button functions |
65 -(IBAction) buttonPressed:(id) sender { |
68 -(IBAction) buttonPressed:(id) sender { |
66 playSound(@"backSound"); |
|
67 [self.tableView setEditing:NO animated:YES]; |
|
68 [[self parentViewController] dismissModalViewControllerAnimated:YES]; |
|
69 } |
|
70 |
|
71 -(IBAction) toggleEdit:(id) sender { |
|
72 BOOL isEditing = self.tableView.editing; |
|
73 [self.tableView setEditing:!isEditing animated:YES]; |
|
74 } |
|
75 |
|
76 -(void) duplicateEntry:(id) sender { |
|
77 UIButton *button = (UIButton *)sender; |
69 UIButton *button = (UIButton *)sender; |
78 NSUInteger row = button.tag; |
70 |
79 |
71 if (button.tag == 0) { |
80 if (self.listOfSavegames == nil) |
72 playSound(@"backSound"); |
81 [self updateTable]; |
73 [self.tableView setEditing:NO animated:YES]; |
82 |
74 [[self parentViewController] dismissModalViewControllerAnimated:YES]; |
83 [(EditableCellView *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]] save:nil]; |
|
84 NSString *currentSaveName = [self.listOfSavegames objectAtIndex:row]; |
|
85 NSString *newSaveName = [[currentSaveName stringByDeletingPathExtension] stringByAppendingFormat:@" %d.hws",[self.listOfSavegames count]]; |
|
86 |
|
87 NSString *currentFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),currentSaveName]; |
|
88 NSString *newFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),newSaveName]; |
|
89 [[NSFileManager defaultManager] copyItemAtPath:currentFilePath toPath:newFilePath error:nil]; |
|
90 [self.listOfSavegames addObject:newSaveName]; |
|
91 [self.listOfSavegames sortUsingSelector:@selector(compare:)]; |
|
92 |
|
93 [self.tableView reloadData]; |
|
94 } |
|
95 |
|
96 -(IBAction) clearAll:(id) sender { |
|
97 NSString *titleStr, *cancelStr, *confirmStr; |
|
98 if (IS_IPAD()) { |
|
99 titleStr = nil; |
|
100 cancelStr = nil; |
|
101 confirmStr = NSLocalizedString(@"Tap to confirm",@""); |
|
102 } else { |
75 } else { |
|
76 NSString *titleStr, *cancelStr, *confirmStr; |
103 titleStr = NSLocalizedString(@"Are you reeeeeally sure?", @""); |
77 titleStr = NSLocalizedString(@"Are you reeeeeally sure?", @""); |
104 cancelStr = NSLocalizedString(@"Well, maybe not...", @""); |
78 cancelStr = NSLocalizedString(@"Well, maybe not...", @""); |
105 confirmStr = NSLocalizedString(@"Of course!", @""); |
79 confirmStr = NSLocalizedString(@"Of course!", @""); |
106 } |
80 |
107 |
81 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:titleStr |
108 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:titleStr |
82 delegate:self |
109 delegate:self |
83 cancelButtonTitle:cancelStr |
110 cancelButtonTitle:cancelStr |
84 destructiveButtonTitle:confirmStr |
111 destructiveButtonTitle:confirmStr |
85 otherButtonTitles:nil]; |
112 otherButtonTitles:nil]; |
86 |
113 |
87 if (IS_IPAD()) |
114 if (IS_IPAD()) |
88 [actionSheet showFromBarButtonItem:(UIBarButtonItem *)sender animated:YES]; |
115 [actionSheet showFromBarButtonItem:(UIBarButtonItem *)sender animated:YES]; |
89 else |
116 else |
90 [actionSheet showInView:self.view]; |
117 [actionSheet showInView:self.view]; |
91 [actionSheet release]; |
118 [actionSheet release]; |
92 } |
119 } |
93 } |
120 |
94 |
121 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex { |
95 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex { |
122 if ([actionSheet cancelButtonIndex] != buttonIndex) { |
96 if ([actionSheet cancelButtonIndex] != buttonIndex) { |
|
97 // remove all files and recreate the directory |
123 [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL]; |
98 [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL]; |
124 [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL]; |
99 [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL]; |
125 [self updateTable]; |
100 |
|
101 // update the table and the cached list |
|
102 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:self.numberOfItems]; |
|
103 for (int i = 0; i < self.numberOfItems; i++) |
|
104 [array addObject:[NSIndexPath indexPathForRow:i inSection:0]]; |
|
105 self.numberOfItems = 0; |
|
106 [self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationTop]; |
|
107 [self.listOfSavegames removeAllObjects]; |
|
108 |
|
109 [array release]; |
126 } |
110 } |
127 } |
111 } |
128 |
112 |
129 #pragma mark - |
113 #pragma mark - |
130 #pragma mark Table view data source |
114 #pragma mark Table view data source |
131 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
115 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
132 return 1; |
116 return 1; |
133 } |
117 } |
134 |
118 |
135 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
119 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
136 if (self.listOfSavegames == nil) |
120 return self.numberOfItems; |
137 [self updateTable]; |
|
138 return [self.listOfSavegames count]; |
|
139 } |
121 } |
140 |
122 |
141 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
123 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
142 static NSString *CellIdentifier = @"Cell"; |
124 static NSString *CellIdentifier = @"Cell"; |
143 |
125 |
146 EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
128 EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
147 if (editableCell == nil) { |
129 if (editableCell == nil) { |
148 editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
130 editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
149 editableCell.delegate = self; |
131 editableCell.delegate = self; |
150 } |
132 } |
151 editableCell.tag = [indexPath row]; |
|
152 editableCell.respectEditing = YES; |
133 editableCell.respectEditing = YES; |
153 editableCell.textField.text = [[self.listOfSavegames objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
134 editableCell.textField.text = [[self.listOfSavegames objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
154 editableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
135 editableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
155 |
|
156 UIImage *addImg = [UIImage imageWithContentsOfFile:@"plus.png"]; |
|
157 UIButton *customButton = [UIButton buttonWithType:UIButtonTypeContactAdd]; |
|
158 customButton.tag = [indexPath row]; |
|
159 [customButton setImage:addImg forState:UIControlStateNormal]; |
|
160 [customButton addTarget:self action:@selector(duplicateEntry:) forControlEvents:UIControlEventTouchUpInside]; |
|
161 editableCell.editingAccessoryView = customButton; |
|
162 |
136 |
163 return (UITableViewCell *)editableCell; |
137 return (UITableViewCell *)editableCell; |
164 } |
138 } |
165 |
139 |
166 -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger) section { |
140 -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger) section { |
167 UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)]; |
141 UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 60)]; |
168 footer.backgroundColor = [UIColor clearColor]; |
142 footer.backgroundColor = [UIColor clearColor]; |
169 |
143 |
170 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width*80/100, 100)]; |
144 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width*60/100, 60)]; |
171 label.center = CGPointMake(self.tableView.frame.size.width/2, 70); |
145 label.center = CGPointMake(self.tableView.frame.size.width/2, 30); |
172 label.textAlignment = UITextAlignmentCenter; |
146 label.textAlignment = UITextAlignmentCenter; |
173 label.font = [UIFont systemFontOfSize:16]; |
147 label.font = [UIFont italicSystemFontOfSize:16]; |
174 label.textColor = [UIColor lightGrayColor]; |
148 label.textColor = [UIColor lightGrayColor]; |
175 label.numberOfLines = 5; |
149 label.numberOfLines = 5; |
176 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.\nCompleted games are removed at the end of the match.",@""); |
150 label.text = NSLocalizedString(@"Press to resume playing or swipe to delete the save file.",@""); |
177 |
151 |
178 label.backgroundColor = [UIColor clearColor]; |
152 label.backgroundColor = [UIColor clearColor]; |
179 [footer addSubview:label]; |
153 [footer addSubview:label]; |
180 [label release]; |
154 [label release]; |
181 return [footer autorelease]; |
155 return [footer autorelease]; |
182 } |
156 } |
183 |
157 |
184 -(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { |
158 -(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { |
185 return 125; |
159 return 60; |
186 } |
160 } |
187 |
161 |
188 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
162 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
189 NSUInteger row = [indexPath row]; |
163 [(EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath] save:nil]; |
190 [(EditableCellView *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]] save:nil]; |
164 self.numberOfItems--; |
191 |
165 [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; |
192 if (self.listOfSavegames == nil) |
166 |
193 [self updateTable]; |
167 NSString *saveName = [self.listOfSavegames objectAtIndex:[indexPath row]]; |
194 |
168 NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),saveName]; |
195 NSString *saveName = [self.listOfSavegames objectAtIndex:row]; |
169 [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil]; |
196 NSString *currentFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),saveName]; |
|
197 [[NSFileManager defaultManager] removeItemAtPath:currentFilePath error:nil]; |
|
198 [self.listOfSavegames removeObject:saveName]; |
170 [self.listOfSavegames removeObject:saveName]; |
199 |
|
200 [self.tableView reloadData]; |
|
201 } |
171 } |
202 |
172 |
203 #pragma mark - |
173 #pragma mark - |
204 #pragma mark Table view delegate |
174 #pragma mark Table view delegate |
205 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
175 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
206 [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
176 [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
207 if (self.listOfSavegames == nil) |
177 if (self.listOfSavegames == nil) |
208 [self updateTable]; |
178 [self updateTable]; |
209 |
179 |
|
180 // duplicate the entry |
210 [(EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath] save:nil]; |
181 [(EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath] save:nil]; |
|
182 |
|
183 NSString *currentSaveName = [self.listOfSavegames objectAtIndex:[indexPath row]]; |
|
184 NSString *currentFilePath = [[NSString alloc] initWithFormat:@"%@/%@",SAVES_DIRECTORY(),currentSaveName]; |
|
185 NSString *newSaveName = [[NSString alloc] initWithFormat:@"[%@] %@",NSLocalizedString(@"Backup",@""),currentSaveName]; |
|
186 NSString *newFilePath = [[NSString alloc] initWithFormat:@"%@/%@",SAVES_DIRECTORY(),newSaveName]; |
|
187 |
|
188 [self.listOfSavegames addObject:newSaveName]; |
|
189 [newSaveName release]; |
|
190 [[NSFileManager defaultManager] copyItemAtPath:currentFilePath toPath:newFilePath error:nil]; |
|
191 [newFilePath release]; |
|
192 |
|
193 self.numberOfItems++; |
|
194 [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; |
211 |
195 |
212 if (self.interfaceBridge == nil) { |
196 if (self.interfaceBridge == nil) { |
213 GameInterfaceBridge *bridge = [[GameInterfaceBridge alloc] initWithController:self]; |
197 GameInterfaceBridge *bridge = [[GameInterfaceBridge alloc] initWithController:self]; |
214 self.interfaceBridge = bridge; |
198 self.interfaceBridge = bridge; |
215 [bridge release]; |
199 [bridge release]; |
216 } |
200 } |
217 |
201 |
218 NSString *filePath = [[NSString alloc] initWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:[indexPath row]]]; |
202 [self.interfaceBridge startSaveGame:currentFilePath]; |
219 [self.interfaceBridge startSaveGame:filePath]; |
203 [currentFilePath release]; |
220 [filePath release]; |
|
221 } |
204 } |
222 |
205 |
223 #pragma mark - |
206 #pragma mark - |
224 #pragma mark editableCellView delegate |
207 #pragma mark editableCellView delegate |
225 // rename old file if names differ |
208 // rename old file if names differ |