project_files/HedgewarsMobile/Classes/SchemeWeaponConfigViewController.m
changeset 6107 0741c0f0203e
parent 6078 8c0cc07731e5
child 6108 7a8da11a6144
equal deleted inserted replaced
6106:ba098945bd72 6107:0741c0f0203e
    31 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    31 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    32     return rotationManager(interfaceOrientation);
    32     return rotationManager(interfaceOrientation);
    33 }
    33 }
    34 
    34 
    35 #pragma mark -
    35 #pragma mark -
       
    36 #pragma mark custom setters/getters
       
    37 -(NSString *)selectedScheme {
       
    38     if (selectedScheme == nil)
       
    39         self.selectedScheme = @"Default.plist";
       
    40     return selectedScheme;
       
    41 }
       
    42 
       
    43 -(NSString *)selectedWeapon {
       
    44     if (selectedWeapon == nil)
       
    45         self.selectedWeapon = @"Default.plist";
       
    46     return selectedWeapon;
       
    47 }
       
    48 
       
    49 -(NSString *)selectedScript {
       
    50     if (selectedScript == nil)
       
    51         self.selectedScript = @"Normal.plist";
       
    52     return selectedScript;
       
    53 }
       
    54 
       
    55 -(NSString *)scriptCommand {
       
    56     if (scriptCommand == nil)
       
    57         self.scriptCommand = @"";
       
    58     return scriptCommand;
       
    59 }
       
    60 
       
    61 -(NSArray *)listOfSchemes {
       
    62     if (listOfSchemes == nil)
       
    63         self.listOfSchemes = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCHEMES_DIRECTORY() error:NULL];
       
    64     return listOfSchemes;
       
    65 }
       
    66 
       
    67 -(NSArray *)listOfWeapons {
       
    68     if (listOfWeapons == nil)
       
    69         self.listOfWeapons = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL];
       
    70     return listOfWeapons;
       
    71 }
       
    72 
       
    73 -(NSArray *)listOfScripts {
       
    74     if (listOfScripts == nil)
       
    75         self.listOfScripts = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCRIPTS_DIRECTORY() error:NULL];
       
    76     return listOfScripts;
       
    77 }
       
    78 
       
    79 -(UISegmentedControl *)topControl {
       
    80     if (topControl == nil) {
       
    81         NSArray *array = [[NSArray alloc] initWithObjects:
       
    82                           NSLocalizedString(@"Scheme",@""),
       
    83                           NSLocalizedString(@"Weapon",@""),
       
    84                           NSLocalizedString(@"Style",@""),nil];
       
    85         UISegmentedControl *controller = [[UISegmentedControl alloc] initWithItems:array];
       
    86         [array release];
       
    87         [controller addTarget:self.tableView action:@selector(reloadData) forControlEvents:UIControlEventValueChanged];
       
    88         controller.segmentedControlStyle = UISegmentedControlStyleBar;
       
    89         controller.tintColor = [UIColor lightGrayColor];
       
    90         controller.selectedSegmentIndex = 0;
       
    91         self.topControl = controller;
       
    92         [controller release];
       
    93     }
       
    94     return topControl;
       
    95 }
       
    96 
       
    97 -(void) viewWillAppear:(BOOL) animated {
       
    98     [super viewWillAppear:animated];
       
    99     [self.tableView reloadData];
       
   100 }
       
   101 
       
   102 #pragma mark -
    36 #pragma mark View lifecycle
   103 #pragma mark View lifecycle
    37 -(void) viewDidLoad {
   104 -(void) viewDidLoad {
    38     [super viewDidLoad];
   105     [super viewDidLoad];
    39 
   106 
    40     CGSize screenSize = [[UIScreen mainScreen] bounds].size;
   107     CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    41     self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
   108     self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
    42 
       
    43     self.selectedScheme = nil;
       
    44     self.selectedWeapon = nil;
       
    45     self.selectedScript = nil;
       
    46     self.scriptCommand = nil;
       
    47 
   109 
    48     if ([self.tableView respondsToSelector:@selector(setBackgroundView:)]) {
   110     if ([self.tableView respondsToSelector:@selector(setBackgroundView:)]) {
    49         if (IS_IPAD())
   111         if (IS_IPAD())
    50             [self.tableView setBackgroundView:nil];
   112             [self.tableView setBackgroundView:nil];
    51         else {
   113         else {
    61 
   123 
    62     self.tableView.separatorColor = [UIColor darkYellowColor];
   124     self.tableView.separatorColor = [UIColor darkYellowColor];
    63     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   125     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    64 }
   126 }
    65 
   127 
    66 -(void) viewWillAppear:(BOOL) animated {
       
    67     [super viewWillAppear:animated];
       
    68 
       
    69     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCHEMES_DIRECTORY() error:NULL];
       
    70     self.listOfSchemes = contentsOfDir;
       
    71 
       
    72     if (self.selectedScheme == nil && [listOfSchemes containsObject:@"Default.plist"])
       
    73         self.selectedScheme = @"Default.plist";
       
    74     
       
    75     contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL];
       
    76     self.listOfWeapons = contentsOfDir;
       
    77     
       
    78     if (self.selectedWeapon == nil && [listOfWeapons containsObject:@"Default.plist"])
       
    79         self.selectedWeapon = @"Default.plist";
       
    80 
       
    81     contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCRIPTS_DIRECTORY() error:NULL];
       
    82     self.listOfScripts = contentsOfDir;
       
    83     self.selectedScript = @"Normal.plist";
       
    84     self.scriptCommand = @"";
       
    85 
       
    86     [self.tableView reloadData];
       
    87 }
       
    88 
       
    89 
       
    90 #pragma mark -
   128 #pragma mark -
    91 #pragma mark Table view data source
   129 #pragma mark Table view data source
    92 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   130 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    93     if (hideSections)
   131     return (self.hideSections ? 0 : 1);
    94         return 0;
       
    95     else
       
    96         return 1;
       
    97 }
   132 }
    98 
   133 
    99 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   134 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   100     if (self.topControl.selectedSegmentIndex == 0)
   135     if (self.topControl.selectedSegmentIndex == 0)
   101         return [self.listOfSchemes count];
   136         return [self.listOfSchemes count];
   165 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   200 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   166     return 50.0;
   201     return 50.0;
   167 }
   202 }
   168 
   203 
   169 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   204 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   170     if (self.topControl == nil) {
       
   171         NSArray *array = [[NSArray alloc] initWithObjects:NSLocalizedString(@"Scheme",@""),NSLocalizedString(@"Weapon",@""),
       
   172                           NSLocalizedString(@"Style",@""),nil];
       
   173         self.topControl = [[UISegmentedControl alloc] initWithItems:array];
       
   174         [array release];
       
   175         [self.topControl addTarget:self.tableView action:@selector(reloadData) forControlEvents:UIControlEventValueChanged];
       
   176         self.topControl.segmentedControlStyle = UISegmentedControlStyleBar;
       
   177         self.topControl.frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 30);
       
   178         self.topControl.center = CGPointMake(self.view.frame.size.width/2, 24);
       
   179         self.topControl.tintColor = [UIColor lightGrayColor];
       
   180         self.topControl.selectedSegmentIndex = 0;
       
   181     }
       
   182 
       
   183     UIView *theView = [[[UIView alloc] init] autorelease];
   205     UIView *theView = [[[UIView alloc] init] autorelease];
       
   206     self.topControl.frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 30);
       
   207     self.topControl.center = CGPointMake(self.view.frame.size.width/2, 24);
   184     [theView addSubview:self.topControl];
   208     [theView addSubview:self.topControl];
   185     return theView;
   209     return theView;
   186 }
   210 }
   187 
   211 
   188 #pragma mark -
   212 #pragma mark -
   211 
   235 
   212         if (index == 0) {
   236         if (index == 0) {
   213             self.lastIndexPath_sc = indexPath;
   237             self.lastIndexPath_sc = indexPath;
   214             self.selectedScheme = [self.listOfSchemes objectAtIndex:newRow];
   238             self.selectedScheme = [self.listOfSchemes objectAtIndex:newRow];
   215 
   239 
       
   240             // also set weaponset when selecting scheme, if set
   216             NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
   241             NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
   217             if ([[settings objectForKey:@"sync_ws"] boolValue]) {
   242             if ([[settings objectForKey:@"sync_ws"] boolValue]) {
   218                 for (NSString *str in self.listOfWeapons) {
   243                 for (NSString *str in self.listOfWeapons) {
   219                     if ([str isEqualToString:self.selectedScheme]) {
   244                     if ([str isEqualToString:self.selectedScheme]) {
   220                         int index = [self.listOfSchemes indexOfObject:str];
   245                         int index = [self.listOfSchemes indexOfObject:str];
   230             self.selectedWeapon = [self.listOfWeapons objectAtIndex:newRow];
   255             self.selectedWeapon = [self.listOfWeapons objectAtIndex:newRow];
   231         } else {
   256         } else {
   232             self.lastIndexPath_lu = indexPath;
   257             self.lastIndexPath_lu = indexPath;
   233             self.selectedScript = [self.listOfScripts objectAtIndex:newRow];
   258             self.selectedScript = [self.listOfScripts objectAtIndex:newRow];
   234 
   259 
   235             NSString *path = [[NSString alloc] initWithFormat:@"%@/%@",SCRIPTS_DIRECTORY(),selectedScript];
   260             // some styles disable or force the choice of a particular scheme/weaponset
       
   261             NSString *path = [[NSString alloc] initWithFormat:@"%@/%@",SCRIPTS_DIRECTORY(),self.selectedScript];
   236             NSDictionary *scriptDict = [[NSDictionary alloc] initWithContentsOfFile:path];
   262             NSDictionary *scriptDict = [[NSDictionary alloc] initWithContentsOfFile:path];
   237             [path release];
   263             [path release];
   238             self.scriptCommand = [scriptDict objectForKey:@"command"];
   264             self.scriptCommand = [scriptDict objectForKey:@"command"];
   239             NSString *scheme = [scriptDict objectForKey:@"scheme"];
   265             NSString *scheme = [scriptDict objectForKey:@"scheme"];
   240             if ([scheme isEqualToString:@""]) {
   266             if ([scheme isEqualToString:@""]) {
   260         [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   286         [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   261     }
   287     }
   262     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
   288     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
   263 }
   289 }
   264 
   290 
       
   291 #pragma mark -
       
   292 #pragma mark called externally to empty or fill the sections completely
   265 -(void) fillSections {
   293 -(void) fillSections {
   266     if (hideSections == YES) {
   294     if (self.hideSections == YES) {
   267         hideSections = NO;
   295         self.hideSections = NO;
   268         NSRange range;
   296         NSRange range;
   269         range.location = 0;
   297         range.location = 0;
   270         range.length = 1;
   298         range.length = 1;
   271         NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
   299         NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
   272         [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
   300         [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
   273         self.selectedScheme = @"Default.plist";
       
   274         self.selectedWeapon = @"Default.plist";
       
   275         self.selectedScript = @"Normal.plist";
       
   276 
       
   277         self.tableView.scrollEnabled = YES;
   301         self.tableView.scrollEnabled = YES;
   278 
   302 
   279         [[self.view viewWithTag:LABEL_TAG] removeFromSuperview];
   303         [[self.view viewWithTag:LABEL_TAG] removeFromSuperview];
   280     }
   304     }
   281 }
   305 }
   285     NSRange range;
   309     NSRange range;
   286     range.location = 0;
   310     range.location = 0;
   287     range.length = 1;
   311     range.length = 1;
   288     NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
   312     NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
   289     [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
   313     [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
   290     self.selectedScheme = @"Default.plist";
       
   291     self.selectedWeapon = @"Default.plist";
       
   292     self.selectedScript = @"Normal.plist";
       
   293 
       
   294     self.tableView.scrollEnabled = NO;
   314     self.tableView.scrollEnabled = NO;
   295 
   315 
   296     CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 60);
   316     CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 60);
   297     UILabel *theLabel = [[UILabel alloc] initWithFrame:frame
   317     UILabel *theLabel = [[UILabel alloc] initWithFrame:frame
   298                                               andTitle:NSLocalizedString(@"Missions don't need further configuration",@"")];
   318                                               andTitle:NSLocalizedString(@"Missions don't need further configuration",@"")];
   309 -(void) didReceiveMemoryWarning {
   329 -(void) didReceiveMemoryWarning {
   310     if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) {
   330     if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) {
   311         self.lastIndexPath_sc = nil;
   331         self.lastIndexPath_sc = nil;
   312         self.lastIndexPath_we = nil;
   332         self.lastIndexPath_we = nil;
   313         self.lastIndexPath_lu = nil;
   333         self.lastIndexPath_lu = nil;
   314         self.listOfSchemes = nil;
   334         self.selectedScheme = nil;
   315         self.listOfWeapons = nil;
   335         self.selectedWeapon = nil;
   316         self.listOfScripts = nil;
   336         self.selectedScript = nil;
       
   337         self.scriptCommand = nil;
   317         self.topControl = nil;
   338         self.topControl = nil;
   318         MSG_MEMCLEAN();
   339     }
   319     }
   340     self.listOfSchemes = nil;
       
   341     self.listOfWeapons = nil;
       
   342     self.listOfScripts = nil;
       
   343     MSG_MEMCLEAN();
   320     [super didReceiveMemoryWarning];
   344     [super didReceiveMemoryWarning];
   321 }
   345 }
   322 
   346 
   323 -(void) viewDidUnload {
   347 -(void) viewDidUnload {
   324     self.listOfSchemes = nil;
   348     self.listOfSchemes = nil;
   334     self.topControl = nil;
   358     self.topControl = nil;
   335     MSG_DIDUNLOAD();
   359     MSG_DIDUNLOAD();
   336     [super viewDidUnload];
   360     [super viewDidUnload];
   337 }
   361 }
   338 
   362 
   339 
       
   340 -(void) dealloc {
   363 -(void) dealloc {
   341     releaseAndNil(listOfSchemes);
   364     releaseAndNil(listOfSchemes);
   342     releaseAndNil(listOfWeapons);
   365     releaseAndNil(listOfWeapons);
   343     releaseAndNil(listOfScripts);
   366     releaseAndNil(listOfScripts);
   344     releaseAndNil(lastIndexPath_sc);
   367     releaseAndNil(lastIndexPath_sc);