project_files/HedgewarsMobile/Classes/SingleWeaponViewController.m
changeset 3782 dc3531e49e4c
parent 3766 36b625111609
child 3825 fd6c20cd90e3
equal deleted inserted replaced
3781:2bfda544ae48 3782:dc3531e49e4c
    10 #import "WeaponCellView.h"
    10 #import "WeaponCellView.h"
    11 #import "CommodityFunctions.h"
    11 #import "CommodityFunctions.h"
    12 #import "UIImageExtra.h"
    12 #import "UIImageExtra.h"
    13 
    13 
    14 @implementation SingleWeaponViewController
    14 @implementation SingleWeaponViewController
    15 @synthesize weaponName, ammoStoreImage, ammoNames;
    15 @synthesize weaponName, description, ammoStoreImage, ammoNames;
    16 
    16 
    17 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    17 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    18     return rotationManager(interfaceOrientation);
    18     return rotationManager(interfaceOrientation);
    19 }
    19 }
    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     // also increment CURRENT_AMMOSIZE in CommodityFunctions.h
    26     // also increment CURRENT_AMMOSIZE in CommodityFunctions.h
    27     NSArray *array = [[NSArray alloc] initWithObjects:
    27     NSArray *array = [[NSArray alloc] initWithObjects:
    28                       NSLocalizedString(@"Grenade",@""),
    28                       NSLocalizedString(@"Grenade",@""),
    29                       NSLocalizedString(@"Cluster Bomb",@""),
    29                       NSLocalizedString(@"Cluster Bomb",@""),
    30                       NSLocalizedString(@"Bazooka",@""),
    30                       NSLocalizedString(@"Bazooka",@""),
    74                       NSLocalizedString(@"Sticky Mine",@""),
    74                       NSLocalizedString(@"Sticky Mine",@""),
    75                       NSLocalizedString(@"Hammer",@""),
    75                       NSLocalizedString(@"Hammer",@""),
    76                       nil];
    76                       nil];
    77     self.ammoNames = array;
    77     self.ammoNames = array;
    78     [array release];
    78     [array release];
    79 
    79     
    80     quantity = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    80     quantity = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    81     probability = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    81     probability = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    82     delay = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    82     delay = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    83     crateness = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    83     crateness = (char *)malloc(sizeof(char)*(CURRENT_AMMOSIZE+1));
    84 
    84     
    85     NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()];
    85     NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()];
    86     UIImage *img = [[UIImage alloc] initWithContentsOfFile:str];
    86     UIImage *img = [[UIImage alloc] initWithContentsOfFile:str];
    87     self.ammoStoreImage = img;
    87     self.ammoStoreImage = img;
    88     [img release];
    88     [img release];
    89 
    89     
    90     self.title = NSLocalizedString(@"Edit weapons preferences",@"");
    90     self.title = NSLocalizedString(@"Edit weapons preferences",@"");
    91 }
    91 }
    92 
    92 
    93 -(void) viewWillAppear:(BOOL) animated {
    93 -(void) viewWillAppear:(BOOL) animated {
    94     [super viewWillAppear:animated];
    94     [super viewWillAppear:animated];
    95 
    95     
    96     NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName];
    96     NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName];
    97     NSDictionary *weapon = [[NSDictionary alloc] initWithContentsOfFile:ammoFile];
    97     NSDictionary *weapon = [[NSDictionary alloc] initWithContentsOfFile:ammoFile];
    98     [ammoFile release];
    98     [ammoFile release];
    99 
    99     
       
   100     self.description = [weapon objectForKey:@"description"];
   100     const char *tmp1 = [[weapon objectForKey:@"ammostore_initialqt"] UTF8String];
   101     const char *tmp1 = [[weapon objectForKey:@"ammostore_initialqt"] UTF8String];
   101     const char *tmp2 = [[weapon objectForKey:@"ammostore_probability"] UTF8String];
   102     const char *tmp2 = [[weapon objectForKey:@"ammostore_probability"] UTF8String];
   102     const char *tmp3 = [[weapon objectForKey:@"ammostore_delay"] UTF8String];
   103     const char *tmp3 = [[weapon objectForKey:@"ammostore_delay"] UTF8String];
   103     const char *tmp4 = [[weapon objectForKey:@"ammostore_crate"] UTF8String];
   104     const char *tmp4 = [[weapon objectForKey:@"ammostore_crate"] UTF8String];
   104     [weapon release];
   105     [weapon release];
   105 
   106     
   106     // if the new weaponset is diffrent from the older we need to update it replacing
   107     // if the new weaponset is diffrent from the older we need to update it replacing
   107     // the missing ammos with 0 quantity
   108     // the missing ammos with 0 quantity
   108     int oldlen = strlen(tmp1);
   109     int oldlen = strlen(tmp1);
   109     for (int i = 0; i < oldlen; i++) {
   110     for (int i = 0; i < oldlen; i++) {
   110         quantity[i] = tmp1[i];
   111         quantity[i] = tmp1[i];
   116         quantity[i] = '0';
   117         quantity[i] = '0';
   117         probability[i] = '0';
   118         probability[i] = '0';
   118         delay[i] = '0';
   119         delay[i] = '0';
   119         crateness[i] = '0';
   120         crateness[i] = '0';
   120     }
   121     }
   121 
   122     
   122     [self.tableView reloadData];
   123     [self.tableView reloadData];
   123 }
   124 }
   124 
   125 
   125 -(void) viewWillDisappear:(BOOL) animated {
   126 -(void) viewWillDisappear:(BOOL) animated {
   126     [super viewWillDisappear:animated];
   127     [super viewWillDisappear:animated];
   130 -(void) saveAmmos {
   131 -(void) saveAmmos {
   131     quantity[CURRENT_AMMOSIZE] = '\0';
   132     quantity[CURRENT_AMMOSIZE] = '\0';
   132     probability[CURRENT_AMMOSIZE] = '\0';
   133     probability[CURRENT_AMMOSIZE] = '\0';
   133     delay[CURRENT_AMMOSIZE] = '\0';
   134     delay[CURRENT_AMMOSIZE] = '\0';
   134     crateness[CURRENT_AMMOSIZE] = '\0';
   135     crateness[CURRENT_AMMOSIZE] = '\0';
   135 
   136     
   136     NSString *quantityStr = [NSString stringWithUTF8String:quantity];
   137     NSString *quantityStr = [NSString stringWithUTF8String:quantity];
   137     NSString *probabilityStr = [NSString stringWithUTF8String:probability];
   138     NSString *probabilityStr = [NSString stringWithUTF8String:probability];
   138     NSString *delayStr = [NSString stringWithUTF8String:delay];
   139     NSString *delayStr = [NSString stringWithUTF8String:delay];
   139     NSString *cratenessStr = [NSString stringWithUTF8String:crateness];
   140     NSString *cratenessStr = [NSString stringWithUTF8String:crateness];
   140 
   141     
   141     NSDictionary *weapon = [[NSDictionary alloc] initWithObjectsAndKeys:
   142     NSDictionary *weapon = [[NSDictionary alloc] initWithObjectsAndKeys:
   142                             [NSNumber numberWithInt:CURRENT_AMMOSIZE],@"version",
   143                             [NSNumber numberWithInt:CURRENT_AMMOSIZE],@"version",
   143                             quantityStr,@"ammostore_initialqt",
   144                             quantityStr,@"ammostore_initialqt",
   144                             probabilityStr,@"ammostore_probability",
   145                             probabilityStr,@"ammostore_probability",
   145                             delayStr,@"ammostore_delay",
   146                             delayStr,@"ammostore_delay",
   146                             cratenessStr,@"ammostore_crate", nil];
   147                             cratenessStr,@"ammostore_crate", 
   147 
   148                             self.description,@"description",
       
   149                             nil];
       
   150     
   148     NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName];
   151     NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName];
   149     [weapon writeToFile:ammoFile atomically:YES];
   152     [weapon writeToFile:ammoFile atomically:YES];
   150     [ammoFile release];
   153     [ammoFile release];
   151     [weapon release];
   154     [weapon release];
   152 }
   155 }
   157     return 2;
   160     return 2;
   158 }
   161 }
   159 
   162 
   160 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   163 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   161     if (section == 0)
   164     if (section == 0)
   162         return 1;
   165         return 2;
   163     else
   166     else
   164         return CURRENT_AMMOSIZE;
   167         return CURRENT_AMMOSIZE;
   165 }
   168 }
   166 
   169 
   167 // Customize the appearance of table view cells.
   170 // Customize the appearance of table view cells.
   168 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   171 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   169     static NSString *CellIdentifier0 = @"Cell0";
   172     static NSString *CellIdentifier0 = @"Cell0";
   170     static NSString *CellIdentifier1 = @"Cell1";
   173     static NSString *CellIdentifier1 = @"Cell1";
   171     NSInteger row = [indexPath row];
   174     NSInteger row = [indexPath row];
   172     UITableViewCell *cell = nil;
   175     UITableViewCell *cell = nil;
   173 
   176     
   174     if (0 == [indexPath section]) {
   177     if (0 == [indexPath section]) {
   175         EditableCellView *customCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   178         EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   176         if (customCell == nil) {
   179         if (editableCell == nil) {
   177             customCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault
   180             editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault
   178                                             reuseIdentifier:CellIdentifier0] autorelease];
   181                                                     reuseIdentifier:CellIdentifier0] autorelease];
   179             customCell.delegate = self;
   182             editableCell.delegate = self;
   180         }
   183         }
   181 
   184         editableCell.tag = row;
   182         customCell.textField.text = self.weaponName;
   185         editableCell.selectionStyle = UITableViewCellSelectionStyleNone;
   183         customCell.detailTextLabel.text = nil;
   186         editableCell.imageView.image = nil;
   184         customCell.imageView.image = nil;
   187         editableCell.detailTextLabel.text = nil;
   185         customCell.selectionStyle = UITableViewCellSelectionStyleNone;
   188         
   186         cell = customCell;
   189         if (row == 0) {
       
   190             editableCell.textField.text = self.weaponName;
       
   191         } else {
       
   192             editableCell.textField.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
       
   193             editableCell.textField.text = self.description;
       
   194             editableCell.textField.placeholder = NSLocalizedString(@"You can add a description if you wish",@"");
       
   195         }
       
   196         cell = editableCell;
   187     } else {
   197     } else {
   188         WeaponCellView *customCell = (WeaponCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   198         WeaponCellView *weaponCell = (WeaponCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   189         if (customCell == nil) {
   199         if (weaponCell == nil) {
   190             customCell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
   200             weaponCell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
   191             customCell.delegate = self;
   201             weaponCell.delegate = self;
   192         }
   202         }
   193 
   203 
   194         int x = ((row*32)/1024)*32;
   204         int x = ((row*32)/1024)*32;
   195         int y = (row*32)%1024;
   205         int y = (row*32)%1024;
   196 
   206 
   197         UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, 32, 32)] makeRoundCornersOfSize:CGSizeMake(7, 7)];
   207         UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, 32, 32)] makeRoundCornersOfSize:CGSizeMake(7, 7)];
   198         customCell.weaponIcon.image = img;
   208         weaponCell.weaponIcon.image = img;
   199         customCell.weaponName.text = [ammoNames objectAtIndex:row];
   209         weaponCell.weaponName.text = [ammoNames objectAtIndex:row];
   200         customCell.tag = row;
   210         weaponCell.tag = row;
   201 
   211 
   202         [customCell.initialSli setValue:[[NSString stringWithFormat:@"%c",quantity[row]] intValue] animated:NO];
   212         [weaponCell.initialSli setValue:[[NSString stringWithFormat:@"%c",quantity[row]] intValue] animated:NO];
   203         [customCell.probabilitySli setValue:[[NSString stringWithFormat:@"%c", probability[row]] intValue] animated:NO];
   213         [weaponCell.probabilitySli setValue:[[NSString stringWithFormat:@"%c", probability[row]] intValue] animated:NO];
   204         [customCell.delaySli setValue:[[NSString stringWithFormat:@"%c", delay[row]] intValue] animated:NO];
   214         [weaponCell.delaySli setValue:[[NSString stringWithFormat:@"%c", delay[row]] intValue] animated:NO];
   205         [customCell.crateSli setValue:[[NSString stringWithFormat:@"%c", crateness[row]] intValue] animated:NO];
   215         [weaponCell.crateSli setValue:[[NSString stringWithFormat:@"%c", crateness[row]] intValue] animated:NO];
   206         cell = customCell;
   216         cell = weaponCell;
   207     }
   217     }
   208 
   218 
   209     cell.selectionStyle = UITableViewCellSelectionStyleNone;
   219     cell.selectionStyle = UITableViewCellSelectionStyleNone;
   210     return cell;
   220     return cell;
   211 }
   221 }
   244 
   254 
   245 #pragma mark -
   255 #pragma mark -
   246 #pragma mark editableCellView delegate
   256 #pragma mark editableCellView delegate
   247 // set the new value
   257 // set the new value
   248 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {
   258 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {
   249     // delete old file
   259     if (tagValue == 0) {
   250     [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName] error:NULL];
   260         // delete old file
   251     // update filename
   261         [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName] error:NULL];
   252     self.weaponName = textString;
   262         // update filename
   253     // save new file
   263         self.weaponName = textString;
   254     [self saveAmmos];
   264         // save new file
       
   265         [self saveAmmos];
       
   266     } else {
       
   267         self.description = textString;
       
   268     }
   255 }
   269 }
   256 
   270 
   257 #pragma mark -
   271 #pragma mark -
   258 #pragma mark WeaponButtonControllerDelegate
   272 #pragma mark WeaponButtonControllerDelegate
   259 -(void) updateValues:(NSArray *)withArray atIndex:(NSInteger) index {
   273 -(void) updateValues:(NSArray *)withArray atIndex:(NSInteger) index {
   273     free(quantity); quantity = NULL;
   287     free(quantity); quantity = NULL;
   274     free(probability); probability = NULL;
   288     free(probability); probability = NULL;
   275     free(delay); delay = NULL;
   289     free(delay); delay = NULL;
   276     free(crateness); crateness = NULL;
   290     free(crateness); crateness = NULL;
   277     [super viewDidUnload];
   291     [super viewDidUnload];
       
   292     self.description = nil;
   278     self.weaponName = nil;
   293     self.weaponName = nil;
   279     self.ammoStoreImage = nil;
   294     self.ammoStoreImage = nil;
   280     self.ammoNames = nil;
   295     self.ammoNames = nil;
   281     MSG_DIDUNLOAD();
   296     MSG_DIDUNLOAD();
   282     [super viewDidUnload];
   297     [super viewDidUnload];
   283 }
   298 }
   284 
   299 
   285 
   300 
   286 -(void) dealloc {
   301 -(void) dealloc {
   287     [weaponName release];
   302     [weaponName release];
       
   303     [description release];
   288     [ammoStoreImage release];
   304     [ammoStoreImage release];
   289     [ammoNames release];
   305     [ammoNames release];
   290     [super dealloc];
   306     [super dealloc];
   291 }
   307 }
   292 
   308