cocoaTouch/SingleSchemeViewController.m
changeset 3479 972ae3ec178a
child 3490 016b3172b645
equal deleted inserted replaced
3478:cbf71e938164 3479:972ae3ec178a
       
     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 
       
   108 // save to file
       
   109 -(void) viewWillDisappear:(BOOL) animated {
       
   110     [super viewWillDisappear:animated];
       
   111     
       
   112     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.title];
       
   113     [self.schemeArray writeToFile:schemeFile atomically:YES];
       
   114     [schemeFile release];
       
   115 }
       
   116 
       
   117 #pragma mark -
       
   118 #pragma mark textfield methods
       
   119 -(void) cancel:(id) sender {
       
   120     if (textFieldBeingEdited != nil)
       
   121         [self.textFieldBeingEdited resignFirstResponder];
       
   122 }
       
   123 
       
   124 // set the new value
       
   125 -(BOOL) save:(id) sender {    
       
   126     if (textFieldBeingEdited != nil) {
       
   127         [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.title] error:NULL];
       
   128         self.title = self.textFieldBeingEdited.text;
       
   129         [self.schemeArray writeToFile:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.title] atomically:YES];
       
   130         [self.textFieldBeingEdited resignFirstResponder];
       
   131         return YES;
       
   132     }
       
   133     return NO;
       
   134 }
       
   135 
       
   136 // the textfield is being modified, update the navigation controller
       
   137 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{   
       
   138     self.textFieldBeingEdited = aTextField;
       
   139 
       
   140     UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"from schemes table")
       
   141                                                                      style:UIBarButtonItemStylePlain
       
   142                                                                     target:self
       
   143                                                                     action:@selector(cancel:)];
       
   144     self.navigationItem.leftBarButtonItem = cancelButton;
       
   145     [cancelButton release];
       
   146     
       
   147     UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"from schemes table")
       
   148                                                                      style:UIBarButtonItemStyleDone
       
   149                                                                     target:self
       
   150                                                                     action:@selector(save:)];
       
   151     self.navigationItem.rightBarButtonItem = saveButton;
       
   152     [saveButton release];
       
   153 }
       
   154 
       
   155 // the textfield has been modified, check for empty strings and restore original navigation bar
       
   156 -(void) textFieldDidEndEditing:(UITextField *)aTextField{
       
   157     if ([textFieldBeingEdited.text length] == 0) 
       
   158         textFieldBeingEdited.text = [NSString stringWithFormat:@"New Scheme"];
       
   159 
       
   160     self.textFieldBeingEdited = nil;
       
   161     self.navigationItem.rightBarButtonItem = self.navigationItem.backBarButtonItem;
       
   162     self.navigationItem.leftBarButtonItem = nil;
       
   163 }
       
   164 
       
   165 // limit the size of the field to 64 characters like in original frontend
       
   166 -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
       
   167     int limit = 64;
       
   168     return !([textField.text length] > limit && [string length] > range.length);
       
   169 }
       
   170 
       
   171 
       
   172 #pragma mark -
       
   173 #pragma mark Table view data source
       
   174 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
       
   175     return 3;
       
   176 }
       
   177 
       
   178 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
   179     switch (section) {
       
   180         case 0:
       
   181             return 1;
       
   182             break;
       
   183         case 1:
       
   184             return [self.basicSettingList count];
       
   185             break;
       
   186         case 2:
       
   187             return [self.gameModifierArray count];
       
   188         default:
       
   189             break;
       
   190     }
       
   191     return 0;
       
   192 }
       
   193 
       
   194 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
   195     static NSString *CellIdentifier0 = @"Cell0";
       
   196     static NSString *CellIdentifier1 = @"Cell1";
       
   197     static NSString *CellIdentifier2 = @"Cell2";
       
   198     
       
   199     UITableViewCell *cell = nil;
       
   200     NSInteger row = [indexPath row];
       
   201     
       
   202     switch ([indexPath section]) {
       
   203         case 0:
       
   204             cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
       
   205             if (cell == nil) {
       
   206                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
       
   207                                                reuseIdentifier:CellIdentifier0] autorelease];
       
   208                 // create a uitextfield for each row, expand it to take the maximum size
       
   209                 UITextField *aTextField = [[UITextField alloc] 
       
   210                                            initWithFrame:CGRectMake(5, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)];
       
   211                 aTextField.clearsOnBeginEditing = NO;
       
   212                 aTextField.returnKeyType = UIReturnKeyDone;
       
   213                 aTextField.adjustsFontSizeToFitWidth = YES;
       
   214                 aTextField.delegate = self;
       
   215                 aTextField.tag = [indexPath row];
       
   216                 aTextField.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2];
       
   217                 aTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
       
   218                 [aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
       
   219                 [cell.contentView addSubview:aTextField];
       
   220                 [aTextField release];
       
   221             }
       
   222             
       
   223             for (UIView *oneView in cell.contentView.subviews) {
       
   224                 if ([oneView isMemberOfClass:[UITextField class]]) {
       
   225                     // we find the uitextfied and we'll use its tag to understand which one is being edited
       
   226                     UITextField *textFieldFound = (UITextField *)oneView;
       
   227                     textFieldFound.text = self.title;
       
   228                 }
       
   229             }
       
   230             cell.detailTextLabel.text = nil;
       
   231             cell.imageView.image = nil;
       
   232             break;
       
   233         case 1:
       
   234             cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
       
   235             if (cell == nil) {
       
   236                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
       
   237                                                reuseIdentifier:CellIdentifier1] autorelease];
       
   238             }
       
   239             
       
   240             UIImage *img = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/icon%@.png",BTN_DIRECTORY(),[[self.basicSettingList objectAtIndex:row] objectForKey:@"image"]]];
       
   241             cell.imageView.image = [img scaleToSize:CGSizeMake(40, 40)];
       
   242             [img release];
       
   243             cell.textLabel.text = [[self.basicSettingList objectAtIndex:row] objectForKey:@"title"];
       
   244             cell.detailTextLabel.text = nil;
       
   245             break;
       
   246         case 2:
       
   247             cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
       
   248             if (cell == nil) {
       
   249                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
       
   250                                                reuseIdentifier:CellIdentifier2] autorelease];
       
   251                 UISwitch *onOff = [[UISwitch alloc] init];
       
   252                 onOff.tag = row;
       
   253                 [onOff addTarget:self action:@selector(toggleSwitch:) forControlEvents:UIControlEventValueChanged];
       
   254                 cell.accessoryView = onOff;
       
   255                 [onOff release];
       
   256             }
       
   257             
       
   258             UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/btn%@.png",BTN_DIRECTORY(),[[self.gameModifierArray objectAtIndex:row] objectForKey:@"image"]]];
       
   259             cell.imageView.image = image;
       
   260             [image release];
       
   261             [cell.imageView.layer setCornerRadius:7.0f];
       
   262             [cell.imageView.layer setMasksToBounds:YES];
       
   263             cell.textLabel.text = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"title"];
       
   264             cell.detailTextLabel.text = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"description"];
       
   265             [(UISwitch *)cell.accessoryView setOn:[[self.schemeArray objectAtIndex:row] boolValue] animated:NO];
       
   266         }
       
   267     
       
   268     return cell;
       
   269 }
       
   270 
       
   271 -(void) toggleSwitch:(id) sender {
       
   272     UISwitch *theSwitch = (UISwitch *)sender;
       
   273     [self.schemeArray replaceObjectAtIndex:theSwitch.tag withObject:[NSNumber numberWithBool:theSwitch.on]];
       
   274 }
       
   275 
       
   276 
       
   277 #pragma mark -
       
   278 #pragma mark Table view delegate
       
   279 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
   280     
       
   281     if ([indexPath section] == 0) {
       
   282         UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
       
   283         for (UIView *oneView in cell.contentView.subviews) {
       
   284             if ([oneView isMemberOfClass:[UITextField class]]) {
       
   285                 textFieldBeingEdited = (UITextField *)oneView;
       
   286                 [textFieldBeingEdited becomeFirstResponder];
       
   287             }
       
   288         }
       
   289     }
       
   290     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
       
   291 }
       
   292 
       
   293 
       
   294 #pragma mark -
       
   295 #pragma mark Memory management
       
   296 -(void) didReceiveMemoryWarning {
       
   297     [super didReceiveMemoryWarning];
       
   298 }
       
   299 
       
   300 -(void) viewDidUnload {
       
   301     self.textFieldBeingEdited = nil;
       
   302     self.schemeArray = nil;
       
   303     self.basicSettingList = nil;
       
   304     self.gameModifierArray = nil;
       
   305 }
       
   306 
       
   307 -(void) dealloc {
       
   308     [textFieldBeingEdited release];
       
   309     [schemeArray release];
       
   310     [basicSettingList release];
       
   311     [gameModifierArray release];
       
   312     [super dealloc];
       
   313 }
       
   314 
       
   315 
       
   316 @end
       
   317