project_files/HedgewarsMobile/Classes/AmmoMenuViewController.m
author koda
Fri, 15 Oct 2010 01:10:59 +0200
changeset 3977 9df7b4812da9
parent 3976 abaf741a4e21
child 3978 9660600e43cb
permissions -rw-r--r--
optimizations to new ammomenu and overlay
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
3952
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    28
#define BTNS_PER_ROW 9
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    29
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    30
@implementation AmmoMenuViewController
3977
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
    31
@synthesize imagesArray, buttonsArray, isVisible;
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    32
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    33
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    34
    return rotationManager(interfaceOrientation);
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    35
}
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    36
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    37
#pragma mark -
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    38
#pragma mark view handling
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    39
-(void) viewDidLoad {    
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    40
    [[NSNotificationCenter defaultCenter] addObserver:self
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
    41
                                             selector:@selector(updateAmmoVisuals)
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    42
                                                 name:@"updateAmmoVisuals"
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    43
                                               object:nil];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    44
     
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    45
    self.view.frame = CGRectMake(0, 0, 480, 320);
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    46
    self.view.backgroundColor = [UIColor blackColor];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    47
    self.view.layer.borderColor = [[UIColor whiteColor] CGColor];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    48
    self.view.layer.borderWidth = 1.3f;
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    49
    [self.view.layer setCornerRadius:10];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    50
    [self.view.layer setMasksToBounds:YES];
3952
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    51
    self.view.autoresizingMask = UIViewAutoresizingNone;
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    52
    
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    53
    self.isVisible = NO;
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    54
    delay = (uint8_t *) calloc(HW_getNumberOfWeapons(), sizeof(uint8_t));
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    55
    HW_getAmmoDelays(delay);
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    56
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    57
    [super viewDidLoad];
3933
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 {
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    61
    [self updateAmmoVisuals];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    62
    [super viewWillAppear:animated];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    63
}
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
-(void) appearInView:(UIView *)container {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    66
    [self viewWillAppear:YES];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    67
    [container addSubview:self.view];
3952
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    68
    if (IS_DUALHEAD() == NO)
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    69
        self.view.center = CGPointMake(container.center.y, container.center.x);
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    70
    else {
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    71
        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    72
        if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    73
            self.view.center = CGPointMake(container.center.y, container.center.x);
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    74
        else
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    75
            self.view.center = CGPointMake(container.center.x, container.center.y);
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    76
    }
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    77
    self.isVisible = YES;
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) disappear {
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    81
    if (self.isVisible)
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    82
        [self.view removeFromSuperview];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    83
    self.isVisible = NO;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    84
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    85
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    86
#pragma mark -
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    87
#pragma mark drawing
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    88
-(void) loadAmmoStuff:(id) object {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    89
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    90
    NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    91
    UIImage *ammoStoreImage = [[UIImage alloc] initWithContentsOfFile:str];
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    92
3977
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
    93
    NSMutableArray *imgs = [[NSMutableArray alloc] initWithCapacity:HW_getNumberOfWeapons()];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    94
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:HW_getNumberOfWeapons()];
3952
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    95
    int i, j, e;
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    96
    for (i = 0, j = 0, e = 0; i < HW_getNumberOfWeapons(); i++) {
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    97
        int x, y;
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
    98
        float w, radius;
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    99
        
3952
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   100
        // move utilities aside and make 'em rounded
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   101
        if (HW_isWeaponAnEffect(i)) {
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   102
            x = 432;
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   103
            y = 20 + 48*e++;
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   104
            w = 1.5;
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   105
            radius = 22;
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   106
        } else {
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   107
            x = 10+(j%BTNS_PER_ROW)*44;
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   108
            y = 10+(j/BTNS_PER_ROW)*44;
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   109
            if (j / BTNS_PER_ROW % 2 != 0)
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   110
                x += 20;
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   111
            w = 1;
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   112
            radius = 6;
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   113
            j++;
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   114
        }
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   115
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   116
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
3952
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   117
        button.frame = CGRectMake(x, y, 40, 40);
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   118
        button.tag = i;
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   119
        button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
3952
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   120
        button.layer.borderWidth = w;
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   121
        [button.layer setCornerRadius:radius];
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   122
        [button.layer setMasksToBounds:YES];
3976
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3952
diff changeset
   123
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3952
diff changeset
   124
        [button addTarget:self action:@selector(buttonReleased:) forControlEvents:UIControlEventTouchUpInside];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   125
        [button setTitleColor:UICOLOR_HW_YELLOW_TEXT forState:UIControlStateNormal];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   126
        button.titleLabel.backgroundColor = [UIColor blackColor];
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   127
        button.titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   128
        [button.titleLabel.layer setCornerRadius:3];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   129
        [button.titleLabel.layer setMasksToBounds:YES];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   130
        button.titleLabel.layer.borderColor = [[UIColor whiteColor] CGColor];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   131
        button.titleLabel.layer.borderWidth = 1;
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   132
        [self.view addSubview:button];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   133
        [array addObject:button];
3977
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   134
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   135
        int x_src = ((i*32)/(int)ammoStoreImage.size.height)*32;
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   136
        int y_src = (i*32)%(int)ammoStoreImage.size.height;
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   137
        UIImage *img = [ammoStoreImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   138
        [imgs addObject:img];
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   139
    }
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   140
    [self performSelectorOnMainThread:@selector(setButtonsArray:) withObject:array waitUntilDone:NO];
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   141
    [array release];
3977
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   142
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   143
    [self performSelectorOnMainThread:@selector(setImagesArray:) withObject:imgs waitUntilDone:NO];
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   144
    [imgs release];
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   145
    [ammoStoreImage release];
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   146
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   147
    [self performSelectorOnMainThread:@selector(updateAmmoVisuals) withObject:nil waitUntilDone:YES];
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   148
    UIActivityIndicatorView *spinner = (UIActivityIndicatorView *)object;
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   149
    [spinner stopAnimating];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   150
    [pool drain];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   151
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   152
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   153
-(void) updateAmmoVisuals {
3977
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   154
    if (self.buttonsArray == nil || self.imagesArray == nil) {
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   155
        UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   156
        spinner.hidesWhenStopped = YES;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   157
        spinner.center = self.view.center;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   158
        [spinner startAnimating];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   159
        [self.view addSubview:spinner];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   160
        [NSThread detachNewThreadSelector:@selector(loadAmmoStuff:) toTarget:self withObject:spinner];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   161
        [spinner release];
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   162
        return;
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   163
    }
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   164
    
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   165
    int *loadout = (int *)calloc(HW_getNumberOfWeapons(), sizeof(int));
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   166
    int res = HW_getAmmoCounts(loadout);
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   167
    int turns = HW_getTurnsForCurrentTeam();
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   168
    
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   169
    if (res == 0) {
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   170
        self.view.userInteractionEnabled = YES;
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   171
        
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   172
        for (int i = 0; i < HW_getNumberOfWeapons(); i++) {
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   173
            UIButton *button = [self.buttonsArray objectAtIndex:i];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   174
            if (loadout[i] > 0) {
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   175
                if (delay[i]-turns >= 0) {
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   176
                    button.layer.borderColor = [[UIColor lightGrayColor] CGColor];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   177
                    [button setTitle:[NSString stringWithFormat:@" %d ",delay[i]-turns+1] forState:UIControlStateNormal];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   178
                    if (button.currentBackgroundImage == nil) {
3977
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   179
                        UIImage *img = [self.imagesArray objectAtIndex:i];
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   180
                        [button setBackgroundImage:[img convertToGrayScale] forState:UIControlStateNormal];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   181
                        button.imageView.tag = 10000;
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   182
                    }
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   183
                } else {
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   184
                    button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
3976
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3952
diff changeset
   185
                    [button setTitle:nil forState:UIControlStateNormal];
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   186
                    if (button.currentBackgroundImage == nil || button.imageView.tag == 10000) {
3977
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   187
                        UIImage *img = [self.imagesArray objectAtIndex:i];
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   188
                        [button setBackgroundImage:img forState:UIControlStateNormal];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   189
                        button.imageView.tag = 0;
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   190
                    }
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   191
                }
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   192
                button.enabled = YES;
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   193
            } else {
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   194
                if (button.enabled == YES)
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   195
                    [button setBackgroundImage:nil forState:UIControlStateNormal];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   196
                button.layer.borderColor = [[UIColor darkGrayColor] CGColor];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   197
                button.enabled = NO;
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   198
            }
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   199
        }
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   200
    } else {
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   201
        self.view.userInteractionEnabled = NO;
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   202
    }
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   203
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   204
    free(loadout);
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   205
    loadout = NULL;
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   206
}
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
#pragma mark -
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   209
#pragma mark user interaction
3976
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3952
diff changeset
   210
-(void) buttonPressed:(id)  sender {
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   211
    UIButton *theButton = (UIButton *)sender;
3976
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3952
diff changeset
   212
    if (theButton.currentTitle != nil) {
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3952
diff changeset
   213
        DLog(@"eeee");
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3952
diff changeset
   214
    }
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3952
diff changeset
   215
}
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3952
diff changeset
   216
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3952
diff changeset
   217
-(void) buttonReleased:(id) sender {
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3952
diff changeset
   218
    UIButton *theButton = (UIButton *)sender;
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3952
diff changeset
   219
    if (theButton.currentTitle == nil) {
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3952
diff changeset
   220
        HW_setWeapon(theButton.tag);
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3952
diff changeset
   221
        playSound(@"clickSound");
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3952
diff changeset
   222
    }
3941
017b2b31e1c6 integrate nicely with external display
koda
parents: 3940
diff changeset
   223
    if (IS_DUALHEAD() == NO)
017b2b31e1c6 integrate nicely with external display
koda
parents: 3940
diff changeset
   224
        [self disappear];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   225
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   226
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   227
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   228
    /*
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   229
    NSSet *allTouches = [event allTouches];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   230
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   231
    if ([touches count] == 1) {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   232
        self.view.layer.borderWidth = 3.5;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   233
        startingPoint = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view];
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
    */
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   236
}
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
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   239
    //self.view.layer.borderWidth = 1.3;
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   240
}
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   241
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   242
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   243
    /*
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   244
    NSSet *allTouches = [event allTouches];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   245
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   246
    if ([touches count] == 1) {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   247
        CGPoint touchedPoint = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   248
        CGFloat deltaX = touchedPoint.x - startingPoint.x;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   249
        CGFloat deltaY = touchedPoint.y - startingPoint.y;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   250
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   251
        //startingPoint = touchedPoint;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   252
        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
   253
                                     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
   254
    }
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   255
    */
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   256
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   257
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   258
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   259
    //[self touchesEnded:touches withEvent:event];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   260
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   261
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   262
#pragma mark -
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   263
#pragma mark memory
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   264
-(void) didReceiveMemoryWarning {
3977
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   265
    self.imagesArray = nil;
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   266
    self.buttonsArray = nil;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   267
    MSG_MEMCLEAN();
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   268
    [super didReceiveMemoryWarning];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   269
}
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   270
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   271
-(void) viewDidUnload {
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   272
    [[NSNotificationCenter defaultCenter] removeObserver:self];
3977
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   273
    self.imagesArray = nil;
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   274
    self.buttonsArray = nil;
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   275
    free(delay);
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   276
    delay = NULL;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   277
    MSG_DIDUNLOAD();
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   278
    [super viewDidUnload];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   279
}
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   280
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   281
-(void) dealloc {
3977
9df7b4812da9 optimizations to new ammomenu and overlay
koda
parents: 3976
diff changeset
   282
    [imagesArray release];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   283
    [buttonsArray release];
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   284
    [super dealloc];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   285
}
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   286
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   287
@end
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   288
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   289
void updateVisualsNewTurn (void) {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   290
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateAmmoVisuals" object:nil];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   291
}