project_files/HedgewarsMobile/Classes/LevelViewController.m
author koda
Sat, 04 Sep 2010 16:24:00 +0200
changeset 3829 81db3c85784b
parent 3697 d5b30d6373fc
child 3880 24810907e853
permissions -rw-r--r--
headers ftw, also right project file
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     1
/*
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     3
 * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com>
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     4
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     8
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    12
 * GNU General Public License for more details.
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    13
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    14
 * You should have received a copy of the GNU General Public License
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    15
 * along with this program; if not, write to the Free Software
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    17
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    18
 * File created on 02/04/2010.
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    19
 */
81db3c85784b headers ftw, also right project file
koda
parents: 3697
diff changeset
    20
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
#import "LevelViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
@implementation LevelViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
@synthesize teamDictionary, levelArray, levelSprites, lastIndexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
#pragma mark View lifecycle
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
- (void)viewDidLoad {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
    [super viewDidLoad];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
    NSArray *array = [[NSArray alloc] initWithObjects:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
                      NSLocalizedString(@"Human",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
                      NSLocalizedString(@"Brutal",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
                      NSLocalizedString(@"Aggressive",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
                      NSLocalizedString(@"Bully",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
                      NSLocalizedString(@"Average",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
                      NSLocalizedString(@"Weaky",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
                      nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
    self.levelArray = array;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
    [array release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    50
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
    51
    self.title = NSLocalizedString(@"Set difficulty level",@"");
3547
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
- (void)viewWillAppear:(BOOL)animated {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
    [super viewWillAppear:animated];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
    // this moves the tableview to the top
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
    return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
    return [self.levelArray count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
// Customize the appearance of table view cells.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
    static NSString *CellIdentifier = @"Cell";
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    74
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
    NSInteger row = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
    if (cell == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    80
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
    cell.textLabel.text = [levelArray objectAtIndex:row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
    NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
    if ([[hog objectForKey:@"level"] intValue] == row) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
        cell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
    89
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
    NSString *botlevelPath = [[NSString alloc] initWithFormat:@"%@/%d.png",BOTLEVELS_DIRECTORY(),row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
    UIImage *levelImage = [[UIImage alloc] initWithContentsOfFile:botlevelPath];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
    [botlevelPath release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
    cell.imageView.image = levelImage;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
    [levelImage release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
/*
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
// Override to support conditional editing of the table view.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
    // Return NO if you do not want the specified item to be editable.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
    return YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
}
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
// Override to support editing the table view.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   112
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
    if (editingStyle == UITableViewCellEditingStyleDelete) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
        // Delete the row from the data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   116
    }
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   119
    }
3547
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
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
// Override to support rearranging the table view.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
*/
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
/*
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
// Override to support conditional rearranging of the table view.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
    // Return NO if you do not want the item to be re-orderable.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
    return YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
*/
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
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
    int newRow = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   145
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
    if (newRow != oldRow) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
        NSMutableArray *hogs = [teamDictionary objectForKey:@"hedgehogs"];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   148
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
        for (NSMutableDictionary *hog in hogs) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
            [hog setObject:[NSNumber numberWithInt:newRow] forKey:@"level"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
        }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   152
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
        // tell our boss to write this new stuff on disk
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
        [self.tableView reloadData];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3662
diff changeset
   156
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
    [self.navigationController popViewControllerAnimated:YES];
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
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   166
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167
-(void) didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   168
    // Releases the view if it doesn't have a superview.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   170
    // Relinquish ownership any cached data, images, etc that aren't in use.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   171
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   172
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   173
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   174
    self.lastIndexPath = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   175
    self.teamDictionary = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   176
    self.levelArray = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   177
    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
   178
    MSG_DIDUNLOAD();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   179
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   180
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   181
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   182
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   183
    [levelArray release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   184
    [levelSprites release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   185
    [teamDictionary release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   186
    [lastIndexPath release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   187
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   188
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   189
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   190
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   191
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   192