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