project_files/HedgewarsMobile/Classes/SavedGamesViewController.m
author koda
Thu, 23 Sep 2010 01:29:20 +0200
changeset 3898 0a9c3735a713
parent 3893 568bfd083465
child 3903 db01c37494af
permissions -rw-r--r--
implemented reloading savefiles
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    23
#import "SDL_uikitappdelegate.h"
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    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
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    27
@synthesize tableView, listOfSavegames;
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    28
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    29
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    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
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    33
-(void) viewDidLoad {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    34
    self.tableView.backgroundView = nil;
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    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
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    38
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    39
-(void) viewWillAppear:(BOOL)animated {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    40
    [super viewWillAppear:animated];
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    41
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    42
    NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SAVES_DIRECTORY() error:NULL];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    43
    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    44
    self.listOfSavegames = array;
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    45
    [array release];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    46
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    47
    [self.tableView reloadData];    
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    48
}
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    49
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    50
-(IBAction) buttonPressed:(id) sender {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    51
    playSound(@"backSound");
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    52
    [[self parentViewController] dismissModalViewControllerAnimated:YES];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    53
}
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    54
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    55
#pragma mark -
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    56
#pragma mark Table view data source
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    57
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    58
    return 1;
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    59
}
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    60
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    61
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    62
    return [self.listOfSavegames count];
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    63
}
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    64
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    65
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    66
    static NSString *CellIdentifier = @"Cell";
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    67
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    68
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    69
    if (cell == nil)
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    70
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    71
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    72
    // first all the names, then the title (which is offset 5)
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    73
    cell.textLabel.text = [[self.listOfSavegames objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    74
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    75
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    76
    return cell;
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    77
}
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    78
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    79
#pragma mark -
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    80
#pragma mark Table view delegate
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    81
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    82
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    83
    
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    84
    NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:[indexPath row]]];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    85
    
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    86
    NSDictionary *allDataNecessary = [NSDictionary dictionaryWithObjectsAndKeys:
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    87
                                      [NSDictionary dictionary],@"game_dictionary",
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    88
                                      filePath,@"savefile",
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    89
                                      [NSNumber numberWithBool:NO],@"netgame",
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    90
                                      nil];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    91
    [[SDLUIKitDelegate sharedAppDelegate] startSDLgame:allDataNecessary];
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    92
}
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    93
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    94
#pragma mark -
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    95
#pragma mark Memory Management
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    96
-(void) didReceiveMemoryWarning {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    97
    [super didReceiveMemoryWarning];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    98
}
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    99
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   100
-(void) viewDidUnload {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   101
    self.tableView = nil;
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   102
    self.listOfSavegames = nil;
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   103
    [super viewDidUnload];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   104
}
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   105
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   106
-(void) dealloc {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   107
    [tableView release];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   108
    [listOfSavegames release];
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
   109
    [super dealloc];
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
   110
}
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
   111
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
   112
@end