project_files/HedgewarsMobile/Classes/SingleSchemeViewController.m
changeset 3514 59dbd31e9953
parent 3513 f589230fa21b
child 3546 ccf4854df294
equal deleted inserted replaced
3513:f589230fa21b 3514:59dbd31e9953
       
     1 //
       
     2 //  SingleSchemeViewController.m
       
     3 //  Hedgewars
       
     4 //
       
     5 //  Created by Vittorio on 23/05/10.
       
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
       
     7 //
       
     8 
       
     9 #import "SingleSchemeViewController.h"
       
    10 #import <QuartzCore/QuartzCore.h>
       
    11 #import "CommodityFunctions.h"
       
    12 #import "UIImageExtra.h"
       
    13 
       
    14 @implementation SingleSchemeViewController
       
    15 @synthesize textFieldBeingEdited, schemeArray, basicSettingList, gameModifierArray;
       
    16 
       
    17 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
       
    18 	return rotationManager(interfaceOrientation);
       
    19 }
       
    20 
       
    21 #pragma mark -
       
    22 #pragma mark View lifecycle
       
    23 -(void) viewDidLoad {
       
    24     [super viewDidLoad];
       
    25 
       
    26     NSArray *mods = [[NSArray alloc] initWithObjects:
       
    27                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Fort Mode",@""),@"title",
       
    28                       NSLocalizedString(@"Defend your fort and destroy the opponents (two team colours max)",@""),@"description",
       
    29                       @"Forts",@"image",nil],
       
    30                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Divide Team",@""),@"title",
       
    31                       NSLocalizedString(@"Teams will start on opposite sides of the terrain (two team colours max)",@""),@"description",
       
    32                       @"TeamsDivide",@"image",nil],
       
    33                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Solid Land",@""),@"title",
       
    34                       NSLocalizedString(@"Land can not be destroyed",@""),@"description",
       
    35                       @"Solid",@"image",nil],
       
    36                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Add Border",@""),@"title",
       
    37                       NSLocalizedString(@"Add an indestructable border around the terrain",@""),@"description",
       
    38                       @"Border",@"image",nil],
       
    39                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Low Gravity",@""),@"title",
       
    40                       NSLocalizedString(@"Lower gravity",@""),@"description",
       
    41                       @"LowGravity",@"image",nil],
       
    42                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Laser Sight",@""),@"title",
       
    43                       NSLocalizedString(@"Assisted aiming with laser sight",@""),@"description",
       
    44                       @"LaserSight",@"image",nil],
       
    45                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Invulnerable",@""),@"title",
       
    46                       NSLocalizedString(@"All hogs have a personal forcefield",@""),@"description",
       
    47                       @"Invulnerable",@"image",nil],
       
    48                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Add Mines",@""),@"title",
       
    49                       NSLocalizedString(@"Enable random mines",@""),@"description",
       
    50                       @"Mines",@"image",nil],
       
    51                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Vampirism Mode",@""),@"title",
       
    52                       NSLocalizedString(@"Gain 80% of the damage you do back in health",@""),@"description",
       
    53                       @"Vampiric",@"image",nil],
       
    54                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Karma Mode",@""),@"title",
       
    55                       NSLocalizedString(@"Share your opponents pain, share their damage",@""),@"description",
       
    56                       @"Karma",@"image",nil],
       
    57                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Artillery Mode",@""),@"title",
       
    58                       NSLocalizedString(@"Your hogs are unable to move, test your aim",@""),@"description",
       
    59                       @"Artillery",@"image",nil],
       
    60                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Random Order",@""),@"title",
       
    61                       NSLocalizedString(@"Order of play is random instead of in room order",@""),@"description",
       
    62                       @"RandomOrder",@"image",nil],
       
    63                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"King Mode",@""),@"title",
       
    64                       NSLocalizedString(@"Play with a King. If he dies, your side loses",@""),@"description",
       
    65                       @"King",@"image",nil],
       
    66                      [NSDictionary dictionaryWithObjectsAndKeys: NSLocalizedString(@"Place Hedgehogs",@""),@"title",
       
    67                       NSLocalizedString(@"Take turns placing your hedgehogs pre-game",@""),@"description",
       
    68                       @"PlaceHog",@"image",nil],
       
    69                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Clan Shares Ammo",@""),@"title",
       
    70                       NSLocalizedString(@"Ammo is shared between all clan teams",@""),@"description",
       
    71                       @"SharedAmmo",@"image",nil],
       
    72                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Disable Girders",@""),@"title",
       
    73                       NSLocalizedString(@"Disable girders when generating random maps",@""),@"description",
       
    74                       @"DisableGirders",@"image",nil],
       
    75                      [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Disable Land Objects",@""),@"title",
       
    76                       NSLocalizedString(@"Disable land objects when generating maps",@""),@"description",
       
    77                       @"DisableLandObjects",@"image",nil],
       
    78                      nil];
       
    79     self.gameModifierArray = mods;
       
    80     [mods release];
       
    81     
       
    82     NSArray *basicSettings = [[NSArray alloc] initWithObjects:
       
    83                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Damage Modifier",@""),@"title",@"Damage",@"image",nil],
       
    84                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Turn Time",@""),@"title",@"Time",@"image",nil],
       
    85                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Initial Health",@""),@"title",@"Health",@"image",nil],
       
    86                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Sudden Death Timeout",@""),@"title",@"SuddenDeath",@"image",nil],
       
    87                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Crate Drops",@""),@"title",@"Box",@"image",nil],
       
    88                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Mines Time",@""),@"title",@"Time",@"image",nil],
       
    89                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Mines Number",@""),@"title",@"Mine",@"image",nil],
       
    90                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Dud Mines Probability",@""),@"title",@"Dud",@"image",nil],
       
    91                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Explosives",@""),@"title",@"Damage",@"image",nil],
       
    92                               nil];
       
    93     self.basicSettingList = basicSettings;
       
    94     [basicSettings release];
       
    95 }
       
    96 
       
    97 // load from file
       
    98 -(void) viewWillAppear:(BOOL) animated {
       
    99     [super viewWillAppear:animated];
       
   100     
       
   101     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.title];
       
   102 	NSMutableArray *scheme = [[NSMutableArray alloc] initWithContentsOfFile:schemeFile];
       
   103     self.schemeArray = scheme;
       
   104     [scheme release];
       
   105 	[schemeFile release];
       
   106     
       
   107     [self.tableView reloadData];
       
   108 }
       
   109 
       
   110 // save to file
       
   111 -(void) viewWillDisappear:(BOOL) animated {
       
   112     [super viewWillDisappear:animated];
       
   113     
       
   114     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.title];
       
   115     [self.schemeArray writeToFile:schemeFile atomically:YES];
       
   116     [schemeFile release];
       
   117 }
       
   118 
       
   119 #pragma mark -
       
   120 #pragma mark textfield methods
       
   121 -(void) cancel:(id) sender {
       
   122     if (textFieldBeingEdited != nil)
       
   123         [self.textFieldBeingEdited resignFirstResponder];
       
   124 }
       
   125 
       
   126 // set the new value
       
   127 -(BOOL) save:(id) sender {    
       
   128     if (textFieldBeingEdited != nil) {
       
   129         [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.title] error:NULL];
       
   130         self.title = self.textFieldBeingEdited.text;
       
   131         [self.schemeArray writeToFile:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.title] atomically:YES];
       
   132         [self.textFieldBeingEdited resignFirstResponder];
       
   133         return YES;
       
   134     }
       
   135     return NO;
       
   136 }
       
   137 
       
   138 // the textfield is being modified, update the navigation controller
       
   139 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{   
       
   140     self.textFieldBeingEdited = aTextField;
       
   141 
       
   142     UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"from schemes table")
       
   143                                                                      style:UIBarButtonItemStylePlain
       
   144                                                                     target:self
       
   145                                                                     action:@selector(cancel:)];
       
   146     self.navigationItem.leftBarButtonItem = cancelButton;
       
   147     [cancelButton release];
       
   148     
       
   149     UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"from schemes table")
       
   150                                                                      style:UIBarButtonItemStyleDone
       
   151                                                                     target:self
       
   152                                                                     action:@selector(save:)];
       
   153     self.navigationItem.rightBarButtonItem = saveButton;
       
   154     [saveButton release];
       
   155 }
       
   156 
       
   157 // the textfield has been modified, check for empty strings and restore original navigation bar
       
   158 -(void) textFieldDidEndEditing:(UITextField *)aTextField{
       
   159     if ([textFieldBeingEdited.text length] == 0) 
       
   160         textFieldBeingEdited.text = [NSString stringWithFormat:@"New Scheme"];
       
   161 
       
   162     self.textFieldBeingEdited = nil;
       
   163     self.navigationItem.rightBarButtonItem = self.navigationItem.backBarButtonItem;
       
   164     self.navigationItem.leftBarButtonItem = nil;
       
   165 }
       
   166 
       
   167 // limit the size of the field to 64 characters like in original frontend
       
   168 -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
       
   169     int limit = 64;
       
   170     return !([textField.text length] > limit && [string length] > range.length);
       
   171 }
       
   172 
       
   173 
       
   174 #pragma mark -
       
   175 #pragma mark Table view data source
       
   176 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
       
   177     return 3;
       
   178 }
       
   179 
       
   180 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
   181     switch (section) {
       
   182         case 0:
       
   183             return 1;
       
   184             break;
       
   185         case 1:
       
   186             return [self.basicSettingList count];
       
   187             break;
       
   188         case 2:
       
   189             return [self.gameModifierArray count];
       
   190         default:
       
   191             break;
       
   192     }
       
   193     return 0;
       
   194 }
       
   195 
       
   196 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
   197     static NSString *CellIdentifier0 = @"Cell0";
       
   198     static NSString *CellIdentifier1 = @"Cell1";
       
   199     static NSString *CellIdentifier2 = @"Cell2";
       
   200     
       
   201     UITableViewCell *cell = nil;
       
   202     NSInteger row = [indexPath row];
       
   203     
       
   204     switch ([indexPath section]) {
       
   205         case 0:
       
   206             cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
       
   207             if (cell == nil) {
       
   208                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
       
   209                                                reuseIdentifier:CellIdentifier0] autorelease];
       
   210                 // create a uitextfield for each row, expand it to take the maximum size
       
   211                 UITextField *aTextField = [[UITextField alloc] 
       
   212                                            initWithFrame:CGRectMake(5, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)];
       
   213                 aTextField.clearsOnBeginEditing = NO;
       
   214                 aTextField.returnKeyType = UIReturnKeyDone;
       
   215                 aTextField.adjustsFontSizeToFitWidth = YES;
       
   216                 aTextField.delegate = self;
       
   217                 aTextField.tag = [indexPath row];
       
   218                 aTextField.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2];
       
   219                 aTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
       
   220                 [aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
       
   221                 [cell.contentView addSubview:aTextField];
       
   222                 [aTextField release];
       
   223             }
       
   224             
       
   225             for (UIView *oneView in cell.contentView.subviews) {
       
   226                 if ([oneView isMemberOfClass:[UITextField class]]) {
       
   227                     // we find the uitextfied and we'll use its tag to understand which one is being edited
       
   228                     UITextField *textFieldFound = (UITextField *)oneView;
       
   229                     textFieldFound.text = self.title;
       
   230                 }
       
   231             }
       
   232             cell.detailTextLabel.text = nil;
       
   233             cell.imageView.image = nil;
       
   234             break;
       
   235         case 1:
       
   236             cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
       
   237             if (cell == nil) {
       
   238                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
       
   239                                                reuseIdentifier:CellIdentifier1] autorelease];
       
   240             }
       
   241             
       
   242             UIImage *img = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/icon%@.png",BTN_DIRECTORY(),[[self.basicSettingList objectAtIndex:row] objectForKey:@"image"]]];
       
   243             cell.imageView.image = [img scaleToSize:CGSizeMake(40, 40)];
       
   244             [img release];
       
   245             cell.textLabel.text = [[self.basicSettingList objectAtIndex:row] objectForKey:@"title"];
       
   246             cell.detailTextLabel.text = nil;
       
   247             break;
       
   248         case 2:
       
   249             cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
       
   250             if (cell == nil) {
       
   251                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
       
   252                                                reuseIdentifier:CellIdentifier2] autorelease];
       
   253                 UISwitch *onOff = [[UISwitch alloc] init];
       
   254                 onOff.tag = row;
       
   255                 [onOff addTarget:self action:@selector(toggleSwitch:) forControlEvents:UIControlEventValueChanged];
       
   256                 cell.accessoryView = onOff;
       
   257                 [onOff release];
       
   258             }
       
   259             
       
   260             UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/btn%@.png",BTN_DIRECTORY(),[[self.gameModifierArray objectAtIndex:row] objectForKey:@"image"]]];
       
   261             cell.imageView.image = image;
       
   262             [image release];
       
   263             [cell.imageView.layer setCornerRadius:7.0f];
       
   264             [cell.imageView.layer setMasksToBounds:YES];
       
   265             cell.textLabel.text = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"title"];
       
   266             cell.detailTextLabel.text = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"description"];
       
   267             [(UISwitch *)cell.accessoryView setOn:[[self.schemeArray objectAtIndex:row] boolValue] animated:NO];
       
   268         }
       
   269     
       
   270     return cell;
       
   271 }
       
   272 
       
   273 -(void) toggleSwitch:(id) sender {
       
   274     UISwitch *theSwitch = (UISwitch *)sender;
       
   275     [self.schemeArray replaceObjectAtIndex:theSwitch.tag withObject:[NSNumber numberWithBool:theSwitch.on]];
       
   276 }
       
   277 
       
   278 
       
   279 #pragma mark -
       
   280 #pragma mark Table view delegate
       
   281 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
   282     
       
   283     if ([indexPath section] == 0) {
       
   284         UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
       
   285         for (UIView *oneView in cell.contentView.subviews) {
       
   286             if ([oneView isMemberOfClass:[UITextField class]]) {
       
   287                 textFieldBeingEdited = (UITextField *)oneView;
       
   288                 [textFieldBeingEdited becomeFirstResponder];
       
   289             }
       
   290         }
       
   291     }
       
   292     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
       
   293 }
       
   294 
       
   295 
       
   296 #pragma mark -
       
   297 #pragma mark Memory management
       
   298 -(void) didReceiveMemoryWarning {
       
   299     [super didReceiveMemoryWarning];
       
   300 }
       
   301 
       
   302 -(void) viewDidUnload {
       
   303     self.textFieldBeingEdited = nil;
       
   304     self.schemeArray = nil;
       
   305     self.basicSettingList = nil;
       
   306     self.gameModifierArray = nil;
       
   307     [super viewDidUnload];
       
   308     MSG_DIDUNLOAD();
       
   309 }
       
   310 
       
   311 -(void) dealloc {
       
   312     [textFieldBeingEdited release];
       
   313     [schemeArray release];
       
   314     [basicSettingList release];
       
   315     [gameModifierArray release];
       
   316     [super dealloc];
       
   317 }
       
   318 
       
   319 
       
   320 @end
       
   321