project_files/HedgewarsMobile/Classes/SingleWeaponViewController.m
changeset 3697 d5b30d6373fc
parent 3662 a44406f4369b
child 3701 8c449776ebe6
equal deleted inserted replaced
3695:c11abf387a7d 3697:d5b30d6373fc
    20 
    20 
    21 #pragma mark -
    21 #pragma mark -
    22 #pragma mark View lifecycle
    22 #pragma mark View lifecycle
    23 -(void) viewDidLoad {
    23 -(void) viewDidLoad {
    24     [super viewDidLoad];
    24     [super viewDidLoad];
    25     
    25 
    26     NSArray *array = [[NSArray alloc] initWithObjects:
    26     NSArray *array = [[NSArray alloc] initWithObjects:
    27                       NSLocalizedString(@"Grenade",@""),
    27                       NSLocalizedString(@"Grenade",@""),
    28                       NSLocalizedString(@"Cluster Bomb",@""),
    28                       NSLocalizedString(@"Cluster Bomb",@""),
    29                       NSLocalizedString(@"Bazooka",@""),
    29                       NSLocalizedString(@"Bazooka",@""),
    30                       NSLocalizedString(@"Homing Bee",@""),
    30                       NSLocalizedString(@"Homing Bee",@""),
    76 
    76 
    77     quantity = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    77     quantity = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    78     probability = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    78     probability = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    79     delay = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    79     delay = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    80     crateness = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    80     crateness = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    81     
    81 
    82     NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()];
    82     NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()];
    83     UIImage *img = [[UIImage alloc] initWithContentsOfFile:str];
    83     UIImage *img = [[UIImage alloc] initWithContentsOfFile:str];
    84     self.ammoStoreImage = img;
    84     self.ammoStoreImage = img;
    85     [img release];
    85     [img release];
    86     
    86 
    87     self.title = NSLocalizedString(@"Edit weapons preferences",@"");
    87     self.title = NSLocalizedString(@"Edit weapons preferences",@"");
    88 }
    88 }
    89 
    89 
    90 -(void) viewWillAppear:(BOOL) animated {
    90 -(void) viewWillAppear:(BOOL) animated {
    91     [super viewWillAppear:animated];
    91     [super viewWillAppear:animated];
    92     
    92 
    93     NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName];
    93     NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName];
    94     NSDictionary *weapon = [[NSDictionary alloc] initWithContentsOfFile:ammoFile];
    94     NSDictionary *weapon = [[NSDictionary alloc] initWithContentsOfFile:ammoFile];
    95     [ammoFile release];
    95     [ammoFile release];
    96     
    96 
    97     const char *tmp1 = [[weapon objectForKey:@"ammostore_initialqt"] UTF8String];
    97     const char *tmp1 = [[weapon objectForKey:@"ammostore_initialqt"] UTF8String];
    98     const char *tmp2 = [[weapon objectForKey:@"ammostore_probability"] UTF8String];
    98     const char *tmp2 = [[weapon objectForKey:@"ammostore_probability"] UTF8String];
    99     const char *tmp3 = [[weapon objectForKey:@"ammostore_delay"] UTF8String];
    99     const char *tmp3 = [[weapon objectForKey:@"ammostore_delay"] UTF8String];
   100     const char *tmp4 = [[weapon objectForKey:@"ammostore_crate"] UTF8String];
   100     const char *tmp4 = [[weapon objectForKey:@"ammostore_crate"] UTF8String];
   101     [weapon release];
   101     [weapon release];
   102     
   102 
   103     // if the new weaponset is diffrent from the older we need to update it replacing
   103     // if the new weaponset is diffrent from the older we need to update it replacing
   104     // the missing ammos with 0 quantity
   104     // the missing ammos with 0 quantity
   105     int oldlen = strlen(tmp1);
   105     int oldlen = strlen(tmp1);
   106     for (int i = 0; i < oldlen; i++) {
   106     for (int i = 0; i < oldlen; i++) {
   107         quantity[i] = tmp1[i];
   107         quantity[i] = tmp1[i];
   113         quantity[i] = '0';
   113         quantity[i] = '0';
   114         probability[i] = '0';
   114         probability[i] = '0';
   115         delay[i] = '0';
   115         delay[i] = '0';
   116         crateness[i] = '0';
   116         crateness[i] = '0';
   117     }
   117     }
   118     
   118 
   119     [self.tableView reloadData];
   119     [self.tableView reloadData];
   120 }
   120 }
   121 
   121 
   122 -(void) viewWillDisappear:(BOOL) animated {
   122 -(void) viewWillDisappear:(BOOL) animated {
   123     [super viewWillDisappear:animated];
   123     [super viewWillDisappear:animated];
   127 -(void) saveAmmos {
   127 -(void) saveAmmos {
   128     quantity[CURRENT_AMMOSIZE] = '\0';
   128     quantity[CURRENT_AMMOSIZE] = '\0';
   129     probability[CURRENT_AMMOSIZE] = '\0';
   129     probability[CURRENT_AMMOSIZE] = '\0';
   130     delay[CURRENT_AMMOSIZE] = '\0';
   130     delay[CURRENT_AMMOSIZE] = '\0';
   131     crateness[CURRENT_AMMOSIZE] = '\0';
   131     crateness[CURRENT_AMMOSIZE] = '\0';
   132     
   132 
   133     NSString *quantityStr = [NSString stringWithUTF8String:quantity];
   133     NSString *quantityStr = [NSString stringWithUTF8String:quantity];
   134     NSString *probabilityStr = [NSString stringWithUTF8String:probability];
   134     NSString *probabilityStr = [NSString stringWithUTF8String:probability];
   135     NSString *delayStr = [NSString stringWithUTF8String:delay];
   135     NSString *delayStr = [NSString stringWithUTF8String:delay];
   136     NSString *cratenessStr = [NSString stringWithUTF8String:crateness];
   136     NSString *cratenessStr = [NSString stringWithUTF8String:crateness];
   137 
   137 
   169     UITableViewCell *cell = nil;
   169     UITableViewCell *cell = nil;
   170 
   170 
   171     if (0 == [indexPath section]) {
   171     if (0 == [indexPath section]) {
   172         EditableCellView *customCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   172         EditableCellView *customCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   173         if (customCell == nil) {
   173         if (customCell == nil) {
   174             customCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault 
   174             customCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault
   175                                             reuseIdentifier:CellIdentifier0] autorelease];
   175                                             reuseIdentifier:CellIdentifier0] autorelease];
   176             customCell.delegate = self;
   176             customCell.delegate = self;
   177         }
   177         }
   178         
   178 
   179         customCell.textField.text = self.weaponName;
   179         customCell.textField.text = self.weaponName;
   180         customCell.detailTextLabel.text = nil;
   180         customCell.detailTextLabel.text = nil;
   181         customCell.imageView.image = nil;
   181         customCell.imageView.image = nil;
   182         customCell.selectionStyle = UITableViewCellSelectionStyleNone;
   182         customCell.selectionStyle = UITableViewCellSelectionStyleNone;
   183         cell = customCell;
   183         cell = customCell;
   185         WeaponCellView *customCell = (WeaponCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   185         WeaponCellView *customCell = (WeaponCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   186         if (customCell == nil) {
   186         if (customCell == nil) {
   187             customCell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
   187             customCell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
   188             customCell.delegate = self;
   188             customCell.delegate = self;
   189         }
   189         }
   190         
   190 
   191         int x = ((row*32)/1024)*32;
   191         int x = ((row*32)/1024)*32;
   192         int y = (row*32)%1024;
   192         int y = (row*32)%1024;
   193         
   193 
   194         UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, 32, 32)] makeRoundCornersOfSize:CGSizeMake(7, 7)];
   194         UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, 32, 32)] makeRoundCornersOfSize:CGSizeMake(7, 7)];
   195         customCell.weaponIcon.image = img;
   195         customCell.weaponIcon.image = img;
   196         customCell.weaponName.text = [ammoNames objectAtIndex:row];
   196         customCell.weaponName.text = [ammoNames objectAtIndex:row];
   197         customCell.tag = row;
   197         customCell.tag = row;
   198         
   198 
   199         [customCell.initialQt setValue:[[NSString stringWithFormat:@"%c",quantity[row]] intValue] animated:NO];
   199         [customCell.initialQt setValue:[[NSString stringWithFormat:@"%c",quantity[row]] intValue] animated:NO];
   200         [customCell.probabilityQt setValue:[[NSString stringWithFormat:@"%c", probability[row]] intValue] animated:NO];
   200         [customCell.probabilityQt setValue:[[NSString stringWithFormat:@"%c", probability[row]] intValue] animated:NO];
   201         [customCell.delayQt setValue:[[NSString stringWithFormat:@"%c", delay[row]] intValue] animated:NO];
   201         [customCell.delayQt setValue:[[NSString stringWithFormat:@"%c", delay[row]] intValue] animated:NO];
   202         [customCell.crateQt setValue:[[NSString stringWithFormat:@"%c", crateness[row]] intValue] animated:NO];
   202         [customCell.crateQt setValue:[[NSString stringWithFormat:@"%c", crateness[row]] intValue] animated:NO];
   203         cell = customCell;
   203         cell = customCell;
   204     }
   204     }
   205     
   205 
   206     cell.selectionStyle = UITableViewCellSelectionStyleNone;
   206     cell.selectionStyle = UITableViewCellSelectionStyleNone;
   207     return cell;
   207     return cell;
   208 }
   208 }
   209 
   209 
   210 -(CGFloat) tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   210 -(CGFloat) tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   224 }
   224 }
   225 
   225 
   226 #pragma mark -
   226 #pragma mark -
   227 #pragma mark editableCellView delegate
   227 #pragma mark editableCellView delegate
   228 // set the new value
   228 // set the new value
   229 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {    
   229 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {
   230     // delete old file
   230     // delete old file
   231     [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName] error:NULL];
   231     [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName] error:NULL];
   232     // update filename
   232     // update filename
   233     self.weaponName = textString;
   233     self.weaponName = textString;
   234     // save new file
   234     // save new file