project_files/HedgewarsMobile/Classes/WeaponSettingsViewController.m
changeset 3697 d5b30d6373fc
parent 3662 a44406f4369b
child 3737 2ba6ac8a114b
equal deleted inserted replaced
3695:c11abf387a7d 3697:d5b30d6373fc
    19 
    19 
    20 #pragma mark -
    20 #pragma mark -
    21 #pragma mark View lifecycle
    21 #pragma mark View lifecycle
    22 -(void) viewDidLoad {
    22 -(void) viewDidLoad {
    23     [super viewDidLoad];
    23     [super viewDidLoad];
    24     
    24 
    25     UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"from the weapon panel")
    25     UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"from the weapon panel")
    26                                                                    style:UIBarButtonItemStyleBordered
    26                                                                    style:UIBarButtonItemStyleBordered
    27                                                                   target:self
    27                                                                   target:self
    28                                                                   action:@selector(toggleEdit:)];
    28                                                                   action:@selector(toggleEdit:)];
    29     self.navigationItem.rightBarButtonItem = editButton;
    29     self.navigationItem.rightBarButtonItem = editButton;
    30     [editButton release];
    30     [editButton release];
    31     
    31 
    32 }
    32 }
    33 
    33 
    34 -(void) viewWillAppear:(BOOL) animated {
    34 -(void) viewWillAppear:(BOOL) animated {
    35     [super viewWillAppear:animated];
    35     [super viewWillAppear:animated];
    36     
    36 
    37     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL];
    37     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL];
    38     NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
    38     NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
    39     self.listOfWeapons = array;
    39     self.listOfWeapons = array;
    40     [array release];
    40     [array release];
    41     
    41 
    42     [self.tableView reloadData];
    42     [self.tableView reloadData];
    43 }
    43 }
    44 
    44 
    45 // modifies the navigation bar to add the "Add" and "Done" buttons
    45 // modifies the navigation bar to add the "Add" and "Done" buttons
    46 -(void) toggleEdit:(id) sender {
    46 -(void) toggleEdit:(id) sender {
    47     BOOL isEditing = self.tableView.editing;
    47     BOOL isEditing = self.tableView.editing;
    48     [self.tableView setEditing:!isEditing animated:YES];
    48     [self.tableView setEditing:!isEditing animated:YES];
    49     
    49 
    50     if (isEditing) {
    50     if (isEditing) {
    51         [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the scheme panel")];
    51         [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the scheme panel")];
    52         [self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered];
    52         [self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered];
    53         self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
    53         self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
    54     } else {
    54     } else {
    63     }
    63     }
    64 }
    64 }
    65 
    65 
    66 -(void) addWeapon:(id) sender {
    66 -(void) addWeapon:(id) sender {
    67     NSString *fileName = [[NSString alloc] initWithFormat:@"Weapon %u.plist", [self.listOfWeapons count]];
    67     NSString *fileName = [[NSString alloc] initWithFormat:@"Weapon %u.plist", [self.listOfWeapons count]];
    68     
    68 
    69     createWeaponNamed([fileName stringByDeletingPathExtension]);
    69     createWeaponNamed([fileName stringByDeletingPathExtension]);
    70     
    70 
    71     [self.listOfWeapons addObject:fileName];
    71     [self.listOfWeapons addObject:fileName];
    72     [fileName release];
    72     [fileName release];
    73     
    73 
    74     // order the array alphabetically, so schemes will keep their position
    74     // order the array alphabetically, so schemes will keep their position
    75     [self.listOfWeapons sortUsingSelector:@selector(compare:)];
    75     [self.listOfWeapons sortUsingSelector:@selector(compare:)];
    76     
    76 
    77     [self.tableView reloadData];
    77     [self.tableView reloadData];
    78 }
    78 }
    79 
    79 
    80 #pragma mark -
    80 #pragma mark -
    81 #pragma mark Table view data source
    81 #pragma mark Table view data source
    87     return [self.listOfWeapons count];
    87     return [self.listOfWeapons count];
    88 }
    88 }
    89 
    89 
    90 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    90 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    91     static NSString *CellIdentifier = @"Cell";
    91     static NSString *CellIdentifier = @"Cell";
    92     
    92 
    93     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    93     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    94     if (cell == nil) {
    94     if (cell == nil) {
    95         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    95         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    96     }
    96     }
    97     
    97 
    98     NSUInteger row = [indexPath row]; 
    98     NSUInteger row = [indexPath row];
    99     NSString *rowString = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension]; 
    99     NSString *rowString = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension];
   100     cell.textLabel.text = rowString; 
   100     cell.textLabel.text = rowString;
   101     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   101     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   102     
   102 
   103     return cell;
   103     return cell;
   104 }
   104 }
   105 
   105 
   106 // delete the row and the file
   106 // delete the row and the file
   107 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   107 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   108     NSUInteger row = [indexPath row];
   108     NSUInteger row = [indexPath row];
   109     
   109 
   110     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]];
   110     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]];
   111     [[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL];
   111     [[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL];
   112     [schemeFile release];
   112     [schemeFile release];
   113     
   113 
   114     [self.listOfWeapons removeObjectAtIndex:row];
   114     [self.listOfWeapons removeObjectAtIndex:row];
   115     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
   115     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
   116 }
   116 }
   117 
   117 
   118 #pragma mark -
   118 #pragma mark -
   119 #pragma mark Table view delegate
   119 #pragma mark Table view delegate
   120 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   120 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   121     if (childController == nil) {
   121     if (childController == nil) {
   122         childController = [[SingleWeaponViewController alloc] initWithStyle:UITableViewStyleGrouped];
   122         childController = [[SingleWeaponViewController alloc] initWithStyle:UITableViewStyleGrouped];
   123     }
   123     }
   124     
   124 
   125     NSInteger row = [indexPath row];
   125     NSInteger row = [indexPath row];
   126     NSString *selectedWeaponFile = [self.listOfWeapons objectAtIndex:row];
   126     NSString *selectedWeaponFile = [self.listOfWeapons objectAtIndex:row];
   127     
   127 
   128     // this must be set so childController can load the correct plist
   128     // this must be set so childController can load the correct plist
   129     childController.weaponName = [selectedWeaponFile stringByDeletingPathExtension];
   129     childController.weaponName = [selectedWeaponFile stringByDeletingPathExtension];
   130     [childController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
   130     [childController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
   131 
   131 
   132     [self.navigationController pushViewController:childController animated:YES];
   132     [self.navigationController pushViewController:childController animated:YES];