project_files/HedgewarsMobile/Classes/SingleWeaponViewController.m
branchios-develop
changeset 12872 00215a7ec5f5
parent 12871 2c06b1120749
child 13166 ba5c794adae3
equal deleted inserted replaced
12871:2c06b1120749 12872:00215a7ec5f5
    18 
    18 
    19 
    19 
    20 #import "SingleWeaponViewController.h"
    20 #import "SingleWeaponViewController.h"
    21 
    21 
    22 @interface SingleWeaponViewController ()
    22 @interface SingleWeaponViewController ()
    23 @property (nonatomic, retain) NSString *trPath;
    23 @property (nonatomic, strong) NSString *trPath;
    24 @property (nonatomic, retain) NSString *trFileName;
    24 @property (nonatomic, strong) NSString *trFileName;
    25 @end
    25 @end
    26 
    26 
    27 @implementation SingleWeaponViewController
    27 @implementation SingleWeaponViewController
    28 @synthesize weaponName, description, ammoStoreImage;
    28 @synthesize weaponName, description, ammoStoreImage;
    29 
    29 
    30 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    30 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    31     return rotationManager(interfaceOrientation);
    31     return rotationManager(interfaceOrientation);
    32 }
    32 }
    33 
    33 
    34 #pragma mark -
    34 #pragma mark -
    35 #pragma mark View lifecycle
    35 #pragma mark View lifecycle
    36 -(void) viewDidLoad {
    36 - (void)viewDidLoad {
    37     [super viewDidLoad];
    37     [super viewDidLoad];
    38 
    38 
    39     self.trPath = [NSString stringWithFormat:@"%@", LOCALE_DIRECTORY()];
    39     self.trPath = [NSString stringWithFormat:@"%@", LOCALE_DIRECTORY()];
    40     self.trFileName = [NSString stringWithFormat:@"%@.txt", [HWUtils languageID]];
    40     self.trFileName = [NSString stringWithFormat:@"%@.txt", [HWUtils languageID]];
    41     // fill the data structure that we are going to read
    41     // fill the data structure that we are going to read
    47     crateness = (char *)malloc(sizeof(char)*(HW_getNumberOfWeapons()+1));
    47     crateness = (char *)malloc(sizeof(char)*(HW_getNumberOfWeapons()+1));
    48 
    48 
    49     NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos_base.png", GRAPHICS_DIRECTORY()];
    49     NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos_base.png", GRAPHICS_DIRECTORY()];
    50     UIImage *img = [[UIImage alloc] initWithContentsOfFile:str];
    50     UIImage *img = [[UIImage alloc] initWithContentsOfFile:str];
    51     self.ammoStoreImage = img;
    51     self.ammoStoreImage = img;
    52     [img release];
       
    53 
    52 
    54     self.title = NSLocalizedString(@"Edit weapons preferences",@"");
    53     self.title = NSLocalizedString(@"Edit weapons preferences",@"");
    55 }
    54 }
    56 
    55 
    57 -(void) viewWillAppear:(BOOL) animated {
    56 - (void)viewWillAppear:(BOOL) animated {
    58     [super viewWillAppear:animated];
    57     [super viewWillAppear:animated];
    59 
    58 
    60     NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName];
    59     NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName];
    61     NSDictionary *weapon = [[NSDictionary alloc] initWithContentsOfFile:ammoFile];
    60     NSDictionary *weapon = [[NSDictionary alloc] initWithContentsOfFile:ammoFile];
    62     [ammoFile release];
       
    63 
    61 
    64     self.description = [weapon objectForKey:@"description"];
    62     self.description = [weapon objectForKey:@"description"];
    65     const char *tmp1 = [[weapon objectForKey:@"ammostore_initialqt"] UTF8String];
    63     const char *tmp1 = [[weapon objectForKey:@"ammostore_initialqt"] UTF8String];
    66     const char *tmp2 = [[weapon objectForKey:@"ammostore_probability"] UTF8String];
    64     const char *tmp2 = [[weapon objectForKey:@"ammostore_probability"] UTF8String];
    67     const char *tmp3 = [[weapon objectForKey:@"ammostore_delay"] UTF8String];
    65     const char *tmp3 = [[weapon objectForKey:@"ammostore_delay"] UTF8String];
    68     const char *tmp4 = [[weapon objectForKey:@"ammostore_crate"] UTF8String];
    66     const char *tmp4 = [[weapon objectForKey:@"ammostore_crate"] UTF8String];
    69     [weapon release];
       
    70 
    67 
    71     // if the new weaponset is diffrent from the older we need to update it replacing
    68     // if the new weaponset is diffrent from the older we need to update it replacing
    72     // the missing ammos with 0 quantity
    69     // the missing ammos with 0 quantity
    73     int oldlen = strlen(tmp1);
    70     int oldlen = strlen(tmp1);
    74     for (int i = 0; i < oldlen; i++) {
    71     for (int i = 0; i < oldlen; i++) {
    85     }
    82     }
    86 
    83 
    87     [self.tableView reloadData];
    84     [self.tableView reloadData];
    88 }
    85 }
    89 
    86 
    90 -(void) viewWillDisappear:(BOOL) animated {
    87 - (void)viewWillDisappear:(BOOL) animated {
    91     [super viewWillDisappear:animated];
    88     [super viewWillDisappear:animated];
    92     [self saveAmmos];
    89     [self saveAmmos];
    93 }
    90 }
    94 
    91 
    95 -(void) saveAmmos {
    92 - (void)saveAmmos {
    96     quantity[HW_getNumberOfWeapons()] = '\0';
    93     quantity[HW_getNumberOfWeapons()] = '\0';
    97     probability[HW_getNumberOfWeapons()] = '\0';
    94     probability[HW_getNumberOfWeapons()] = '\0';
    98     delay[HW_getNumberOfWeapons()] = '\0';
    95     delay[HW_getNumberOfWeapons()] = '\0';
    99     crateness[HW_getNumberOfWeapons()] = '\0';
    96     crateness[HW_getNumberOfWeapons()] = '\0';
   100 
    97 
   111                             self.description,@"description",
   108                             self.description,@"description",
   112                             nil];
   109                             nil];
   113 
   110 
   114     NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName];
   111     NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName];
   115     [weapon writeToFile:ammoFile atomically:YES];
   112     [weapon writeToFile:ammoFile atomically:YES];
   116     [ammoFile release];
       
   117     [weapon release];
       
   118 }
   113 }
   119 
   114 
   120 #pragma mark -
   115 #pragma mark -
   121 #pragma mark Table view data source
   116 #pragma mark Table view data source
   122 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   117 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   123     return 2;
   118     return 2;
   124 }
   119 }
   125 
   120 
   126 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   121 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   127     if (section == 0)
   122     if (section == 0)
   128         return 2;
   123         return 2;
   129     else
   124     else
   130         return HW_getNumberOfWeapons();
   125         return HW_getNumberOfWeapons();
   131 }
   126 }
   138     UITableViewCell *cell = nil;
   133     UITableViewCell *cell = nil;
   139 
   134 
   140     if (0 == [indexPath section]) {
   135     if (0 == [indexPath section]) {
   141         EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   136         EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   142         if (editableCell == nil) {
   137         if (editableCell == nil) {
   143             editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault
   138             editableCell = [[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault
   144                                                     reuseIdentifier:CellIdentifier0] autorelease];
   139                                                     reuseIdentifier:CellIdentifier0];
   145             editableCell.delegate = self;
   140             editableCell.delegate = self;
   146         }
   141         }
   147         editableCell.tag = row;
   142         editableCell.tag = row;
   148         editableCell.selectionStyle = UITableViewCellSelectionStyleNone;
   143         editableCell.selectionStyle = UITableViewCellSelectionStyleNone;
   149         editableCell.imageView.image = nil;
   144         editableCell.imageView.image = nil;
   160         }
   155         }
   161         cell = editableCell;
   156         cell = editableCell;
   162     } else {
   157     } else {
   163         WeaponCellView *weaponCell = (WeaponCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   158         WeaponCellView *weaponCell = (WeaponCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   164         if (weaponCell == nil) {
   159         if (weaponCell == nil) {
   165             weaponCell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
   160             weaponCell = [[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
   166             weaponCell.delegate = self;
   161             weaponCell.delegate = self;
   167         }
   162         }
   168 
   163 
   169         CGFloat theScale = [[UIScreen mainScreen] safeScale];
   164         CGFloat theScale = [[UIScreen mainScreen] safeScale];
   170         int size = 32 * theScale;
   165         int size = 32 * theScale;
   211     return sectionTitle;
   206     return sectionTitle;
   212 }
   207 }
   213 
   208 
   214 #pragma mark -
   209 #pragma mark -
   215 #pragma mark Table view delegate
   210 #pragma mark Table view delegate
   216 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   211 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   217     if (0 == [indexPath section]) {
   212     if (0 == [indexPath section]) {
   218         EditableCellView *editableCell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath];
   213         EditableCellView *editableCell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath];
   219         [editableCell replyKeyboard];
   214         [editableCell replyKeyboard];
   220     }
   215     }
   221 }
   216 }
   222 
   217 
   223 #pragma mark -
   218 #pragma mark -
   224 #pragma mark editableCellView delegate
   219 #pragma mark editableCellView delegate
   225 // set the new value
   220 // set the new value
   226 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {
   221 - (void)saveTextFieldValue:(NSString *)textString withTag:(NSInteger)tagValue {
   227     if (tagValue == 0) {
   222     if (tagValue == 0) {
   228         // delete old file
   223         // delete old file
   229         [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName] error:NULL];
   224         [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.weaponName] error:NULL];
   230         // update filename
   225         // update filename
   231         self.weaponName = textString;
   226         self.weaponName = textString;
   236     }
   231     }
   237 }
   232 }
   238 
   233 
   239 #pragma mark -
   234 #pragma mark -
   240 #pragma mark WeaponButtonControllerDelegate
   235 #pragma mark WeaponButtonControllerDelegate
   241 -(void) updateValues:(NSArray *)withArray atIndex:(NSInteger) index {
   236 - (void)updateValues:(NSArray *)withArray atIndex:(NSInteger)index {
   242     quantity[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:0] intValue]] characterAtIndex:0];
   237     quantity[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:0] intValue]] characterAtIndex:0];
   243     probability[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:1] intValue]] characterAtIndex:0];
   238     probability[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:1] intValue]] characterAtIndex:0];
   244     delay[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:2] intValue]] characterAtIndex:0];
   239     delay[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:2] intValue]] characterAtIndex:0];
   245     crateness[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:3] intValue]] characterAtIndex:0];
   240     crateness[index] = [[NSString stringWithFormat:@"%d",[[withArray objectAtIndex:3] intValue]] characterAtIndex:0];
   246 }
   241 }
   247 
   242 
   248 #pragma mark -
   243 #pragma mark -
   249 #pragma mark Memory management
   244 #pragma mark Memory management
   250 -(void) didReceiveMemoryWarning {
   245 
   251     [super didReceiveMemoryWarning];
   246 - (void)dealloc {
   252 }
       
   253 
       
   254 -(void) viewDidUnload {
       
   255     free(quantity); quantity = NULL;
   247     free(quantity); quantity = NULL;
   256     free(probability); probability = NULL;
   248     free(probability); probability = NULL;
   257     free(delay); delay = NULL;
   249     free(delay); delay = NULL;
   258     free(crateness); crateness = NULL;
   250     free(crateness); crateness = NULL;
   259     [super viewDidUnload];
   251 }
   260     self.description = nil;
       
   261     self.weaponName = nil;
       
   262     self.ammoStoreImage = nil;
       
   263     MSG_DIDUNLOAD();
       
   264     [super viewDidUnload];
       
   265 }
       
   266 
       
   267 
       
   268 -(void) dealloc {
       
   269     releaseAndNil(_trPath);
       
   270     releaseAndNil(_trFileName);
       
   271     
       
   272     releaseAndNil(weaponName);
       
   273     releaseAndNil(description);
       
   274     releaseAndNil(ammoStoreImage);
       
   275     [super dealloc];
       
   276 }
       
   277 
       
   278 
   252 
   279 @end
   253 @end
   280 
   254