project_files/HedgewarsMobile/Classes/HogButtonView.m
author koda
Mon, 30 Aug 2010 06:33:56 +0200
changeset 3792 dd9345e74b66
parent 3783 8e9daf967406
child 3829 81db3c85784b
permissions -rw-r--r--
help page for lobby
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];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    19
3547
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];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    23
3547
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 {
3783
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3697
diff changeset
    32
    playSound(@"clickSound");
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
    self.highlighted = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
    NSInteger number = self.numberOfHogs;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
    number++;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
    [self drawManyHogs:number];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
-(void) drawManyHogs:(NSInteger) hogs {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
    if (numberOfHogs != hogs) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
        if (hogs <= MAX_HOGS && hogs >= 1)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
            numberOfHogs = hogs;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
        else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
            if (hogs > MAX_HOGS)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
                numberOfHogs = 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
            else
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
                numberOfHogs = MAX_HOGS;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
        [ownerDictionary setObject:[NSNumber numberWithInt:numberOfHogs] forKey:@"number"];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    50
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
        UIImage *teamHogs = [[[UIImage alloc] init] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
        for (int i = 0; i < numberOfHogs; i++) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
            teamHogs = [singleHog mergeWith:teamHogs
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3547
diff changeset
    54
                                    atPoint:CGPointMake(8, 0)
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
                                     atSize:CGSizeMake(88, 32)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
        [self setImage:teamHogs forState:UIControlStateNormal];
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
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
    [ownerDictionary release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
    [singleHog release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
    [super dealloc];
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
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
@end