project_files/HedgewarsMobile/Classes/SchemeWeaponConfigViewController.m
changeset 5451 e359a79e3d08
parent 5208 878e551f0b4a
child 5455 df05cdb998ed
equal deleted inserted replaced
5449:a03d0acd7f06 5451:e359a79e3d08
    23 #import "CommodityFunctions.h"
    23 #import "CommodityFunctions.h"
    24 
    24 
    25 #define LABEL_TAG 57423
    25 #define LABEL_TAG 57423
    26 
    26 
    27 @implementation SchemeWeaponConfigViewController
    27 @implementation SchemeWeaponConfigViewController
    28 @synthesize listOfSchemes, listOfWeapons, lastIndexPath_sc, lastIndexPath_we, selectedScheme, selectedWeapon, syncSwitch;
    28 @synthesize listOfSchemes, listOfWeapons, lastIndexPath_sc, lastIndexPath_we, selectedScheme, selectedWeapon;
    29 
    29 
    30 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    30 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    31     return rotationManager(interfaceOrientation);
    31     return rotationManager(interfaceOrientation);
    32 }
    32 }
    33 
    33 
    83 #pragma mark Table view data source
    83 #pragma mark Table view data source
    84 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    84 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    85     if (hideSections)
    85     if (hideSections)
    86         return 0;
    86         return 0;
    87     else
    87     else
    88         return 3;
    88         return 2;
    89 }
    89 }
    90 
    90 
    91 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    91 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    92     if (section == 0)
    92     if (section == 0)
    93         return [self.listOfSchemes count];
    93         return [self.listOfSchemes count];
    94     else if (section == 1)
    94     else
    95         return [self.listOfWeapons count];
    95         return [self.listOfWeapons count];
    96     else
       
    97         return 1;
       
    98 }
    96 }
    99 
    97 
   100 // Customize the appearance of table view cells.
    98 // Customize the appearance of table view cells.
   101 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    99 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   102     static NSString *CellIdentifier = @"Cell";
   100     static NSString *CellIdentifier = @"Cell";
   118             UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]];
   116             UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]];
   119             cell.accessoryView = checkbox;
   117             cell.accessoryView = checkbox;
   120             [checkbox release];
   118             [checkbox release];
   121             self.lastIndexPath_sc = indexPath;
   119             self.lastIndexPath_sc = indexPath;
   122         }
   120         }
   123     } else if (1 == section) {
   121     } else {
   124         cell.textLabel.text = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension];
   122         cell.textLabel.text = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension];
   125         NSString *str = [NSString stringWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]];
   123         NSString *str = [NSString stringWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]];
   126         NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:str];
   124         NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:str];
   127         cell.detailTextLabel.text = [dict objectForKey:@"description"];
   125         cell.detailTextLabel.text = [dict objectForKey:@"description"];
   128         [dict release];
   126         [dict release];
   130             UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]];
   128             UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]];
   131             cell.accessoryView = checkbox;
   129             cell.accessoryView = checkbox;
   132             [checkbox release];
   130             [checkbox release];
   133             self.lastIndexPath_we = indexPath;
   131             self.lastIndexPath_we = indexPath;
   134         }
   132         }
   135     } else {
       
   136         if (self.syncSwitch == nil) {
       
   137             UISwitch *theSwitch = [[UISwitch alloc] init];
       
   138             [theSwitch setOn:YES];
       
   139             self.syncSwitch = theSwitch;
       
   140             [theSwitch release];
       
   141         }
       
   142         cell.textLabel.text = IS_IPAD() ? NSLocalizedString(@"Sync Schemes",@"") : NSLocalizedString(@"Sync Schemes and Weapons",@"");
       
   143         cell.detailTextLabel.text = IS_IPAD() ? nil : NSLocalizedString(@"Choosing a Scheme will select its associated Weapon",@"");
       
   144         cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
       
   145         cell.accessoryView = self.syncSwitch;
       
   146     }
   133     }
   147 
   134 
   148     cell.backgroundColor = UICOLOR_HW_ALMOSTBLACK;
   135     cell.backgroundColor = UICOLOR_HW_ALMOSTBLACK;
   149     cell.textLabel.textColor = UICOLOR_HW_YELLOW_TEXT;
   136     cell.textLabel.textColor = UICOLOR_HW_YELLOW_TEXT;
   150     cell.detailTextLabel.textColor = [UIColor whiteColor];
   137     cell.detailTextLabel.textColor = [UIColor whiteColor];
   158 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   145 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   159     CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 30);
   146     CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 30);
   160     NSString *text;
   147     NSString *text;
   161     if (section == 0) 
   148     if (section == 0) 
   162         text = NSLocalizedString(@"Schemes",@"");
   149         text = NSLocalizedString(@"Schemes",@"");
   163     else if (section == 1)
   150     else
   164         text = NSLocalizedString(@"Weapons",@"");
   151         text = NSLocalizedString(@"Weapons",@"");
   165     else
       
   166         text = NSLocalizedString(@"Options",@"");
       
   167 
   152 
   168     UILabel *theLabel = createBlueLabel(text, frame);
   153     UILabel *theLabel = createBlueLabel(text, frame);
   169     theLabel.center = CGPointMake(self.view.frame.size.width/2, 20);
   154     theLabel.center = CGPointMake(self.view.frame.size.width/2, 20);
   170 
   155 
   171     UIView *theView = [[[UIView alloc] init] autorelease];
   156     UIView *theView = [[[UIView alloc] init] autorelease];
   196         oldCell.accessoryView = nil;
   181         oldCell.accessoryView = nil;
   197 
   182 
   198         if ([indexPath section] == 0) {
   183         if ([indexPath section] == 0) {
   199             self.lastIndexPath_sc = indexPath;
   184             self.lastIndexPath_sc = indexPath;
   200             self.selectedScheme = [self.listOfSchemes objectAtIndex:newRow];
   185             self.selectedScheme = [self.listOfSchemes objectAtIndex:newRow];
   201             if (self.syncSwitch.on) {
   186 
       
   187             NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
       
   188             if ([[settings objectForKey:@"sync_ws"] boolValue]) {
   202                 for (NSString *str in self.listOfWeapons) {
   189                 for (NSString *str in self.listOfWeapons) {
   203                     if ([str isEqualToString:self.selectedScheme]) {
   190                     if ([str isEqualToString:self.selectedScheme]) {
   204                         int index = [self.listOfSchemes indexOfObject:str];
   191                         int index = [self.listOfSchemes indexOfObject:str];
   205                         self.selectedWeapon = str;
   192                         self.selectedWeapon = str;
   206                         self.lastIndexPath_we = [NSIndexPath indexPathForRow:index inSection:1];
   193                         self.lastIndexPath_we = [NSIndexPath indexPathForRow:index inSection:1];
   222 -(void) fillSections {
   209 -(void) fillSections {
   223     if (hideSections == YES) {
   210     if (hideSections == YES) {
   224         hideSections = NO;
   211         hideSections = NO;
   225         NSRange range;
   212         NSRange range;
   226         range.location = 0;
   213         range.location = 0;
   227         range.length = 3;
   214         range.length = 2;
   228         NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
   215         NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
   229         [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
   216         [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
   230         self.selectedScheme = @"Default.plist";
   217         self.selectedScheme = @"Default.plist";
   231         self.selectedWeapon = @"Default.plist";
   218         self.selectedWeapon = @"Default.plist";
   232 
   219 
   238 
   225 
   239 -(void) emptySections {
   226 -(void) emptySections {
   240     hideSections = YES;
   227     hideSections = YES;
   241     NSRange range;
   228     NSRange range;
   242     range.location = 0;
   229     range.location = 0;
   243     range.length = 3;
   230     range.length = 2;
   244     NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
   231     NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
   245     [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
   232     [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
   246     self.selectedScheme = @"Default.plist";
   233     self.selectedScheme = @"Default.plist";
   247     self.selectedWeapon = @"Default.plist";
   234     self.selectedWeapon = @"Default.plist";
   248 
   235 
   264     if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) {
   251     if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) {
   265         self.lastIndexPath_sc = nil;
   252         self.lastIndexPath_sc = nil;
   266         self.lastIndexPath_we = nil;
   253         self.lastIndexPath_we = nil;
   267         self.listOfSchemes = nil;
   254         self.listOfSchemes = nil;
   268         self.listOfWeapons = nil;
   255         self.listOfWeapons = nil;
   269         self.syncSwitch = nil;
       
   270         MSG_MEMCLEAN();
   256         MSG_MEMCLEAN();
   271     }
   257     }
   272     [super didReceiveMemoryWarning];
   258     [super didReceiveMemoryWarning];
   273 }
   259 }
   274 
   260 
   277     self.listOfWeapons = nil;
   263     self.listOfWeapons = nil;
   278     self.lastIndexPath_sc = nil;
   264     self.lastIndexPath_sc = nil;
   279     self.lastIndexPath_we = nil;
   265     self.lastIndexPath_we = nil;
   280     self.selectedScheme = nil;
   266     self.selectedScheme = nil;
   281     self.selectedWeapon = nil;
   267     self.selectedWeapon = nil;
   282     self.syncSwitch = nil;
       
   283     MSG_DIDUNLOAD();
   268     MSG_DIDUNLOAD();
   284     [super viewDidUnload];
   269     [super viewDidUnload];
   285 }
   270 }
   286 
   271 
   287 
   272 
   290     releaseAndNil(listOfWeapons);
   275     releaseAndNil(listOfWeapons);
   291     releaseAndNil(lastIndexPath_sc);
   276     releaseAndNil(lastIndexPath_sc);
   292     releaseAndNil(lastIndexPath_we);
   277     releaseAndNil(lastIndexPath_we);
   293     releaseAndNil(selectedScheme);
   278     releaseAndNil(selectedScheme);
   294     releaseAndNil(selectedWeapon);
   279     releaseAndNil(selectedWeapon);
   295     releaseAndNil(syncSwitch);
       
   296     [super dealloc];
   280     [super dealloc];
   297 }
   281 }
   298 
   282 
   299 
   283 
   300 @end
   284 @end