project_files/HedgewarsMobile/Classes/HogButtonView.m
author koda
Sat, 17 Jul 2010 16:35:14 +0200
changeset 3649 bc35f8fee587
parent 3547 02875b1145b7
child 3697 d5b30d6373fc
permissions -rw-r--r--
aim now starts slow and then moves faster
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     1
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     2
//  HogButtonView.m
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     3
//  HedgewarsMobile
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     4
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     5
//  Created by Vittorio on 20/04/10.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     7
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     8
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     9
#import "HogButtonView.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    10
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    11
#import "UIImageExtra.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    12
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    13
@implementation HogButtonView
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    14
@synthesize singleHog, numberOfHogs, ownerDictionary;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    15
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    16
-(id) initWithFrame:(CGRect)frame {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    17
    if ((self = [super initWithFrame:frame])) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    18
        self.backgroundColor = [UIColor clearColor];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
        NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
        UIImage *normalHogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
        [normalHogFile release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
        self.singleHog = normalHogSprite;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
        [normalHogSprite release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
        [self addTarget:self action:@selector(addOne) forControlEvents:UIControlEventTouchUpInside];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
    return self;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
-(void) addOne {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
    self.highlighted = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
    NSInteger number = self.numberOfHogs;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
    number++;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
    [self drawManyHogs:number];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
-(void) drawManyHogs:(NSInteger) hogs {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
    if (numberOfHogs != hogs) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
        if (hogs <= MAX_HOGS && hogs >= 1)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
            numberOfHogs = hogs;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
        else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
            if (hogs > MAX_HOGS)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
                numberOfHogs = 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
            else
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
                numberOfHogs = MAX_HOGS;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
        [ownerDictionary setObject:[NSNumber numberWithInt:numberOfHogs] forKey:@"number"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
        UIImage *teamHogs = [[[UIImage alloc] init] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
        for (int i = 0; i < numberOfHogs; i++) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
            teamHogs = [singleHog mergeWith:teamHogs
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
                                    atPoint:CGPointMake(8, 0) 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
                                     atSize:CGSizeMake(88, 32)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
        [self setImage:teamHogs forState:UIControlStateNormal];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
    [ownerDictionary release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
    [singleHog release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
@end