project_files/HedgewarsMobile/Classes/SingleWeaponViewController.m
changeset 3659 f8d5ac50e307
parent 3644 42c5684289ae
child 3660 bc125bea5849
equal deleted inserted replaced
3658:113cb9345be1 3659:f8d5ac50e307
    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 ammoStoreImage, ammoNames;
    15 @synthesize weaponName, 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 
    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.tableView.rowHeight = 120;
    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.title];
    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];
   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];
   124     
   124     [self saveAmmos];
       
   125 }
       
   126 
       
   127 -(void) saveAmmos {
   125     quantity[CURRENT_AMMOSIZE] = '\0';
   128     quantity[CURRENT_AMMOSIZE] = '\0';
   126     probability[CURRENT_AMMOSIZE] = '\0';
   129     probability[CURRENT_AMMOSIZE] = '\0';
   127     delay[CURRENT_AMMOSIZE] = '\0';
   130     delay[CURRENT_AMMOSIZE] = '\0';
   128     crateness[CURRENT_AMMOSIZE] = '\0';
   131     crateness[CURRENT_AMMOSIZE] = '\0';
   129     
   132     
   137                             quantityStr,@"ammostore_initialqt",
   140                             quantityStr,@"ammostore_initialqt",
   138                             probabilityStr,@"ammostore_probability",
   141                             probabilityStr,@"ammostore_probability",
   139                             delayStr,@"ammostore_delay",
   142                             delayStr,@"ammostore_delay",
   140                             cratenessStr,@"ammostore_crate", nil];
   143                             cratenessStr,@"ammostore_crate", nil];
   141 
   144 
   142     NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.title];
   145     NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName];
   143     [weapon writeToFile:ammoFile atomically:YES];
   146     [weapon writeToFile:ammoFile atomically:YES];
   144     [ammoFile release];
   147     [ammoFile release];
   145     [weapon release];
   148     [weapon release];
   146 }
   149 }
   147 
   150 
   148 #pragma mark -
   151 #pragma mark -
   149 #pragma mark Table view data source
   152 #pragma mark Table view data source
   150 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   153 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   151     return 1;
   154     return 2;
   152 }
   155 }
   153 
   156 
   154 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   157 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   155     return CURRENT_AMMOSIZE;
   158     if (section == 0)
       
   159         return 1;
       
   160     else
       
   161         return CURRENT_AMMOSIZE;
   156 }
   162 }
   157 
   163 
   158 // Customize the appearance of table view cells.
   164 // Customize the appearance of table view cells.
   159 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   165 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   160     static NSString *CellIdentifier = @"Cell";
   166     static NSString *CellIdentifier0 = @"Cell0";
       
   167     static NSString *CellIdentifier1 = @"Cell1";
   161     NSInteger row = [indexPath row];
   168     NSInteger row = [indexPath row];
   162     
   169     UITableViewCell *cell = nil;
   163     WeaponCellView *cell = (WeaponCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   170 
   164     if (cell == nil) {
   171     if (0 == [indexPath section]) {
   165         cell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   172         EditableCellView *customCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   166         cell.delegate = self;
   173         if (customCell == nil) {
   167     }
   174             customCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault 
   168     
   175                                             reuseIdentifier:CellIdentifier0] autorelease];
   169     int x = ((row*32)/1024)*32;
   176             customCell.delegate = self;
   170     int y = (row*32)%1024;
   177         }
   171 
   178         
   172     UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, 32, 32)] makeRoundCornersOfSize:CGSizeMake(7, 7)];
   179         customCell.textField.text = self.weaponName;
   173     cell.weaponIcon.image = img;
   180         customCell.detailTextLabel.text = nil;
   174     cell.weaponName.text = [ammoNames objectAtIndex:row];
   181         customCell.imageView.image = nil;
   175     cell.tag = row;
   182         customCell.selectionStyle = UITableViewCellSelectionStyleNone;
   176     
   183         cell = customCell;
   177     [cell.initialQt setValue:[[NSString stringWithFormat:@"%c",quantity[row]] intValue] animated:NO];
   184     } else {
   178     [cell.probabilityQt setValue:[[NSString stringWithFormat:@"%c", probability[row]] intValue] animated:NO];
   185         WeaponCellView *customCell = (WeaponCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   179     [cell.delayQt setValue:[[NSString stringWithFormat:@"%c", delay[row]] intValue] animated:NO];
   186         if (customCell == nil) {
   180     [cell.crateQt setValue:[[NSString stringWithFormat:@"%c", crateness[row]] intValue] animated:NO];
   187             customCell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
       
   188             customCell.delegate = self;
       
   189         }
       
   190         
       
   191         int x = ((row*32)/1024)*32;
       
   192         int y = (row*32)%1024;
       
   193         
       
   194         UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, 32, 32)] makeRoundCornersOfSize:CGSizeMake(7, 7)];
       
   195         customCell.weaponIcon.image = img;
       
   196         customCell.weaponName.text = [ammoNames objectAtIndex:row];
       
   197         customCell.tag = row;
       
   198         
       
   199         [customCell.initialQt setValue:[[NSString stringWithFormat:@"%c",quantity[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];
       
   202         [customCell.crateQt setValue:[[NSString stringWithFormat:@"%c", crateness[row]] intValue] animated:NO];
       
   203         cell = customCell;
       
   204     }
   181     
   205     
   182     cell.selectionStyle = UITableViewCellSelectionStyleNone;
   206     cell.selectionStyle = UITableViewCellSelectionStyleNone;
   183     return cell;
   207     return cell;
   184 }
   208 }
   185 
   209 
       
   210 -(CGFloat) tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
       
   211     if (0 == [indexPath section])
       
   212         return aTableView.rowHeight;
       
   213     else
       
   214         return 120;
       
   215 }
   186 
   216 
   187 #pragma mark -
   217 #pragma mark -
   188 #pragma mark Table view delegate
   218 #pragma mark Table view delegate
   189 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   219 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   190     // Navigation logic may go here. Create and push another view controller.
   220     if (0 == [indexPath section]) {
   191     /*
   221         EditableCellView *editableCell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath];
   192      <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
   222         [editableCell replyKeyboard];
   193      // ...
   223     }
   194      // Pass the selected object to the new view controller.
   224 }
   195      [self.navigationController pushViewController:detailViewController animated:YES];
   225 
   196      [detailViewController release];
   226 #pragma mark -
   197      */
   227 #pragma mark editableCellView delegate
       
   228 // set the new value
       
   229 -(void) saveTextFieldValue:(NSString *)textString {    
       
   230     // delete old file
       
   231     [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName] error:NULL];
       
   232     // update filename
       
   233     self.weaponName = textString;
       
   234     // save new file
       
   235     [self saveAmmos];
   198 }
   236 }
   199 
   237 
   200 #pragma mark -
   238 #pragma mark -
   201 #pragma mark WeaponButtonControllerDelegate
   239 #pragma mark WeaponButtonControllerDelegate
   202 -(void) updateValues:(NSArray *)withArray atIndex:(NSInteger) index {
   240 -(void) updateValues:(NSArray *)withArray atIndex:(NSInteger) index {
   207 }
   245 }
   208 
   246 
   209 #pragma mark -
   247 #pragma mark -
   210 #pragma mark Memory management
   248 #pragma mark Memory management
   211 -(void) didReceiveMemoryWarning {
   249 -(void) didReceiveMemoryWarning {
   212     // Releases the view if it doesn't have a superview.
       
   213     [super didReceiveMemoryWarning];
   250     [super didReceiveMemoryWarning];
   214     // Relinquish ownership any cached data, images, etc that aren't in use.
       
   215 }
   251 }
   216 
   252 
   217 -(void) viewDidUnload {
   253 -(void) viewDidUnload {
   218     free(quantity);
   254     free(quantity); quantity = NULL;
   219     free(probability);
   255     free(probability); probability = NULL;
   220     free(delay);
   256     free(delay); delay = NULL;
   221     free(crateness);
   257     free(crateness); crateness = NULL;
   222     [super viewDidUnload];
   258     [super viewDidUnload];
       
   259     self.weaponName = nil;
       
   260     self.ammoStoreImage = nil;
       
   261     self.ammoNames = nil;
   223     MSG_DIDUNLOAD();
   262     MSG_DIDUNLOAD();
   224 }
   263 }
   225 
   264 
   226 
   265 
   227 -(void) dealloc {
   266 -(void) dealloc {
       
   267     [weaponName release];
       
   268     [ammoStoreImage release];
       
   269     [ammoNames release];
   228     [super dealloc];
   270     [super dealloc];
   229 }
   271 }
   230 
   272 
   231 
   273 
   232 @end
   274 @end