project_files/HedgewarsMobile/Classes/SchemeWeaponConfigViewController.m
changeset 3546 ccf4854df294
parent 3523 6592fbb969da
child 3547 02875b1145b7
equal deleted inserted replaced
3545:b07ee704f35d 3546:ccf4854df294
     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_sc, lastIndexPath_we, 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 
       
    28 -(void) viewWillAppear:(BOOL) animated {
       
    29     [super viewWillAppear:animated];
       
    30 
       
    31     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCHEMES_DIRECTORY() error:NULL];
       
    32     self.listOfSchemes = contentsOfDir;
       
    33     
       
    34     contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL];
       
    35     self.listOfWeapons = contentsOfDir;
       
    36     
       
    37     self.selectedScheme = @"Default.plist";
       
    38     self.selectedWeapon = @"Default.plist";
       
    39     
       
    40     [self.tableView reloadData];
       
    41 }
       
    42 
       
    43 
       
    44 
       
    45 #pragma mark -
       
    46 #pragma mark Table view data source
       
    47 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
       
    48     return 2;
       
    49 }
       
    50 
       
    51 
       
    52 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
    53     if (section == 0) 
       
    54         return [self.listOfSchemes count];
       
    55     else
       
    56         return [self.listOfWeapons count];
       
    57 }
       
    58 
       
    59 
       
    60 // Customize the appearance of table view cells.
       
    61 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
    62     static NSString *CellIdentifier = @"Cell";
       
    63     NSInteger row = [indexPath row];
       
    64     
       
    65     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
       
    66     if (cell == nil) {
       
    67         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
       
    68     }
       
    69     
       
    70     cell.accessoryType = UITableViewCellAccessoryNone;
       
    71     if ([indexPath section] == 0) {
       
    72         cell.textLabel.text = [[self.listOfSchemes objectAtIndex:row] stringByDeletingPathExtension];
       
    73         if ([[self.listOfSchemes objectAtIndex:row] isEqualToString:self.selectedScheme]) {
       
    74             cell.accessoryType = UITableViewCellAccessoryCheckmark;
       
    75             self.lastIndexPath_sc = indexPath;
       
    76         }
       
    77     } else {
       
    78         cell.textLabel.text = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension];
       
    79         if ([[self.listOfWeapons objectAtIndex:row] isEqualToString:self.selectedWeapon]) {
       
    80             cell.accessoryType = UITableViewCellAccessoryCheckmark;
       
    81             self.lastIndexPath_we = indexPath;
       
    82         }
       
    83     }
       
    84 
       
    85     return cell;
       
    86 }
       
    87 
       
    88 
       
    89 #pragma mark -
       
    90 #pragma mark Table view delegate
       
    91 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
    92     NSIndexPath *lastIndexPath;
       
    93     if ([indexPath section] == 0)
       
    94         lastIndexPath = self.lastIndexPath_sc;
       
    95     else
       
    96         lastIndexPath = self.lastIndexPath_we;
       
    97     
       
    98     int newRow = [indexPath row];
       
    99     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
       
   100     
       
   101     if (newRow != oldRow) {
       
   102         //TODO: this code works only for a single section table
       
   103         UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
       
   104         newCell.accessoryType = UITableViewCellAccessoryCheckmark;
       
   105         UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
       
   106         oldCell.accessoryType = UITableViewCellAccessoryNone;
       
   107         
       
   108         if ([indexPath section] == 0) {
       
   109             self.lastIndexPath_sc = indexPath;
       
   110             self.selectedScheme = [self.listOfSchemes objectAtIndex:newRow];
       
   111         } else {
       
   112             self.lastIndexPath_we = indexPath;
       
   113             self.selectedWeapon = [self.listOfWeapons objectAtIndex:newRow];
       
   114         }        
       
   115         
       
   116         [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
       
   117     }
       
   118     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
       
   119 }
       
   120 
       
   121 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section {
       
   122     if (section == 0) {
       
   123         return NSLocalizedString(@"Schemes",@"");
       
   124     } else {
       
   125         return NSLocalizedString(@"Weapons",@"");;
       
   126     }
       
   127 }
       
   128 
       
   129 #pragma mark -
       
   130 #pragma mark Memory management
       
   131 -(void) didReceiveMemoryWarning {
       
   132     // Releases the view if it doesn't have a superview.
       
   133     [super didReceiveMemoryWarning];
       
   134     // Relinquish ownership any cached data, images, etc that aren't in use.
       
   135 }
       
   136 
       
   137 -(void) viewDidUnload {
       
   138     self.listOfSchemes = nil;
       
   139     self.listOfWeapons = nil;
       
   140     self.lastIndexPath_sc = nil;
       
   141     self.lastIndexPath_we = nil;
       
   142     self.selectedScheme = nil;
       
   143     self.selectedWeapon = nil;
       
   144     MSG_DIDUNLOAD();
       
   145 }
       
   146 
       
   147 
       
   148 -(void) dealloc {
       
   149     [listOfSchemes release];
       
   150     [listOfWeapons release];
       
   151     [lastIndexPath_sc release];
       
   152     [lastIndexPath_we release];
       
   153     [selectedScheme release];
       
   154     [selectedWeapon release];
       
   155     [super dealloc];
       
   156 }
       
   157 
       
   158 
       
   159 @end
       
   160