cocoaTouch/otherSrc/HogButtonView.m
changeset 3364 e5403e2bf02c
parent 3361 cfc6cd502f85
equal deleted inserted replaced
3363:bcd6d76db4f7 3364:e5403e2bf02c
     9 #import "HogButtonView.h"
     9 #import "HogButtonView.h"
    10 #import "CommodityFunctions.h"
    10 #import "CommodityFunctions.h"
    11 #import "UIImageExtra.h"
    11 #import "UIImageExtra.h"
    12 
    12 
    13 @implementation HogButtonView
    13 @implementation HogButtonView
    14 @synthesize singleHog;
    14 @synthesize singleHog, numberOfHogs, ownerDictionary;
    15 
    15 
    16 -(id) initWithFrame:(CGRect)frame {
    16 -(id) initWithFrame:(CGRect)frame {
    17     if ((self = [super initWithFrame:frame])) {
    17     if ((self = [super initWithFrame:frame])) {
    18         numberOfHogs = 4;
       
    19         self.backgroundColor = [UIColor clearColor];
    18         self.backgroundColor = [UIColor clearColor];
    20         
    19         
    21         NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()];
    20         NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()];
    22         UIImage *normalHogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)];
    21         UIImage *normalHogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)];
    23         [normalHogFile release];
    22         [normalHogFile release];
    24         
    23         
    25         self.singleHog = normalHogSprite;
    24         self.singleHog = normalHogSprite;
    26         [normalHogSprite release];
    25         [normalHogSprite release];
    27         
    26         [self addTarget:self action:@selector(addOne) forControlEvents:UIControlEventTouchUpInside];
    28         [self drawManyHogs];
       
    29     }
    27     }
    30     return self;
    28     return self;
    31 }
    29 }
    32 
    30 
    33 -(void) drawManyHogs {
    31 -(void) addOne {
    34     UIImage *teamHogs = [[UIImage alloc] init];
    32     self.highlighted = NO;
    35     for (int i = 0; i < numberOfHogs; i++) {
    33     NSInteger number = self.numberOfHogs;
    36         teamHogs = [singleHog mergeWith:teamHogs
    34     number++;
    37                                 atPoint:CGPointMake(8, 0) 
    35     [self drawManyHogs:number];
    38                                  atSize:CGSizeMake(88, 32)];
       
    39     }
       
    40     [self setImage:teamHogs forState:UIControlStateNormal];
       
    41 }
    36 }
    42 
    37 
    43 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    38 -(void) drawManyHogs:(NSInteger) hogs {
    44     UITouch *touch = [touches anyObject];
    39     if (numberOfHogs != hogs) {
    45     
    40         if (hogs <= MAX_HOGS && hogs >= 1)
    46 	switch ([touch tapCount]) {
    41             numberOfHogs = hogs;
    47 		case 1:
    42         else {
    48             if (numberOfHogs < MAX_HOGS) {
    43             if (hogs > MAX_HOGS)
    49                 numberOfHogs++;
       
    50             } else {
       
    51                 numberOfHogs = 1;
    44                 numberOfHogs = 1;
    52             }
    45             else
    53 			break;
       
    54 		case 2:
       
    55             if (numberOfHogs > 2) {
       
    56                 numberOfHogs--;
       
    57                 numberOfHogs--;
       
    58             } else {
       
    59                 numberOfHogs = MAX_HOGS;
    46                 numberOfHogs = MAX_HOGS;
    60             }
    47         }
    61             break;
    48         [ownerDictionary setObject:[NSNumber numberWithInt:numberOfHogs] forKey:@"number"];
    62 		default:
    49         
    63 			break;
    50         UIImage *teamHogs = [[[UIImage alloc] init] autorelease];
    64 	}
    51         for (int i = 0; i < numberOfHogs; i++) {
    65     NSLog(@"numberOfHogs: %d", numberOfHogs);
    52             teamHogs = [singleHog mergeWith:teamHogs
    66     [self drawManyHogs];
    53                                     atPoint:CGPointMake(8, 0) 
       
    54                                      atSize:CGSizeMake(88, 32)];
       
    55         }
       
    56         [self setImage:teamHogs forState:UIControlStateNormal];
       
    57     }
    67 }
    58 }
    68 
    59 
    69 -(void) dealloc {
    60 -(void) dealloc {
       
    61     [ownerDictionary release];
    70     [singleHog release];
    62     [singleHog release];
    71     [super dealloc];
    63     [super dealloc];
    72 }
    64 }
    73 
    65 
    74 
    66