project_files/HedgewarsMobile/Classes/AmmoMenuViewController.m
changeset 3933 1a873262f5dd
parent 3924 2a9ace189288
child 3935 5ca27a0e9a63
equal deleted inserted replaced
3930:8b00b4f93242 3933:1a873262f5dd
    24 #import "CommodityFunctions.h"
    24 #import "CommodityFunctions.h"
    25 #import "UIImageExtra.h"
    25 #import "UIImageExtra.h"
    26 #import "PascalImports.h"
    26 #import "PascalImports.h"
    27 
    27 
    28 @implementation AmmoMenuViewController
    28 @implementation AmmoMenuViewController
    29 @synthesize imagesArray;;
    29 @synthesize weaponsImage, buttonsArray, isVisible;
    30 
    30 
    31 
    31 
    32 -(void) viewDidLoad {
    32 -(void) viewDidLoad {
    33     [super viewDidLoad];
    33     [super viewDidLoad];
       
    34     
       
    35     [[NSNotificationCenter defaultCenter] addObserver:self
       
    36                                              selector:@selector(updateVisuals:)
       
    37                                                  name:@"updateAmmoVisuals"
       
    38                                                object:nil];
       
    39      
    34     self.view.frame = CGRectMake(0, 0, 480, 320);
    40     self.view.frame = CGRectMake(0, 0, 480, 320);
    35     self.view.backgroundColor = [UIColor blackColor];
    41     self.view.backgroundColor = [UIColor blackColor];
       
    42     self.view.layer.borderColor = [[UIColor whiteColor] CGColor];
       
    43     self.view.layer.borderWidth = 1.3f;
    36     [self.view.layer setCornerRadius:10];
    44     [self.view.layer setCornerRadius:10];
    37     [self.view.layer setMasksToBounds:YES];
    45     [self.view.layer setMasksToBounds:YES];
    38 
    46 
       
    47     self.isVisible = NO;
       
    48     delay = HW_getAmmoDelays();
       
    49 
       
    50     UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
       
    51     spinner.hidesWhenStopped = YES;
       
    52     spinner.center = self.view.center;
       
    53     [spinner startAnimating];
       
    54     [self.view addSubview:spinner];
       
    55     if (self.buttonsArray == nil)
       
    56         [NSThread detachNewThreadSelector:@selector(loadAmmoStuff:) toTarget:self withObject:spinner];
       
    57     [spinner release];
       
    58 }
       
    59 
       
    60 -(void) viewWillAppear:(BOOL)animated {
       
    61     if (self.buttonsArray != nil)
       
    62         [self updateVisuals:nil];
       
    63     [super viewWillAppear:animated];
       
    64 }
       
    65 
       
    66 -(void) appearInView:(UIView *)container {
       
    67     [self viewWillAppear:YES];
       
    68     [container addSubview:self.view];
       
    69     self.view.center = CGPointMake(container.center.y, container.center.x);
       
    70     self.isVisible = YES;
       
    71     [self viewDidAppear:YES];                 
       
    72 }
       
    73 
       
    74 -(void) disappear {
       
    75 
       
    76     [self.view removeFromSuperview];
       
    77     self.isVisible = NO;
       
    78 }
       
    79 
       
    80 -(void) loadAmmoStuff:(id) object {
       
    81     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
    82     UIActivityIndicatorView *spinner = (UIActivityIndicatorView *)object;
       
    83 
    39     NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()];
    84     NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()];
    40     UIImage *ammoStoreImage = [[UIImage alloc] initWithContentsOfFile:str];
    85     UIImage *ammoStoreImage = [[UIImage alloc] initWithContentsOfFile:str];
    41     
    86     
    42     NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:CURRENT_AMMOSIZE];
    87     NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:HW_getNumberOfWeapons()];
    43     for (int i = 0; i < CURRENT_AMMOSIZE; i++) {
    88     for (int i = 0; i < HW_getNumberOfWeapons(); i++) {
    44         int x_src = ((i*32)/(int)ammoStoreImage.size.height)*32;
    89         int x_src = ((i*32)/(int)ammoStoreImage.size.height)*32;
    45         int y_src = (i*32)%(int)ammoStoreImage.size.height;
    90         int y_src = (i*32)%(int)ammoStoreImage.size.height;
    46         int x_dst = 10+(i%10)*44;
    91         int x_dst = 10+(i%10)*44;
    47         int y_dst = 10+(i/10)*44;
    92         int y_dst = 10+(i/10)*44;
    48         
    93         
    49         if (i / 10 % 2 != 0)
    94         if (i / 10 % 2 != 0)
    50             x_dst += 20;
    95             x_dst += 20;
    51         UIImage *img = [ammoStoreImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
    96         UIImage *img = [ammoStoreImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
    52         UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x_dst, y_dst, 40, 40)];
    97         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    53         button.tag = i+1;
    98         button.frame = CGRectMake(x_dst, y_dst, 40, 40);
       
    99         button.tag = i;
    54         button.layer.borderWidth = 1;
   100         button.layer.borderWidth = 1;
    55         button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
   101         button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
    56         [button.layer setCornerRadius:6];
   102         [button.layer setCornerRadius:6];
    57         [button.layer setMasksToBounds:YES];
   103         [button.layer setMasksToBounds:YES];
    58         [button setImage:img forState:UIControlStateNormal];
   104         [button setBackgroundImage:img forState:UIControlStateNormal];
    59         [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
   105         [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
       
   106         [button setTitleColor:UICOLOR_HW_YELLOW_TEXT forState:UIControlStateNormal];
       
   107         button.titleLabel.backgroundColor = [UIColor blackColor];
       
   108         button.titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
       
   109         [button.titleLabel.layer setCornerRadius:3];
       
   110         [button.titleLabel.layer setMasksToBounds:YES];
       
   111         button.titleLabel.layer.borderColor = [[UIColor whiteColor] CGColor];
       
   112         button.titleLabel.layer.borderWidth = 1;
    60         [self.view addSubview:button];
   113         [self.view addSubview:button];
    61         [array addObject:button];
   114         [array addObject:button];
    62         [button release];
   115     }
    63     }
   116     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:array,@"array",ammoStoreImage,@"image",spinner,@"spinner",nil];
    64     self.imagesArray = array;
       
    65     [array release];
   117     [array release];
    66     [ammoStoreImage release];
   118     [ammoStoreImage release];
    67 
   119 
    68 }
   120     [self performSelectorOnMainThread:@selector(ready:) withObject:dict waitUntilDone:NO];
    69 
   121     
       
   122     [pool drain];
       
   123 }
       
   124 
       
   125 -(void) ready:(id) object {
       
   126     NSDictionary *dict = (NSDictionary *)object;
       
   127     [[dict objectForKey:@"spinner"] stopAnimating];
       
   128     self.weaponsImage = [dict objectForKey:@"image"];
       
   129     self.buttonsArray = [dict objectForKey:@"array"];
       
   130     [self updateVisuals:nil];
       
   131 }
       
   132 
       
   133 -(void) updateVisuals:(NSNotification *) object {
       
   134     unsigned char *loadout = HW_getAmmoCounts();
       
   135     int turns = HW_getTurnsForCurrentTeam();
       
   136 
       
   137     if (self.buttonsArray == nil) {
       
   138         UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
       
   139         spinner.hidesWhenStopped = YES;
       
   140         spinner.center = self.view.center;
       
   141         [spinner startAnimating];
       
   142         [self.view addSubview:spinner];
       
   143         [NSThread detachNewThreadSelector:@selector(loadAmmoStuff:) toTarget:self withObject:spinner];
       
   144         [spinner release];
       
   145     }
       
   146     
       
   147     if (loadout == NULL) {
       
   148         self.view.userInteractionEnabled = NO;
       
   149         return;
       
   150     } else
       
   151         self.view.userInteractionEnabled = YES;
       
   152 
       
   153     for (int i = 0; i < HW_getNumberOfWeapons(); i++) {
       
   154         UIButton *button = [self.buttonsArray objectAtIndex:i];
       
   155         if (loadout[i] > 0) {
       
   156             /*if (button.enabled == NO) {
       
   157                 int x_src = ((i*32)/(int)self.weaponsImage.size.height)*32;
       
   158                 int y_src = (i*32)%(int)self.weaponsImage.size.height;
       
   159                 UIImage *img = [self.weaponsImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
       
   160                 [button setBackgroundImage:img forState:UIControlStateNormal];
       
   161             }*/
       
   162             button.enabled = YES;
       
   163             button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
       
   164         } else {
       
   165             /*if (button.enabled == YES) {
       
   166                 int x_src = ((i*32)/(int)self.weaponsImage.size.height)*32;
       
   167                 int y_src = (i*32)%(int)self.weaponsImage.size.height;
       
   168                 UIImage *img = [self.weaponsImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
       
   169                 [button setBackgroundImage:img forState:UIControlStateNormal];
       
   170             }*/
       
   171             button.enabled = NO;
       
   172             button.layer.borderColor = [[UIColor darkGrayColor] CGColor];
       
   173             //NSLog(@"disabled: %d",button.tag);
       
   174         }
       
   175         
       
   176         if (button.enabled == YES) {
       
   177             if (delay[i]-turns >= 0) {
       
   178             //    NSLog(@"delayed(%d) %d",delay[i], button.tag);
       
   179                 button.layer.borderColor = [[UIColor lightGrayColor] CGColor];
       
   180                 [button setTitle:[NSString stringWithFormat:@" %d ",delay[i]-turns+1] forState:UIControlStateNormal];
       
   181             } else {
       
   182              //   NSLog(@"enabled %d",button.tag);
       
   183                 button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
       
   184                 [button setTitle:@"" forState:UIControlStateNormal];
       
   185             }
       
   186         }
       
   187     }
       
   188 }
       
   189 
       
   190 #pragma mark -
       
   191 #pragma mark user interaction
    70 -(void) buttonPressed:(id) sender {
   192 -(void) buttonPressed:(id) sender {
    71     UIButton *theButton = (UIButton *)sender;
   193     UIButton *theButton = (UIButton *)sender;
    72     HW_setWeapon(theButton.tag);
   194     HW_setWeapon(theButton.tag);
    73 }
   195     playSound(@"clickSound");
    74 
   196     [self disappear];
       
   197 }
       
   198 
       
   199 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
       
   200     /*
       
   201     NSSet *allTouches = [event allTouches];
       
   202 
       
   203     if ([touches count] == 1) {
       
   204         self.view.layer.borderWidth = 3.5;
       
   205         startingPoint = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view];
       
   206     }
       
   207     */
       
   208 }
       
   209 
       
   210 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
       
   211     //self.view.layer.borderWidth = 1.3;
       
   212 }
       
   213 
       
   214 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
       
   215     /*
       
   216     NSSet *allTouches = [event allTouches];
       
   217 
       
   218     if ([touches count] == 1) {
       
   219         CGPoint touchedPoint = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view];
       
   220         CGFloat deltaX = touchedPoint.x - startingPoint.x;
       
   221         CGFloat deltaY = touchedPoint.y - startingPoint.y;
       
   222 
       
   223         //startingPoint = touchedPoint;
       
   224         self.view.frame = CGRectMake(self.view.frame.origin.x + deltaX, self.view.frame.origin.y + deltaY,
       
   225                                      self.view.frame.size.width, self.view.frame.size.height);
       
   226     }
       
   227     */
       
   228 }
       
   229 
       
   230 -(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
       
   231     //[self touchesEnded:touches withEvent:event];
       
   232 }
       
   233 
       
   234 #pragma mark -
       
   235 #pragma mark memory
    75 -(void) didReceiveMemoryWarning {
   236 -(void) didReceiveMemoryWarning {
    76     // Releases the view if it doesn't have a superview.
   237     self.weaponsImage = nil;
       
   238     self.buttonsArray = nil;
       
   239     MSG_MEMCLEAN();
    77     [super didReceiveMemoryWarning];
   240     [super didReceiveMemoryWarning];
    78     // Release any cached data, images, etc that aren't in use.
       
    79 }
   241 }
    80 
   242 
    81 -(void) viewDidUnload {
   243 -(void) viewDidUnload {
       
   244     [[NSNotificationCenter defaultCenter] removeObserver:self];
       
   245     self.weaponsImage = nil;
       
   246     self.buttonsArray = nil;
       
   247     delay = NULL;
       
   248     MSG_DIDUNLOAD();
    82     [super viewDidUnload];
   249     [super viewDidUnload];
    83     self.imagesArray = nil;
       
    84 }
   250 }
    85 
   251 
    86 -(void) dealloc {
   252 -(void) dealloc {
    87     [imagesArray release];
   253     [weaponsImage release];
       
   254     [buttonsArray release];
    88     [super dealloc];
   255     [super dealloc];
    89 }
   256 }
    90 
   257 
    91 
       
    92 @end
   258 @end
       
   259 
       
   260 void updateVisualsNewTurn (void) {
       
   261     [[NSNotificationCenter defaultCenter] postNotificationName:@"updateAmmoVisuals" object:nil];
       
   262 }