project_files/HedgewarsMobile/Classes/AmmoMenuViewController.m
changeset 3940 cc29628976cc
parent 3935 5ca27a0e9a63
child 3941 017b2b31e1c6
equal deleted inserted replaced
3939:e6fddcb04dc2 3940:cc29628976cc
    26 #import "PascalImports.h"
    26 #import "PascalImports.h"
    27 
    27 
    28 @implementation AmmoMenuViewController
    28 @implementation AmmoMenuViewController
    29 @synthesize weaponsImage, buttonsArray, isVisible;
    29 @synthesize weaponsImage, buttonsArray, isVisible;
    30 
    30 
    31 
    31 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    32 -(void) viewDidLoad {
    32     return rotationManager(interfaceOrientation);
    33     [super viewDidLoad];
    33 }
    34     
    34 
       
    35 #pragma mark -
       
    36 #pragma mark view handling
       
    37 -(void) viewDidLoad {    
    35     [[NSNotificationCenter defaultCenter] addObserver:self
    38     [[NSNotificationCenter defaultCenter] addObserver:self
    36                                              selector:@selector(updateAmmoVisuals)
    39                                              selector:@selector(updateAmmoVisuals)
    37                                                  name:@"updateAmmoVisuals"
    40                                                  name:@"updateAmmoVisuals"
    38                                                object:nil];
    41                                                object:nil];
    39      
    42      
    43     self.view.layer.borderWidth = 1.3f;
    46     self.view.layer.borderWidth = 1.3f;
    44     [self.view.layer setCornerRadius:10];
    47     [self.view.layer setCornerRadius:10];
    45     [self.view.layer setMasksToBounds:YES];
    48     [self.view.layer setMasksToBounds:YES];
    46 
    49 
    47     self.isVisible = NO;
    50     self.isVisible = NO;
    48     delay = HW_getAmmoDelays();
    51     delay = (uint8_t *) calloc(HW_getNumberOfWeapons(), sizeof(uint8_t));
    49 
    52     HW_getAmmoDelays(delay);
    50     UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    53 
    51     spinner.hidesWhenStopped = YES;
    54     [super viewDidLoad];
    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 }
    55 }
    59 
    56 
    60 -(void) viewWillAppear:(BOOL)animated {
    57 -(void) viewWillAppear:(BOOL)animated {
    61     if (self.buttonsArray != nil)
    58     [self updateAmmoVisuals];
    62         [self updateAmmoVisuals];
       
    63     [super viewWillAppear:animated];
    59     [super viewWillAppear:animated];
    64 }
    60 }
    65 
    61 
    66 -(void) appearInView:(UIView *)container {
    62 -(void) appearInView:(UIView *)container {
    67     [self viewWillAppear:YES];
    63     [self viewWillAppear:YES];
    68     [container addSubview:self.view];
    64     [container addSubview:self.view];
    69     self.view.center = CGPointMake(container.center.y, container.center.x);
    65     self.view.center = CGPointMake(container.center.y, container.center.x);
    70     self.isVisible = YES;
    66     self.isVisible = YES;
    71     [self viewDidAppear:YES];                 
       
    72 }
    67 }
    73 
    68 
    74 -(void) disappear {
    69 -(void) disappear {
    75 
    70     if (self.isVisible)
    76     [self.view removeFromSuperview];
    71         [self.view removeFromSuperview];
    77     self.isVisible = NO;
    72     self.isVisible = NO;
    78 }
    73 }
    79 
    74 
       
    75 #pragma mark -
       
    76 #pragma mark drawing
    80 -(void) loadAmmoStuff:(id) object {
    77 -(void) loadAmmoStuff:(id) object {
    81     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    78     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    82     UIActivityIndicatorView *spinner = (UIActivityIndicatorView *)object;
       
    83 
    79 
    84     NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()];
    80     NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()];
    85     UIImage *ammoStoreImage = [[UIImage alloc] initWithContentsOfFile:str];
    81     UIImage *ammoStoreImage = [[UIImage alloc] initWithContentsOfFile:str];
    86     
    82     [self performSelectorOnMainThread:@selector(setWeaponsImage:) withObject:ammoStoreImage waitUntilDone:NO];
       
    83     [ammoStoreImage release];
       
    84 
    87     NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:HW_getNumberOfWeapons()];
    85     NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:HW_getNumberOfWeapons()];
    88     for (int i = 0; i < HW_getNumberOfWeapons(); i++) {
    86     for (int i = 0; i < HW_getNumberOfWeapons(); i++) {
    89         int x_src = ((i*32)/(int)ammoStoreImage.size.height)*32;
       
    90         int y_src = (i*32)%(int)ammoStoreImage.size.height;
       
    91         int x_dst = 10+(i%10)*44;
    87         int x_dst = 10+(i%10)*44;
    92         int y_dst = 10+(i/10)*44;
    88         int y_dst = 10+(i/10)*44;
    93         
    89         
    94         if (i / 10 % 2 != 0)
    90         if (i / 10 % 2 != 0)
    95             x_dst += 20;
    91             x_dst += 20;
    96         UIImage *img = [ammoStoreImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
       
    97         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    92         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    98         button.frame = CGRectMake(x_dst, y_dst, 40, 40);
    93         button.frame = CGRectMake(x_dst, y_dst, 40, 40);
    99         button.tag = i;
    94         button.tag = i;
   100         button.layer.borderWidth = 1;
    95         button.layer.borderWidth = 1;
   101         button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
    96         button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
   102         [button.layer setCornerRadius:6];
    97         [button.layer setCornerRadius:6];
   103         [button.layer setMasksToBounds:YES];
    98         [button.layer setMasksToBounds:YES];
   104         [button setBackgroundImage:img forState:UIControlStateNormal];
       
   105         [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    99         [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
   106         [button setTitleColor:UICOLOR_HW_YELLOW_TEXT forState:UIControlStateNormal];
   100         [button setTitleColor:UICOLOR_HW_YELLOW_TEXT forState:UIControlStateNormal];
   107         button.titleLabel.backgroundColor = [UIColor blackColor];
   101         button.titleLabel.backgroundColor = [UIColor blackColor];
   108         button.titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
   102         button.titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
   109         [button.titleLabel.layer setCornerRadius:3];
   103         [button.titleLabel.layer setCornerRadius:3];
   111         button.titleLabel.layer.borderColor = [[UIColor whiteColor] CGColor];
   105         button.titleLabel.layer.borderColor = [[UIColor whiteColor] CGColor];
   112         button.titleLabel.layer.borderWidth = 1;
   106         button.titleLabel.layer.borderWidth = 1;
   113         [self.view addSubview:button];
   107         [self.view addSubview:button];
   114         [array addObject:button];
   108         [array addObject:button];
   115     }
   109     }
   116     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:array,@"array",ammoStoreImage,@"image",spinner,@"spinner",nil];
   110     [self performSelectorOnMainThread:@selector(setButtonsArray:) withObject:array waitUntilDone:NO];
   117     [array release];
   111     [array release];
   118     [ammoStoreImage release];
       
   119 
       
   120     [self performSelectorOnMainThread:@selector(ready:) withObject:dict waitUntilDone:NO];
       
   121     
   112     
       
   113     [self performSelectorOnMainThread:@selector(updateAmmoVisuals) withObject:nil waitUntilDone:NO];
       
   114     UIActivityIndicatorView *spinner = (UIActivityIndicatorView *)object;
       
   115     [spinner stopAnimating];
   122     [pool drain];
   116     [pool drain];
   123 }
   117 }
   124 
   118 
   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 updateAmmoVisuals];
       
   131 }
       
   132 
       
   133 -(void) updateAmmoVisuals {
   119 -(void) updateAmmoVisuals {
   134     unsigned char *loadout = HW_getAmmoCounts();
   120     if (self.buttonsArray == nil || self.weaponsImage == nil) {
   135     int turns = HW_getTurnsForCurrentTeam();
       
   136 
       
   137     if (self.buttonsArray == nil) {
       
   138         UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
   121         UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
   139         spinner.hidesWhenStopped = YES;
   122         spinner.hidesWhenStopped = YES;
   140         spinner.center = self.view.center;
   123         spinner.center = self.view.center;
   141         [spinner startAnimating];
   124         [spinner startAnimating];
   142         [self.view addSubview:spinner];
   125         [self.view addSubview:spinner];
   143         [NSThread detachNewThreadSelector:@selector(loadAmmoStuff:) toTarget:self withObject:spinner];
   126         [NSThread detachNewThreadSelector:@selector(loadAmmoStuff:) toTarget:self withObject:spinner];
   144         [spinner release];
   127         [spinner release];
       
   128         return;
   145     }
   129     }
   146     
   130     
   147     if (loadout == NULL) {
   131     [NSThread detachNewThreadSelector:@selector(drawingThread) toTarget:self withObject:nil];
       
   132 }
       
   133 
       
   134 -(void) drawingThread {
       
   135     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
   136     int *loadout = (int *)calloc(HW_getNumberOfWeapons(), sizeof(int));
       
   137     int res = HW_getAmmoCounts(loadout);
       
   138     int turns = HW_getTurnsForCurrentTeam();
       
   139     
       
   140     if (res == 0) {
       
   141         self.view.userInteractionEnabled = YES;
       
   142         
       
   143         for (int i = 0; i < HW_getNumberOfWeapons(); i++) {
       
   144             UIButton *button = [self.buttonsArray objectAtIndex:i];
       
   145             if (loadout[i] > 0) {
       
   146                 if (delay[i]-turns >= 0) {
       
   147                     button.layer.borderColor = [[UIColor lightGrayColor] CGColor];
       
   148                     [button setTitle:[NSString stringWithFormat:@" %d ",delay[i]-turns+1] forState:UIControlStateNormal];
       
   149                     if (button.currentBackgroundImage == nil) {
       
   150                         int x_src = ((i*32)/(int)self.weaponsImage.size.height)*32;
       
   151                         int y_src = (i*32)%(int)self.weaponsImage.size.height;
       
   152                         UIImage *img = [self.weaponsImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
       
   153                         [button setBackgroundImage:[img convertToGrayScale] forState:UIControlStateNormal];
       
   154                         button.imageView.tag = 10000;
       
   155                     }
       
   156                 } else {
       
   157                     button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
       
   158                     [button setTitle:@"" forState:UIControlStateNormal];
       
   159                     if (button.currentBackgroundImage == nil || button.imageView.tag == 10000) {
       
   160                         int x_src = ((i*32)/(int)self.weaponsImage.size.height)*32;
       
   161                         int y_src = (i*32)%(int)self.weaponsImage.size.height;
       
   162                         UIImage *img = [self.weaponsImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
       
   163                         [button setBackgroundImage:img forState:UIControlStateNormal];
       
   164                         button.imageView.tag = 0;
       
   165                     }
       
   166                 }
       
   167                 button.enabled = YES;
       
   168             } else {
       
   169                 if (button.enabled == YES)
       
   170                     [button setBackgroundImage:nil forState:UIControlStateNormal];
       
   171                 button.layer.borderColor = [[UIColor darkGrayColor] CGColor];
       
   172                 button.enabled = NO;
       
   173             }
       
   174             
       
   175         }
       
   176     } else {
   148         self.view.userInteractionEnabled = NO;
   177         self.view.userInteractionEnabled = NO;
   149         return;
   178     }
   150     } else
   179 
   151         self.view.userInteractionEnabled = YES;
   180     free(loadout);
   152 
   181     loadout = NULL;
   153     for (int i = 0; i < HW_getNumberOfWeapons(); i++) {
   182     [pool drain];
   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                 [button setBackgroundImage:nil forState:UIControlStateNormal];
       
   167             button.enabled = NO;
       
   168             button.layer.borderColor = [[UIColor darkGrayColor] CGColor];
       
   169         }
       
   170         
       
   171         if (button.enabled == YES) {
       
   172             if (delay[i]-turns >= 0) {
       
   173                 button.layer.borderColor = [[UIColor lightGrayColor] CGColor];
       
   174                 [button setTitle:[NSString stringWithFormat:@" %d ",delay[i]-turns+1] forState:UIControlStateNormal];
       
   175                 if (button.enabled == YES) {
       
   176                     int x_src = ((i*32)/(int)self.weaponsImage.size.height)*32;
       
   177                     int y_src = (i*32)%(int)self.weaponsImage.size.height;
       
   178                     UIImage *img = [self.weaponsImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
       
   179                     [button setBackgroundImage:[img convertToGrayScale] forState:UIControlStateNormal];
       
   180                 }
       
   181             } else {
       
   182                 button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
       
   183                 [button setTitle:@"" forState:UIControlStateNormal];
       
   184                 if (button.enabled == YES) {
       
   185                     int x_src = ((i*32)/(int)self.weaponsImage.size.height)*32;
       
   186                     int y_src = (i*32)%(int)self.weaponsImage.size.height;
       
   187                     UIImage *img = [self.weaponsImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
       
   188                     [button setBackgroundImage:img forState:UIControlStateNormal];
       
   189                 }
       
   190             }
       
   191         }
       
   192     }
       
   193 }
   183 }
   194 
   184 
   195 #pragma mark -
   185 #pragma mark -
   196 #pragma mark user interaction
   186 #pragma mark user interaction
   197 -(void) buttonPressed:(id) sender {
   187 -(void) buttonPressed:(id) sender {
   247 
   237 
   248 -(void) viewDidUnload {
   238 -(void) viewDidUnload {
   249     [[NSNotificationCenter defaultCenter] removeObserver:self];
   239     [[NSNotificationCenter defaultCenter] removeObserver:self];
   250     self.weaponsImage = nil;
   240     self.weaponsImage = nil;
   251     self.buttonsArray = nil;
   241     self.buttonsArray = nil;
       
   242     free(delay);
   252     delay = NULL;
   243     delay = NULL;
   253     MSG_DIDUNLOAD();
   244     MSG_DIDUNLOAD();
   254     [super viewDidUnload];
   245     [super viewDidUnload];
   255 }
   246 }
   256 
   247