project_files/HedgewarsMobile/Classes/SavedGamesViewController.m
author koda
Wed, 02 Mar 2011 00:27:20 +0100
changeset 4976 088d40d8aba2
parent 4912 862b134284b2
child 5002 a9c44a8ffec8
permissions -rw-r--r--
Happy 2011 :)
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
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4912
diff changeset
     3
 * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
3893
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"
4763
c228a4841e3f unify stats display on ipad and non-ipad and on saved and normal games
koda
parents: 4476
diff changeset
    24
#import "StatsPageViewController.h"
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    25
#import "CommodityFunctions.h"
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    26
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    27
@implementation SavedGamesViewController
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    28
@synthesize tableView, listOfSavegames;
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    29
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    30
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    31
    return rotationManager(interfaceOrientation);
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    32
}
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    33
3923
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3904
diff changeset
    34
-(void) updateTable {
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3904
diff changeset
    35
    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
    36
    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
    37
    self.listOfSavegames = array;
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3904
diff changeset
    38
    [array release];
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3904
diff changeset
    39
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3904
diff changeset
    40
    [self.tableView reloadData];
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3904
diff changeset
    41
}
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3904
diff changeset
    42
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    43
-(void) viewDidLoad {
4115
222b8016c773 make the support page more portable
koda
parents: 4082
diff changeset
    44
    if ([self.tableView respondsToSelector:@selector(setBackgroundView:)])
4082
bfe14b38dad1 better support for wimpier i-devices
koda
parents: 4030
diff changeset
    45
        self.tableView.backgroundView = nil;
bfe14b38dad1 better support for wimpier i-devices
koda
parents: 4030
diff changeset
    46
bfe14b38dad1 better support for wimpier i-devices
koda
parents: 4030
diff changeset
    47
    NSString *imgName;
4030
adbb37b8c487 fix background for the about and saves controller
koda
parents: 3996
diff changeset
    48
    if (IS_IPAD())
adbb37b8c487 fix background for the about and saves controller
koda
parents: 3996
diff changeset
    49
        imgName = @"mediumBackground~ipad.png";
adbb37b8c487 fix background for the about and saves controller
koda
parents: 3996
diff changeset
    50
    else
adbb37b8c487 fix background for the about and saves controller
koda
parents: 3996
diff changeset
    51
        imgName = @"smallerBackground~iphone.png";
adbb37b8c487 fix background for the about and saves controller
koda
parents: 3996
diff changeset
    52
    UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgName];
adbb37b8c487 fix background for the about and saves controller
koda
parents: 3996
diff changeset
    53
    self.view.backgroundColor = [UIColor colorWithPatternImage:img];
adbb37b8c487 fix background for the about and saves controller
koda
parents: 3996
diff changeset
    54
    [img release];
4082
bfe14b38dad1 better support for wimpier i-devices
koda
parents: 4030
diff changeset
    55
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    56
    [super viewDidLoad];
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    57
}
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    58
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    59
-(void) viewWillAppear:(BOOL)animated {
3978
9660600e43cb fix some glitches
koda
parents: 3971
diff changeset
    60
    [self updateTable];
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    61
    [super viewWillAppear:animated];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    62
}
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
    63
3923
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3904
diff changeset
    64
#pragma mark -
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3904
diff changeset
    65
#pragma mark button functions
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    66
-(IBAction) buttonPressed:(id) sender {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    67
    playSound(@"backSound");
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
    68
    [self.tableView setEditing:NO animated:YES];
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    69
    [[self parentViewController] dismissModalViewControllerAnimated:YES];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    70
}
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
    71
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
    72
-(IBAction) toggleEdit:(id) sender {
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
    73
    BOOL isEditing = self.tableView.editing;
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
    74
    [self.tableView setEditing:!isEditing animated:YES];
3971
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    75
}
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    76
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    77
-(void) duplicateEntry:(id) sender {
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    78
    UIButton *button = (UIButton *)sender;
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    79
    NSUInteger row = button.tag;
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    80
    
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    81
    if (self.listOfSavegames == nil)
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    82
        [self updateTable];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    83
    
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    84
    [(EditableCellView *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]] save:nil];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    85
    NSString *currentSaveName = [self.listOfSavegames objectAtIndex:row];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    86
    NSString *newSaveName = [[currentSaveName stringByDeletingPathExtension] stringByAppendingFormat:@" %d.hws",[self.listOfSavegames count]];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    87
    
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    88
    NSString *currentFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),currentSaveName];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    89
    NSString *newFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),newSaveName];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    90
    [[NSFileManager defaultManager] copyItemAtPath:currentFilePath toPath:newFilePath error:nil];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    91
    [self.listOfSavegames addObject:newSaveName];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    92
    [self.listOfSavegames sortUsingSelector:@selector(compare:)];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    93
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    94
    [self.tableView reloadData];
3923
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3904
diff changeset
    95
}
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3904
diff changeset
    96
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3904
diff changeset
    97
-(IBAction) clearAll:(id) sender {
3971
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
    98
    NSString *titleStr, *cancelStr, *confirmStr;
3996
eb549fd864a5 the ios port is also iphone compatible
koda
parents: 3978
diff changeset
    99
    if (IS_IPAD()) {
3971
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   100
        titleStr = nil;
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   101
        cancelStr = nil;
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   102
        confirmStr = NSLocalizedString(@"Tap to confirm",@"");
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   103
    } else {
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   104
        titleStr = NSLocalizedString(@"Are you reeeeeally sure?", @"");
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   105
        cancelStr = NSLocalizedString(@"Well, maybe not...", @"");
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   106
        confirmStr = NSLocalizedString(@"Of course!", @"");
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   107
    }
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   108
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   109
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:titleStr
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   110
                                                             delegate:self
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   111
                                                    cancelButtonTitle:cancelStr
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   112
                                               destructiveButtonTitle:confirmStr
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   113
                                                    otherButtonTitles:nil];
4362
8dae325dc625 added missing graphics and fixed some glitches/crashes/bugs
koda
parents: 4115
diff changeset
   114
8dae325dc625 added missing graphics and fixed some glitches/crashes/bugs
koda
parents: 4115
diff changeset
   115
    if (IS_IPAD())
8dae325dc625 added missing graphics and fixed some glitches/crashes/bugs
koda
parents: 4115
diff changeset
   116
        [actionSheet showFromBarButtonItem:(UIBarButtonItem *)sender animated:YES];
8dae325dc625 added missing graphics and fixed some glitches/crashes/bugs
koda
parents: 4115
diff changeset
   117
    else
8dae325dc625 added missing graphics and fixed some glitches/crashes/bugs
koda
parents: 4115
diff changeset
   118
        [actionSheet showInView:self.view];
3971
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   119
    [actionSheet release];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   120
}
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   121
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   122
-(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   123
    if ([actionSheet cancelButtonIndex] != buttonIndex) {
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   124
        [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   125
        [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   126
        [self updateTable];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   127
    }
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   128
}
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   129
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   130
#pragma mark -
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   131
#pragma mark Table view data source
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   132
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   133
    return 1;
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   134
}
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   135
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   136
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
3971
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   137
    if (self.listOfSavegames == nil)
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   138
        [self updateTable];
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   139
    return [self.listOfSavegames count];
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
   140
}
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
   141
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   142
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   143
    static NSString *CellIdentifier = @"Cell";
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
   144
3971
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   145
    if (self.listOfSavegames == nil)
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   146
        [self updateTable];
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   147
    EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   148
    if (editableCell == nil) {
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   149
        editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   150
        editableCell.delegate = self;
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   151
    }
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   152
    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
   153
    editableCell.respectEditing = YES;
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   154
    editableCell.textField.text = [[self.listOfSavegames objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   155
    editableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   156
    
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   157
    UIImage *addImg = [UIImage imageWithContentsOfFile:@"plus.png"];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   158
    UIButton *customButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   159
    customButton.tag = [indexPath row];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   160
    [customButton setImage:addImg forState:UIControlStateNormal];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   161
    [customButton addTarget:self action:@selector(duplicateEntry:) forControlEvents:UIControlEventTouchUpInside];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   162
    editableCell.editingAccessoryView = customButton;
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   163
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   164
    return (UITableViewCell *)editableCell;
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   165
}
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
   166
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   167
-(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
   168
    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
   169
    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
   170
    
22e4d74240e5 finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents: 3903
diff changeset
   171
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width*80/100, 100)];
4115
222b8016c773 make the support page more portable
koda
parents: 4082
diff changeset
   172
    label.center = CGPointMake(self.tableView.frame.size.width/2, 70);
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
   173
    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
   174
    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
   175
    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
   176
    label.numberOfLines = 5;
4476
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4362
diff changeset
   177
    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.",@"");
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
   178
22e4d74240e5 finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents: 3903
diff changeset
   179
    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
   180
    [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
   181
    [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
   182
    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
   183
}
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   184
4115
222b8016c773 make the support page more portable
koda
parents: 4082
diff changeset
   185
-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
222b8016c773 make the support page more portable
koda
parents: 4082
diff changeset
   186
    return 125;
222b8016c773 make the support page more portable
koda
parents: 4082
diff changeset
   187
}
222b8016c773 make the support page more portable
koda
parents: 4082
diff changeset
   188
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   189
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   190
    NSUInteger row = [indexPath row];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   191
    [(EditableCellView *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]] save:nil];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   192
    
3971
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   193
    if (self.listOfSavegames == nil)
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   194
        [self updateTable];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   195
    
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   196
    NSString *saveName = [self.listOfSavegames objectAtIndex:row];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   197
    NSString *currentFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),saveName];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   198
    [[NSFileManager defaultManager] removeItemAtPath:currentFilePath error:nil];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   199
    [self.listOfSavegames removeObject:saveName];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   200
    
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   201
    [self.tableView reloadData];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   202
}
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   203
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   204
#pragma mark -
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   205
#pragma mark Table view delegate
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   206
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   207
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
3971
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   208
    if (self.listOfSavegames == nil)
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   209
        [self updateTable];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   210
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   211
    [(EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath] save:nil];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   212
    
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   213
    NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:[indexPath row]]];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   214
    
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   215
    NSDictionary *allDataNecessary = [NSDictionary dictionaryWithObjectsAndKeys:
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   216
                                      filePath,@"savefile",
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   217
                                      [NSNumber numberWithBool:NO],@"netgame",
3978
9660600e43cb fix some glitches
koda
parents: 3971
diff changeset
   218
                                      [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:self.interfaceOrientation] forKey:@"orientation"],@"game_dictionary",
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   219
                                      nil];
4763
c228a4841e3f unify stats display on ipad and non-ipad and on saved and normal games
koda
parents: 4476
diff changeset
   220
4861
91f889289a47 (ios) perform a small change about how to close the game window, should hopefully save memory and avoid crashes (who am i kidding? that's the usual PR talk...)
koda
parents: 4856
diff changeset
   221
    // also modify GameConfigViewController.m
4763
c228a4841e3f unify stats display on ipad and non-ipad and on saved and normal games
koda
parents: 4476
diff changeset
   222
    StatsPageViewController *statsPage = [[StatsPageViewController alloc] initWithStyle:UITableViewStyleGrouped];
c228a4841e3f unify stats display on ipad and non-ipad and on saved and normal games
koda
parents: 4476
diff changeset
   223
    statsPage.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
c228a4841e3f unify stats display on ipad and non-ipad and on saved and normal games
koda
parents: 4476
diff changeset
   224
    if ([statsPage respondsToSelector:@selector(setModalPresentationStyle:)])
c228a4841e3f unify stats display on ipad and non-ipad and on saved and normal games
koda
parents: 4476
diff changeset
   225
        statsPage.modalPresentationStyle = UIModalPresentationPageSheet;
4861
91f889289a47 (ios) perform a small change about how to close the game window, should hopefully save memory and avoid crashes (who am i kidding? that's the usual PR talk...)
koda
parents: 4856
diff changeset
   226
    // avoid showing the stat page immediately, but wait for 3 seconds
91f889289a47 (ios) perform a small change about how to close the game window, should hopefully save memory and avoid crashes (who am i kidding? that's the usual PR talk...)
koda
parents: 4856
diff changeset
   227
    [self performSelector:@selector(presentModalViewController:animated:) withObject:statsPage afterDelay:3];
4763
c228a4841e3f unify stats display on ipad and non-ipad and on saved and normal games
koda
parents: 4476
diff changeset
   228
4912
862b134284b2 change order of statspage/gamerun when running in dual screen
koda
parents: 4861
diff changeset
   229
    NSArray *stats;
862b134284b2 change order of statspage/gamerun when running in dual screen
koda
parents: 4861
diff changeset
   230
    if (IS_DUALHEAD()) {
862b134284b2 change order of statspage/gamerun when running in dual screen
koda
parents: 4861
diff changeset
   231
        stats = [[SDLUIKitDelegate sharedAppDelegate] startSDLgame:allDataNecessary];
862b134284b2 change order of statspage/gamerun when running in dual screen
koda
parents: 4861
diff changeset
   232
        [self presentModalViewController:statsPage animated:NO];
862b134284b2 change order of statspage/gamerun when running in dual screen
koda
parents: 4861
diff changeset
   233
    } else {
862b134284b2 change order of statspage/gamerun when running in dual screen
koda
parents: 4861
diff changeset
   234
        [self performSelector:@selector(presentModalViewController:animated:) withObject:statsPage afterDelay:3];
862b134284b2 change order of statspage/gamerun when running in dual screen
koda
parents: 4861
diff changeset
   235
        stats = [[SDLUIKitDelegate sharedAppDelegate] startSDLgame:allDataNecessary];
862b134284b2 change order of statspage/gamerun when running in dual screen
koda
parents: 4861
diff changeset
   236
    }
862b134284b2 change order of statspage/gamerun when running in dual screen
koda
parents: 4861
diff changeset
   237
4856
dbc256913b35 implemented ranking in ios stats pages
koda
parents: 4763
diff changeset
   238
    if ([stats count] <= 1) {
dbc256913b35 implemented ranking in ios stats pages
koda
parents: 4763
diff changeset
   239
        DLog(@"%@",stats);
4763
c228a4841e3f unify stats display on ipad and non-ipad and on saved and normal games
koda
parents: 4476
diff changeset
   240
        [statsPage dismissModalViewControllerAnimated:NO];
c228a4841e3f unify stats display on ipad and non-ipad and on saved and normal games
koda
parents: 4476
diff changeset
   241
    } else {
c228a4841e3f unify stats display on ipad and non-ipad and on saved and normal games
koda
parents: 4476
diff changeset
   242
        statsPage.statsArray = stats;
c228a4841e3f unify stats display on ipad and non-ipad and on saved and normal games
koda
parents: 4476
diff changeset
   243
        [statsPage.tableView reloadData];
c228a4841e3f unify stats display on ipad and non-ipad and on saved and normal games
koda
parents: 4476
diff changeset
   244
        [statsPage viewWillAppear:YES];
c228a4841e3f unify stats display on ipad and non-ipad and on saved and normal games
koda
parents: 4476
diff changeset
   245
    }
c228a4841e3f unify stats display on ipad and non-ipad and on saved and normal games
koda
parents: 4476
diff changeset
   246
    // reload needed because when ending game the entry remains there
c228a4841e3f unify stats display on ipad and non-ipad and on saved and normal games
koda
parents: 4476
diff changeset
   247
    [self.tableView reloadData];
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
   248
}
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
   249
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   250
#pragma mark -
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   251
#pragma mark editableCellView delegate
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   252
// rename old file if names differ
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   253
-(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {
3971
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   254
    if (self.listOfSavegames == nil)
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   255
        [self updateTable];
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   256
    NSString *oldFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:tagValue]];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   257
    NSString *newFilePath = [NSString stringWithFormat:@"%@/%@.hws",SAVES_DIRECTORY(),textString];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   258
    
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   259
    if ([oldFilePath isEqualToString:newFilePath] == NO) {
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   260
        [[NSFileManager defaultManager] moveItemAtPath:oldFilePath toPath:newFilePath error:nil];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   261
        [self.listOfSavegames replaceObjectAtIndex:tagValue withObject:[textString stringByAppendingString:@".hws"]];
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   262
    }
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   263
    
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   264
}
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   265
db01c37494af pimp-my-savagame-table
koda
parents: 3898
diff changeset
   266
#pragma mark -
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   267
#pragma mark Memory Management
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   268
-(void) didReceiveMemoryWarning {
3971
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   269
    self.listOfSavegames = nil;
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   270
    MSG_MEMCLEAN();
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   271
    [super didReceiveMemoryWarning];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   272
}
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
   273
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   274
-(void) viewDidUnload {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   275
    self.tableView = nil;
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   276
    self.listOfSavegames = nil;
3971
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   277
    MSG_DIDUNLOAD();
3898
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   278
    [super viewDidUnload];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   279
}
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   280
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   281
-(void) dealloc {
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   282
    [tableView release];
0a9c3735a713 implemented reloading savefiles
koda
parents: 3893
diff changeset
   283
    [listOfSavegames release];
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
   284
    [super dealloc];
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
   285
}
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
   286
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents:
diff changeset
   287
@end