project_files/HedgewarsMobile/Classes/SingleWeaponViewController.m
changeset 3621 a8ddf681ba7d
parent 3547 02875b1145b7
child 3623 f14db208f2fa
--- a/project_files/HedgewarsMobile/Classes/SingleWeaponViewController.m	Sat Jul 03 15:31:36 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/SingleWeaponViewController.m	Sun Jul 04 00:33:59 2010 +0200
@@ -7,143 +7,180 @@
 //
 
 #import "SingleWeaponViewController.h"
-
+#import "WeaponCellView.h"
+#import "CommodityFunctions.h"
+#import "UIImageExtra.h"
 
 @implementation SingleWeaponViewController
-
-
-#pragma mark -
-#pragma mark Initialization
+@synthesize ammoStoreImage, ammoNames;
 
-/*
-- (id)initWithStyle:(UITableViewStyle)style {
-    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
-    if ((self = [super initWithStyle:style])) {
-    }
-    return self;
+-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
+    return rotationManager(interfaceOrientation);
 }
-*/
-
 
 #pragma mark -
 #pragma mark View lifecycle
-
-/*
-- (void)viewDidLoad {
+-(void) viewDidLoad {
     [super viewDidLoad];
+    
+    NSArray *array = [[NSArray alloc] initWithObjects:
+                      NSLocalizedString(@"Grenade",@""),
+                      NSLocalizedString(@"Cluster Bomb",@""),
+                      NSLocalizedString(@"Bazooka",@""),
+                      NSLocalizedString(@"Homing Bee",@""),
+                      NSLocalizedString(@"Shotgun",@""),
+                      NSLocalizedString(@"Pick Hammer",@""),
+                      NSLocalizedString(@"Skip",@""),
+                      NSLocalizedString(@"Rope",@""),
+                      NSLocalizedString(@"Mine",@""),
+                      NSLocalizedString(@"Deagle",@""),
+                      NSLocalizedString(@"Dynamite",@""),
+                      NSLocalizedString(@"Fire Punch",@""),
+                      NSLocalizedString(@"Slash",@""),
+                      NSLocalizedString(@"Baseball bat",@""),
+                      NSLocalizedString(@"Parachute",@""),
+                      NSLocalizedString(@"Air Attack",@""),
+                      NSLocalizedString(@"Mines Attack",@""),
+                      NSLocalizedString(@"Blow Torch",@""),
+                      NSLocalizedString(@"Construction",@""),
+                      NSLocalizedString(@"Teleport",@""),
+                      NSLocalizedString(@"Switch Hedgehog",@""),
+                      NSLocalizedString(@"Mortar",@""),
+                      NSLocalizedString(@"Kamikaze",@""),
+                      NSLocalizedString(@"Cake",@""),
+                      NSLocalizedString(@"Seduction",@""),
+                      NSLocalizedString(@"Watermelon Bomb",@""),
+                      NSLocalizedString(@"Hellish Hand Grenade",@""),
+                      NSLocalizedString(@"Napalm Attack",@""),
+                      NSLocalizedString(@"Drill Rocket",@""),
+                      NSLocalizedString(@"Ballgun",@""),
+                      NSLocalizedString(@"RC Plane",@""),
+                      NSLocalizedString(@"Low Gravity",@""),
+                      NSLocalizedString(@"Extra Damage",@""),
+                      NSLocalizedString(@"Invulnerable",@""),
+                      NSLocalizedString(@"Extra Time",@""),
+                      NSLocalizedString(@"Laser Sight",@""),
+                      NSLocalizedString(@"Vampirism",@""),
+                      NSLocalizedString(@"Sniper Rifle",@""),
+                      NSLocalizedString(@"Flying Saucer",@""),
+                      NSLocalizedString(@"Molotov Cocktail",@""),
+                      NSLocalizedString(@"Birdy",@""),
+                      NSLocalizedString(@"Portable Portal Device",@""),
+                      NSLocalizedString(@"Piano Attack",@""),
+                      NSLocalizedString(@"Old Limburger",@""),
+                      NSLocalizedString(@"Sine Gun",@""),
+                      NSLocalizedString(@"Flamethrower",@""),
+                      nil];
+    self.ammoNames = array;
+    [array release];
 
-    // Uncomment the following line to preserve selection between presentations.
-    self.clearsSelectionOnViewWillAppear = NO;
- 
-    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
-    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
-}
-*/
-
-/*
-- (void)viewWillAppear:(BOOL)animated {
-    [super viewWillAppear:animated];
+    ammoSize = [self.ammoNames count];
+    quantity = (char *)malloc(sizeof(char)*ammoSize);
+    probability = (char *)malloc(sizeof(char)*ammoSize);
+    delay = (char *)malloc(sizeof(char)*ammoSize);
+    crateness = (char *)malloc(sizeof(char)*ammoSize);
+    
+    NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()];
+    UIImage *img = [[UIImage alloc] initWithContentsOfFile:str];
+    self.ammoStoreImage = img;
+    [img release];
+    
+    self.tableView.rowHeight = 75;
 }
-*/
-/*
-- (void)viewDidAppear:(BOOL)animated {
-    [super viewDidAppear:animated];
-}
-*/
-/*
-- (void)viewWillDisappear:(BOOL)animated {
-    [super viewWillDisappear:animated];
+
+-(void) viewWillAppear:(BOOL) animated {
+    [super viewWillAppear:animated];
+    
+    NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.title];
+    NSDictionary *weapon = [[NSDictionary alloc] initWithContentsOfFile:ammoFile];
+    [ammoFile release];
+    
+    const char *tmp1 = [[weapon objectForKey:@"ammostore_initialqt"] UTF8String];
+    const char *tmp2 = [[weapon objectForKey:@"ammostore_probability"] UTF8String];
+    const char *tmp3 = [[weapon objectForKey:@"ammostore_delay"] UTF8String];
+    const char *tmp4 = [[weapon objectForKey:@"ammostore_crate"] UTF8String];
+    [weapon release];
+    
+    // if the new weaponset is diffrent from the older we need to update it replacing
+    // the missing ammos with 0 quantity
+    int oldlen = strlen(tmp1);
+    for (int i = 0; i < oldlen; i++) {
+        quantity[i] = tmp1[i];
+        probability[i] = tmp2[i];
+        delay[i] = tmp3[i];
+        crateness[i] = tmp4[i];
+    }
+    for (int i = oldlen; i < ammoSize; i++) {
+        quantity[i] = '0';
+        probability[i] = '0';
+        delay[i] = '0';
+        crateness[i] = '0';
+    }
+    
+    [self.tableView reloadData];
 }
-*/
-/*
-- (void)viewDidDisappear:(BOOL)animated {
-    [super viewDidDisappear:animated];
+
+-(void) viewWillDisappear:(BOOL) animated {
+    [super viewWillDisappear:animated];
+    
+    NSString *quantityStr = [NSString stringWithUTF8String:quantity];
+    NSString *probabilityStr = [NSString stringWithUTF8String:probability];
+    NSString *delayStr = [NSString stringWithUTF8String:delay];
+    NSString *cratenessStr = [NSString stringWithUTF8String:crateness];
+
+    NSDictionary *weapon = [[NSDictionary alloc] initWithObjectsAndKeys:
+                            [NSNumber numberWithInt:CURRENT_AMMOSIZE],@"version",
+                            quantityStr,@"ammostore_initialqt",
+                            probabilityStr,@"ammostore_probability",
+                            delayStr,@"ammostore_delay",
+                            cratenessStr,@"ammostore_crate", nil];
+
+    NSString *ammoFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",WEAPONS_DIRECTORY(),self.title];
+    [weapon writeToFile:ammoFile atomically:YES];
+    [ammoFile release];
+    [weapon release];
 }
-*/
-/*
-// Override to allow orientations other than the default portrait orientation.
-- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
-    // Return YES for supported orientations
-    return (interfaceOrientation == UIInterfaceOrientationPortrait);
-}
-*/
-
 
 #pragma mark -
 #pragma mark Table view data source
-
-- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
-    // Return the number of sections.
-    return 0;
+-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
+    return 1;
 }
 
 
-- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
-    // Return the number of rows in the section.
-    return 0;
+-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
+    return ammoSize;
 }
 
 
 // Customize the appearance of table view cells.
-- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-    
+-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *CellIdentifier = @"Cell";
+    NSInteger row = [indexPath row];
     
-    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
+    WeaponCellView *cell = (WeaponCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
-        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
+        cell = [[[WeaponCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
     }
     
-    // Configure the cell...
-    
+    int x = ((row*32)/1024)*32;
+    int y = (row*32)%1024;
+
+    UIImage *img = [[self.ammoStoreImage cutAt:CGRectMake(x, y, 32, 32)] makeRoundCornersOfSize:CGSizeMake(7, 7)];
+    cell.weaponIcon.image = img;
+    cell.weaponName.text = [ammoNames objectAtIndex:row];
+
+    cell.initialQt.titleLabel.text = [NSString stringWithFormat:@"%c",quantity[row]];
+    cell.probability.titleLabel.text = [NSString stringWithFormat:@"%c",probability[row]];
+    cell.delay.titleLabel.text = [NSString stringWithFormat:@"%c",delay[row]];
+    cell.initialQt.titleLabel.text = [NSString stringWithFormat:@"%c",crateness[row]];
     return cell;
 }
 
 
-/*
-// Override to support conditional editing of the table view.
-- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
-    // Return NO if you do not want the specified item to be editable.
-    return YES;
-}
-*/
-
-
-/*
-// Override to support editing the table view.
-- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
-    
-    if (editingStyle == UITableViewCellEditingStyleDelete) {
-        // Delete the row from the data source
-        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
-    }   
-    else if (editingStyle == UITableViewCellEditingStyleInsert) {
-        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
-    }   
-}
-*/
-
-
-/*
-// Override to support rearranging the table view.
-- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
-}
-*/
-
-
-/*
-// Override to support conditional rearranging of the table view.
-- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
-    // Return NO if you do not want the item to be re-orderable.
-    return YES;
-}
-*/
-
-
 #pragma mark -
 #pragma mark Table view delegate
-
-- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
+-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     // Navigation logic may go here. Create and push another view controller.
     /*
      <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
@@ -157,21 +194,23 @@
 
 #pragma mark -
 #pragma mark Memory management
-
-- (void)didReceiveMemoryWarning {
+-(void) didReceiveMemoryWarning {
     // Releases the view if it doesn't have a superview.
     [super didReceiveMemoryWarning];
-    
     // Relinquish ownership any cached data, images, etc that aren't in use.
 }
 
-- (void)viewDidUnload {
-    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
-    // For example: self.myOutlet = nil;
+-(void) viewDidUnload {
+    free(quantity);
+    free(probability);
+    free(delay);
+    free(crateness);
+    [super viewDidUnload];
+    MSG_DIDUNLOAD();
 }
 
 
-- (void)dealloc {
+-(void) dealloc {
     [super dealloc];
 }