project_files/HedgewarsMobile/Classes/WeaponSettingsViewController.m
author koda
Sun, 20 Jun 2010 18:35:59 +0200
changeset 3523 6592fbb969da
parent 3522 156c04c6a3d8
child 3546 ccf4854df294
permissions -rw-r--r--
fix zoom smoothness some more work for weapons on the ifrontend bugfix for the gameconfig page
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
     1
//
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
     2
//  WeaponSettingsViewController.m
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
     3
//  HedgewarsMobile
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
     4
//
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
     5
//  Created by Vittorio on 19/04/10.
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
     7
//
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
     8
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
     9
#import "WeaponSettingsViewController.h"
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    10
#import "CommodityFunctions.h"
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    11
#import "SingleWeaponViewController.h"
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    12
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    13
@implementation WeaponSettingsViewController
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    14
@synthesize listOfWeapons;
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    15
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    16
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    17
    return rotationManager(interfaceOrientation);
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    18
}
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    19
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    20
#pragma mark -
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    21
#pragma mark View lifecycle
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    22
-(void) viewDidLoad {
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    23
    [super viewDidLoad];
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    24
    
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    25
    UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"from the weapon panel")
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    26
                                                                   style:UIBarButtonItemStyleBordered
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    27
                                                                  target:self
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    28
                                                                  action:@selector(toggleEdit:)];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    29
    self.navigationItem.rightBarButtonItem = editButton;
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    30
    [editButton release];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    31
    
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    32
}
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    33
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    34
-(void) viewWillAppear:(BOOL) animated {
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    35
    [super viewWillAppear:animated];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    36
    
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    37
    NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    38
    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    39
    self.listOfWeapons = array;
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    40
    [array release];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    41
    
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    42
    [self.tableView reloadData];
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    43
}
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    44
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    45
// modifies the navigation bar to add the "Add" and "Done" buttons
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    46
-(void) toggleEdit:(id) sender {
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    47
    BOOL isEditing = self.tableView.editing;
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    48
    [self.tableView setEditing:!isEditing animated:YES];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    49
    
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    50
    if (isEditing) {
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    51
        [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the scheme panel")];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    52
        [self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    53
        self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    54
    } else {
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    55
        [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the scheme panel")];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    56
        [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    57
        UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the scheme panel")
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    58
                                                                      style:UIBarButtonItemStyleBordered
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    59
                                                                     target:self
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    60
                                                                     action:@selector(addWeapon:)];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    61
        self.navigationItem.leftBarButtonItem = addButton;
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    62
        [addButton release];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    63
    }
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    64
}
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    65
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    66
-(void) addWeapon:(id) sender {
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    67
    NSString *fileName = [[NSString alloc] initWithFormat:@"Weapon %u.plist", [self.listOfWeapons count]];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    68
    
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    69
    createWeaponNamed([fileName stringByDeletingPathExtension]);
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    70
    
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    71
    [self.listOfWeapons addObject:fileName];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    72
    [fileName release];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    73
    
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    74
    // order the array alphabetically, so schemes will keep their position
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    75
    [self.listOfWeapons sortUsingSelector:@selector(compare:)];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    76
    
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    77
    [self.tableView reloadData];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    78
}
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    79
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    80
#pragma mark -
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    81
#pragma mark Table view data source
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    82
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    83
    return 1;
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    84
}
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    85
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    86
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    87
    return [self.listOfWeapons count];
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    88
}
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    89
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    90
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    91
    static NSString *CellIdentifier = @"Cell";
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    92
    
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    93
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    94
    if (cell == nil) {
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    95
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    96
    }
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
    97
    
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    98
    NSUInteger row = [indexPath row]; 
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
    99
    NSString *rowString = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension]; 
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   100
    cell.textLabel.text = rowString; 
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   101
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   102
    
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   103
    return cell;
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   104
}
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   105
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   106
// delete the row and the file
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   107
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   108
    NSUInteger row = [indexPath row];
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   109
    
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   110
    NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   111
    [[NSFileManager defaultManager] removeItemAtPath:schemeFile error:NULL];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   112
    [schemeFile release];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   113
    
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   114
    [self.listOfWeapons removeObjectAtIndex:row];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   115
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   116
}
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   117
3523
6592fbb969da fix zoom smoothness
koda
parents: 3522
diff changeset
   118
-(BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
6592fbb969da fix zoom smoothness
koda
parents: 3522
diff changeset
   119
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
6592fbb969da fix zoom smoothness
koda
parents: 3522
diff changeset
   120
    if (![cell.textLabel.text isEqualToString:@"Default"]) 
6592fbb969da fix zoom smoothness
koda
parents: 3522
diff changeset
   121
        return YES;
6592fbb969da fix zoom smoothness
koda
parents: 3522
diff changeset
   122
    else
6592fbb969da fix zoom smoothness
koda
parents: 3522
diff changeset
   123
        return NO;
6592fbb969da fix zoom smoothness
koda
parents: 3522
diff changeset
   124
}
6592fbb969da fix zoom smoothness
koda
parents: 3522
diff changeset
   125
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   126
#pragma mark -
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   127
#pragma mark Table view delegate
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   128
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   129
    if (childController == nil) {
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   130
        childController = [[SingleWeaponViewController alloc] initWithStyle:UITableViewStyleGrouped];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   131
    }
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   132
    
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   133
    NSInteger row = [indexPath row];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   134
    NSString *selectedWeaponFile = [self.listOfWeapons objectAtIndex:row];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   135
    
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   136
    // this must be set so childController can load the correct plist
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   137
    childController.title = [selectedWeaponFile stringByDeletingPathExtension];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   138
    [childController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   139
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   140
    [self.navigationController pushViewController:childController animated:YES];
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   141
}
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   142
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   143
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   144
#pragma mark -
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   145
#pragma mark Memory management
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   146
-(void)didReceiveMemoryWarning {
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   147
    [super didReceiveMemoryWarning];
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   148
    if (childController.view.superview == nil )
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   149
        childController = nil;
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   150
}
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   151
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   152
-(void) viewDidUnload {
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   153
    self.listOfWeapons = nil;
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   154
    childController = nil;
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   155
    [super viewDidUnload];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   156
    MSG_DIDUNLOAD();
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   157
}
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   158
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   159
3522
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   160
-(void) dealloc {
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   161
    [self.listOfWeapons release];
156c04c6a3d8 add initial stubs for selecting weapons in the ifrontend
koda
parents: 3514
diff changeset
   162
    [childController release];
3356
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   163
    [super dealloc];
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   164
}
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   165
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   166
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   167
@end
3ae3fccb439e add missing controller stubs
koda
parents:
diff changeset
   168