project_files/HedgewarsMobile/Classes/SchemeSettingsViewController.m
author koda
Mon, 01 Nov 2010 18:51:38 +0100
changeset 4082 bfe14b38dad1
parent 3916 e7d665a4ef42
child 4281 e033cf015b2c
permissions -rw-r--r--
better support for wimpier i-devices
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     1
/*
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     3
 * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com>
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     4
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     8
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    12
 * GNU General Public License for more details.
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    13
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    14
 * You should have received a copy of the GNU General Public License
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    15
 * along with this program; if not, write to the Free Software
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    17
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    18
 * File created on 19/04/2010.
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    19
 */
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    20
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
#import "SchemeSettingsViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
#import "SingleSchemeViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
@implementation SchemeSettingsViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
@synthesize listOfSchemes;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
#pragma mark View lifecycle
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
-(void) viewDidLoad {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
    [super viewDidLoad];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    37
3916
e7d665a4ef42 implemented endless turns support and added Timeless scheme (also fixed a crasher)
koda
parents: 3903
diff changeset
    38
    UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"")
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
                                                                   style:UIBarButtonItemStyleBordered
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
                                                                  target:self
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
                                                                  action:@selector(toggleEdit:)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
    self.navigationItem.rightBarButtonItem = editButton;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
    [editButton release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
-(void) viewWillAppear:(BOOL) animated {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
    [super viewWillAppear:animated];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    48
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
    NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCHEMES_DIRECTORY() error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
    self.listOfSchemes = array;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
    [array release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    53
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
// modifies the navigation bar to add the "Add" and "Done" buttons
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
-(void) toggleEdit:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
    BOOL isEditing = self.tableView.editing;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
    [self.tableView setEditing:!isEditing animated:YES];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    61
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
    if (isEditing) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
        [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the scheme panel")];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
        [self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
        self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
        [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the scheme panel")];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
        [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
        UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the scheme panel")
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
                                                                      style:UIBarButtonItemStyleBordered
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
                                                                     target:self
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
                                                                     action:@selector(addScheme:)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
        self.navigationItem.leftBarButtonItem = addButton;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
        [addButton release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
-(void) addScheme:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
    NSString *fileName = [[NSString alloc] initWithFormat:@"Scheme %u.plist", [self.listOfSchemes count]];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    80
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
    createSchemeNamed([fileName stringByDeletingPathExtension]);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    82
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
    [self.listOfSchemes addObject:fileName];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
    [fileName release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    85
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
    // order the array alphabetically, so schemes will keep their position
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
    [self.listOfSchemes sortUsingSelector:@selector(compare:)];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    88
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
    return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
    return [self.listOfSchemes count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
    static NSString *CellIdentifier = @"Cell";
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   104
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
    if (cell == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   109
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   110
    NSUInteger row = [indexPath row];
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   111
    NSString *rowString = [[self.listOfSchemes objectAtIndex:row] stringByDeletingPathExtension];
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   112
    cell.textLabel.text = rowString;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   114
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
// delete the row and the file
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
    NSUInteger row = [indexPath row];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   121
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
    NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),[self.listOfSchemes objectAtIndex:row]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
    [[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
    [schemeFile release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   125
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
    [self.listOfSchemes removeObjectAtIndex:row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
    if (childController == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
        childController = [[SingleSchemeViewController alloc] initWithStyle:UITableViewStyleGrouped];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   136
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
    NSInteger row = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
    NSString *selectedSchemeFile = [self.listOfSchemes objectAtIndex:row];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   139
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
    // this must be set so childController can load the correct plist
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents: 3548
diff changeset
   141
    childController.schemeName = [selectedSchemeFile stringByDeletingPathExtension];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
    [childController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
    [self.navigationController pushViewController:childController animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
-(void)didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
    if (childController.view.superview == nil )
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
        childController = nil;
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents: 3548
diff changeset
   154
    MSG_MEMCLEAN();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
    self.listOfSchemes = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
    childController = nil;
3662
a44406f4369b polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents: 3659
diff changeset
   160
    MSG_DIDUNLOAD();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   163
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   164
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   165
-(void) dealloc {
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents: 3548
diff changeset
   166
    [listOfSchemes release];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167
    [childController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   168
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   170
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   171
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   172
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   173