project_files/HedgewarsMobile/Classes/SavedGamesViewController.m
changeset 3903 db01c37494af
parent 3898 0a9c3735a713
child 3904 22e4d74240e5
equal deleted inserted replaced
3902:3aac7ca07b0e 3903:db01c37494af
    47     [self.tableView reloadData];    
    47     [self.tableView reloadData];    
    48 }
    48 }
    49 
    49 
    50 -(IBAction) buttonPressed:(id) sender {
    50 -(IBAction) buttonPressed:(id) sender {
    51     playSound(@"backSound");
    51     playSound(@"backSound");
       
    52     [self.tableView setEditing:NO animated:YES];
    52     [[self parentViewController] dismissModalViewControllerAnimated:YES];
    53     [[self parentViewController] dismissModalViewControllerAnimated:YES];
       
    54 }
       
    55 
       
    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",@"")];
    53 }
    66 }
    54 
    67 
    55 #pragma mark -
    68 #pragma mark -
    56 #pragma mark Table view data source
    69 #pragma mark Table view data source
    57 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    70 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    63 }
    76 }
    64 
    77 
    65 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    78 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    66     static NSString *CellIdentifier = @"Cell";
    79     static NSString *CellIdentifier = @"Cell";
    67 
    80 
    68     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    81     EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    69     if (cell == nil)
    82     if (editableCell == nil) {
    70         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    83         editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
       
    84         editableCell.delegate = self;
       
    85     }
       
    86     editableCell.tag = [indexPath row];
    71 
    87 
    72     // first all the names, then the title (which is offset 5)
    88     editableCell.textField.text = [[self.listOfSavegames objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
    73     cell.textLabel.text = [[self.listOfSavegames objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
    89     editableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    74     cell.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;
    75 
    97 
    76     return cell;
    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 }*/
       
   107 
       
   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 }
       
   119 
       
   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];
    77 }
   136 }
    78 
   137 
    79 #pragma mark -
   138 #pragma mark -
    80 #pragma mark Table view delegate
   139 #pragma mark Table view delegate
    81 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   140 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    82     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
   141     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
       
   142     
       
   143     [(EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath] save:nil];
    83     
   144     
    84     NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:[indexPath row]]];
   145     NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:[indexPath row]]];
    85     
   146     
    86     NSDictionary *allDataNecessary = [NSDictionary dictionaryWithObjectsAndKeys:
   147     NSDictionary *allDataNecessary = [NSDictionary dictionaryWithObjectsAndKeys:
    87                                       [NSDictionary dictionary],@"game_dictionary",
   148                                       [NSDictionary dictionary],@"game_dictionary",
    88                                       filePath,@"savefile",
   149                                       filePath,@"savefile",
    89                                       [NSNumber numberWithBool:NO],@"netgame",
   150                                       [NSNumber numberWithBool:NO],@"netgame",
    90                                       nil];
   151                                       nil];
    91     [[SDLUIKitDelegate sharedAppDelegate] startSDLgame:allDataNecessary];
   152     [[SDLUIKitDelegate sharedAppDelegate] startSDLgame:allDataNecessary];
       
   153 }
       
   154 
       
   155 #pragma mark -
       
   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     
    92 }
   167 }
    93 
   168 
    94 #pragma mark -
   169 #pragma mark -
    95 #pragma mark Memory Management
   170 #pragma mark Memory Management
    96 -(void) didReceiveMemoryWarning {
   171 -(void) didReceiveMemoryWarning {