project_files/HedgewarsMobile/Classes/AmmoMenuViewController.m
author koda
Sat, 09 Oct 2010 18:00:53 +0200
changeset 3935 5ca27a0e9a63
parent 3933 1a873262f5dd
child 3940 cc29628976cc
permissions -rw-r--r--
made the new menu toggable
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
     1
/*
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
     3
 * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com>
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
     4
 *
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
     8
 *
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    12
 * GNU General Public License for more details.
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    13
 *
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    17
 *
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    18
 * File created on 03/10/2010.
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    19
 */
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    20
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    21
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    22
#import "AmmoMenuViewController.h"
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    23
#import <QuartzCore/QuartzCore.h>
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    24
#import "CommodityFunctions.h"
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    25
#import "UIImageExtra.h"
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    26
#import "PascalImports.h"
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    27
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    28
@implementation AmmoMenuViewController
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    29
@synthesize weaponsImage, buttonsArray, isVisible;
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    30
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    31
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    32
-(void) viewDidLoad {
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    33
    [super viewDidLoad];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    34
    
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    35
    [[NSNotificationCenter defaultCenter] addObserver:self
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
    36
                                             selector:@selector(updateAmmoVisuals)
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    37
                                                 name:@"updateAmmoVisuals"
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    38
                                               object:nil];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    39
     
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    40
    self.view.frame = CGRectMake(0, 0, 480, 320);
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    41
    self.view.backgroundColor = [UIColor blackColor];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    42
    self.view.layer.borderColor = [[UIColor whiteColor] CGColor];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    43
    self.view.layer.borderWidth = 1.3f;
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    44
    [self.view.layer setCornerRadius:10];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    45
    [self.view.layer setMasksToBounds:YES];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    46
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    47
    self.isVisible = NO;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    48
    delay = HW_getAmmoDelays();
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    49
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    50
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    51
    spinner.hidesWhenStopped = YES;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    52
    spinner.center = self.view.center;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    53
    [spinner startAnimating];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    54
    [self.view addSubview:spinner];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    55
    if (self.buttonsArray == nil)
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    56
        [NSThread detachNewThreadSelector:@selector(loadAmmoStuff:) toTarget:self withObject:spinner];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    57
    [spinner release];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    58
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    59
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    60
-(void) viewWillAppear:(BOOL)animated {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    61
    if (self.buttonsArray != nil)
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
    62
        [self updateAmmoVisuals];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    63
    [super viewWillAppear:animated];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    64
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    65
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    66
-(void) appearInView:(UIView *)container {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    67
    [self viewWillAppear:YES];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    68
    [container addSubview:self.view];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    69
    self.view.center = CGPointMake(container.center.y, container.center.x);
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    70
    self.isVisible = YES;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    71
    [self viewDidAppear:YES];                 
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    72
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    73
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    74
-(void) disappear {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    75
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    76
    [self.view removeFromSuperview];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    77
    self.isVisible = NO;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    78
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    79
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    80
-(void) loadAmmoStuff:(id) object {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    81
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    82
    UIActivityIndicatorView *spinner = (UIActivityIndicatorView *)object;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    83
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    84
    NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    85
    UIImage *ammoStoreImage = [[UIImage alloc] initWithContentsOfFile:str];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    86
    
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    87
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:HW_getNumberOfWeapons()];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    88
    for (int i = 0; i < HW_getNumberOfWeapons(); i++) {
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    89
        int x_src = ((i*32)/(int)ammoStoreImage.size.height)*32;
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    90
        int y_src = (i*32)%(int)ammoStoreImage.size.height;
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    91
        int x_dst = 10+(i%10)*44;
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    92
        int y_dst = 10+(i/10)*44;
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    93
        
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    94
        if (i / 10 % 2 != 0)
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    95
            x_dst += 20;
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    96
        UIImage *img = [ammoStoreImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    97
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    98
        button.frame = CGRectMake(x_dst, y_dst, 40, 40);
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    99
        button.tag = i;
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   100
        button.layer.borderWidth = 1;
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   101
        button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   102
        [button.layer setCornerRadius:6];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   103
        [button.layer setMasksToBounds:YES];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   104
        [button setBackgroundImage:img forState:UIControlStateNormal];
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   105
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   106
        [button setTitleColor:UICOLOR_HW_YELLOW_TEXT forState:UIControlStateNormal];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   107
        button.titleLabel.backgroundColor = [UIColor blackColor];
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   108
        button.titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   109
        [button.titleLabel.layer setCornerRadius:3];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   110
        [button.titleLabel.layer setMasksToBounds:YES];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   111
        button.titleLabel.layer.borderColor = [[UIColor whiteColor] CGColor];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   112
        button.titleLabel.layer.borderWidth = 1;
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   113
        [self.view addSubview:button];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   114
        [array addObject:button];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   115
    }
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   116
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:array,@"array",ammoStoreImage,@"image",spinner,@"spinner",nil];
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   117
    [array release];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   118
    [ammoStoreImage release];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   119
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   120
    [self performSelectorOnMainThread:@selector(ready:) withObject:dict waitUntilDone:NO];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   121
    
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   122
    [pool drain];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   123
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   124
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   125
-(void) ready:(id) object {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   126
    NSDictionary *dict = (NSDictionary *)object;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   127
    [[dict objectForKey:@"spinner"] stopAnimating];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   128
    self.weaponsImage = [dict objectForKey:@"image"];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   129
    self.buttonsArray = [dict objectForKey:@"array"];
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   130
    [self updateAmmoVisuals];
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   131
}
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   132
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   133
-(void) updateAmmoVisuals {
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   134
    unsigned char *loadout = HW_getAmmoCounts();
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   135
    int turns = HW_getTurnsForCurrentTeam();
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   136
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   137
    if (self.buttonsArray == nil) {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   138
        UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   139
        spinner.hidesWhenStopped = YES;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   140
        spinner.center = self.view.center;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   141
        [spinner startAnimating];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   142
        [self.view addSubview:spinner];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   143
        [NSThread detachNewThreadSelector:@selector(loadAmmoStuff:) toTarget:self withObject:spinner];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   144
        [spinner release];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   145
    }
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   146
    
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   147
    if (loadout == NULL) {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   148
        self.view.userInteractionEnabled = NO;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   149
        return;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   150
    } else
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   151
        self.view.userInteractionEnabled = YES;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   152
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   153
    for (int i = 0; i < HW_getNumberOfWeapons(); i++) {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   154
        UIButton *button = [self.buttonsArray objectAtIndex:i];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   155
        if (loadout[i] > 0) {
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   156
            if (button.enabled == NO) {
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   157
                int x_src = ((i*32)/(int)self.weaponsImage.size.height)*32;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   158
                int y_src = (i*32)%(int)self.weaponsImage.size.height;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   159
                UIImage *img = [self.weaponsImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   160
                [button setBackgroundImage:img forState:UIControlStateNormal];
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   161
            }
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   162
            button.enabled = YES;
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   163
            button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   164
        } else {
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   165
            if (button.enabled == YES)
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   166
                [button setBackgroundImage:nil forState:UIControlStateNormal];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   167
            button.enabled = NO;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   168
            button.layer.borderColor = [[UIColor darkGrayColor] CGColor];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   169
        }
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   170
        
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   171
        if (button.enabled == YES) {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   172
            if (delay[i]-turns >= 0) {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   173
                button.layer.borderColor = [[UIColor lightGrayColor] CGColor];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   174
                [button setTitle:[NSString stringWithFormat:@" %d ",delay[i]-turns+1] forState:UIControlStateNormal];
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   175
                if (button.enabled == YES) {
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   176
                    int x_src = ((i*32)/(int)self.weaponsImage.size.height)*32;
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   177
                    int y_src = (i*32)%(int)self.weaponsImage.size.height;
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   178
                    UIImage *img = [self.weaponsImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   179
                    [button setBackgroundImage:[img convertToGrayScale] forState:UIControlStateNormal];
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   180
                }
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   181
            } else {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   182
                button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   183
                [button setTitle:@"" forState:UIControlStateNormal];
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   184
                if (button.enabled == YES) {
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   185
                    int x_src = ((i*32)/(int)self.weaponsImage.size.height)*32;
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   186
                    int y_src = (i*32)%(int)self.weaponsImage.size.height;
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   187
                    UIImage *img = [self.weaponsImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   188
                    [button setBackgroundImage:img forState:UIControlStateNormal];
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   189
                }
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   190
            }
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   191
        }
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   192
    }
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   193
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   194
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   195
#pragma mark -
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   196
#pragma mark user interaction
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   197
-(void) buttonPressed:(id) sender {
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   198
    UIButton *theButton = (UIButton *)sender;
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   199
    HW_setWeapon(theButton.tag);
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   200
    playSound(@"clickSound");
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   201
    [self disappear];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   202
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   203
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   204
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   205
    /*
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   206
    NSSet *allTouches = [event allTouches];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   207
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   208
    if ([touches count] == 1) {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   209
        self.view.layer.borderWidth = 3.5;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   210
        startingPoint = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   211
    }
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   212
    */
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   213
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   214
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   215
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   216
    //self.view.layer.borderWidth = 1.3;
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   217
}
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   218
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   219
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   220
    /*
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   221
    NSSet *allTouches = [event allTouches];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   222
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   223
    if ([touches count] == 1) {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   224
        CGPoint touchedPoint = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   225
        CGFloat deltaX = touchedPoint.x - startingPoint.x;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   226
        CGFloat deltaY = touchedPoint.y - startingPoint.y;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   227
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   228
        //startingPoint = touchedPoint;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   229
        self.view.frame = CGRectMake(self.view.frame.origin.x + deltaX, self.view.frame.origin.y + deltaY,
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   230
                                     self.view.frame.size.width, self.view.frame.size.height);
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   231
    }
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   232
    */
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   233
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   234
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   235
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   236
    //[self touchesEnded:touches withEvent:event];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   237
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   238
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   239
#pragma mark -
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   240
#pragma mark memory
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   241
-(void) didReceiveMemoryWarning {
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   242
    self.weaponsImage = nil;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   243
    self.buttonsArray = nil;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   244
    MSG_MEMCLEAN();
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   245
    [super didReceiveMemoryWarning];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   246
}
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   247
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   248
-(void) viewDidUnload {
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   249
    [[NSNotificationCenter defaultCenter] removeObserver:self];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   250
    self.weaponsImage = nil;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   251
    self.buttonsArray = nil;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   252
    delay = NULL;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   253
    MSG_DIDUNLOAD();
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   254
    [super viewDidUnload];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   255
}
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   256
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   257
-(void) dealloc {
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   258
    [weaponsImage release];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   259
    [buttonsArray release];
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   260
    [super dealloc];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   261
}
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   262
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   263
@end
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   264
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   265
void updateVisualsNewTurn (void) {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   266
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateAmmoVisuals" object:nil];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   267
}