project_files/HedgewarsMobile/Classes/SingleSchemeViewController.m
branchios-develop
changeset 12872 00215a7ec5f5
parent 11292 83738cdd750d
child 13166 ba5c794adae3
equal deleted inserted replaced
12871:2c06b1120749 12872:00215a7ec5f5
    27 
    27 
    28 
    28 
    29 @implementation SingleSchemeViewController
    29 @implementation SingleSchemeViewController
    30 @synthesize schemeName, schemeDictionary, basicSettingList, gameModifierArray;
    30 @synthesize schemeName, schemeDictionary, basicSettingList, gameModifierArray;
    31 
    31 
    32 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    32 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    33     return rotationManager(interfaceOrientation);
    33     return rotationManager(interfaceOrientation);
    34 }
    34 }
    35 
    35 
    36 #pragma mark -
    36 #pragma mark -
    37 #pragma mark View lifecycle
    37 #pragma mark View lifecycle
    38 -(void) viewDidLoad {
    38 - (void)viewDidLoad {
    39     [super viewDidLoad];
    39     [super viewDidLoad];
    40 
    40 
    41     // title, description, image name (+btn)
    41     // title, description, image name (+btn)
    42     NSArray *mods = [[NSArray alloc] initWithContentsOfFile:GAMEMODS_FILE()];
    42     NSArray *mods = [[NSArray alloc] initWithContentsOfFile:GAMEMODS_FILE()];
    43     self.gameModifierArray = mods;
    43     self.gameModifierArray = mods;
    44     [mods release];
       
    45 
    44 
    46     // title, image name (+icon), default value, max value, min value
    45     // title, image name (+icon), default value, max value, min value
    47     NSArray *basicSettings = [[NSArray alloc] initWithContentsOfFile:BASICFLAGS_FILE()];
    46     NSArray *basicSettings = [[NSArray alloc] initWithContentsOfFile:BASICFLAGS_FILE()];
    48     self.basicSettingList = basicSettings;
    47     self.basicSettingList = basicSettings;
    49     [basicSettings release];
       
    50 
    48 
    51     self.title = NSLocalizedString(@"Edit scheme preferences",@"");
    49     self.title = NSLocalizedString(@"Edit scheme preferences",@"");
    52 }
    50 }
    53 
    51 
    54 // load from file
    52 // load from file
    55 -(void) viewWillAppear:(BOOL) animated {
    53 - (void)viewWillAppear:(BOOL) animated {
    56     [super viewWillAppear:animated];
    54     [super viewWillAppear:animated];
    57 
    55 
    58     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
    56     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
    59     NSMutableDictionary *scheme = [[NSMutableDictionary alloc] initWithContentsOfFile:schemeFile];
    57     NSMutableDictionary *scheme = [[NSMutableDictionary alloc] initWithContentsOfFile:schemeFile];
    60     [schemeFile release];
       
    61     self.schemeDictionary = scheme;
    58     self.schemeDictionary = scheme;
    62     [scheme release];
       
    63 
    59 
    64     [self.tableView reloadData];
    60     [self.tableView reloadData];
    65 }
    61 }
    66 
    62 
    67 // save to file
    63 // save to file
    68 -(void) viewWillDisappear:(BOOL) animated {
    64 - (void)viewWillDisappear:(BOOL) animated {
    69     [super viewWillDisappear:animated];
    65     [super viewWillDisappear:animated];
    70 
    66 
    71     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
    67     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
    72     [self.schemeDictionary writeToFile:schemeFile atomically:YES];
    68     [self.schemeDictionary writeToFile:schemeFile atomically:YES];
    73     [schemeFile release];
       
    74 }
    69 }
    75 
    70 
    76 // force a redraw of the game mod section to reposition the slider
    71 // force a redraw of the game mod section to reposition the slider
    77 -(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    72 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    78     if (IS_IPAD() == NO)
    73     if (IS_IPAD() == NO)
    79         return;
    74         return;
    80     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
    75     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
    81 }
    76 }
    82 
    77 
    83 #pragma mark -
    78 #pragma mark -
    84 #pragma mark editableCellView delegate
    79 #pragma mark editableCellView delegate
    85 // set the new value
    80 // set the new value
    86 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {
    81 - (void)saveTextFieldValue:(NSString *)textString withTag:(NSInteger)tagValue {
    87     if (tagValue == 0) {
    82     if (tagValue == 0) {
    88         // delete old file
    83         // delete old file
    89         [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName] error:NULL];
    84         [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName] error:NULL];
    90         // update filename
    85         // update filename
    91         self.schemeName = textString;
    86         self.schemeName = textString;
    96     }
    91     }
    97 }
    92 }
    98 
    93 
    99 #pragma mark -
    94 #pragma mark -
   100 #pragma mark Table view data source
    95 #pragma mark Table view data source
   101 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    96 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   102     return 3;
    97     return 3;
   103 }
    98 }
   104 
    99 
   105 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   100 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   106     switch (section) {
   101     switch (section) {
   107         case 0:
   102         case 0:
   108             return 2;
   103             return 2;
   109             break;
   104             break;
   110         case 1:
   105         case 1:
   126     UITableViewCell *cell = nil;
   121     UITableViewCell *cell = nil;
   127     EditableCellView *editableCell = nil;
   122     EditableCellView *editableCell = nil;
   128     NSInteger row = [indexPath row];
   123     NSInteger row = [indexPath row];
   129 
   124 
   130     switch ([indexPath section]) {
   125     switch ([indexPath section]) {
   131         case 0:
   126         case 0: {
   132             editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   127             editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   133             if (editableCell == nil) {
   128             if (editableCell == nil) {
   134                 editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault
   129                 editableCell = [[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault
   135                                                reuseIdentifier:CellIdentifier0] autorelease];
   130                                                         reuseIdentifier:CellIdentifier0];
   136                 editableCell.delegate = self;
   131                 editableCell.delegate = self;
   137             }
   132             }
   138             editableCell.tag = row;
   133             editableCell.tag = row;
   139             editableCell.selectionStyle = UITableViewCellSelectionStyleNone;
   134             editableCell.selectionStyle = UITableViewCellSelectionStyleNone;
   140             editableCell.imageView.image = nil;
   135             editableCell.imageView.image = nil;
   149                 editableCell.textField.text = [self.schemeDictionary objectForKey:@"description"];
   144                 editableCell.textField.text = [self.schemeDictionary objectForKey:@"description"];
   150                 editableCell.textField.placeholder = NSLocalizedString(@"You can add a description if you wish",@"");
   145                 editableCell.textField.placeholder = NSLocalizedString(@"You can add a description if you wish",@"");
   151             }
   146             }
   152             cell = editableCell;
   147             cell = editableCell;
   153             break;
   148             break;
   154         case 1:
   149         }
       
   150         case 1: {
   155             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   151             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   156             NSDictionary *detail = [self.basicSettingList objectAtIndex:row];
   152             NSDictionary *detail = [self.basicSettingList objectAtIndex:row];
   157             // need to offset this section (see format in CommodityFunctions.m and above)
   153             // need to offset this section (see format in CommodityFunctions.m and above)
   158             if (cell == nil) {
   154             if (cell == nil) {
   159                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
   155                 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
   160                                                reuseIdentifier:CellIdentifier1] autorelease];
   156                                                reuseIdentifier:CellIdentifier1];
   161 
   157 
   162                 UISlider *slider = [[UISlider alloc] init];
   158                 UISlider *slider = [[UISlider alloc] init];
   163                 [slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
   159                 [slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
   164                 [cell.contentView addSubview:slider];
   160                 [cell.contentView addSubview:slider];
   165                 [slider release];
       
   166 
   161 
   167                 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 200, 30)];
   162                 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 200, 30)];
   168                 label.tag = LABEL_TAG;
   163                 label.tag = LABEL_TAG;
   169                 label.backgroundColor = [UIColor clearColor];
   164                 label.backgroundColor = [UIColor clearColor];
   170                 label.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
   165                 label.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
   171                 [cell.contentView addSubview:label];
   166                 [cell.contentView addSubview:label];
   172                 [label release];
       
   173             }
   167             }
   174 
   168 
   175             UIImage *img = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/icon%@.png",ICONS_DIRECTORY(),
   169             UIImage *img = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/icon%@.png",ICONS_DIRECTORY(),
   176                                                                     [[self.basicSettingList objectAtIndex:row] objectForKey:@"image"]]];
   170                                                                     [[self.basicSettingList objectAtIndex:row] objectForKey:@"image"]]];
   177             cell.imageView.image = img;
   171             cell.imageView.image = img;
   178             [img release];
       
   179 
   172 
   180             UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:LABEL_TAG];
   173             UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:LABEL_TAG];
   181             NSString *basicSettingTitleKey = [[self.basicSettingList objectAtIndex:row] objectForKey:@"title"];
   174             NSString *basicSettingTitleKey = [[self.basicSettingList objectAtIndex:row] objectForKey:@"title"];
   182             cellLabel.text = NSLocalizedStringFromTable(basicSettingTitleKey, @"Scheme", nil);
   175             cellLabel.text = NSLocalizedStringFromTable(basicSettingTitleKey, @"Scheme", nil);
   183             cellLabel.adjustsFontSizeToFitWidth = YES;
   176             cellLabel.adjustsFontSizeToFitWidth = YES;
   216                 prestring = [NSString stringWithFormat:@" %@",prestring];
   209                 prestring = [NSString stringWithFormat:@" %@",prestring];
   217             cell.detailTextLabel.text = prestring;
   210             cell.detailTextLabel.text = prestring;
   218 
   211 
   219             cell.selectionStyle = UITableViewCellSelectionStyleBlue;
   212             cell.selectionStyle = UITableViewCellSelectionStyleBlue;
   220             break;
   213             break;
   221         case 2:
   214         }
       
   215         case 2: {
   222             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier2];
   216             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier2];
   223             if (cell == nil) {
   217             if (cell == nil) {
   224                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
   218                 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
   225                                                reuseIdentifier:CellIdentifier2] autorelease];
   219                                                reuseIdentifier:CellIdentifier2];
   226                 UISwitch *onOff = [[UISwitch alloc] init];
   220                 UISwitch *onOff = [[UISwitch alloc] init];
   227                 [onOff addTarget:self action:@selector(toggleSwitch:) forControlEvents:UIControlEventValueChanged];
   221                 [onOff addTarget:self action:@selector(toggleSwitch:) forControlEvents:UIControlEventValueChanged];
   228                 cell.accessoryView = onOff;
   222                 cell.accessoryView = onOff;
   229                 [onOff release];
       
   230             }
   223             }
   231 
   224 
   232             UISwitch *switcher = (UISwitch *)cell.accessoryView;
   225             UISwitch *switcher = (UISwitch *)cell.accessoryView;
   233             switcher.tag = SWITCH_TAG + row;
   226             switcher.tag = SWITCH_TAG + row;
   234             [switcher setOn:[[[self.schemeDictionary objectForKey:@"gamemod"] objectAtIndex:row] boolValue] animated:NO];
   227             [switcher setOn:[[[self.schemeDictionary objectForKey:@"gamemod"] objectAtIndex:row] boolValue] animated:NO];
   235 
   228 
   236             UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/btn%@.png",ICONS_DIRECTORY(),
   229             UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/btn%@.png",ICONS_DIRECTORY(),
   237                                                                       [[self.gameModifierArray objectAtIndex:row] objectForKey:@"image"]]];
   230                                                                       [[self.gameModifierArray objectAtIndex:row] objectForKey:@"image"]]];
   238             cell.imageView.image = image;
   231             cell.imageView.image = image;
   239             [image release];
       
   240             cell.imageView.layer.cornerRadius = 6.0f;
   232             cell.imageView.layer.cornerRadius = 6.0f;
   241             cell.imageView.layer.masksToBounds = YES;
   233             cell.imageView.layer.masksToBounds = YES;
   242             NSString *gameModTitleKey = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"title"];
   234             NSString *gameModTitleKey = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"title"];
   243             cell.textLabel.text = NSLocalizedStringFromTable(gameModTitleKey, @"Scheme", nil);
   235             cell.textLabel.text = NSLocalizedStringFromTable(gameModTitleKey, @"Scheme", nil);
   244             NSString *gameModDescKey = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"description"];
   236             NSString *gameModDescKey = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"description"];
   246             cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
   238             cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
   247             cell.detailTextLabel.minimumFontSize = 6;
   239             cell.detailTextLabel.minimumFontSize = 6;
   248 
   240 
   249             cell.selectionStyle = UITableViewCellSelectionStyleNone;
   241             cell.selectionStyle = UITableViewCellSelectionStyleNone;
   250         }
   242         }
       
   243     }
   251 
   244 
   252     return cell;
   245     return cell;
   253 }
   246 }
   254 
   247 
   255 -(void) toggleSwitch:(id) sender {
   248 - (void)toggleSwitch:(id)sender {
   256     UISwitch *theSwitch = (UISwitch *)sender;
   249     UISwitch *theSwitch = (UISwitch *)sender;
   257     NSMutableArray *array = [self.schemeDictionary objectForKey:@"gamemod"];
   250     NSMutableArray *array = [self.schemeDictionary objectForKey:@"gamemod"];
   258     [array replaceObjectAtIndex:theSwitch.tag-SWITCH_TAG withObject:[NSNumber numberWithBool:theSwitch.on]];
   251     [array replaceObjectAtIndex:theSwitch.tag-SWITCH_TAG withObject:[NSNumber numberWithBool:theSwitch.on]];
   259 }
   252 }
   260 
   253 
   261 -(void) sliderChanged:(id) sender {
   254 - (void)sliderChanged:(id)sender {
   262     // the slider that changed is sent as object
   255     // the slider that changed is sent as object
   263     UISlider *theSlider = (UISlider *)sender;
   256     UISlider *theSlider = (UISlider *)sender;
   264     // create the indexPath of the row of the slider
   257     // create the indexPath of the row of the slider
   265     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:theSlider.tag-SLIDER_TAG inSection:1];
   258     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:theSlider.tag-SLIDER_TAG inSection:1];
   266     // get its cell
   259     // get its cell
   277     [array replaceObjectAtIndex:theSlider.tag-SLIDER_TAG withObject:[NSNumber numberWithInteger:theSlider.value]];
   270     [array replaceObjectAtIndex:theSlider.tag-SLIDER_TAG withObject:[NSNumber numberWithInteger:theSlider.value]];
   278 }
   271 }
   279 
   272 
   280 #pragma mark -
   273 #pragma mark -
   281 #pragma mark Table view delegate
   274 #pragma mark Table view delegate
   282 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   275 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   283     UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
   276     UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
   284     EditableCellView *editableCell = nil;
   277     EditableCellView *editableCell = nil;
   285     UISlider *cellSlider = nil;
   278     UISlider *cellSlider = nil;
   286 
   279 
   287     switch ([indexPath section]) {
   280     switch ([indexPath section]) {
   371         return [NSString stringWithFormat:@"%ld", (long)sliderValue];
   364         return [NSString stringWithFormat:@"%ld", (long)sliderValue];
   372 }
   365 }
   373 
   366 
   374 #pragma mark -
   367 #pragma mark -
   375 #pragma mark Memory management
   368 #pragma mark Memory management
   376 -(void) didReceiveMemoryWarning {
   369 
       
   370 - (void)didReceiveMemoryWarning {
   377     [super didReceiveMemoryWarning];
   371     [super didReceiveMemoryWarning];
   378     self.basicSettingList = nil;
   372     self.basicSettingList = nil;
   379     self.gameModifierArray = nil;
   373     self.gameModifierArray = nil;
   380 }
   374 }
   381 
   375 
   382 -(void) viewDidUnload {
       
   383     self.schemeName = nil;
       
   384     self.schemeDictionary = nil;
       
   385     self.basicSettingList = nil;
       
   386     self.gameModifierArray = nil;
       
   387     MSG_DIDUNLOAD();
       
   388     [super viewDidUnload];
       
   389 }
       
   390 
       
   391 -(void) dealloc {
       
   392     releaseAndNil(schemeName);
       
   393     releaseAndNil(schemeDictionary);
       
   394     releaseAndNil(basicSettingList);
       
   395     releaseAndNil(gameModifierArray);
       
   396     [super dealloc];
       
   397 }
       
   398 
       
   399 @end
   376 @end