project_files/HedgewarsMobile/Classes/SingleTeamViewController.m
changeset 3547 02875b1145b7
parent 3546 ccf4854df294
child 3574 78fc6e61570b
equal deleted inserted replaced
3546:ccf4854df294 3547:02875b1145b7
       
     1 //
       
     2 //  SingleTeamViewController.m
       
     3 //  HedgewarsMobile
       
     4 //
       
     5 //  Created by Vittorio on 02/04/10.
       
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
       
     7 //
       
     8 
       
     9 #import "SingleTeamViewController.h"
       
    10 #import "HogHatViewController.h"
       
    11 #import "GravesViewController.h"
       
    12 #import "VoicesViewController.h"
       
    13 #import "FortsViewController.h"
       
    14 #import "FlagsViewController.h"
       
    15 #import "LevelViewController.h"
       
    16 #import "CommodityFunctions.h"
       
    17 #import "UIImageExtra.h"
       
    18 
       
    19 #define TEAMNAME_TAG 1234
       
    20 
       
    21 @implementation SingleTeamViewController
       
    22 @synthesize teamDictionary, normalHogSprite, secondaryItems, textFieldBeingEdited, teamName;
       
    23 
       
    24 
       
    25 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
       
    26     return rotationManager(interfaceOrientation);
       
    27 }
       
    28 
       
    29 
       
    30 #pragma mark -
       
    31 #pragma mark textfield methods
       
    32 -(void) cancel:(id) sender {
       
    33     if (textFieldBeingEdited != nil)
       
    34         [self.textFieldBeingEdited resignFirstResponder];
       
    35 }
       
    36 
       
    37 // set the new value
       
    38 -(BOOL) save:(id) sender {
       
    39     NSInteger index = textFieldBeingEdited.tag;
       
    40     
       
    41     if (textFieldBeingEdited != nil) {
       
    42         if (TEAMNAME_TAG == index) {
       
    43             [self.teamDictionary setObject:textFieldBeingEdited.text forKey:@"teamname"];
       
    44         } else {
       
    45             //replace the old value with the new one            
       
    46             NSMutableDictionary *hog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:index];
       
    47             [hog setObject:textFieldBeingEdited.text forKey:@"hogname"];
       
    48         }
       
    49         
       
    50         isWriteNeeded = YES;
       
    51         [self.textFieldBeingEdited resignFirstResponder];
       
    52         return YES;
       
    53     }
       
    54     return NO;
       
    55 }
       
    56 
       
    57 // the textfield is being modified, update the navigation controller
       
    58 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{   
       
    59     self.textFieldBeingEdited = aTextField;
       
    60 
       
    61     UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"from the hog name table")
       
    62                                                                      style:UIBarButtonItemStylePlain
       
    63                                                                     target:self
       
    64                                                                     action:@selector(cancel:)];
       
    65     self.navigationItem.leftBarButtonItem = cancelButton;
       
    66     [cancelButton release];
       
    67     
       
    68     UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"from the hog name table")
       
    69                                                                      style:UIBarButtonItemStyleDone
       
    70                                                                     target:self
       
    71                                                                     action:@selector(save:)];
       
    72     self.navigationItem.rightBarButtonItem = saveButton;
       
    73     [saveButton release];
       
    74 }
       
    75 
       
    76 // the textfield has been modified, check for empty strings and restore original navigation bar
       
    77 -(void) textFieldDidEndEditing:(UITextField *)aTextField{
       
    78     if ([textFieldBeingEdited.text length] == 0) 
       
    79         textFieldBeingEdited.text = [NSString stringWithFormat:@"hedgehog %d",textFieldBeingEdited.tag];
       
    80 
       
    81     self.textFieldBeingEdited = nil;
       
    82     self.navigationItem.rightBarButtonItem = self.navigationItem.backBarButtonItem;
       
    83     self.navigationItem.leftBarButtonItem = nil;
       
    84 }
       
    85 
       
    86 // limit the size of the field to 64 characters like in original frontend
       
    87 -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
       
    88     int limit = 64;
       
    89     return !([textField.text length] > limit && [string length] > range.length);
       
    90 }
       
    91 
       
    92 
       
    93 #pragma mark -
       
    94 #pragma mark View lifecycle
       
    95 -(void) viewDidLoad {
       
    96     [super viewDidLoad];
       
    97     
       
    98     // labels for the entries
       
    99     NSArray *array = [[NSArray alloc] initWithObjects:
       
   100                       NSLocalizedString(@"Grave",@""),
       
   101                       NSLocalizedString(@"Voice",@""),
       
   102                       NSLocalizedString(@"Fort",@""),
       
   103                       NSLocalizedString(@"Flag",@""),
       
   104                       NSLocalizedString(@"Level",@""),nil];
       
   105     self.secondaryItems = array;
       
   106     [array release];
       
   107 
       
   108     // load the base hog image, drawing will occure in cellForRow...
       
   109     NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()];
       
   110     UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)];
       
   111     [normalHogFile release];
       
   112     self.normalHogSprite = hogSprite;
       
   113     [hogSprite release];
       
   114     
       
   115     // listen if any childController modifies the plist and write it if needed
       
   116     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setWriteNeeded) name:@"setWriteNeedTeams" object:nil];
       
   117     isWriteNeeded = NO;
       
   118 }
       
   119 
       
   120 -(void) viewWillAppear:(BOOL)animated {
       
   121     [super viewWillAppear:animated];
       
   122     
       
   123     // load data about the team and write if there has been a change
       
   124     if (isWriteNeeded) 
       
   125         [self writeFile];
       
   126     
       
   127     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title];
       
   128     NSMutableDictionary *teamDict = [[NSMutableDictionary alloc] initWithContentsOfFile:teamFile];
       
   129     self.teamDictionary = teamDict;
       
   130     [teamDict release];
       
   131     [teamFile release];
       
   132     
       
   133     self.teamName = self.title;
       
   134     
       
   135     [self.tableView reloadData];
       
   136 }
       
   137 
       
   138 // write on file if there has been a change
       
   139 -(void) viewWillDisappear:(BOOL)animated {
       
   140     [super viewWillDisappear:animated];
       
   141 
       
   142     // end the editing of the current field
       
   143     if (textFieldBeingEdited != nil) {
       
   144         [self save:nil];
       
   145     }
       
   146     
       
   147     if (isWriteNeeded) 
       
   148         [self writeFile];        
       
   149 }
       
   150 
       
   151 #pragma mark -
       
   152 // needed by other classes to warn about a user change
       
   153 -(void) setWriteNeeded {
       
   154     isWriteNeeded = YES;
       
   155 }
       
   156 
       
   157 -(void) writeFile {
       
   158     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),self.title];
       
   159 
       
   160     NSString *newTeamName = [self.teamDictionary objectForKey:@"teamname"];
       
   161     if (![newTeamName isEqualToString:self.teamName]) {
       
   162         //delete old
       
   163         [[NSFileManager defaultManager] removeItemAtPath:teamFile error:NULL];
       
   164         [teamFile release];
       
   165         self.title = newTeamName;
       
   166         self.teamName = newTeamName;
       
   167         teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),newTeamName];
       
   168     }
       
   169     
       
   170     [self.teamDictionary writeToFile:teamFile atomically:YES];
       
   171     NSLog(@"writing: %@",teamDictionary);
       
   172     isWriteNeeded = NO;
       
   173     [teamFile release];
       
   174 }
       
   175 
       
   176 #pragma mark -
       
   177 #pragma mark Table view data source
       
   178 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
       
   179     return 3;
       
   180 }
       
   181 
       
   182 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
   183     NSInteger rows = 0;
       
   184     switch (section) {
       
   185         case 0: // team name
       
   186             rows = 1;
       
   187             break;
       
   188         case 1: // team members
       
   189             rows = MAX_HOGS;
       
   190             break;
       
   191         case 2: // team details
       
   192             rows = [self.secondaryItems count];
       
   193             break;
       
   194         default:
       
   195             break;
       
   196     }
       
   197     return rows;
       
   198 }
       
   199 
       
   200 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
   201     static NSString *CellIdentifier0 = @"Cell0";
       
   202     static NSString *CellIdentifier1 = @"Cell1";
       
   203     static NSString *CellIdentifier2 = @"Cell2";
       
   204     
       
   205     NSArray *hogArray;
       
   206     UITableViewCell *cell = nil;
       
   207     NSInteger row = [indexPath row];
       
   208     UIImage *accessoryImage;
       
   209     
       
   210     switch ([indexPath section]) {
       
   211         case 0:
       
   212             cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
       
   213             if (cell == nil) {
       
   214                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
       
   215                                                reuseIdentifier:CellIdentifier0] autorelease];
       
   216                 // create a uitextfield for each row, expand it to take the maximum size
       
   217                 UITextField *aTextField = [[UITextField alloc] 
       
   218                                            initWithFrame:CGRectMake(5, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)];
       
   219                 aTextField.clearsOnBeginEditing = NO;
       
   220                 aTextField.returnKeyType = UIReturnKeyDone;
       
   221                 aTextField.adjustsFontSizeToFitWidth = YES;
       
   222                 aTextField.delegate = self;
       
   223                 aTextField.tag = [indexPath row];
       
   224                 aTextField.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2];
       
   225                 aTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
       
   226                 [aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
       
   227                 [cell.contentView addSubview:aTextField];
       
   228                 [aTextField release];
       
   229             }
       
   230             
       
   231             cell.imageView.image = nil;
       
   232             cell.accessoryType = UITableViewCellAccessoryNone;
       
   233             for (UIView *oneView in cell.contentView.subviews) {
       
   234                 if ([oneView isMemberOfClass:[UITextField class]]) {
       
   235                     // we find the uitextfied and we'll use its tag to understand which one is being edited
       
   236                     UITextField *textFieldFound = (UITextField *)oneView;
       
   237                     textFieldFound.text = [self.teamDictionary objectForKey:@"teamname"];
       
   238                     textFieldFound.tag = TEAMNAME_TAG;
       
   239                 }
       
   240             }            
       
   241             break;
       
   242         case 1:
       
   243             cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
       
   244             if (cell == nil) {
       
   245                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
       
   246                                                reuseIdentifier:CellIdentifier1] autorelease];
       
   247                 
       
   248                 // create a uitextfield for each row, expand it to take the maximum size
       
   249                 UITextField *aTextField = [[UITextField alloc] 
       
   250                                            initWithFrame:CGRectMake(42, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)];
       
   251                 aTextField.clearsOnBeginEditing = NO;
       
   252                 aTextField.returnKeyType = UIReturnKeyDone;
       
   253                 aTextField.adjustsFontSizeToFitWidth = YES;
       
   254                 aTextField.delegate = self;
       
   255                 aTextField.tag = [indexPath row];
       
   256                 aTextField.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2];
       
   257                 aTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
       
   258                 [aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
       
   259                 [cell.contentView addSubview:aTextField];
       
   260                 [aTextField release];
       
   261             }
       
   262             
       
   263             hogArray = [self.teamDictionary objectForKey:@"hedgehogs"];
       
   264             
       
   265             NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@.png", HATS_DIRECTORY(), [[hogArray objectAtIndex:row] objectForKey:@"hat"]];
       
   266             UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)];
       
   267             [hatFile release];
       
   268             cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, -5)];
       
   269             [hatSprite release];
       
   270                         
       
   271             for (UIView *oneView in cell.contentView.subviews) {
       
   272                 if ([oneView isMemberOfClass:[UITextField class]]) {
       
   273                     // we find the uitextfied and we'll use its tag to understand which one is being edited
       
   274                     UITextField *textFieldFound = (UITextField *)oneView;
       
   275                     textFieldFound.text = [[hogArray objectAtIndex:row] objectForKey:@"hogname"];
       
   276                 }
       
   277             }
       
   278             
       
   279             cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
       
   280             break;
       
   281         case 2:
       
   282             cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
       
   283             if (cell == nil) {
       
   284                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
       
   285                                                reuseIdentifier:CellIdentifier2] autorelease];
       
   286             }
       
   287             
       
   288             cell.textLabel.text = [self.secondaryItems objectAtIndex:row];
       
   289             cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       
   290             switch (row) {
       
   291                 case 0: // grave
       
   292                     accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png",
       
   293                                                                               GRAVES_DIRECTORY(),[teamDictionary objectForKey:@"grave"]]
       
   294                                                                     andCutAt:CGRectMake(0,0,32,32)];
       
   295                     cell.imageView.image = accessoryImage;
       
   296                     [accessoryImage release];
       
   297                     break;
       
   298                 case 2: // fort
       
   299                     accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@L.png",
       
   300                                                                               FORTS_DIRECTORY(),[teamDictionary objectForKey:@"fort"]]];
       
   301                     cell.imageView.image = [accessoryImage scaleToSize:CGSizeMake(42, 42)];
       
   302                     [accessoryImage release];
       
   303                     break;
       
   304                     
       
   305                 case 3: // flags
       
   306                     accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png",
       
   307                                                                               FLAGS_DIRECTORY(),[teamDictionary objectForKey:@"flag"]]];
       
   308                     cell.imageView.image = accessoryImage;
       
   309                     [accessoryImage release];
       
   310                     break;
       
   311                 case 4: // level
       
   312                     accessoryImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%d.png",
       
   313                                                                               BOTLEVELS_DIRECTORY(),[[[[teamDictionary objectForKey:@"hedgehogs"]
       
   314                                                                                                       objectAtIndex:0] objectForKey:@"level"]
       
   315                                                                                                      intValue]]];
       
   316                     cell.imageView.image = accessoryImage;
       
   317                     [accessoryImage release];
       
   318                     break;
       
   319                 default:
       
   320                     cell.imageView.image = nil;
       
   321                     break;
       
   322             }
       
   323             break;
       
   324     }
       
   325     
       
   326     return cell;
       
   327 }
       
   328 
       
   329 
       
   330 #pragma mark -
       
   331 #pragma mark Table view delegate
       
   332 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
   333     NSInteger row = [indexPath row];
       
   334     NSInteger section = [indexPath section];
       
   335     UITableViewController *nextController = nil;
       
   336     UITableViewCell *cell;
       
   337     
       
   338     if (2 == section) {
       
   339         switch (row) {
       
   340             case 0: // grave
       
   341                 if (nil == gravesViewController)
       
   342                     gravesViewController = [[GravesViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   343                 
       
   344                 nextController = gravesViewController;
       
   345                 break;
       
   346             case 1: // voice
       
   347                 if (nil == voicesViewController)
       
   348                     voicesViewController = [[VoicesViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   349                 
       
   350                 nextController = voicesViewController;                    
       
   351                 break;
       
   352             case 2: // fort
       
   353                 if (nil == fortsViewController)
       
   354                     fortsViewController = [[FortsViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   355                 
       
   356                 nextController = fortsViewController;
       
   357                 break;
       
   358             case 3: // flag
       
   359                 if (nil == flagsViewController) 
       
   360                     flagsViewController = [[FlagsViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   361                 
       
   362                 nextController = flagsViewController;
       
   363                 break;
       
   364             case 4: // level
       
   365                 if (nil == levelViewController)
       
   366                     levelViewController = [[LevelViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   367                 
       
   368                 nextController = levelViewController;
       
   369                 break;
       
   370         }
       
   371         
       
   372         nextController.title = [secondaryItems objectAtIndex:row];
       
   373         [nextController setTeamDictionary:teamDictionary];
       
   374         [self.navigationController pushViewController:nextController animated:YES];
       
   375     } else {
       
   376         cell = [aTableView cellForRowAtIndexPath:indexPath];
       
   377         for (UIView *oneView in cell.contentView.subviews) {
       
   378             if ([oneView isMemberOfClass:[UITextField class]]) {
       
   379                 textFieldBeingEdited = (UITextField *)oneView;
       
   380                 [textFieldBeingEdited becomeFirstResponder];
       
   381             }
       
   382         }
       
   383         [aTableView deselectRowAtIndexPath:indexPath animated:NO];
       
   384     }
       
   385 
       
   386 }
       
   387 
       
   388 // action to perform when you want to change a hog hat
       
   389 -(void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
       
   390     if (nil == hogHatViewController) {
       
   391         hogHatViewController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   392     }
       
   393     
       
   394     // cache the dictionary file of the team, so that other controllers can modify it
       
   395     hogHatViewController.teamDictionary = self.teamDictionary;
       
   396     hogHatViewController.selectedHog = [indexPath row];
       
   397     
       
   398     [self.navigationController pushViewController:hogHatViewController animated:YES];
       
   399 }
       
   400 
       
   401 
       
   402 #pragma mark -
       
   403 #pragma mark Memory management
       
   404 -(void) didReceiveMemoryWarning {
       
   405     // Releases the view if it doesn't have a superview.
       
   406     [super didReceiveMemoryWarning];
       
   407     // Relinquish ownership any cached data, images, etc that aren't in use.
       
   408     if (hogHatViewController.view.superview == nil)
       
   409         hogHatViewController = nil;
       
   410     if (gravesViewController.view.superview == nil)
       
   411         gravesViewController = nil;
       
   412     if (voicesViewController.view.superview == nil)
       
   413         voicesViewController = nil;
       
   414     if (fortsViewController.view.superview == nil)
       
   415         fortsViewController = nil;
       
   416     if (flagsViewController.view.superview == nil)
       
   417         flagsViewController = nil;
       
   418     if (levelViewController.view.superview == nil)
       
   419         levelViewController = nil;
       
   420 }
       
   421 
       
   422 -(void) viewDidUnload {
       
   423     self.teamDictionary = nil;
       
   424     self.textFieldBeingEdited = nil;
       
   425     self.teamName = nil;
       
   426     self.normalHogSprite = nil;
       
   427     self.secondaryItems = nil;
       
   428     hogHatViewController = nil;
       
   429     flagsViewController = nil;
       
   430     fortsViewController = nil;
       
   431     gravesViewController = nil;
       
   432     levelViewController = nil;
       
   433     [super viewDidUnload];
       
   434     MSG_DIDUNLOAD();
       
   435 }
       
   436 
       
   437 -(void) dealloc {
       
   438     [teamDictionary release];
       
   439     [textFieldBeingEdited release];
       
   440     [teamName release];
       
   441     [normalHogSprite release];
       
   442     [secondaryItems release];
       
   443     [hogHatViewController release];
       
   444     [fortsViewController release];
       
   445     [gravesViewController release];
       
   446     [flagsViewController release];
       
   447     [levelViewController release];
       
   448     [super dealloc];
       
   449 }
       
   450 
       
   451 
       
   452 @end
       
   453