- Added functionality to select one hat for all hogs at team settings ios-revival
authorantonc27 <antonc27@mail.ru>
Fri, 18 Sep 2015 05:37:55 +0200
branchios-revival
changeset 11187 b2112ed988cb
parent 11186 55f20efae2e4
child 11188 e03bb95edf44
- Added functionality to select one hat for all hogs at team settings
project_files/HedgewarsMobile/Classes/HogHatViewController.m
project_files/HedgewarsMobile/Classes/SingleTeamViewController.m
--- a/project_files/HedgewarsMobile/Classes/HogHatViewController.m	Fri Sep 18 04:33:37 2015 +0200
+++ b/project_files/HedgewarsMobile/Classes/HogHatViewController.m	Fri Sep 18 05:37:55 2015 +0200
@@ -77,7 +77,6 @@
     if (cell == nil)
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
 
-    NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:self.selectedHog];
     NSString *hat = [self.hatArray objectAtIndex:[indexPath row]];
     cell.textLabel.text = [hat stringByDeletingPathExtension];
 
@@ -87,6 +86,7 @@
     cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, 5)];
     [hatSprite release];
 
+    NSDictionary *hog = (self.selectedHog != -1) ? [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:self.selectedHog] : nil;
     if ([[hat stringByDeletingPathExtension] isEqualToString:[hog objectForKey:@"hat"]]) {
         cell.accessoryType = UITableViewCellAccessoryCheckmark;
     } else {
@@ -102,14 +102,21 @@
 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     NSInteger selectedRow = [indexPath row];
+    NSString *newHat = [[self.hatArray objectAtIndex:selectedRow] stringByDeletingPathExtension];
     
-    // update data on the hog dictionary
-    NSDictionary *oldHog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:self.selectedHog];
-
-    NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary:oldHog];
-    [newHog setObject:[[self.hatArray objectAtIndex:selectedRow] stringByDeletingPathExtension] forKey:@"hat"];
-    [[self.teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:self.selectedHog withObject:newHog];
-    [newHog release];
+    // update data on the hogs dictionary
+    if (self.selectedHog != -1)
+    {
+        // update only selected hog with new hat
+        [self updateTeamDictionaryWithNewHat:newHat forStartHogIndex:self.selectedHog toEndHogIndex:self.selectedHog];
+    }
+    else
+    {
+        // update all hogs with new hat
+        NSInteger startIndex = 0;
+        NSInteger endIndex = [[self.teamDictionary objectForKey:@"hedgehogs"] count] - 1;
+        [self updateTeamDictionaryWithNewHat:newHat forStartHogIndex:startIndex toEndHogIndex:endIndex];
+    }
 
     // tell our boss to write this new stuff on disk
     [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
@@ -117,6 +124,19 @@
     [self.navigationController popViewControllerAnimated:YES];
 }
 
+- (void)updateTeamDictionaryWithNewHat:(NSString *)newHat forStartHogIndex:(NSInteger)startIndex toEndHogIndex:(NSInteger)endIndex
+{
+    NSMutableArray *hogsArray = [self.teamDictionary objectForKey:@"hedgehogs"];
+    
+    for (NSInteger i=startIndex; i <= endIndex; i++)
+    {
+        NSDictionary *oldHog = [hogsArray objectAtIndex:i];
+        NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary:oldHog];
+        [newHog setObject:newHat forKey:@"hat"];
+        [hogsArray replaceObjectAtIndex:i withObject:newHog];
+        [newHog release];
+    }
+}
 
 #pragma mark -
 #pragma mark Memory management
--- a/project_files/HedgewarsMobile/Classes/SingleTeamViewController.m	Fri Sep 18 04:33:37 2015 +0200
+++ b/project_files/HedgewarsMobile/Classes/SingleTeamViewController.m	Fri Sep 18 05:37:55 2015 +0200
@@ -146,7 +146,7 @@
             rows = 1;
             break;
         case 1: // team members
-            rows = HW_getMaxNumberOfHogs();
+            rows = HW_getMaxNumberOfHogs() + 1; // one for 'Select one hat for all hogs' cell
             break;
         case 2: // team details
             rows = [self.secondaryItems count];
@@ -180,6 +180,7 @@
     static NSString *CellIdentifier0 = @"Cell0";
     static NSString *CellIdentifier1 = @"Cell1";
     static NSString *CellIdentifier2 = @"Cell2";
+    static NSString *CellIdentifierDefault = @"CellDefault";
 
     NSArray *hogArray;
     UITableViewCell *cell = nil;
@@ -204,6 +205,21 @@
             cell = editableCell;
             break;
         case 1:
+            if ([indexPath row] == HW_getMaxNumberOfHogs())
+            {
+                cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierDefault];
+                if (cell == nil)
+                {
+                    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
+                                                   reuseIdentifier:CellIdentifierDefault] autorelease];
+                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
+                }
+                
+                cell.textLabel.text = NSLocalizedString(@"Select one hat for all hogs", nil);
+                
+                break;
+            }
+            
             editableCell = (EditableCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
             if (editableCell == nil) {
                 editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault
@@ -332,30 +348,39 @@
                 break;
         }
     } else {
-        EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath];
-        [cell replyKeyboard];
-        [aTableView deselectRowAtIndexPath:indexPath animated:NO];
+        if (section == 1 && row == HW_getMaxNumberOfHogs()) {
+            // 'Select one hat for all hogs' selected
+            [self showHogHatViewControllerForHogIndex:-1];
+        } else {
+            EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath];
+            [cell replyKeyboard];
+            [aTableView deselectRowAtIndexPath:indexPath animated:NO];
+        }
     }
 
 }
 
 // action to perform when you want to change a hog hat
 -(void) tableView:(UITableView *)aTableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
-    if (nil == hogHatViewController)
-        hogHatViewController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped];
-
-    // cache the dictionary file of the team, so that other controllers can modify it
-    hogHatViewController.teamDictionary = self.teamDictionary;
-    hogHatViewController.selectedHog = [indexPath row];
-
     // if we are editing the field undo any change before proceeding
     EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath];
     [cell cancel:nil];
+    
+    [self showHogHatViewControllerForHogIndex:[indexPath row]];
+}
 
+- (void)showHogHatViewControllerForHogIndex:(NSInteger)hogIndex
+{
+    if (nil == hogHatViewController)
+        hogHatViewController = [[HogHatViewController alloc] initWithStyle:UITableViewStyleGrouped];
+    
+    // cache the dictionary file of the team, so that other controllers can modify it
+    hogHatViewController.teamDictionary = self.teamDictionary;
+    hogHatViewController.selectedHog = hogIndex;
+    
     [self.navigationController pushViewController:hogHatViewController animated:YES];
 }
 
-
 #pragma mark -
 #pragma mark Memory management
 -(void) didReceiveMemoryWarning {