project_files/HedgewarsMobile/Classes/AmmoMenuViewController.m
author koda
Tue, 12 Oct 2010 05:06:30 +0200
changeset 3971 5c82ee165ed5
parent 3952 d6412423da45
child 3976 abaf741a4e21
permissions -rw-r--r--
minor stuff
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
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    31
@synthesize weaponsImage, 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];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
    90
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    91
    NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
    92
    UIImage *ammoStoreImage = [[UIImage alloc] initWithContentsOfFile:str];
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    93
    [self performSelectorOnMainThread:@selector(setWeaponsImage:) withObject:ammoStoreImage waitUntilDone:NO];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    94
    [ammoStoreImage release];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
    95
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];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   125
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   126
        [button setTitleColor:UICOLOR_HW_YELLOW_TEXT forState:UIControlStateNormal];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   127
        button.titleLabel.backgroundColor = [UIColor blackColor];
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   128
        button.titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   129
        [button.titleLabel.layer setCornerRadius:3];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   130
        [button.titleLabel.layer setMasksToBounds:YES];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   131
        button.titleLabel.layer.borderColor = [[UIColor whiteColor] CGColor];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   132
        button.titleLabel.layer.borderWidth = 1;
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   133
        [self.view addSubview:button];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   134
        [array addObject:button];
3952
d6412423da45 moved some utilities to a separate column with round buttons
koda
parents: 3941
diff changeset
   135
        
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   136
    }
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   137
    [self performSelectorOnMainThread:@selector(setButtonsArray:) withObject:array waitUntilDone:NO];
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   138
    [array release];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   139
    
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   140
    [self performSelectorOnMainThread:@selector(updateAmmoVisuals) withObject:nil waitUntilDone:NO];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   141
    UIActivityIndicatorView *spinner = (UIActivityIndicatorView *)object;
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   142
    [spinner stopAnimating];
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   143
    [pool drain];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   144
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   145
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   146
-(void) updateAmmoVisuals {
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   147
    if (self.buttonsArray == nil || self.weaponsImage == nil) {
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   148
        UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   149
        spinner.hidesWhenStopped = YES;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   150
        spinner.center = self.view.center;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   151
        [spinner startAnimating];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   152
        [self.view addSubview:spinner];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   153
        [NSThread detachNewThreadSelector:@selector(loadAmmoStuff:) toTarget:self withObject:spinner];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   154
        [spinner release];
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   155
        return;
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   156
    }
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   157
    
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   158
    [NSThread detachNewThreadSelector:@selector(drawingThread) toTarget:self withObject:nil];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   159
}
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   160
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   161
-(void) drawingThread {
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   162
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   163
    int *loadout = (int *)calloc(HW_getNumberOfWeapons(), sizeof(int));
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   164
    int res = HW_getAmmoCounts(loadout);
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   165
    int turns = HW_getTurnsForCurrentTeam();
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   166
    
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   167
    if (res == 0) {
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   168
        self.view.userInteractionEnabled = YES;
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   169
        
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   170
        for (int i = 0; i < HW_getNumberOfWeapons(); i++) {
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   171
            UIButton *button = [self.buttonsArray objectAtIndex:i];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   172
            if (loadout[i] > 0) {
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   173
                if (delay[i]-turns >= 0) {
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   174
                    button.layer.borderColor = [[UIColor lightGrayColor] CGColor];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   175
                    [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
   176
                    if (button.currentBackgroundImage == nil) {
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   177
                        int x_src = ((i*32)/(int)self.weaponsImage.size.height)*32;
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   178
                        int y_src = (i*32)%(int)self.weaponsImage.size.height;
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   179
                        UIImage *img = [self.weaponsImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
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];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   185
                    [button setTitle:@"" forState:UIControlStateNormal];
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) {
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   187
                        int x_src = ((i*32)/(int)self.weaponsImage.size.height)*32;
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   188
                        int y_src = (i*32)%(int)self.weaponsImage.size.height;
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   189
                        UIImage *img = [self.weaponsImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   190
                        [button setBackgroundImage:img forState:UIControlStateNormal];
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   191
                        button.imageView.tag = 0;
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;
3935
5ca27a0e9a63 made the new menu toggable
koda
parents: 3933
diff changeset
   200
            }
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
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;
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   209
    [pool drain];
3933
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
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   212
#pragma mark -
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   213
#pragma mark user interaction
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   214
-(void) buttonPressed:(id) sender {
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   215
    UIButton *theButton = (UIButton *)sender;
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   216
    HW_setWeapon(theButton.tag);
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   217
    playSound(@"clickSound");
3941
017b2b31e1c6 integrate nicely with external display
koda
parents: 3940
diff changeset
   218
    if (IS_DUALHEAD() == NO)
017b2b31e1c6 integrate nicely with external display
koda
parents: 3940
diff changeset
   219
        [self disappear];
3933
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
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   222
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   223
    /*
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   224
    NSSet *allTouches = [event allTouches];
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
    if ([touches count] == 1) {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   227
        self.view.layer.borderWidth = 3.5;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   228
        startingPoint = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view];
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
    */
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
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   234
    //self.view.layer.borderWidth = 1.3;
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   235
}
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   236
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   237
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
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
    NSSet *allTouches = [event allTouches];
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
    if ([touches count] == 1) {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   242
        CGPoint touchedPoint = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   243
        CGFloat deltaX = touchedPoint.x - startingPoint.x;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   244
        CGFloat deltaY = touchedPoint.y - startingPoint.y;
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
        //startingPoint = touchedPoint;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   247
        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
   248
                                     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
   249
    }
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
}
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   252
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   253
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   254
    //[self touchesEnded:touches withEvent:event];
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
#pragma mark -
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   258
#pragma mark memory
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   259
-(void) didReceiveMemoryWarning {
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   260
    self.weaponsImage = nil;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   261
    self.buttonsArray = nil;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   262
    MSG_MEMCLEAN();
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   263
    [super didReceiveMemoryWarning];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   264
}
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   265
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   266
-(void) viewDidUnload {
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   267
    [[NSNotificationCenter defaultCenter] removeObserver:self];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   268
    self.weaponsImage = nil;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   269
    self.buttonsArray = nil;
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3935
diff changeset
   270
    free(delay);
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   271
    delay = NULL;
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   272
    MSG_DIDUNLOAD();
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   273
    [super viewDidUnload];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   274
}
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   275
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   276
-(void) dealloc {
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   277
    [weaponsImage release];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   278
    [buttonsArray release];
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   279
    [super dealloc];
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   280
}
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   281
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   282
@end
3924
2a9ace189288 WIP for an objc ammomenu implementation
koda
parents:
diff changeset
   283
3933
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   284
void updateVisualsNewTurn (void) {
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   285
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateAmmoVisuals" object:nil];
1a873262f5dd polishing the cocoa ammomenu a little, still requires work
koda
parents: 3924
diff changeset
   286
}