project_files/HedgewarsMobile/Classes/SchemeWeaponConfigViewController.m
changeset 6115 485cfecadc9a
parent 6108 7a8da11a6144
child 6209 074ab6ebcb3e
equal deleted inserted replaced
6114:443cce9e060f 6115:485cfecadc9a
    23 #import <QuartzCore/QuartzCore.h>
    23 #import <QuartzCore/QuartzCore.h>
    24 
    24 
    25 
    25 
    26 #define LABEL_TAG 57423
    26 #define LABEL_TAG 57423
    27 
    27 
       
    28 static SchemeWeaponConfigViewController *controllerInstance;
       
    29 
    28 @implementation SchemeWeaponConfigViewController
    30 @implementation SchemeWeaponConfigViewController
    29 @synthesize listOfSchemes, listOfWeapons, listOfScripts, lastIndexPath_sc, lastIndexPath_we, lastIndexPath_lu,
    31 @synthesize tableView, listOfSchemes, listOfWeapons, listOfScripts, lastIndexPath_sc, lastIndexPath_we, lastIndexPath_lu,
    30             selectedScheme, selectedWeapon, selectedScript, scriptCommand, topControl, hideSections;
    32             selectedScheme, selectedWeapon, selectedScript, scriptCommand, topControl, sectionsHidden;
    31 
    33 
    32 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    34 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    33     return rotationManager(interfaceOrientation);
    35     return rotationManager(interfaceOrientation);
    34 }
    36 }
    35 
    37 
    93         [controller release];
    95         [controller release];
    94     }
    96     }
    95     return topControl;
    97     return topControl;
    96 }
    98 }
    97 
    99 
    98 -(void) viewWillAppear:(BOOL) animated {
       
    99     [super viewWillAppear:animated];
       
   100     [self.tableView reloadData];
       
   101 }
       
   102 
       
   103 #pragma mark -
   100 #pragma mark -
   104 #pragma mark View lifecycle
   101 #pragma mark View lifecycle
   105 -(void) viewDidLoad {
   102 -(void) viewDidLoad {
   106     [super viewDidLoad];
   103     self.sectionsHidden = NO;
   107 
   104 
   108     CGSize screenSize = [[UIScreen mainScreen] bounds].size;
   105     UITableView *aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
   109     self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
   106                                                            style:UITableViewStyleGrouped];
   110 
   107     aTableView.delegate = self;
       
   108     aTableView.dataSource = self;
   111     if (IS_IPAD()) {
   109     if (IS_IPAD()) {
   112         [self.tableView setBackgroundColorForAnyTable:[UIColor darkBlueColorTransparent]];
   110         [aTableView setBackgroundColorForAnyTable:[UIColor darkBlueColorTransparent]];
   113         self.tableView.layer.borderColor = [[UIColor darkYellowColor] CGColor];
   111         aTableView.layer.borderColor = [[UIColor darkYellowColor] CGColor];
   114         self.tableView.layer.borderWidth = 2.7f;
   112         aTableView.layer.borderWidth = 2.7f;
   115         self.tableView.layer.cornerRadius = 8;
   113         aTableView.layer.cornerRadius = 8;
   116         self.tableView.contentInset = UIEdgeInsetsMake(5, 0, 5, 0);
   114         aTableView.contentInset = UIEdgeInsetsMake(5, 0, 5, 0);
   117     } else {
   115     } else {
   118         UIImage *backgroundImage = [[UIImage alloc] initWithContentsOfFile:@"background~iphone.png"];
   116         UIImage *backgroundImage = [[UIImage alloc] initWithContentsOfFile:@"background~iphone.png"];
   119         UIImageView *background = [[UIImageView alloc] initWithImage:backgroundImage];
   117         UIImageView *background = [[UIImageView alloc] initWithImage:backgroundImage];
   120         [backgroundImage release];
   118         [backgroundImage release];
   121         [self.tableView setBackgroundView:background];
   119         [aTableView setBackgroundView:background];
   122         [background release];
   120         [background release];
   123     }
   121     }
   124 
   122 
   125     self.tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
   123     aTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
   126     self.tableView.separatorColor = [UIColor whiteColor];
   124     aTableView.separatorColor = [UIColor whiteColor];
   127     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   125     aTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
       
   126     self.tableView = aTableView;
       
   127     [aTableView release];
       
   128     [self.view addSubview:self.tableView];
       
   129 
       
   130     [super viewDidLoad];
       
   131     controllerInstance = self;
   128 }
   132 }
   129 
   133 
   130 #pragma mark -
   134 #pragma mark -
   131 #pragma mark Table view data source
   135 #pragma mark Table view data source
   132 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   136 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   133     return (self.hideSections ? 0 : 1);
   137     return (self.sectionsHidden ? 0 : 1);
   134 }
   138 }
   135 
   139 
   136 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   140 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   137     if (self.topControl.selectedSegmentIndex == 0)
   141     if (self.topControl.selectedSegmentIndex == 0)
   138         return [self.listOfSchemes count];
   142         return [self.listOfSchemes count];
   141     else
   145     else
   142         return [self.listOfScripts count];
   146         return [self.listOfScripts count];
   143 }
   147 }
   144 
   148 
   145 // Customize the appearance of table view cells.
   149 // Customize the appearance of table view cells.
   146 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   150 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   147     static NSString *CellIdentifier = @"Cell";
   151     static NSString *CellIdentifier = @"Cell";
   148     NSInteger index = self.topControl.selectedSegmentIndex;
   152     NSInteger index = self.topControl.selectedSegmentIndex;
   149     NSInteger row = [indexPath row];
   153     NSInteger row = [indexPath row];
   150 
   154 
   151     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   155     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
   152     if (cell == nil)
   156     if (cell == nil)
   153         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
   157         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
   154 
   158 
   155     cell.accessoryView = nil;
   159     cell.accessoryView = nil;
   156     if (0 == index) {
   160     if (0 == index) {
   245                 for (NSString *str in self.listOfWeapons) {
   249                 for (NSString *str in self.listOfWeapons) {
   246                     if ([str isEqualToString:self.selectedScheme]) {
   250                     if ([str isEqualToString:self.selectedScheme]) {
   247                         int index = [self.listOfSchemes indexOfObject:str];
   251                         int index = [self.listOfSchemes indexOfObject:str];
   248                         self.selectedWeapon = str;
   252                         self.selectedWeapon = str;
   249                         self.lastIndexPath_we = [NSIndexPath indexPathForRow:index inSection:1];
   253                         self.lastIndexPath_we = [NSIndexPath indexPathForRow:index inSection:1];
   250                         [self.tableView reloadData];
       
   251                         break;
   254                         break;
   252                     }
   255                     }
   253                 }
   256                 }
   254             }
   257             }
   255         } else if (index == 1) {
   258         } else if (index == 1) {
   290     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
   293     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
   291 }
   294 }
   292 
   295 
   293 #pragma mark -
   296 #pragma mark -
   294 #pragma mark called externally to empty or fill the sections completely
   297 #pragma mark called externally to empty or fill the sections completely
   295 -(void) fillSections {
   298 +(void) fillInstanceSections {
   296     if (self.hideSections == YES) {
   299     if (controllerInstance.sectionsHidden == YES) {
   297         self.hideSections = NO;
   300         controllerInstance.sectionsHidden = NO;
   298         NSRange range;
   301         NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)];
   299         range.location = 0;
   302         [controllerInstance.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
   300         range.length = 1;
   303         controllerInstance.tableView.scrollEnabled = YES;
   301         NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
   304 
   302         [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
   305         [[controllerInstance.view viewWithTag:LABEL_TAG] removeFromSuperview];
   303         self.tableView.scrollEnabled = YES;
   306     }
   304 
   307 }
   305         [[self.view viewWithTag:LABEL_TAG] removeFromSuperview];
   308 
   306     }
   309 +(void) emptyInstanceSections {
   307 }
   310     if (controllerInstance.sectionsHidden == NO) {
   308 
   311         controllerInstance.sectionsHidden = YES;
   309 -(void) emptySections {
   312         NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)];
   310     hideSections = YES;
   313         [controllerInstance.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
   311     NSRange range;
   314         controllerInstance.tableView.scrollEnabled = NO;
   312     range.location = 0;
   315 
   313     range.length = 1;
   316         CGRect frame = CGRectMake(0, 0, controllerInstance.view.frame.size.width * 80/100, 60);
   314     NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
   317         UILabel *theLabel = [[UILabel alloc] initWithFrame:frame
   315     [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
   318                                                   andTitle:NSLocalizedString(@"Missions don't need further configuration",@"")];
   316     self.tableView.scrollEnabled = NO;
   319         theLabel.center = CGPointMake(controllerInstance.view.frame.size.width/2, controllerInstance.view.frame.size.height/2);
   317 
   320         theLabel.numberOfLines = 2;
   318     CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 60);
   321         theLabel.tag = LABEL_TAG;
   319     UILabel *theLabel = [[UILabel alloc] initWithFrame:frame
   322 
   320                                               andTitle:NSLocalizedString(@"Missions don't need further configuration",@"")];
   323         [controllerInstance.view addSubview:theLabel];
   321     theLabel.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
   324         [theLabel release];
   322     theLabel.numberOfLines = 2;
   325     }
   323     theLabel.tag = LABEL_TAG;
       
   324 
       
   325     [self.view addSubview:theLabel];
       
   326     [theLabel release];
       
   327 }
   326 }
   328 
   327 
   329 #pragma mark -
   328 #pragma mark -
   330 #pragma mark Memory management
   329 #pragma mark Memory management
   331 -(void) didReceiveMemoryWarning {
   330 -(void) didReceiveMemoryWarning {
   332     if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) {
   331     if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) {
       
   332         self.tableView = nil;
   333         self.lastIndexPath_sc = nil;
   333         self.lastIndexPath_sc = nil;
   334         self.lastIndexPath_we = nil;
   334         self.lastIndexPath_we = nil;
   335         self.lastIndexPath_lu = nil;
   335         self.lastIndexPath_lu = nil;
   336         self.selectedScheme = nil;
   336         self.selectedScheme = nil;
   337         self.selectedWeapon = nil;
   337         self.selectedWeapon = nil;
   345     MSG_MEMCLEAN();
   345     MSG_MEMCLEAN();
   346     [super didReceiveMemoryWarning];
   346     [super didReceiveMemoryWarning];
   347 }
   347 }
   348 
   348 
   349 -(void) viewDidUnload {
   349 -(void) viewDidUnload {
       
   350     self.tableView = nil;
   350     self.listOfSchemes = nil;
   351     self.listOfSchemes = nil;
   351     self.listOfWeapons = nil;
   352     self.listOfWeapons = nil;
   352     self.listOfScripts = nil;
   353     self.listOfScripts = nil;
   353     self.lastIndexPath_sc = nil;
   354     self.lastIndexPath_sc = nil;
   354     self.lastIndexPath_we = nil;
   355     self.lastIndexPath_we = nil;
   361     MSG_DIDUNLOAD();
   362     MSG_DIDUNLOAD();
   362     [super viewDidUnload];
   363     [super viewDidUnload];
   363 }
   364 }
   364 
   365 
   365 -(void) dealloc {
   366 -(void) dealloc {
       
   367     releaseAndNil(tableView);
   366     releaseAndNil(listOfSchemes);
   368     releaseAndNil(listOfSchemes);
   367     releaseAndNil(listOfWeapons);
   369     releaseAndNil(listOfWeapons);
   368     releaseAndNil(listOfScripts);
   370     releaseAndNil(listOfScripts);
   369     releaseAndNil(lastIndexPath_sc);
   371     releaseAndNil(lastIndexPath_sc);
   370     releaseAndNil(lastIndexPath_we);
   372     releaseAndNil(lastIndexPath_we);