project_files/HedgewarsMobile/Classes/LevelViewController.m
changeset 3880 24810907e853
parent 3829 81db3c85784b
child 3883 5934ddf9ed5d
equal deleted inserted replaced
3878:348f4104461d 3880:24810907e853
    32 }
    32 }
    33 
    33 
    34 
    34 
    35 #pragma mark -
    35 #pragma mark -
    36 #pragma mark View lifecycle
    36 #pragma mark View lifecycle
    37 - (void)viewDidLoad {
    37 -(void) viewDidLoad {
    38     [super viewDidLoad];
    38     [super viewDidLoad];
    39 
    39 
    40     NSArray *array = [[NSArray alloc] initWithObjects:
    40     NSArray *array = [[NSArray alloc] initWithObjects:
    41                       NSLocalizedString(@"Human",@""),
       
    42                       NSLocalizedString(@"Brutal",@""),
    41                       NSLocalizedString(@"Brutal",@""),
    43                       NSLocalizedString(@"Aggressive",@""),
    42                       NSLocalizedString(@"Aggressive",@""),
    44                       NSLocalizedString(@"Bully",@""),
    43                       NSLocalizedString(@"Bully",@""),
    45                       NSLocalizedString(@"Average",@""),
    44                       NSLocalizedString(@"Average",@""),
    46                       NSLocalizedString(@"Weaky",@""),
    45                       NSLocalizedString(@"Weaky",@""),
    49     [array release];
    48     [array release];
    50 
    49 
    51     self.title = NSLocalizedString(@"Set difficulty level",@"");
    50     self.title = NSLocalizedString(@"Set difficulty level",@"");
    52 }
    51 }
    53 
    52 
    54 - (void)viewWillAppear:(BOOL)animated {
    53 -(void) viewWillAppear:(BOOL)animated {
    55     [super viewWillAppear:animated];
    54     [super viewWillAppear:animated];
       
    55     
       
    56     if ([[[[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0] objectForKey:@"level"] intValue] == 0)
       
    57         numberOfSections = 1;
       
    58     else
       
    59         numberOfSections = 2;
       
    60     
    56     [self.tableView reloadData];
    61     [self.tableView reloadData];
    57     // this moves the tableview to the top
    62     // this moves the tableview to the top
    58     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    63     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    59 }
    64 }
    60 
    65 
       
    66 -(void) viewWillDisappear:(BOOL)animated {
       
    67  // stuff like checking that at least 1 field was selected   
       
    68 }
       
    69 
    61 #pragma mark -
    70 #pragma mark -
    62 #pragma mark Table view data source
    71 #pragma mark Table view data source
    63 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    72 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    64     return 1;
    73     return numberOfSections;
    65 }
    74 }
    66 
    75 
    67 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    76 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
    68     return [self.levelArray count];
    77     if (section == 0)
       
    78         return 1;
       
    79     else
       
    80         return 5;
    69 }
    81 }
    70 
    82 
    71 // Customize the appearance of table view cells.
    83 // Customize the appearance of table view cells.
    72 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    84 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    73     static NSString *CellIdentifier = @"Cell";
    85     static NSString *CellIdentifier0 = @"Cell0";
       
    86     static NSString *CellIdentifier1 = @"Cell1";
    74 
    87 
    75     NSInteger row = [indexPath row];
    88     NSInteger row = [indexPath row];
    76     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    89     NSInteger section = [indexPath section];
    77     if (cell == nil) {
    90     UITableViewCell *cell;
    78         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    91     
       
    92     if (section == 0) {
       
    93         cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
       
    94         if (cell == nil) {
       
    95             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0] autorelease];
       
    96             UISwitch *theSwitch = [[UISwitch alloc] init];
       
    97             if (numberOfSections == 1)
       
    98                 theSwitch.on = NO;
       
    99             else
       
   100                 theSwitch.on = YES;
       
   101             [theSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
       
   102             cell.accessoryView = theSwitch;
       
   103             [theSwitch release];
       
   104         }
       
   105         cell.textLabel.text = NSLocalizedString(@"Hogs controlled by AI",@"");
       
   106     } else {
       
   107         cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
       
   108         if (cell == nil)
       
   109             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
       
   110         
       
   111         cell.textLabel.text = [levelArray objectAtIndex:row];
       
   112         NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0];
       
   113         if ([[hog objectForKey:@"level"] intValue] == row+1) {
       
   114             cell.accessoryType = UITableViewCellAccessoryCheckmark;
       
   115             self.lastIndexPath = indexPath;
       
   116         } else {
       
   117             cell.accessoryType = UITableViewCellAccessoryNone;
       
   118         }
       
   119         
       
   120         NSString *botlevelPath = [[NSString alloc] initWithFormat:@"%@/%d.png",BOTLEVELS_DIRECTORY(),row+1];
       
   121         UIImage *levelImage = [[UIImage alloc] initWithContentsOfFile:botlevelPath];
       
   122         [botlevelPath release];
       
   123         cell.imageView.image = levelImage;
       
   124         [levelImage release];
    79     }
   125     }
    80 
   126     
    81     cell.textLabel.text = [levelArray objectAtIndex:row];
       
    82     NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0];
       
    83     if ([[hog objectForKey:@"level"] intValue] == row) {
       
    84         cell.accessoryType = UITableViewCellAccessoryCheckmark;
       
    85         self.lastIndexPath = indexPath;
       
    86     } else {
       
    87         cell.accessoryType = UITableViewCellAccessoryNone;
       
    88     }
       
    89 
       
    90     NSString *botlevelPath = [[NSString alloc] initWithFormat:@"%@/%d.png",BOTLEVELS_DIRECTORY(),row];
       
    91     UIImage *levelImage = [[UIImage alloc] initWithContentsOfFile:botlevelPath];
       
    92     [botlevelPath release];
       
    93     cell.imageView.image = levelImage;
       
    94     [levelImage release];
       
    95 
       
    96     return cell;
   127     return cell;
    97 }
   128 }
    98 
   129 
    99 
   130 -(void) switchValueChanged:(id) sender {
   100 /*
   131     UISwitch *theSwitch = (UISwitch *)sender;
   101 // Override to support conditional editing of the table view.
   132     NSIndexSet *sections = [[NSIndexSet alloc] initWithIndex:1];
   102 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
   133     NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"];
   103     // Return NO if you do not want the specified item to be editable.
   134 
   104     return YES;
   135     if (theSwitch.on) {
   105 }
   136         numberOfSections = 2;
   106 */
   137         [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
   107 
   138         for (NSMutableDictionary *hog in hogs)
   108 
   139             [hog setObject:[NSNumber numberWithInt:4] forKey:@"level"];
   109 /*
   140         
   110 // Override to support editing the table view.
       
   111 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
       
   112 
       
   113     if (editingStyle == UITableViewCellEditingStyleDelete) {
       
   114         // Delete the row from the data source
       
   115         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
       
   116     }
       
   117     else if (editingStyle == UITableViewCellEditingStyleInsert) {
       
   118         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
       
   119     }
       
   120 }
       
   121 */
       
   122 
       
   123 
       
   124 /*
       
   125 // Override to support rearranging the table view.
       
   126 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
       
   127 }
       
   128 */
       
   129 
       
   130 
       
   131 /*
       
   132 // Override to support conditional rearranging of the table view.
       
   133 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
       
   134     // Return NO if you do not want the item to be re-orderable.
       
   135     return YES;
       
   136 }
       
   137 */
       
   138 
       
   139 
       
   140 #pragma mark -
       
   141 #pragma mark Table view delegate
       
   142 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
   143     int newRow = [indexPath row];
       
   144     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
       
   145 
       
   146     if (newRow != oldRow) {
       
   147         NSMutableArray *hogs = [teamDictionary objectForKey:@"hedgehogs"];
       
   148 
       
   149         for (NSMutableDictionary *hog in hogs) {
       
   150             [hog setObject:[NSNumber numberWithInt:newRow] forKey:@"level"];
       
   151         }
       
   152 
       
   153         // tell our boss to write this new stuff on disk
       
   154         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   141         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   155         [self.tableView reloadData];
   142         [self.tableView reloadData];
   156 
   143     } else {
   157         self.lastIndexPath = indexPath;
   144         numberOfSections = 1;
   158         [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   145         [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
       
   146         
       
   147         for (NSMutableDictionary *hog in hogs)
       
   148             [hog setObject:[NSNumber numberWithInt:0] forKey:@"level"];
       
   149 
       
   150         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
       
   151     }
       
   152     [sections release];
       
   153 }
       
   154 
       
   155 
       
   156 #pragma mark -
       
   157 #pragma mark Table view delegate
       
   158 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
   159     int newRow = [indexPath row];
       
   160     int oldRow = (self.lastIndexPath != nil) ? [self.lastIndexPath row] : -1;
       
   161 
       
   162     if ([indexPath section] != 0) { 
       
   163         if (newRow != oldRow) {
       
   164             NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"];
       
   165             
       
   166             for (NSMutableDictionary *hog in hogs)
       
   167                 [hog setObject:[NSNumber numberWithInt:newRow+1] forKey:@"level"];
       
   168             
       
   169             // tell our boss to write this new stuff on disk
       
   170             [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
       
   171             [self.tableView reloadData];
       
   172             
       
   173             self.lastIndexPath = indexPath;
       
   174             [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
       
   175         }
       
   176         [self.navigationController popViewControllerAnimated:YES];
   159     }
   177     }
   160     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
   178     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
   161     [self.navigationController popViewControllerAnimated:YES];
       
   162 }
   179 }
   163 
   180 
   164 
   181 
   165 #pragma mark -
   182 #pragma mark -
   166 #pragma mark Memory management
   183 #pragma mark Memory management