project_files/HedgewarsMobile/Classes/SchemeWeaponConfigViewController.m
changeset 5455 df05cdb998ed
parent 5451 e359a79e3d08
child 6078 8c0cc07731e5
equal deleted inserted replaced
5453:ea6f7631df4a 5455:df05cdb998ed
    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;
    28 @synthesize listOfSchemes, listOfWeapons, listOfScripts, lastIndexPath_sc, lastIndexPath_we, lastIndexPath_lu,
       
    29             selectedScheme, selectedWeapon, selectedScript, scriptCommand, topControl, hideSections;
    29 
    30 
    30 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    31 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    31     return rotationManager(interfaceOrientation);
    32     return rotationManager(interfaceOrientation);
    32 }
    33 }
    33 
    34 
    39     CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    40     CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    40     self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
    41     self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
    41 
    42 
    42     self.selectedScheme = nil;
    43     self.selectedScheme = nil;
    43     self.selectedWeapon = nil;
    44     self.selectedWeapon = nil;
       
    45     self.selectedScript = nil;
       
    46     self.scriptCommand = nil;
    44 
    47 
    45     if ([self.tableView respondsToSelector:@selector(setBackgroundView:)]) {
    48     if ([self.tableView respondsToSelector:@selector(setBackgroundView:)]) {
    46         if (IS_IPAD())
    49         if (IS_IPAD())
    47             [self.tableView setBackgroundView:nil];
    50             [self.tableView setBackgroundView:nil];
    48         else {
    51         else {
    72     contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL];
    75     contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL];
    73     self.listOfWeapons = contentsOfDir;
    76     self.listOfWeapons = contentsOfDir;
    74     
    77     
    75     if (self.selectedWeapon == nil && [listOfWeapons containsObject:@"Default.plist"])
    78     if (self.selectedWeapon == nil && [listOfWeapons containsObject:@"Default.plist"])
    76         self.selectedWeapon = @"Default.plist";
    79         self.selectedWeapon = @"Default.plist";
    77     
    80 
       
    81     contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCRIPTS_DIRECTORY() error:NULL];
       
    82     self.listOfScripts = contentsOfDir;
       
    83     self.selectedScript = @"Normal.plist";
       
    84     self.scriptCommand = @"";
       
    85 
    78     [self.tableView reloadData];
    86     [self.tableView reloadData];
    79 }
    87 }
    80 
    88 
    81 
    89 
    82 #pragma mark -
    90 #pragma mark -
    83 #pragma mark Table view data source
    91 #pragma mark Table view data source
    84 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    92 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    85     if (hideSections)
    93     if (hideSections)
    86         return 0;
    94         return 0;
    87     else
    95     else
    88         return 2;
    96         return 1;
    89 }
    97 }
    90 
    98 
    91 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    99 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    92     if (section == 0)
   100     if (self.topControl.selectedSegmentIndex == 0)
    93         return [self.listOfSchemes count];
   101         return [self.listOfSchemes count];
       
   102     else if (self.topControl.selectedSegmentIndex == 1)
       
   103         return [self.listOfWeapons count];
    94     else
   104     else
    95         return [self.listOfWeapons count];
   105         return [self.listOfScripts count];
    96 }
   106 }
    97 
   107 
    98 // Customize the appearance of table view cells.
   108 // Customize the appearance of table view cells.
    99 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   109 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   100     static NSString *CellIdentifier = @"Cell";
   110     static NSString *CellIdentifier = @"Cell";
       
   111     NSInteger index = self.topControl.selectedSegmentIndex;
   101     NSInteger row = [indexPath row];
   112     NSInteger row = [indexPath row];
   102     NSInteger section = [indexPath section];
       
   103 
   113 
   104     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   114     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   105     if (cell == nil)
   115     if (cell == nil)
   106         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
   116         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
   107 
   117 
   108     cell.accessoryView = nil;
   118     cell.accessoryView = nil;
   109     if (0 == section) {
   119     if (0 == index) {
   110         cell.textLabel.text = [[self.listOfSchemes objectAtIndex:row] stringByDeletingPathExtension];
   120         cell.textLabel.text = [[self.listOfSchemes objectAtIndex:row] stringByDeletingPathExtension];
   111         NSString *str = [NSString stringWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),[self.listOfSchemes objectAtIndex:row]];
   121         NSString *str = [NSString stringWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),[self.listOfSchemes objectAtIndex:row]];
   112         NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:str];
   122         NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:str];
   113         cell.detailTextLabel.text = [dict objectForKey:@"description"];
   123         cell.detailTextLabel.text = [dict objectForKey:@"description"];
   114         [dict release];
   124         [dict release];
   116             UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]];
   126             UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]];
   117             cell.accessoryView = checkbox;
   127             cell.accessoryView = checkbox;
   118             [checkbox release];
   128             [checkbox release];
   119             self.lastIndexPath_sc = indexPath;
   129             self.lastIndexPath_sc = indexPath;
   120         }
   130         }
   121     } else {
   131     } else if (1 == index) {
   122         cell.textLabel.text = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension];
   132         cell.textLabel.text = [[self.listOfWeapons objectAtIndex:row] stringByDeletingPathExtension];
   123         NSString *str = [NSString stringWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]];
   133         NSString *str = [NSString stringWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),[self.listOfWeapons objectAtIndex:row]];
   124         NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:str];
   134         NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:str];
   125         cell.detailTextLabel.text = [dict objectForKey:@"description"];
   135         cell.detailTextLabel.text = [dict objectForKey:@"description"];
   126         [dict release];
   136         [dict release];
   128             UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]];
   138             UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]];
   129             cell.accessoryView = checkbox;
   139             cell.accessoryView = checkbox;
   130             [checkbox release];
   140             [checkbox release];
   131             self.lastIndexPath_we = indexPath;
   141             self.lastIndexPath_we = indexPath;
   132         }
   142         }
       
   143     } else {
       
   144         cell.textLabel.text = [[self.listOfScripts objectAtIndex:row] stringByDeletingPathExtension];
       
   145         NSString *str = [NSString stringWithFormat:@"%@/%@",SCRIPTS_DIRECTORY(),[self.listOfScripts objectAtIndex:row]];
       
   146         NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:str];
       
   147         cell.detailTextLabel.text = [dict objectForKey:@"description"];
       
   148         [dict release];
       
   149         if ([[self.listOfScripts objectAtIndex:row] isEqualToString:self.selectedScript]) {
       
   150             UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]];
       
   151             cell.accessoryView = checkbox;
       
   152             [checkbox release];
       
   153             self.lastIndexPath_lu = indexPath;
       
   154         }
   133     }
   155     }
   134 
   156 
   135     cell.backgroundColor = UICOLOR_HW_ALMOSTBLACK;
   157     cell.backgroundColor = UICOLOR_HW_ALMOSTBLACK;
   136     cell.textLabel.textColor = UICOLOR_HW_YELLOW_TEXT;
   158     cell.textLabel.textColor = UICOLOR_HW_YELLOW_TEXT;
   137     cell.detailTextLabel.textColor = [UIColor whiteColor];
   159     cell.detailTextLabel.textColor = [UIColor whiteColor];
       
   160     cell.textLabel.adjustsFontSizeToFitWidth = YES;
       
   161     cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
   138     return cell;
   162     return cell;
   139 }
   163 }
   140 
   164 
   141 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   165 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   142     return 40.0;
   166     return 50.0;
   143 }
   167 }
   144 
   168 
   145 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   169 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   146     CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 30);
   170     if (self.topControl == nil) {
   147     NSString *text;
   171         NSArray *array = [[NSArray alloc] initWithObjects:NSLocalizedString(@"Scheme",@""),NSLocalizedString(@"Weapon",@""),
   148     if (section == 0) 
   172                           NSLocalizedString(@"Style",@""),nil];
   149         text = NSLocalizedString(@"Schemes",@"");
   173         self.topControl = [[UISegmentedControl alloc] initWithItems:array];
   150     else
   174         [array release];
   151         text = NSLocalizedString(@"Weapons",@"");
   175         [self.topControl addTarget:self.tableView action:@selector(reloadData) forControlEvents:UIControlEventValueChanged];
   152 
   176         self.topControl.segmentedControlStyle = UISegmentedControlStyleBar;
   153     UILabel *theLabel = createBlueLabel(text, frame);
   177         self.topControl.frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 30);
   154     theLabel.center = CGPointMake(self.view.frame.size.width/2, 20);
   178         self.topControl.center = CGPointMake(self.view.frame.size.width/2, 24);
       
   179         self.topControl.tintColor = [UIColor lightGrayColor];
       
   180         self.topControl.selectedSegmentIndex = 0;
       
   181     }
   155 
   182 
   156     UIView *theView = [[[UIView alloc] init] autorelease];
   183     UIView *theView = [[[UIView alloc] init] autorelease];
   157     [theView addSubview:theLabel];
   184     [theView addSubview:self.topControl];
   158     [theLabel release];
       
   159     return theView;
   185     return theView;
   160 }
   186 }
   161 
   187 
   162 #pragma mark -
   188 #pragma mark -
   163 #pragma mark Table view delegate
   189 #pragma mark Table view delegate
   164 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   190 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   165     NSIndexPath *lastIndexPath;
   191     NSIndexPath *lastIndexPath;
   166     if ([indexPath section] == 0)
   192     NSInteger index = self.topControl.selectedSegmentIndex;
       
   193     if (index == 0)
   167         lastIndexPath = self.lastIndexPath_sc;
   194         lastIndexPath = self.lastIndexPath_sc;
       
   195     else if (index == 1)
       
   196         lastIndexPath = self.lastIndexPath_we;
   168     else
   197     else
   169         lastIndexPath = self.lastIndexPath_we;
   198         lastIndexPath = self.lastIndexPath_lu;
   170 
   199 
   171     int newRow = [indexPath row];
   200     int newRow = [indexPath row];
   172     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   201     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   173 
   202 
   174     if (newRow != oldRow) {
   203     if (newRow != oldRow) {
   178         newCell.accessoryView = checkbox;
   207         newCell.accessoryView = checkbox;
   179         [checkbox release];
   208         [checkbox release];
   180         UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
   209         UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
   181         oldCell.accessoryView = nil;
   210         oldCell.accessoryView = nil;
   182 
   211 
   183         if ([indexPath section] == 0) {
   212         if (index == 0) {
   184             self.lastIndexPath_sc = indexPath;
   213             self.lastIndexPath_sc = indexPath;
   185             self.selectedScheme = [self.listOfSchemes objectAtIndex:newRow];
   214             self.selectedScheme = [self.listOfSchemes objectAtIndex:newRow];
   186 
   215 
   187             NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
   216             NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
   188             if ([[settings objectForKey:@"sync_ws"] boolValue]) {
   217             if ([[settings objectForKey:@"sync_ws"] boolValue]) {
   194                         [self.tableView reloadData];
   223                         [self.tableView reloadData];
   195                         break;
   224                         break;
   196                     }
   225                     }
   197                 }
   226                 }
   198             }
   227             }
   199         } else {
   228         } else if (index == 1) {
   200             self.lastIndexPath_we = indexPath;
   229             self.lastIndexPath_we = indexPath;
   201             self.selectedWeapon = [self.listOfWeapons objectAtIndex:newRow];
   230             self.selectedWeapon = [self.listOfWeapons objectAtIndex:newRow];
       
   231         } else {
       
   232             self.lastIndexPath_lu = indexPath;
       
   233             self.selectedScript = [self.listOfScripts objectAtIndex:newRow];
       
   234 
       
   235             NSString *path = [[NSString alloc] initWithFormat:@"%@/%@",SCRIPTS_DIRECTORY(),selectedScript];
       
   236             NSDictionary *scriptDict = [[NSDictionary alloc] initWithContentsOfFile:path];
       
   237             [path release];
       
   238             self.scriptCommand = [scriptDict objectForKey:@"command"];
       
   239             NSString *scheme = [scriptDict objectForKey:@"scheme"];
       
   240             if ([scheme isEqualToString:@""]) {
       
   241                 self.selectedScheme = @"Default.plist";
       
   242                 [self.topControl setEnabled:NO forSegmentAtIndex:0];
       
   243             } else {
       
   244                 self.selectedScheme = scheme;
       
   245                 [self.topControl setEnabled:YES forSegmentAtIndex:0];
       
   246             }
       
   247 
       
   248             NSString *weapon = [scriptDict objectForKey:@"weapon"];
       
   249             if ([weapon isEqualToString:@""]) {
       
   250                 self.selectedWeapon = @"Default.plist";
       
   251                 [self.topControl setEnabled:NO forSegmentAtIndex:1];
       
   252             } else {
       
   253                 self.selectedWeapon = weapon;
       
   254                 [self.topControl setEnabled:YES forSegmentAtIndex:1];
       
   255             }
       
   256 
       
   257             [scriptDict release];
   202         }
   258         }
   203 
   259 
   204         [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   260         [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   205     }
   261     }
   206     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
   262     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
   209 -(void) fillSections {
   265 -(void) fillSections {
   210     if (hideSections == YES) {
   266     if (hideSections == YES) {
   211         hideSections = NO;
   267         hideSections = NO;
   212         NSRange range;
   268         NSRange range;
   213         range.location = 0;
   269         range.location = 0;
   214         range.length = 2;
   270         range.length = 1;
   215         NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
   271         NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
   216         [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
   272         [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
   217         self.selectedScheme = @"Default.plist";
   273         self.selectedScheme = @"Default.plist";
   218         self.selectedWeapon = @"Default.plist";
   274         self.selectedWeapon = @"Default.plist";
       
   275         self.selectedScript = @"Normal.plist";
   219 
   276 
   220         self.tableView.scrollEnabled = YES;
   277         self.tableView.scrollEnabled = YES;
   221 
   278 
   222         [[self.view viewWithTag:LABEL_TAG] removeFromSuperview];
   279         [[self.view viewWithTag:LABEL_TAG] removeFromSuperview];
   223     }
   280     }
   225 
   282 
   226 -(void) emptySections {
   283 -(void) emptySections {
   227     hideSections = YES;
   284     hideSections = YES;
   228     NSRange range;
   285     NSRange range;
   229     range.location = 0;
   286     range.location = 0;
   230     range.length = 2;
   287     range.length = 1;
   231     NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
   288     NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
   232     [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
   289     [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
   233     self.selectedScheme = @"Default.plist";
   290     self.selectedScheme = @"Default.plist";
   234     self.selectedWeapon = @"Default.plist";
   291     self.selectedWeapon = @"Default.plist";
       
   292     self.selectedScript = @"Normal.plist";
   235 
   293 
   236     self.tableView.scrollEnabled = NO;
   294     self.tableView.scrollEnabled = NO;
   237 
   295 
   238     CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 60);
   296     CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 60);
   239     UILabel *theLabel = createBlueLabel(NSLocalizedString(@"Missions don't need further configuration",@""), frame);
   297     UILabel *theLabel = createBlueLabel(NSLocalizedString(@"Missions don't need further configuration",@""), frame);
   249 #pragma mark Memory management
   307 #pragma mark Memory management
   250 -(void) didReceiveMemoryWarning {
   308 -(void) didReceiveMemoryWarning {
   251     if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) {
   309     if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) {
   252         self.lastIndexPath_sc = nil;
   310         self.lastIndexPath_sc = nil;
   253         self.lastIndexPath_we = nil;
   311         self.lastIndexPath_we = nil;
       
   312         self.lastIndexPath_lu = nil;
   254         self.listOfSchemes = nil;
   313         self.listOfSchemes = nil;
   255         self.listOfWeapons = nil;
   314         self.listOfWeapons = nil;
       
   315         self.listOfScripts = nil;
       
   316         self.topControl = nil;
   256         MSG_MEMCLEAN();
   317         MSG_MEMCLEAN();
   257     }
   318     }
   258     [super didReceiveMemoryWarning];
   319     [super didReceiveMemoryWarning];
   259 }
   320 }
   260 
   321 
   261 -(void) viewDidUnload {
   322 -(void) viewDidUnload {
   262     self.listOfSchemes = nil;
   323     self.listOfSchemes = nil;
   263     self.listOfWeapons = nil;
   324     self.listOfWeapons = nil;
       
   325     self.listOfScripts = nil;
   264     self.lastIndexPath_sc = nil;
   326     self.lastIndexPath_sc = nil;
   265     self.lastIndexPath_we = nil;
   327     self.lastIndexPath_we = nil;
       
   328     self.lastIndexPath_lu = nil;
   266     self.selectedScheme = nil;
   329     self.selectedScheme = nil;
   267     self.selectedWeapon = nil;
   330     self.selectedWeapon = nil;
       
   331     self.selectedScript = nil;
       
   332     self.scriptCommand = nil;
       
   333     self.topControl = nil;
   268     MSG_DIDUNLOAD();
   334     MSG_DIDUNLOAD();
   269     [super viewDidUnload];
   335     [super viewDidUnload];
   270 }
   336 }
   271 
   337 
   272 
   338 
   273 -(void) dealloc {
   339 -(void) dealloc {
   274     releaseAndNil(listOfSchemes);
   340     releaseAndNil(listOfSchemes);
   275     releaseAndNil(listOfWeapons);
   341     releaseAndNil(listOfWeapons);
       
   342     releaseAndNil(listOfScripts);
   276     releaseAndNil(lastIndexPath_sc);
   343     releaseAndNil(lastIndexPath_sc);
   277     releaseAndNil(lastIndexPath_we);
   344     releaseAndNil(lastIndexPath_we);
       
   345     releaseAndNil(lastIndexPath_lu);
   278     releaseAndNil(selectedScheme);
   346     releaseAndNil(selectedScheme);
   279     releaseAndNil(selectedWeapon);
   347     releaseAndNil(selectedWeapon);
       
   348     releaseAndNil(selectedScript);
       
   349     releaseAndNil(scriptCommand);
       
   350     releaseAndNil(topControl);
   280     [super dealloc];
   351     [super dealloc];
   281 }
   352 }
   282 
   353 
   283 
   354 
   284 @end
   355 @end