project_files/HedgewarsMobile/Classes/LevelViewController.m
branchios-develop
changeset 12872 00215a7ec5f5
parent 11217 e68b3e392091
equal deleted inserted replaced
12871:2c06b1120749 12872:00215a7ec5f5
    21 
    21 
    22 
    22 
    23 @implementation LevelViewController
    23 @implementation LevelViewController
    24 @synthesize teamDictionary, levelArray, levelSprites, lastIndexPath;
    24 @synthesize teamDictionary, levelArray, levelSprites, lastIndexPath;
    25 
    25 
    26 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    26 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    27     return rotationManager(interfaceOrientation);
    27     return rotationManager(interfaceOrientation);
    28 }
    28 }
    29 
    29 
    30 #pragma mark -
    30 #pragma mark -
    31 #pragma mark View lifecycle
    31 #pragma mark View lifecycle
    32 -(void) viewDidLoad {
    32 - (void)viewDidLoad {
    33     [super viewDidLoad];
    33     [super viewDidLoad];
    34 
    34 
    35     NSArray *array = [[NSArray alloc] initWithObjects:
    35     NSArray *array = [[NSArray alloc] initWithObjects:
    36                       NSLocalizedString(@"Brutal",@""),
    36                       NSLocalizedString(@"Brutal",@""),
    37                       NSLocalizedString(@"Aggressive",@""),
    37                       NSLocalizedString(@"Aggressive",@""),
    38                       NSLocalizedString(@"Bully",@""),
    38                       NSLocalizedString(@"Bully",@""),
    39                       NSLocalizedString(@"Average",@""),
    39                       NSLocalizedString(@"Average",@""),
    40                       NSLocalizedString(@"Weaky",@""),
    40                       NSLocalizedString(@"Weaky",@""),
    41                       nil];
    41                       nil];
    42     self.levelArray = array;
    42     self.levelArray = array;
    43     [array release];
       
    44 
    43 
    45     self.title = NSLocalizedString(@"Set difficulty level",@"");
    44     self.title = NSLocalizedString(@"Set difficulty level",@"");
    46 }
    45 }
    47 
    46 
    48 -(void) viewWillAppear:(BOOL)animated {
    47 - (void)viewWillAppear:(BOOL)animated {
    49     [super viewWillAppear:animated];
    48     [super viewWillAppear:animated];
    50 
    49 
    51     if ([[[[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0] objectForKey:@"level"] intValue] == 0)
    50     if ([[[[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0] objectForKey:@"level"] intValue] == 0)
    52         numberOfSections = 1;
    51         numberOfSections = 1;
    53     else
    52     else
    58     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    57     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    59 }
    58 }
    60 
    59 
    61 #pragma mark -
    60 #pragma mark -
    62 #pragma mark Table view data source
    61 #pragma mark Table view data source
    63 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    62 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    64     return numberOfSections;
    63     return numberOfSections;
    65 }
    64 }
    66 
    65 
    67 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
    66 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    68     if (section == 0)
    67     if (section == 0)
    69         return 1;
    68         return 1;
    70     else
    69     else
    71         return 5;
    70         return 5;
    72 }
    71 }
    81     UITableViewCell *cell;
    80     UITableViewCell *cell;
    82 
    81 
    83     if (section == 0) {
    82     if (section == 0) {
    84         cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
    83         cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
    85         if (cell == nil) {
    84         if (cell == nil) {
    86             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0] autorelease];
    85             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0];
    87             UISwitch *theSwitch = [[UISwitch alloc] init];
    86             UISwitch *theSwitch = [[UISwitch alloc] init];
    88             [theSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    87             [theSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    89             cell.accessoryView = theSwitch;
    88             cell.accessoryView = theSwitch;
    90             [theSwitch release];
       
    91         }
    89         }
    92         UISwitch *theSwitch = (UISwitch *)cell.accessoryView;
    90         UISwitch *theSwitch = (UISwitch *)cell.accessoryView;
    93         if (numberOfSections == 1)
    91         if (numberOfSections == 1)
    94             theSwitch.on = NO;
    92             theSwitch.on = NO;
    95         else
    93         else
    96             theSwitch.on = YES;
    94             theSwitch.on = YES;
    97         cell.textLabel.text = NSLocalizedString(@"Hogs controlled by AI",@"");
    95         cell.textLabel.text = NSLocalizedString(@"Hogs controlled by AI",@"");
    98     } else {
    96     } else {
    99         cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    97         cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   100         if (cell == nil)
    98         if (cell == nil)
   101             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
    99             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
   102 
   100 
   103         cell.textLabel.text = [levelArray objectAtIndex:row];
   101         cell.textLabel.text = [levelArray objectAtIndex:row];
   104         NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0];
   102         NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0];
   105         if ([[hog objectForKey:@"level"] intValue] == row+1) {
   103         if ([[hog objectForKey:@"level"] intValue] == row+1) {
   106             cell.accessoryType = UITableViewCellAccessoryCheckmark;
   104             cell.accessoryType = UITableViewCellAccessoryCheckmark;
   109             cell.accessoryType = UITableViewCellAccessoryNone;
   107             cell.accessoryType = UITableViewCellAccessoryNone;
   110         }
   108         }
   111 
   109 
   112         NSString *botlevelPath = [[NSString alloc] initWithFormat:@"%@/bot%d.png",[[NSBundle mainBundle] resourcePath],row+1];
   110         NSString *botlevelPath = [[NSString alloc] initWithFormat:@"%@/bot%d.png",[[NSBundle mainBundle] resourcePath],row+1];
   113         UIImage *levelImage = [[UIImage alloc] initWithContentsOfFile:botlevelPath];
   111         UIImage *levelImage = [[UIImage alloc] initWithContentsOfFile:botlevelPath];
   114         [botlevelPath release];
       
   115         cell.imageView.image = levelImage;
   112         cell.imageView.image = levelImage;
   116         [levelImage release];
       
   117     }
   113     }
   118 
   114 
   119     return cell;
   115     return cell;
   120 }
   116 }
   121 
   117 
   122 -(void) switchValueChanged:(id) sender {
   118 - (void)switchValueChanged:(id)sender {
   123     UISwitch *theSwitch = (UISwitch *)sender;
   119     UISwitch *theSwitch = (UISwitch *)sender;
   124     NSIndexSet *sections = [[NSIndexSet alloc] initWithIndex:1];
   120     NSIndexSet *sections = [[NSIndexSet alloc] initWithIndex:1];
   125     NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"];
   121     NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"];
   126     NSInteger level;
   122     NSInteger level;
   127 
   123 
   132     } else {
   128     } else {
   133         numberOfSections = 1;
   129         numberOfSections = 1;
   134         [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
   130         [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
   135         level = 0;
   131         level = 0;
   136     }
   132     }
   137     [sections release];
       
   138 
   133 
   139     DLog(@"New level is %ld", (long)level);
   134     DLog(@"New level is %ld", (long)level);
   140     for (NSMutableDictionary *hog in hogs)
   135     for (NSMutableDictionary *hog in hogs)
   141         [hog setObject:[NSNumber numberWithInteger:level] forKey:@"level"];
   136         [hog setObject:[NSNumber numberWithInteger:level] forKey:@"level"];
   142 
   137 
   145 }
   140 }
   146 
   141 
   147 
   142 
   148 #pragma mark -
   143 #pragma mark -
   149 #pragma mark Table view delegate
   144 #pragma mark Table view delegate
   150 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   145 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   151     NSInteger newRow = [indexPath row];
   146     NSInteger newRow = [indexPath row];
   152     NSInteger oldRow = (self.lastIndexPath != nil) ? [self.lastIndexPath row] : -1;
   147     NSInteger oldRow = (self.lastIndexPath != nil) ? [self.lastIndexPath row] : -1;
   153 
   148 
   154     if ([indexPath section] != 0) {
   149     if ([indexPath section] != 0) {
   155         if (newRow != oldRow) {
   150         if (newRow != oldRow) {
   172 }
   167 }
   173 
   168 
   174 
   169 
   175 #pragma mark -
   170 #pragma mark -
   176 #pragma mark Memory management
   171 #pragma mark Memory management
   177 -(void) didReceiveMemoryWarning {
   172 
       
   173 - (void)didReceiveMemoryWarning {
   178     self.lastIndexPath = nil;
   174     self.lastIndexPath = nil;
   179     MSG_MEMCLEAN();
   175     MSG_MEMCLEAN();
   180     [super didReceiveMemoryWarning];
   176     [super didReceiveMemoryWarning];
   181 }
   177 }
   182 
   178 
   183 -(void) viewDidUnload {
       
   184     self.lastIndexPath = nil;
       
   185     self.teamDictionary = nil;
       
   186     self.levelArray = nil;
       
   187     self.levelSprites = nil;
       
   188     MSG_DIDUNLOAD();
       
   189     [super viewDidUnload];
       
   190 }
       
   191 
       
   192 -(void) dealloc {
       
   193     releaseAndNil(levelArray);
       
   194     releaseAndNil(levelSprites);
       
   195     releaseAndNil(teamDictionary);
       
   196     releaseAndNil(lastIndexPath);
       
   197     [super dealloc];
       
   198 }
       
   199 
       
   200 
       
   201 @end
   179 @end
   202 
   180