project_files/HedgewarsMobile/Classes/WeaponSettingsViewController.m
branchios-develop
changeset 12872 00215a7ec5f5
parent 11229 b49dfdf628f6
equal deleted inserted replaced
12871:2c06b1120749 12872:00215a7ec5f5
    22 
    22 
    23 
    23 
    24 @implementation WeaponSettingsViewController
    24 @implementation WeaponSettingsViewController
    25 @synthesize listOfWeapons;
    25 @synthesize listOfWeapons;
    26 
    26 
    27 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    27 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    28     return rotationManager(interfaceOrientation);
    28     return rotationManager(interfaceOrientation);
    29 }
    29 }
    30 
    30 
    31 #pragma mark -
    31 #pragma mark -
    32 #pragma mark View lifecycle
    32 #pragma mark View lifecycle
    33 -(void) viewDidLoad {
    33 - (void)viewDidLoad {
    34     [super viewDidLoad];
    34     [super viewDidLoad];
    35 
    35 
    36     UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"")
    36     UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"")
    37                                                                    style:UIBarButtonItemStyleBordered
    37                                                                    style:UIBarButtonItemStyleBordered
    38                                                                   target:self
    38                                                                   target:self
    39                                                                   action:@selector(toggleEdit:)];
    39                                                                   action:@selector(toggleEdit:)];
    40     self.navigationItem.rightBarButtonItem = editButton;
    40     self.navigationItem.rightBarButtonItem = editButton;
    41     [editButton release];
       
    42 
    41 
    43     self.navigationItem.title = NSLocalizedString(@"List of weapons", nil);
    42     self.navigationItem.title = NSLocalizedString(@"List of weapons", nil);
    44 }
    43 }
    45 
    44 
    46 -(void) viewWillAppear:(BOOL) animated {
    45 - (void)viewWillAppear:(BOOL) animated {
    47     [super viewWillAppear:animated];
    46     [super viewWillAppear:animated];
    48 
    47 
    49     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL];
    48     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL];
    50     NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
    49     NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
    51     self.listOfWeapons = array;
    50     self.listOfWeapons = array;
    52     [array release];
       
    53 
    51 
    54     [self.tableView reloadData];
    52     [self.tableView reloadData];
    55 }
    53 }
    56 
    54 
    57 // modifies the navigation bar to add the "Add" and "Done" buttons
    55 // modifies the navigation bar to add the "Add" and "Done" buttons
    58 -(void) toggleEdit:(id) sender {
    56 - (void)toggleEdit:(id)sender {
    59     BOOL isEditing = self.tableView.editing;
    57     BOOL isEditing = self.tableView.editing;
    60     [self.tableView setEditing:!isEditing animated:YES];
    58     [self.tableView setEditing:!isEditing animated:YES];
    61 
    59 
    62     if (isEditing) {
    60     if (isEditing) {
    63         [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the scheme panel")];
    61         [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the scheme panel")];
    69         UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the scheme panel")
    67         UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the scheme panel")
    70                                                                       style:UIBarButtonItemStyleBordered
    68                                                                       style:UIBarButtonItemStyleBordered
    71                                                                      target:self
    69                                                                      target:self
    72                                                                      action:@selector(addWeapon:)];
    70                                                                      action:@selector(addWeapon:)];
    73         self.navigationItem.leftBarButtonItem = addButton;
    71         self.navigationItem.leftBarButtonItem = addButton;
    74         [addButton release];
       
    75     }
    72     }
    76 }
    73 }
    77 
    74 
    78 -(void) addWeapon:(id) sender {
    75 - (void)addWeapon:(id)sender {
    79     NSString *fileName = [[NSString alloc] initWithFormat:@"Weapon %u.plist", [self.listOfWeapons count]];
    76     NSString *fileName = [[NSString alloc] initWithFormat:@"Weapon %u.plist", [self.listOfWeapons count]];
    80 
    77 
    81     [CreationChamber createWeaponNamed:[fileName stringByDeletingPathExtension]];
    78     [CreationChamber createWeaponNamed:[fileName stringByDeletingPathExtension]];
    82 
    79 
    83     [self.listOfWeapons addObject:fileName];
    80     [self.listOfWeapons addObject:fileName];
    86     [self.listOfWeapons sortUsingSelector:@selector(compare:)];
    83     [self.listOfWeapons sortUsingSelector:@selector(compare:)];
    87     [self.tableView reloadData];
    84     [self.tableView reloadData];
    88 
    85 
    89     NSInteger index = [self.listOfWeapons indexOfObject:fileName];
    86     NSInteger index = [self.listOfWeapons indexOfObject:fileName];
    90     [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    87     [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    91     [fileName release];
       
    92 }
    88 }
    93 
    89 
    94 #pragma mark -
    90 #pragma mark -
    95 #pragma mark Table view data source
    91 #pragma mark Table view data source
    96 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    92 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   104 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   100 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   105     static NSString *CellIdentifier = @"Cell";
   101     static NSString *CellIdentifier = @"Cell";
   106 
   102 
   107     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   103     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   108     if (cell == nil) {
   104     if (cell == nil) {
   109         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   105         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   110     }
   106     }
   111 
   107 
   112     NSUInteger row = [indexPath row];
   108     NSUInteger row = [indexPath row];
   113     NSString *rowString = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension];
   109     NSString *rowString = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension];
   114     cell.textLabel.text = rowString;
   110     cell.textLabel.text = rowString;
   116 
   112 
   117     return cell;
   113     return cell;
   118 }
   114 }
   119 
   115 
   120 // delete the row and the file
   116 // delete the row and the file
   121 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   117 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   122     NSUInteger row = [indexPath row];
   118     NSUInteger row = [indexPath row];
   123 
   119 
   124     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]];
   120     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]];
   125     [[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL];
   121     [[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL];
   126     [schemeFile release];
       
   127 
   122 
   128     [self.listOfWeapons removeObjectAtIndex:row];
   123     [self.listOfWeapons removeObjectAtIndex:row];
   129     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
   124     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
   130 }
   125 }
   131 
   126 
   132 #pragma mark -
   127 #pragma mark -
   133 #pragma mark Table view delegate
   128 #pragma mark Table view delegate
   134 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
   129 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
   135 {
   130 {
   136     SingleWeaponViewController *singleWeaponViewController = [[SingleWeaponViewController alloc] initWithStyle:UITableViewStyleGrouped];
   131     SingleWeaponViewController *singleWeaponViewController = [[SingleWeaponViewController alloc] initWithStyle:UITableViewStyleGrouped];
   137 
   132 
   138     NSInteger row = [indexPath row];
   133     NSInteger row = [indexPath row];
   139     NSString *selectedWeaponFile = [self.listOfWeapons objectAtIndex:row];
   134     NSString *selectedWeaponFile = [self.listOfWeapons objectAtIndex:row];
   141     // this must be set so childController can load the correct plist
   136     // this must be set so childController can load the correct plist
   142     singleWeaponViewController.weaponName = [selectedWeaponFile stringByDeletingPathExtension];
   137     singleWeaponViewController.weaponName = [selectedWeaponFile stringByDeletingPathExtension];
   143     [singleWeaponViewController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
   138     [singleWeaponViewController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
   144 
   139 
   145     [self.navigationController pushViewController:singleWeaponViewController animated:YES];
   140     [self.navigationController pushViewController:singleWeaponViewController animated:YES];
   146     [singleWeaponViewController release];
       
   147     
   141     
   148     [tableView deselectRowAtIndexPath:indexPath animated:YES];
   142     [tableView deselectRowAtIndexPath:indexPath animated:YES];
   149 }
   143 }
   150 
   144 
   151 
   145 
   152 #pragma mark -
   146 #pragma mark -
   153 #pragma mark Memory management
   147 #pragma mark Memory management
       
   148 
   154 -(void)didReceiveMemoryWarning
   149 -(void)didReceiveMemoryWarning
   155 {
   150 {
   156     [super didReceiveMemoryWarning];
   151     [super didReceiveMemoryWarning];
       
   152     MSG_MEMCLEAN();
   157 }
   153 }
   158 
       
   159 -(void) viewDidUnload
       
   160 {
       
   161     self.listOfWeapons = nil;
       
   162     MSG_DIDUNLOAD();
       
   163     [super viewDidUnload];
       
   164 }
       
   165 
       
   166 
       
   167 -(void) dealloc
       
   168 {
       
   169     releaseAndNil(listOfWeapons);
       
   170     [super dealloc];
       
   171 }
       
   172 
       
   173 
   154 
   174 @end
   155 @end
   175 
   156