project_files/HedgewarsMobile/Classes/SingleWeaponViewController.m
changeset 3621 a8ddf681ba7d
parent 3547 02875b1145b7
child 3623 f14db208f2fa
equal deleted inserted replaced
3620:c28a86f379f6 3621:a8ddf681ba7d
     5 //  Created by Vittorio on 19/06/10.
     5 //  Created by Vittorio on 19/06/10.
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
     7 //
     7 //
     8 
     8 
     9 #import "SingleWeaponViewController.h"
     9 #import "SingleWeaponViewController.h"
    10 
    10 #import "WeaponCellView.h"
       
    11 #import "CommodityFunctions.h"
       
    12 #import "UIImageExtra.h"
    11 
    13 
    12 @implementation SingleWeaponViewController
    14 @implementation SingleWeaponViewController
    13 
    15 @synthesize ammoStoreImage, ammoNames;
    14 
    16 
    15 #pragma mark -
    17 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    16 #pragma mark Initialization
    18     return rotationManager(interfaceOrientation);
    17 
    19 }
    18 /*
    20 
    19 - (id)initWithStyle:(UITableViewStyle)style {
    21 #pragma mark -
    20     // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    22 #pragma mark View lifecycle
    21     if ((self = [super initWithStyle:style])) {
    23 -(void) viewDidLoad {
       
    24     [super viewDidLoad];
       
    25     
       
    26     NSArray *array = [[NSArray alloc] initWithObjects:
       
    27                       NSLocalizedString(@"Grenade",@""),
       
    28                       NSLocalizedString(@"Cluster Bomb",@""),
       
    29                       NSLocalizedString(@"Bazooka",@""),
       
    30                       NSLocalizedString(@"Homing Bee",@""),
       
    31                       NSLocalizedString(@"Shotgun",@""),
       
    32                       NSLocalizedString(@"Pick Hammer",@""),
       
    33                       NSLocalizedString(@"Skip",@""),
       
    34                       NSLocalizedString(@"Rope",@""),
       
    35                       NSLocalizedString(@"Mine",@""),
       
    36                       NSLocalizedString(@"Deagle",@""),
       
    37                       NSLocalizedString(@"Dynamite",@""),
       
    38                       NSLocalizedString(@"Fire Punch",@""),
       
    39                       NSLocalizedString(@"Slash",@""),
       
    40                       NSLocalizedString(@"Baseball bat",@""),
       
    41                       NSLocalizedString(@"Parachute",@""),
       
    42                       NSLocalizedString(@"Air Attack",@""),
       
    43                       NSLocalizedString(@"Mines Attack",@""),
       
    44                       NSLocalizedString(@"Blow Torch",@""),
       
    45                       NSLocalizedString(@"Construction",@""),
       
    46                       NSLocalizedString(@"Teleport",@""),
       
    47                       NSLocalizedString(@"Switch Hedgehog",@""),
       
    48                       NSLocalizedString(@"Mortar",@""),
       
    49                       NSLocalizedString(@"Kamikaze",@""),
       
    50                       NSLocalizedString(@"Cake",@""),
       
    51                       NSLocalizedString(@"Seduction",@""),
       
    52                       NSLocalizedString(@"Watermelon Bomb",@""),
       
    53                       NSLocalizedString(@"Hellish Hand Grenade",@""),
       
    54                       NSLocalizedString(@"Napalm Attack",@""),
       
    55                       NSLocalizedString(@"Drill Rocket",@""),
       
    56                       NSLocalizedString(@"Ballgun",@""),
       
    57                       NSLocalizedString(@"RC Plane",@""),
       
    58                       NSLocalizedString(@"Low Gravity",@""),
       
    59                       NSLocalizedString(@"Extra Damage",@""),
       
    60                       NSLocalizedString(@"Invulnerable",@""),
       
    61                       NSLocalizedString(@"Extra Time",@""),
       
    62                       NSLocalizedString(@"Laser Sight",@""),
       
    63                       NSLocalizedString(@"Vampirism",@""),
       
    64                       NSLocalizedString(@"Sniper Rifle",@""),
       
    65                       NSLocalizedString(@"Flying Saucer",@""),
       
    66                       NSLocalizedString(@"Molotov Cocktail",@""),
       
    67                       NSLocalizedString(@"Birdy",@""),
       
    68                       NSLocalizedString(@"Portable Portal Device",@""),
       
    69                       NSLocalizedString(@"Piano Attack",@""),
       
    70                       NSLocalizedString(@"Old Limburger",@""),
       
    71                       NSLocalizedString(@"Sine Gun",@""),
       
    72                       NSLocalizedString(@"Flamethrower",@""),
       
    73                       nil];
       
    74     self.ammoNames = array;
       
    75     [array release];
       
    76 
       
    77     ammoSize = [self.ammoNames count];
       
    78     quantity = (char *)malloc(sizeof(char)*ammoSize);
       
    79     probability = (char *)malloc(sizeof(char)*ammoSize);
       
    80     delay = (char *)malloc(sizeof(char)*ammoSize);
       
    81     crateness = (char *)malloc(sizeof(char)*ammoSize);
       
    82     
       
    83     NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()];
       
    84     UIImage *img = [[UIImage alloc] initWithContentsOfFile:str];
       
    85     self.ammoStoreImage = img;
       
    86     [img release];
       
    87     
       
    88     self.tableView.rowHeight = 75;
       
    89 }
       
    90 
       
    91 -(void) viewWillAppear:(BOOL) animated {
       
    92     [super viewWillAppear:animated];
       
    93     
       
    94     NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.title];
       
    95     NSDictionary *weapon = [[NSDictionary alloc] initWithContentsOfFile:ammoFile];
       
    96     [ammoFile release];
       
    97     
       
    98     const char *tmp1 = [[weapon objectForKey:@"ammostore_initialqt"] UTF8String];
       
    99     const char *tmp2 = [[weapon objectForKey:@"ammostore_probability"] UTF8String];
       
   100     const char *tmp3 = [[weapon objectForKey:@"ammostore_delay"] UTF8String];
       
   101     const char *tmp4 = [[weapon objectForKey:@"ammostore_crate"] UTF8String];
       
   102     [weapon release];
       
   103     
       
   104     // if the new weaponset is diffrent from the older we need to update it replacing
       
   105     // the missing ammos with 0 quantity
       
   106     int oldlen = strlen(tmp1);
       
   107     for (int i = 0; i < oldlen; i++) {
       
   108         quantity[i] = tmp1[i];
       
   109         probability[i] = tmp2[i];
       
   110         delay[i] = tmp3[i];
       
   111         crateness[i] = tmp4[i];
    22     }
   112     }
    23     return self;
   113     for (int i = oldlen; i < ammoSize; i++) {
    24 }
   114         quantity[i] = '0';
    25 */
   115         probability[i] = '0';
    26 
   116         delay[i] = '0';
    27 
   117         crateness[i] = '0';
    28 #pragma mark -
   118     }
    29 #pragma mark View lifecycle
   119     
    30 
   120     [self.tableView reloadData];
    31 /*
   121 }
    32 - (void)viewDidLoad {
   122 
    33     [super viewDidLoad];
   123 -(void) viewWillDisappear:(BOOL) animated {
    34 
       
    35     // Uncomment the following line to preserve selection between presentations.
       
    36     self.clearsSelectionOnViewWillAppear = NO;
       
    37  
       
    38     // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
       
    39     // self.navigationItem.rightBarButtonItem = self.editButtonItem;
       
    40 }
       
    41 */
       
    42 
       
    43 /*
       
    44 - (void)viewWillAppear:(BOOL)animated {
       
    45     [super viewWillAppear:animated];
       
    46 }
       
    47 */
       
    48 /*
       
    49 - (void)viewDidAppear:(BOOL)animated {
       
    50     [super viewDidAppear:animated];
       
    51 }
       
    52 */
       
    53 /*
       
    54 - (void)viewWillDisappear:(BOOL)animated {
       
    55     [super viewWillDisappear:animated];
   124     [super viewWillDisappear:animated];
    56 }
   125     
    57 */
   126     NSString *quantityStr = [NSString stringWithUTF8String:quantity];
    58 /*
   127     NSString *probabilityStr = [NSString stringWithUTF8String:probability];
    59 - (void)viewDidDisappear:(BOOL)animated {
   128     NSString *delayStr = [NSString stringWithUTF8String:delay];
    60     [super viewDidDisappear:animated];
   129     NSString *cratenessStr = [NSString stringWithUTF8String:crateness];
    61 }
   130 
    62 */
   131     NSDictionary *weapon = [[NSDictionary alloc] initWithObjectsAndKeys:
    63 /*
   132                             [NSNumber numberWithInt:CURRENT_AMMOSIZE],@"version",
    64 // Override to allow orientations other than the default portrait orientation.
   133                             quantityStr,@"ammostore_initialqt",
    65 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   134                             probabilityStr,@"ammostore_probability",
    66     // Return YES for supported orientations
   135                             delayStr,@"ammostore_delay",
    67     return (interfaceOrientation == UIInterfaceOrientationPortrait);
   136                             cratenessStr,@"ammostore_crate", nil];
    68 }
   137 
    69 */
   138     NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.title];
    70 
   139     [weapon writeToFile:ammoFile atomically:YES];
       
   140     [ammoFile release];
       
   141     [weapon release];
       
   142 }
    71 
   143 
    72 #pragma mark -
   144 #pragma mark -
    73 #pragma mark Table view data source
   145 #pragma mark Table view data source
    74 
   146 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    75 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   147     return 1;
    76     // Return the number of sections.
   148 }
    77     return 0;
   149 
    78 }
   150 
    79 
   151 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    80 
   152     return ammoSize;
    81 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
    82     // Return the number of rows in the section.
       
    83     return 0;
       
    84 }
   153 }
    85 
   154 
    86 
   155 
    87 // Customize the appearance of table view cells.
   156 // Customize the appearance of table view cells.
    88 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   157 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    89     
       
    90     static NSString *CellIdentifier = @"Cell";
   158     static NSString *CellIdentifier = @"Cell";
    91     
   159     NSInteger row = [indexPath row];
    92     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   160     
       
   161     WeaponCellView *cell = (WeaponCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    93     if (cell == nil) {
   162     if (cell == nil) {
    94         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   163         cell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    95     }
   164     }
    96     
   165     
    97     // Configure the cell...
   166     int x = ((row*32)/1024)*32;
    98     
   167     int y = (row*32)%1024;
       
   168 
       
   169     UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, 32, 32)] makeRoundCornersOfSize:CGSizeMake(7, 7)];
       
   170     cell.weaponIcon.image = img;
       
   171     cell.weaponName.text = [ammoNames objectAtIndex:row];
       
   172 
       
   173     cell.initialQt.titleLabel.text = [NSString stringWithFormat:@"%c",quantity[row]];
       
   174     cell.probability.titleLabel.text = [NSString stringWithFormat:@"%c",probability[row]];
       
   175     cell.delay.titleLabel.text = [NSString stringWithFormat:@"%c",delay[row]];
       
   176     cell.initialQt.titleLabel.text = [NSString stringWithFormat:@"%c",crateness[row]];
    99     return cell;
   177     return cell;
   100 }
   178 }
   101 
   179 
   102 
   180 
   103 /*
       
   104 // Override to support conditional editing of the table view.
       
   105 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
       
   106     // Return NO if you do not want the specified item to be editable.
       
   107     return YES;
       
   108 }
       
   109 */
       
   110 
       
   111 
       
   112 /*
       
   113 // Override to support editing the table view.
       
   114 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
       
   115     
       
   116     if (editingStyle == UITableViewCellEditingStyleDelete) {
       
   117         // Delete the row from the data source
       
   118         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
       
   119     }   
       
   120     else if (editingStyle == UITableViewCellEditingStyleInsert) {
       
   121         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
       
   122     }   
       
   123 }
       
   124 */
       
   125 
       
   126 
       
   127 /*
       
   128 // Override to support rearranging the table view.
       
   129 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
       
   130 }
       
   131 */
       
   132 
       
   133 
       
   134 /*
       
   135 // Override to support conditional rearranging of the table view.
       
   136 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
       
   137     // Return NO if you do not want the item to be re-orderable.
       
   138     return YES;
       
   139 }
       
   140 */
       
   141 
       
   142 
       
   143 #pragma mark -
   181 #pragma mark -
   144 #pragma mark Table view delegate
   182 #pragma mark Table view delegate
   145 
   183 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   146 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
   147     // Navigation logic may go here. Create and push another view controller.
   184     // Navigation logic may go here. Create and push another view controller.
   148     /*
   185     /*
   149      <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
   186      <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
   150      // ...
   187      // ...
   151      // Pass the selected object to the new view controller.
   188      // Pass the selected object to the new view controller.
   155 }
   192 }
   156 
   193 
   157 
   194 
   158 #pragma mark -
   195 #pragma mark -
   159 #pragma mark Memory management
   196 #pragma mark Memory management
   160 
   197 -(void) didReceiveMemoryWarning {
   161 - (void)didReceiveMemoryWarning {
       
   162     // Releases the view if it doesn't have a superview.
   198     // Releases the view if it doesn't have a superview.
   163     [super didReceiveMemoryWarning];
   199     [super didReceiveMemoryWarning];
   164     
       
   165     // Relinquish ownership any cached data, images, etc that aren't in use.
   200     // Relinquish ownership any cached data, images, etc that aren't in use.
   166 }
   201 }
   167 
   202 
   168 - (void)viewDidUnload {
   203 -(void) viewDidUnload {
   169     // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
   204     free(quantity);
   170     // For example: self.myOutlet = nil;
   205     free(probability);
   171 }
   206     free(delay);
   172 
   207     free(crateness);
   173 
   208     [super viewDidUnload];
   174 - (void)dealloc {
   209     MSG_DIDUNLOAD();
       
   210 }
       
   211 
       
   212 
       
   213 -(void) dealloc {
   175     [super dealloc];
   214     [super dealloc];
   176 }
   215 }
   177 
   216 
   178 
   217 
   179 @end
   218 @end