project_files/HedgewarsMobile/Classes/LevelViewController.m
author koda
Thu, 22 Jul 2010 03:08:17 +0200
changeset 3662 a44406f4369b
parent 3547 02875b1145b7
child 3697 d5b30d6373fc
permissions -rw-r--r--
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
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
//  LevelViewController.m
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     3
//  HedgewarsMobile
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     4
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     5
//  Created by Vittorio on 02/04/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 "LevelViewController.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
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    13
@implementation LevelViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    14
@synthesize teamDictionary, levelArray, levelSprites, lastIndexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    15
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    16
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    17
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    18
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
#pragma mark View lifecycle
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
- (void)viewDidLoad {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
    [super viewDidLoad];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
    NSArray *array = [[NSArray alloc] initWithObjects:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
                      NSLocalizedString(@"Human",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
                      NSLocalizedString(@"Brutal",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
                      NSLocalizedString(@"Aggressive",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
                      NSLocalizedString(@"Bully",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
                      NSLocalizedString(@"Average",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
                      NSLocalizedString(@"Weaky",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
                      nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
    self.levelArray = array;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
    [array release];
3662
a44406f4369b polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents: 3547
diff changeset
    37
    
a44406f4369b polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents: 3547
diff changeset
    38
    self.title = NSLocalizedString(@"Set difficulty level",@"");
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
- (void)viewWillAppear:(BOOL)animated {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
    [super viewWillAppear:animated];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
    // this moves the tableview to the top
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
    return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
    return [self.levelArray count];
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
// Customize the appearance of table view cells.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
    static NSString *CellIdentifier = @"Cell";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
    NSInteger row = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
    if (cell == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
    cell.textLabel.text = [levelArray objectAtIndex:row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
    NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
    if ([[hog objectForKey:@"level"] intValue] == row) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
        cell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
    NSString *botlevelPath = [[NSString alloc] initWithFormat:@"%@/%d.png",BOTLEVELS_DIRECTORY(),row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
    UIImage *levelImage = [[UIImage alloc] initWithContentsOfFile:botlevelPath];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
    [botlevelPath release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
    cell.imageView.image = levelImage;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
    [levelImage release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
/*
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
// Override to support conditional editing of the table view.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
    // Return NO if you do not want the specified item to be editable.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
    return YES;
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
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
/*
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
// Override to support editing the table view.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
    if (editingStyle == UITableViewCellEditingStyleDelete) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
        // Delete the row from the data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
    }   
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
    }   
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
*/
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
/*
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
// Override to support rearranging the table view.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
*/
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
/*
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
// Override to support conditional rearranging of the table view.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
    // Return NO if you do not want the item to be re-orderable.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
    return YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
*/
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
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
    int newRow = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
    if (newRow != oldRow) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
        NSMutableArray *hogs = [teamDictionary objectForKey:@"hedgehogs"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
        for (NSMutableDictionary *hog in hogs) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
            [hog setObject:[NSNumber numberWithInt:newRow] forKey:@"level"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
        // tell our boss to write this new stuff on disk
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
        [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
    [self.navigationController popViewControllerAnimated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
-(void) didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
    // Releases the view if it doesn't have a superview.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
    // Relinquish ownership any cached data, images, etc that aren't in use.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
    self.lastIndexPath = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
    self.teamDictionary = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   163
    self.levelArray = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   164
    self.levelSprites = nil;
3662
a44406f4369b polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents: 3547
diff changeset
   165
    MSG_DIDUNLOAD();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   166
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   168
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   170
    [levelArray release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   171
    [levelSprites release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   172
    [teamDictionary release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   173
    [lastIndexPath release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   174
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   175
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   176
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   177
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   178
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   179