project_files/HedgewarsMobile/Classes/WeaponSettingsViewController.m
changeset 3522 156c04c6a3d8
parent 3514 59dbd31e9953
child 3523 6592fbb969da
equal deleted inserted replaced
3520:c3dcb80bdb1d 3522:156c04c6a3d8
     5 //  Created by Vittorio on 19/04/10.
     5 //  Created by Vittorio on 19/04/10.
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
     7 //
     7 //
     8 
     8 
     9 #import "WeaponSettingsViewController.h"
     9 #import "WeaponSettingsViewController.h"
    10 
    10 #import "CommodityFunctions.h"
       
    11 #import "SingleWeaponViewController.h"
    11 
    12 
    12 @implementation WeaponSettingsViewController
    13 @implementation WeaponSettingsViewController
       
    14 @synthesize listOfWeapons;
    13 
    15 
    14 
    16 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    15 #pragma mark -
    17     return rotationManager(interfaceOrientation);
    16 #pragma mark Initialization
       
    17 
       
    18 /*
       
    19 - (id)initWithStyle:(UITableViewStyle)style {
       
    20     // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
       
    21     if ((self = [super initWithStyle:style])) {
       
    22     }
       
    23     return self;
       
    24 }
    18 }
    25 */
       
    26 
       
    27 
    19 
    28 #pragma mark -
    20 #pragma mark -
    29 #pragma mark View lifecycle
    21 #pragma mark View lifecycle
    30 
    22 -(void) viewDidLoad {
    31 /*
       
    32 - (void)viewDidLoad {
       
    33     [super viewDidLoad];
    23     [super viewDidLoad];
    34 
    24     
    35     // Uncomment the following line to preserve selection between presentations.
    25     UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"from the weapon panel")
    36     self.clearsSelectionOnViewWillAppear = NO;
    26                                                                    style:UIBarButtonItemStyleBordered
    37  
    27                                                                   target:self
    38     // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    28                                                                   action:@selector(toggleEdit:)];
    39     // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    29     self.navigationItem.rightBarButtonItem = editButton;
    40 }
    30     [editButton release];
    41 */
    31     
    42 
       
    43 /*
       
    44 - (void)viewWillAppear:(BOOL)animated {
       
    45     [super viewWillAppear:animated];
       
    46 }
       
    47 */
       
    48 /*
       
    49 - (void)viewDidAppear:(BOOL)animated {
       
    50     [super viewDidAppear:animated];
       
    51 }
       
    52 */
       
    53 /*
       
    54 - (void)viewWillDisappear:(BOOL)animated {
       
    55     [super viewWillDisappear:animated];
       
    56 }
       
    57 */
       
    58 /*
       
    59 - (void)viewDidDisappear:(BOOL)animated {
       
    60     [super viewDidDisappear:animated];
       
    61 }
       
    62 */
       
    63 
       
    64 
       
    65 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       
    66     // Override to allow orientations other than the default portrait orientation.
       
    67     return YES;
       
    68 }
    32 }
    69 
    33 
       
    34 -(void) viewWillAppear:(BOOL) animated {
       
    35     [super viewWillAppear:animated];
       
    36     
       
    37     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL];
       
    38     NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
       
    39     self.listOfWeapons = array;
       
    40     [array release];
       
    41     
       
    42     [self.tableView reloadData];
       
    43 }
       
    44 
       
    45 // modifies the navigation bar to add the "Add" and "Done" buttons
       
    46 -(void) toggleEdit:(id) sender {
       
    47     BOOL isEditing = self.tableView.editing;
       
    48     [self.tableView setEditing:!isEditing animated:YES];
       
    49     
       
    50     if (isEditing) {
       
    51         [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the scheme panel")];
       
    52         [self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered];
       
    53         self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
       
    54     } else {
       
    55         [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the scheme panel")];
       
    56         [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
       
    57         UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the scheme panel")
       
    58                                                                       style:UIBarButtonItemStyleBordered
       
    59                                                                      target:self
       
    60                                                                      action:@selector(addWeapon:)];
       
    61         self.navigationItem.leftBarButtonItem = addButton;
       
    62         [addButton release];
       
    63     }
       
    64 }
       
    65 
       
    66 -(void) addWeapon:(id) sender {
       
    67     NSString *fileName = [[NSString alloc] initWithFormat:@"Weapon %u.plist", [self.listOfWeapons count]];
       
    68     
       
    69     createWeaponNamed([fileName stringByDeletingPathExtension]);
       
    70     
       
    71     [self.listOfWeapons addObject:fileName];
       
    72     [fileName release];
       
    73     
       
    74     // order the array alphabetically, so schemes will keep their position
       
    75     [self.listOfWeapons sortUsingSelector:@selector(compare:)];
       
    76     
       
    77     [self.tableView reloadData];
       
    78 }
    70 
    79 
    71 #pragma mark -
    80 #pragma mark -
    72 #pragma mark Table view data source
    81 #pragma mark Table view data source
    73 
    82 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    74 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
       
    75     // Return the number of sections.
       
    76     return 1;
    83     return 1;
    77 }
    84 }
    78 
    85 
    79 
    86 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    80 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    87     return [self.listOfWeapons count];
    81     // Return the number of rows in the section.
       
    82     return 1;
       
    83 }
    88 }
    84 
    89 
    85 
    90 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    86 // Customize the appearance of table view cells.
       
    87 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
    88     
       
    89     static NSString *CellIdentifier = @"Cell";
    91     static NSString *CellIdentifier = @"Cell";
    90     
    92     
    91     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    93     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    92     if (cell == nil) {
    94     if (cell == nil) {
    93         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    95         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    94     }
    96     }
    95     
    97     
    96     // Configure the cell...
    98     NSUInteger row = [indexPath row]; 
       
    99     NSString *rowString = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension]; 
       
   100     cell.textLabel.text = rowString; 
       
   101     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    97     
   102     
    98     return cell;
   103     return cell;
    99 }
   104 }
   100 
   105 
   101 
   106 // delete the row and the file
   102 /*
   107 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   103 // Override to support conditional editing of the table view.
   108     NSUInteger row = [indexPath row];
   104 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
   109     
   105     // Return NO if you do not want the specified item to be editable.
   110     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]];
   106     return YES;
   111     [[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL];
       
   112     [schemeFile release];
       
   113     
       
   114     [self.listOfWeapons removeObjectAtIndex:row];
       
   115     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
   107 }
   116 }
   108 */
       
   109 
       
   110 
       
   111 /*
       
   112 // Override to support editing the table view.
       
   113 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
       
   114     
       
   115     if (editingStyle == UITableViewCellEditingStyleDelete) {
       
   116         // Delete the row from the data source
       
   117         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
       
   118     }   
       
   119     else if (editingStyle == UITableViewCellEditingStyleInsert) {
       
   120         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
       
   121     }   
       
   122 }
       
   123 */
       
   124 
       
   125 
       
   126 /*
       
   127 // Override to support rearranging the table view.
       
   128 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
       
   129 }
       
   130 */
       
   131 
       
   132 
       
   133 /*
       
   134 // Override to support conditional rearranging of the table view.
       
   135 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
       
   136     // Return NO if you do not want the item to be re-orderable.
       
   137     return YES;
       
   138 }
       
   139 */
       
   140 
       
   141 
   117 
   142 #pragma mark -
   118 #pragma mark -
   143 #pragma mark Table view delegate
   119 #pragma mark Table view delegate
       
   120 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
   121     if (childController == nil) {
       
   122         childController = [[SingleWeaponViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   123     }
       
   124     
       
   125     NSInteger row = [indexPath row];
       
   126     NSString *selectedWeaponFile = [self.listOfWeapons objectAtIndex:row];
       
   127     
       
   128     // this must be set so childController can load the correct plist
       
   129     childController.title = [selectedWeaponFile stringByDeletingPathExtension];
       
   130     [childController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
   144 
   131 
   145 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   132     [self.navigationController pushViewController:childController animated:YES];
   146     // Navigation logic may go here. Create and push another view controller.
       
   147 	/*
       
   148 	 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
       
   149      // ...
       
   150      // Pass the selected object to the new view controller.
       
   151 	 [self.navigationController pushViewController:detailViewController animated:YES];
       
   152 	 [detailViewController release];
       
   153 	 */
       
   154 }
   133 }
   155 
   134 
   156 
   135 
   157 #pragma mark -
   136 #pragma mark -
   158 #pragma mark Memory management
   137 #pragma mark Memory management
   159 
   138 -(void)didReceiveMemoryWarning {
   160 - (void)didReceiveMemoryWarning {
       
   161     // Releases the view if it doesn't have a superview.
       
   162     [super didReceiveMemoryWarning];
   139     [super didReceiveMemoryWarning];
   163     
   140     if (childController.view.superview == nil )
   164     // Relinquish ownership any cached data, images, etc that aren't in use.
   141         childController = nil;
   165 }
   142 }
   166 
   143 
   167 - (void)viewDidUnload {
   144 -(void) viewDidUnload {
   168     // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
   145     self.listOfWeapons = nil;
   169     // For example: self.myOutlet = nil;
   146     childController = nil;
       
   147     [super viewDidUnload];
       
   148     MSG_DIDUNLOAD();
   170 }
   149 }
   171 
   150 
   172 
   151 
   173 - (void)dealloc {
   152 -(void) dealloc {
       
   153     [self.listOfWeapons release];
       
   154     [childController release];
   174     [super dealloc];
   155     [super dealloc];
   175 }
   156 }
   176 
   157 
   177 
   158 
   178 @end
   159 @end