project_files/HedgewarsMobile/Classes/SchemeWeaponConfigViewController.m
changeset 3514 59dbd31e9953
parent 3513 f589230fa21b
child 3523 6592fbb969da
equal deleted inserted replaced
3513:f589230fa21b 3514:59dbd31e9953
       
     1 //
       
     2 //  SchemeWeaponConfigViewController.m
       
     3 //  Hedgewars
       
     4 //
       
     5 //  Created by Vittorio on 13/06/10.
       
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
       
     7 //
       
     8 
       
     9 #import "SchemeWeaponConfigViewController.h"
       
    10 #import "CommodityFunctions.h"
       
    11 
       
    12 @implementation SchemeWeaponConfigViewController
       
    13 @synthesize listOfSchemes, listOfWeapons, lastIndexPath, selectedScheme, selectedWeapon;
       
    14 
       
    15 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       
    16     return rotationManager(interfaceOrientation);
       
    17 }
       
    18 
       
    19 #pragma mark -
       
    20 #pragma mark View lifecycle
       
    21 -(void) viewDidLoad {
       
    22     [super viewDidLoad];
       
    23 
       
    24     CGSize screenSize = [[UIScreen mainScreen] bounds].size;
       
    25     self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
       
    26 
       
    27     self.selectedScheme = @"Default.plist";
       
    28     self.selectedWeapon = @"Default.plist";
       
    29 }
       
    30 
       
    31 -(void) viewWillAppear:(BOOL) animated {
       
    32     [super viewWillAppear:animated];
       
    33 
       
    34     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCHEMES_DIRECTORY() error:NULL];
       
    35     self.listOfSchemes = contentsOfDir;
       
    36     
       
    37     [self.tableView reloadData];
       
    38 }
       
    39 
       
    40 /*
       
    41 - (void)viewDidAppear:(BOOL)animated {
       
    42     [super viewDidAppear:animated];
       
    43 }
       
    44 */
       
    45 /*
       
    46 - (void)viewWillDisappear:(BOOL)animated {
       
    47     [super viewWillDisappear:animated];
       
    48 }
       
    49 */
       
    50 /*
       
    51 - (void)viewDidDisappear:(BOOL)animated {
       
    52     [super viewDidDisappear:animated];
       
    53 }
       
    54 */
       
    55 
       
    56 
       
    57 #pragma mark -
       
    58 #pragma mark Table view data source
       
    59 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
       
    60     return 2;
       
    61 }
       
    62 
       
    63 
       
    64 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
    65     if (section == 0) 
       
    66         return [self.listOfSchemes count];
       
    67     else
       
    68         return [self.listOfWeapons count];
       
    69 }
       
    70 
       
    71 
       
    72 // Customize the appearance of table view cells.
       
    73 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
    74     static NSString *CellIdentifier = @"Cell";
       
    75     NSInteger row = [indexPath row];
       
    76     
       
    77     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
       
    78     if (cell == nil) {
       
    79         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
       
    80     }
       
    81     
       
    82     cell.accessoryType = UITableViewCellAccessoryNone;
       
    83     if ([indexPath section] == 0) {
       
    84         cell.textLabel.text = [[self.listOfSchemes objectAtIndex:row] stringByDeletingPathExtension];
       
    85         if ([[self.listOfSchemes objectAtIndex:row] isEqualToString:self.selectedScheme]) {
       
    86             cell.accessoryType = UITableViewCellAccessoryCheckmark;
       
    87             self.lastIndexPath = indexPath;
       
    88         }
       
    89     } else {
       
    90         cell.textLabel.text = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension];
       
    91         if ([[self.listOfWeapons objectAtIndex:row] isEqualToString:self.selectedWeapon]) {
       
    92             cell.accessoryType = UITableViewCellAccessoryCheckmark;
       
    93             self.lastIndexPath = indexPath;
       
    94         }
       
    95     }
       
    96 
       
    97     return cell;
       
    98 }
       
    99 
       
   100 
       
   101 #pragma mark -
       
   102 #pragma mark Table view delegate
       
   103 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
   104     int newRow = [indexPath row];
       
   105     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
       
   106     
       
   107     if (newRow != oldRow) {
       
   108         //TODO: this code works only for a single section table
       
   109         UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
       
   110         newCell.accessoryType = UITableViewCellAccessoryCheckmark;
       
   111         UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
       
   112         oldCell.accessoryType = UITableViewCellAccessoryNone;
       
   113         self.lastIndexPath = indexPath;
       
   114         self.selectedScheme = [self.listOfSchemes objectAtIndex:newRow];
       
   115         [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
       
   116     }
       
   117     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
       
   118 }
       
   119 
       
   120 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section {
       
   121     if (section == 0) {
       
   122         return NSLocalizedString(@"Schemes",@"");
       
   123     } else {
       
   124         return NSLocalizedString(@"Weapons",@"");;
       
   125     }
       
   126 }
       
   127 
       
   128 #pragma mark -
       
   129 #pragma mark Memory management
       
   130 -(void) didReceiveMemoryWarning {
       
   131     // Releases the view if it doesn't have a superview.
       
   132     [super didReceiveMemoryWarning];
       
   133     // Relinquish ownership any cached data, images, etc that aren't in use.
       
   134 }
       
   135 
       
   136 -(void) viewDidUnload {
       
   137     self.listOfSchemes = nil;
       
   138     self.listOfWeapons = nil;
       
   139     self.lastIndexPath = nil;
       
   140     self.selectedScheme = nil;
       
   141     self.selectedWeapon = nil;
       
   142     MSG_DIDUNLOAD();
       
   143 }
       
   144 
       
   145 
       
   146 -(void) dealloc {
       
   147     [listOfSchemes release];
       
   148     [listOfWeapons release];
       
   149     [lastIndexPath release];
       
   150     [selectedScheme release];
       
   151     [selectedWeapon release];
       
   152     [super dealloc];
       
   153 }
       
   154 
       
   155 
       
   156 @end
       
   157