project_files/HedgewarsMobile/Classes/WeaponSettingsViewController.m
author koda
Sat, 10 Jul 2010 18:42:47 +0200
changeset 3635 38d3e31556d3
parent 3548 4d220ee7c75f
child 3659 f8d5ac50e307
permissions -rw-r--r--
improvements to touch interface (tap to select weap, don't move camera for spourious taps, ask for confirmation when using click-weapons)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     1
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     2
//  WeaponSettingsViewController.m
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     3
//  HedgewarsMobile
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     4
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     5
//  Created by Vittorio on 19/04/10.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     7
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     8
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     9
#import "WeaponSettingsViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    10
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    11
#import "SingleWeaponViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    12
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    13
@implementation WeaponSettingsViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    14
@synthesize listOfWeapons;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    15
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    16
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    17
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    18
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
#pragma mark View lifecycle
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
-(void) viewDidLoad {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
    [super viewDidLoad];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
    UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"from the weapon panel")
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
                                                                   style:UIBarButtonItemStyleBordered
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
                                                                  target:self
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
                                                                  action:@selector(toggleEdit:)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
    self.navigationItem.rightBarButtonItem = editButton;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
    [editButton release];
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
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
-(void) viewWillAppear:(BOOL) animated {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
    [super viewWillAppear:animated];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
    NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
    self.listOfWeapons = array;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
    [array release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
// modifies the navigation bar to add the "Add" and "Done" buttons
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
-(void) toggleEdit:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
    BOOL isEditing = self.tableView.editing;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
    [self.tableView setEditing:!isEditing animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
    if (isEditing) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
        [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the scheme panel")];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
        [self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
        self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
        [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the scheme panel")];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
        [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
        UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the scheme panel")
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
                                                                      style:UIBarButtonItemStyleBordered
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
                                                                     target:self
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
                                                                     action:@selector(addWeapon:)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
        self.navigationItem.leftBarButtonItem = addButton;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
        [addButton release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
-(void) addWeapon:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
    NSString *fileName = [[NSString alloc] initWithFormat:@"Weapon %u.plist", [self.listOfWeapons count]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
    createWeaponNamed([fileName stringByDeletingPathExtension]);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
    [self.listOfWeapons addObject:fileName];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
    [fileName release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
    // order the array alphabetically, so schemes will keep their position
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
    [self.listOfWeapons sortUsingSelector:@selector(compare:)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
    return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
    return [self.listOfWeapons count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
    static NSString *CellIdentifier = @"Cell";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
    if (cell == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
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
    NSUInteger row = [indexPath row]; 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
    NSString *rowString = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension]; 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
    cell.textLabel.text = rowString; 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
// delete the row and the file
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
    NSUInteger row = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
    NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
    [[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
    [schemeFile release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
    [self.listOfWeapons removeObjectAtIndex:row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
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
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
    if (childController == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
        childController = [[SingleWeaponViewController alloc] initWithStyle:UITableViewStyleGrouped];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
    NSInteger row = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
    NSString *selectedWeaponFile = [self.listOfWeapons objectAtIndex:row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
    // this must be set so childController can load the correct plist
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
    childController.title = [selectedWeaponFile stringByDeletingPathExtension];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
    [childController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
    [self.navigationController pushViewController:childController animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
-(void)didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
    if (childController.view.superview == nil )
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
        childController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
    self.listOfWeapons = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
    childController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
    MSG_DIDUNLOAD();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
    [self.listOfWeapons release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
    [childController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160