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