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