author | koda |
Sat, 25 Sep 2010 18:38:05 +0200 | |
changeset 3903 | db01c37494af |
parent 3898 | 0a9c3735a713 |
child 3904 | 22e4d74240e5 |
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 |
|
3898 | 33 |
-(void) viewDidLoad { |
34 |
self.tableView.backgroundView = nil; |
|
35 |
||
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
36 |
[super viewDidLoad]; |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
37 |
} |
3898 | 38 |
|
39 |
-(void) viewWillAppear:(BOOL)animated { |
|
40 |
[super viewWillAppear:animated]; |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
41 |
|
3898 | 42 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SAVES_DIRECTORY() error:NULL]; |
43 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES]; |
|
44 |
self.listOfSavegames = array; |
|
45 |
[array release]; |
|
46 |
||
47 |
[self.tableView reloadData]; |
|
48 |
} |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
49 |
|
3898 | 50 |
-(IBAction) buttonPressed:(id) sender { |
51 |
playSound(@"backSound"); |
|
3903 | 52 |
[self.tableView setEditing:NO animated:YES]; |
3898 | 53 |
[[self parentViewController] dismissModalViewControllerAnimated:YES]; |
54 |
} |
|
55 |
||
3903 | 56 |
// modifies the navigation bar to add the "Add" and "Done" buttons |
57 |
-(IBAction) toggleEdit:(id) sender { |
|
58 |
BOOL isEditing = self.tableView.editing; |
|
59 |
[self.tableView setEditing:!isEditing animated:YES]; |
|
60 |
||
61 |
UIBarButtonItem *barButton = (UIBarButtonItem *)sender; |
|
62 |
if (isEditing) |
|
63 |
[barButton setTitle:NSLocalizedString(@"Edit",@"")]; |
|
64 |
else |
|
65 |
[barButton setTitle:NSLocalizedString(@"Commit",@"")]; |
|
66 |
} |
|
67 |
||
3898 | 68 |
#pragma mark - |
69 |
#pragma mark Table view data source |
|
70 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
71 |
return 1; |
|
72 |
} |
|
73 |
||
74 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
75 |
return [self.listOfSavegames count]; |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
76 |
} |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
77 |
|
3898 | 78 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
79 |
static NSString *CellIdentifier = @"Cell"; |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
80 |
|
3903 | 81 |
EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
82 |
if (editableCell == nil) { |
|
83 |
editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
84 |
editableCell.delegate = self; |
|
85 |
} |
|
86 |
editableCell.tag = [indexPath row]; |
|
87 |
||
88 |
editableCell.textField.text = [[self.listOfSavegames objectAtIndex:[indexPath row]] stringByDeletingPathExtension]; |
|
89 |
editableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
|
90 |
||
91 |
UIImage *addImg = [UIImage imageWithContentsOfFile:@"plus.png"]; |
|
92 |
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeContactAdd]; |
|
93 |
customButton.tag = [indexPath row]; |
|
94 |
[customButton setImage:addImg forState:UIControlStateNormal]; |
|
95 |
[customButton addTarget:self action:@selector(duplicateEntry:) forControlEvents:UIControlEventTouchUpInside]; |
|
96 |
editableCell.editingAccessoryView = customButton; |
|
97 |
||
98 |
return (UITableViewCell *)editableCell; |
|
99 |
} |
|
100 |
/* |
|
101 |
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger) section { |
|
102 |
UITableViewCellEditingStyleInsert |
|
103 |
}*//* |
|
104 |
-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
105 |
return UITableViewCellEditingStyleInsert; |
|
106 |
}*/ |
|
3898 | 107 |
|
3903 | 108 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
109 |
NSUInteger row = [indexPath row]; |
|
110 |
[(EditableCellView *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]] save:nil]; |
|
111 |
||
112 |
NSString *saveName = [self.listOfSavegames objectAtIndex:row]; |
|
113 |
NSString *currentFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),saveName]; |
|
114 |
[[NSFileManager defaultManager] removeItemAtPath:currentFilePath error:nil]; |
|
115 |
[self.listOfSavegames removeObject:saveName]; |
|
116 |
||
117 |
[self.tableView reloadData]; |
|
118 |
} |
|
3898 | 119 |
|
3903 | 120 |
-(void) duplicateEntry:(id) sender { |
121 |
UIButton *button = (UIButton *)sender; |
|
122 |
NSUInteger row = button.tag; |
|
123 |
||
124 |
[(EditableCellView *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]] save:nil]; |
|
125 |
NSString *currentSaveName = [self.listOfSavegames objectAtIndex:row]; |
|
126 |
NSString *newSaveName = [[currentSaveName stringByDeletingPathExtension] stringByAppendingFormat:@" %d.hws",[self.listOfSavegames count]]; |
|
127 |
||
128 |
NSString *currentFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),currentSaveName]; |
|
129 |
NSString *newFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),newSaveName]; |
|
130 |
[[NSFileManager defaultManager] copyItemAtPath:currentFilePath toPath:newFilePath error:nil]; |
|
131 |
[self.listOfSavegames addObject:newSaveName]; |
|
132 |
[self.listOfSavegames sortUsingSelector:@selector(compare:)]; |
|
133 |
||
134 |
//[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[self.listOfSavegames indexOfObject:newSaveName] inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; |
|
135 |
[self.tableView reloadData]; |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
136 |
} |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
137 |
|
3898 | 138 |
#pragma mark - |
139 |
#pragma mark Table view delegate |
|
140 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
141 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
142 |
||
3903 | 143 |
[(EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath] save:nil]; |
144 |
||
3898 | 145 |
NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:[indexPath row]]]; |
146 |
||
147 |
NSDictionary *allDataNecessary = [NSDictionary dictionaryWithObjectsAndKeys: |
|
148 |
[NSDictionary dictionary],@"game_dictionary", |
|
149 |
filePath,@"savefile", |
|
150 |
[NSNumber numberWithBool:NO],@"netgame", |
|
151 |
nil]; |
|
152 |
[[SDLUIKitDelegate sharedAppDelegate] startSDLgame:allDataNecessary]; |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
153 |
} |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
154 |
|
3898 | 155 |
#pragma mark - |
3903 | 156 |
#pragma mark editableCellView delegate |
157 |
// rename old file if names differ |
|
158 |
-(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue { |
|
159 |
NSString *oldFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:tagValue]]; |
|
160 |
NSString *newFilePath = [NSString stringWithFormat:@"%@/%@.hws",SAVES_DIRECTORY(),textString]; |
|
161 |
||
162 |
if ([oldFilePath isEqualToString:newFilePath] == NO) { |
|
163 |
[[NSFileManager defaultManager] moveItemAtPath:oldFilePath toPath:newFilePath error:nil]; |
|
164 |
[self.listOfSavegames replaceObjectAtIndex:tagValue withObject:[textString stringByAppendingString:@".hws"]]; |
|
165 |
} |
|
166 |
||
167 |
} |
|
168 |
||
169 |
#pragma mark - |
|
3898 | 170 |
#pragma mark Memory Management |
171 |
-(void) didReceiveMemoryWarning { |
|
172 |
[super didReceiveMemoryWarning]; |
|
173 |
} |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
174 |
|
3898 | 175 |
-(void) viewDidUnload { |
176 |
self.tableView = nil; |
|
177 |
self.listOfSavegames = nil; |
|
178 |
[super viewDidUnload]; |
|
179 |
} |
|
180 |
||
181 |
-(void) dealloc { |
|
182 |
[tableView release]; |
|
183 |
[listOfSavegames release]; |
|
3893
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
184 |
[super dealloc]; |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
185 |
} |
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
186 |
|
568bfd083465
allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff
changeset
|
187 |
@end |